This sample program shows how ordinary matrix multiplication as can be implemented in mat_gen_dim. Note that the second blt of the first matrix must equal the first blt of the second matrix.
#include "SOME_DIRECTORY/mat_gen_dim.h"
using namespace mat_gen_dim;
template <typename compo, typename alloc>
var_matrix<compo,alloc> ordinary_matrix_mult (
sub_matrix_base<compo,alloc> const & mat_a,
sub_matrix_base<compo,alloc> const & mat_b
) {
vec_bool const which {false,true,true,false};
struct mul {
compo operator() (compo const a, compo const b) { return a * b; }
};
struct add_asgn {
compo operator() (compo & z, compo const y) { return z += y; }
};
return inner_product<mul,add_asgn> (mat_a, mat_b, which);
}
int main () {
form const frm_a {blt{1,3},blt{1,4}};
con_matrix<int> mat_a (frm_a, {
2, 3, -1,
4, -2, 5
} );
cout << "\n mat_a: " << mat_a;
form const frm_b {blt{1,4},blt{1,5}};
con_matrix<int> mat_b (frm_b, {
2, -1, 0, 6,
1, 3, -5, 1,
4, 1, -2, 2
} );
cout << "\n mat_b: " << mat_b;
con_matrix<int> mat_c (ordinary_matrix_mult (mat_a, mat_b));
cout << "\n mat_a times mat_b: " << mat_c;
cout << "\n\n now transpose everything: \n";
permu const p10 {1, 0};
con_lens_permu_form<int> tam_b (mat_b, p10);
cout << "\n tam_b: " << tam_b;
con_lens_permu_form<int> tam_a (mat_a, p10);
cout << "\n tam_a: " << tam_a;
con_matrix<int> tam_c (ordinary_matrix_mult (tam_b, tam_a));
cout << "\n tam_b times tam_a: " << tam_c;
return 0;
}
yields this output:
mat_a:
( 1 1 ) = 2 ( 1 2 ) = 3 ( 1 3 ) = -1
( 2 1 ) = 4 ( 2 2 ) = -2 ( 2 3 ) = 5
mat_b:
( 1 1 ) = 2 ( 1 2 ) = -1 ( 1 3 ) = 0 ( 1 4 ) = 6
( 2 1 ) = 1 ( 2 2 ) = 3 ( 2 3 ) = -5 ( 2 4 ) = 1
( 3 1 ) = 4 ( 3 2 ) = 1 ( 3 3 ) = -2 ( 3 4 ) = 2
mat_a times mat_b:
( 1 1 ) = 3 ( 1 2 ) = 6 ( 1 3 ) = -13 ( 1 4 ) = 13
( 2 1 ) = 26 ( 2 2 ) = -5 ( 2 3 ) = 0 ( 2 4 ) = 32
now transpose everything:
tam_b:
( 1 1 ) = 2 ( 1 2 ) = 1 ( 1 3 ) = 4
( 2 1 ) = -1 ( 2 2 ) = 3 ( 2 3 ) = 1
( 3 1 ) = 0 ( 3 2 ) = -5 ( 3 3 ) = -2
( 4 1 ) = 6 ( 4 2 ) = 1 ( 4 3 ) = 2
tam_a:
( 1 1 ) = 2 ( 1 2 ) = 4
( 2 1 ) = 3 ( 2 2 ) = -2
( 3 1 ) = -1 ( 3 2 ) = 5
tam_b times tam_a:
( 1 1 ) = 3 ( 1 2 ) = 26
( 2 1 ) = 6 ( 2 2 ) = -5
( 3 1 ) = -13 ( 3 2 ) = 0
( 4 1 ) = 13 ( 4 2 ) = 32