section 7C2
scalar multiplication and division operators

home

Four binary functions greatly simplify the notation for ordinary scalar multiplication and division:

• operator*=:
template <
typename compo_a,
typename scalar_t = compo_a,
typename alloc_a = std::allocator <compo_a>
> var_matrix<compo_a,alloc_a> operator*= (
var_matrix_base<compo_a,alloc_a> const & mat_a,
scalar_t const scalar_v
) {
struct mul_asgn {
compo_a operator() (compo_a & a, scalar_t const b) { return a *= b; }
};

return scalar_ret_var <
mul_asgn, compo_a,  scalar_t, alloc_a
> (mat_a, scalar_v);
}

• operator/= is analogous
• operator*:
template <
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> operator* (
sub_matrix_base<compo_a,alloc_a> const & mat_a,
scalar_t const scalar_v
) {
struct mul {
compo_z operator() (compo_a const a, scalar_t const b) { return a * b; }
};

return scalar_ret_con <
mul, compo_a,  scalar_t, compo_z, alloc_a, alloc_z
> (mat_a, scalar_v);
}

• operator/ is analogous

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},blt{5,7}};

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

{
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 {mat_a5 *= s};

cout << "\n mat_a5 after: " << mat_a5;
cout << "\n mat_z5 == what is returned: " << mat_z5;
} {
cout << "\n\n -- multiply a copy of a matrix by a scalar, return that copy";
var_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 {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 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