section 7B2
scalar_X

home
Here are three functions that take a parameter and apply the same operation to all components of a matrix:
• scalar_non_var modifies the components of a matrix and returns nothing:
```template <
typename oper,
typename compo_a,
typename scalar_t = compo_a,
typename alloc_a = std::allocator <compo_a>
> void scalar_non_var (
var_matrix_base<compo_a,alloc_a> const & mat_a,
scalar_t const scalar_v
);
```
• scalar_ret_var modifies the components of a matrix and returns that modified version:
```template <
typename oper,
typename compo_a,
typename scalar_t = compo_a,
typename alloc_a = std::allocator <compo_a>
> var_matrix<compo_a,alloc_a> scalar_ret_var (
var_matrix_base<compo_a,alloc_a> const & mat_a,
scalar_t const scalar_v
);
```
• scalar_ret_con modifies the components of a copy of a matrix, and returns the copy:
```template <
typename oper,
typename compo_a,
typename scalar_t = 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> scalar_ret_con (
sub_matrix_base<compo_a,alloc_a> const & mat_a,
scalar_t const scalar_v
);
```

As an example, this program:

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

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

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

{
struct mul_non_var_con {
void operator() (double & a, double const b) { a *= b; }
};

cout << "\n\n -- multiply a matrix by a scalar, return nothing";
var_matrix<double> mat_a4 {sub_matrix_linear (frm,1.0)};
cout << "\n mat_a4 before: " << mat_a4;
double const s {4.0};
cout << "\n scalar: " << s;
scalar_non_var <mul_non_var_con,double> (mat_a4, s);
cout << "\n mat_a4 after: " << mat_a4;
} {
struct mul_ret_var_con {
double operator() (double & a, double const b) { return a *= b; }
};

cout << "\n\n -- multiply a matrix by a scalar, return a copy of it";
var_matrix<double> mat_a5 {sub_matrix_linear (frm,1.0)};
cout << "\n mat_a5 before: " << mat_a5;
double const s {5.0};
cout << "\n scalar: " << s;
var_matrix<double> mat_z5 {scalar_ret_var <mul_ret_var_con,double> (mat_a5, s)};
cout << "\n mat_a5 after: " << mat_a5;
cout << "\n mat_z5 == what is returned: " << mat_z5;
} {
struct mul_ret_con_con {
double operator() (double const a, double const b) { return a * b; }
};

cout << "\n\n -- multiply a copy of a matrix by a scalar, return that copy";
con_matrix<double> mat_a6 {sub_matrix_linear (frm,2.0)};
cout << "\n mat_a6 before: " << mat_a6;
double const s {6.0};
cout << "\n scalar: " << s;
con_matrix<double> mat_z6 {scalar_ret_con <mul_ret_con_con,double> (mat_a6, s)};
cout << "\n mat_a6 after: " << mat_a6;
cout << "\n mat_z6 == what is returned: " << mat_z6;
}
return 0;
}```
yields this output:
``` -- multiply a matrix by a scalar, return nothing
mat_a4 before:
( 1 2 5 ) = 1.125    ( 1 2 6 ) = 1.126    ( 1 3 5 ) = 1.135    ( 1 3 6 ) = 1.136
( 1 4 5 ) = 1.145    ( 1 4 6 ) = 1.146    ( 1 5 5 ) = 1.155    ( 1 5 6 ) = 1.156
( 2 2 5 ) = 1.225    ( 2 2 6 ) = 1.226    ( 2 3 5 ) = 1.235    ( 2 3 6 ) = 1.236
( 2 4 5 ) = 1.245    ( 2 4 6 ) = 1.246    ( 2 5 5 ) = 1.255    ( 2 5 6 ) = 1.256
scalar: 4.000
mat_a4 after:
( 1 2 5 ) = 4.500    ( 1 2 6 ) = 4.504    ( 1 3 5 ) = 4.540    ( 1 3 6 ) = 4.544
( 1 4 5 ) = 4.580    ( 1 4 6 ) = 4.584    ( 1 5 5 ) = 4.620    ( 1 5 6 ) = 4.624
( 2 2 5 ) = 4.900    ( 2 2 6 ) = 4.904    ( 2 3 5 ) = 4.940    ( 2 3 6 ) = 4.944
( 2 4 5 ) = 4.980    ( 2 4 6 ) = 4.984    ( 2 5 5 ) = 5.020    ( 2 5 6 ) = 5.024

-- multiply a matrix by a scalar, return a copy of it
mat_a5 before:
( 1 2 5 ) = 1.125    ( 1 2 6 ) = 1.126    ( 1 3 5 ) = 1.135    ( 1 3 6 ) = 1.136
( 1 4 5 ) = 1.145    ( 1 4 6 ) = 1.146    ( 1 5 5 ) = 1.155    ( 1 5 6 ) = 1.156
( 2 2 5 ) = 1.225    ( 2 2 6 ) = 1.226    ( 2 3 5 ) = 1.235    ( 2 3 6 ) = 1.236
( 2 4 5 ) = 1.245    ( 2 4 6 ) = 1.246    ( 2 5 5 ) = 1.255    ( 2 5 6 ) = 1.256
scalar: 5.000
mat_a5 after:
( 1 2 5 ) = 5.625    ( 1 2 6 ) = 5.630    ( 1 3 5 ) = 5.675    ( 1 3 6 ) = 5.680
( 1 4 5 ) = 5.725    ( 1 4 6 ) = 5.730    ( 1 5 5 ) = 5.775    ( 1 5 6 ) = 5.780
( 2 2 5 ) = 6.125    ( 2 2 6 ) = 6.130    ( 2 3 5 ) = 6.175    ( 2 3 6 ) = 6.180
( 2 4 5 ) = 6.225    ( 2 4 6 ) = 6.230    ( 2 5 5 ) = 6.275    ( 2 5 6 ) = 6.280
mat_z5 == what is returned:
( 1 2 5 ) = 5.625    ( 1 2 6 ) = 5.630    ( 1 3 5 ) = 5.675    ( 1 3 6 ) = 5.680
( 1 4 5 ) = 5.725    ( 1 4 6 ) = 5.730    ( 1 5 5 ) = 5.775    ( 1 5 6 ) = 5.780
( 2 2 5 ) = 6.125    ( 2 2 6 ) = 6.130    ( 2 3 5 ) = 6.175    ( 2 3 6 ) = 6.180
( 2 4 5 ) = 6.225    ( 2 4 6 ) = 6.230    ( 2 5 5 ) = 6.275    ( 2 5 6 ) = 6.280

-- multiply a copy of a matrix by a scalar, return that copy
mat_a6 before:
( 1 2 5 ) = 2.125    ( 1 2 6 ) = 2.126    ( 1 3 5 ) = 2.135    ( 1 3 6 ) = 2.136
( 1 4 5 ) = 2.145    ( 1 4 6 ) = 2.146    ( 1 5 5 ) = 2.155    ( 1 5 6 ) = 2.156
( 2 2 5 ) = 2.225    ( 2 2 6 ) = 2.226    ( 2 3 5 ) = 2.235    ( 2 3 6 ) = 2.236
( 2 4 5 ) = 2.245    ( 2 4 6 ) = 2.246    ( 2 5 5 ) = 2.255    ( 2 5 6 ) = 2.256
scalar: 6.000
mat_a6 after:
( 1 2 5 ) = 2.125    ( 1 2 6 ) = 2.126    ( 1 3 5 ) = 2.135    ( 1 3 6 ) = 2.136
( 1 4 5 ) = 2.145    ( 1 4 6 ) = 2.146    ( 1 5 5 ) = 2.155    ( 1 5 6 ) = 2.156
( 2 2 5 ) = 2.225    ( 2 2 6 ) = 2.226    ( 2 3 5 ) = 2.235    ( 2 3 6 ) = 2.236
( 2 4 5 ) = 2.245    ( 2 4 6 ) = 2.246    ( 2 5 5 ) = 2.255    ( 2 5 6 ) = 2.256
mat_z6 == what is returned:
( 1 2 5 ) = 12.750    ( 1 2 6 ) = 12.756    ( 1 3 5 ) = 12.810    ( 1 3 6 ) = 12.816
( 1 4 5 ) = 12.870    ( 1 4 6 ) = 12.876    ( 1 5 5 ) = 12.930    ( 1 5 6 ) = 12.936
( 2 2 5 ) = 13.350    ( 2 2 6 ) = 13.356    ( 2 3 5 ) = 13.410    ( 2 3 6 ) = 13.416
( 2 4 5 ) = 13.470    ( 2 4 6 ) = 13.476    ( 2 5 5 ) = 13.530    ( 2 5 6 ) = 13.536
```