section 7C1
unary operators

home

Two standard unary operations are:

• operator-:
```template <
typename compo_a,
typename compo_z = compo_a,
typename alloc_a = std::allocator<compo_a>,
typename alloc_z = std::allocator<compo_z>
> var_matrix<compo_z,alloc_z> operator- (
sub_matrix_base<compo_a,alloc_a> const & mat_a
) {
struct neg {
compo_z operator() (compo_a const a) { return -a; }
};
return alone_ret_con <neg,compo_a,compo_z> (mat_a);
}
```
• operator+ is analogous. Although operator+ will have no material effect for many component types, the version in mat_gen_dim dutifully iterates through the matrix and applies the operator to every component in case something nontrivial is supposed to happen.

For example, this program:

```#include "SOME_DIRECTORY/mat_gen_dim.h"
using namespace mat_gen_dim;

form const frm {blt{1,3},blt{2,6}};

int main () {
std::cout.setf (std::ios::fixed, std::ios::floatfield);
std::cout.precision (2);

cout << "\n\n -- negate a matrix";
var_matrix<double> mat_a3 {sub_matrix_linear (frm,0.0)};
cout << "\n mat_a3 before: " << mat_a3;

var_matrix<double> mat_z3 {-mat_a3};

cout << "\n mat_a3 after: " << mat_a3;
cout << "\n mat_z3 == what is returned: " << mat_z3;

return 0;
}
```
yields this output:
``` -- negate a matrix
mat_a3 before:
( 1 2 ) = 0.12    ( 1 3 ) = 0.13    ( 1 4 ) = 0.14    ( 1 5 ) = 0.15
( 2 2 ) = 0.22    ( 2 3 ) = 0.23    ( 2 4 ) = 0.24    ( 2 5 ) = 0.25
mat_a3 after:
( 1 2 ) = 0.12    ( 1 3 ) = 0.13    ( 1 4 ) = 0.14    ( 1 5 ) = 0.15
( 2 2 ) = 0.22    ( 2 3 ) = 0.23    ( 2 4 ) = 0.24    ( 2 5 ) = 0.25
mat_z3 == what is returned:
( 1 2 ) = -0.12    ( 1 3 ) = -0.13    ( 1 4 ) = -0.14    ( 1 5 ) = -0.15
( 2 2 ) = -0.22    ( 2 3 ) = -0.23    ( 2 4 ) = -0.24    ( 2 5 ) = -0.25
```