section 4E
nested matrices

home

In contrast to what C does with arrays, mat_gen_dim follows the Ada policy of establishing no particular relationship between the following two data structures:

• a matrix (the superior) having p dimensions, whose components are themselves matrices (the inferiors) having q dimensions, whose components are of any_type.
• a matrix (the composite) having p + q dimensions, whose components are of any_type.
For instance, a two-dimensional matrix is not a one-dimensional matrix whose components are themselves one-dimensional matrices.

It is a constraint of mat_gen_dim that all the inferiors, in any matrix of matrices, must have the same type of component. The conjoin function introduced below goes further, requring that all the inferiors have equal forms.

While there is no automatic conversion between the composite and superior-inferior arrangements, mat_gen_dim does offer two functions that can perform it:

• disjoin:
• The input is the composite.
• The output is the superior containing the inferiors.
• The which parameter indicates how many dimensions the superior matrix will have; the inferior matrix gets the others.
• Every component of the composite matrix is copied exactly once into an inferior matrix.
```template <
typename compo,
typename alloc = std::allocator<compo>
> var_matrix<var_matrix<compo,alloc>> disjoin
(sub_matrix_base <compo,alloc> const & mat_a, sign_int const which);
```
• conjoin:
• The input is the superior containing the inferiors.
• The output is the composite.
• Every component of an inferior matrix is copied exactly once into the composite matrix.
```template <
typename compo,
typename alloc = std::allocator<compo>,
typename inner = sub_matrix_base<compo,alloc>
> var_matrix<compo,alloc> conjoin
(sub_matrix_base<inner> const & mat_b);
```

The two functions are strict inverses: conjoin can always be called on the result of a disjoin, and it results in the original matrix; and vice versa. Each of these functions returns an X_matrix, unlike a lens which returns something less. It may be possible, but it is difficult to write a disjoin and conjoin that return an X_matrix_base or anything similar.

The relationship among the forms is illustrated by this example:

 Composite: ( 1 =< 10, 2 =< 20, 3 =< 30, 4 =< 40, 5 =< 50 ) Superior: ( 1 =< 10, 2 =< 20, 3 =< 30 ) Inferior: ( 4 =< 40, 5 =< 50 )

In other words, the superior gets the leading subscripts, and the inferiors get the trailing subscripts. Other partitions and sequences can be achieved by employing permu form lenses.

What follows is an example program wherein:

• mat_a equals mat_e, and they have 6 dimensions;
• mat_b equals mat_d, and they have 4 + 2 dimensions;
• mat_c has 2 + 2 + 2 dimensions.
Important is that in the sequence mat_amat_bmat_cmat_dmat_e, no component information is lost.
```#include "SOME_DIRECTORY/mat_gen_dim.h"
using namespace mat_gen_dim;

int main() {
cout.precision (6);
form const frm {blt{0,2},blt{1,3},blt{2,4},blt{3,5},blt{4,6},blt{5,7}};

var_matrix<double> mat_a {sub_matrix_linear (frm, 0.0)};
cout << "\n mat_a:" << mat_a;

var_matrix<var_matrix<double>> mat_b {disjoin (mat_a, 4)};
cout << "\n mat_b:" << mat_b;

var_matrix<var_matrix<var_matrix<double>>> mat_c { disjoin<var_matrix<double>> (mat_b, 2) };
cout << "\n mat_c:" << mat_c;

var_matrix<var_matrix<double>> mat_d {conjoin<var_matrix<double>> (mat_c)};
cout << "\n mat_d:" << mat_d;

var_matrix<double> mat_e {conjoin<double> (mat_d)};
cout << "\n mat_e:" << mat_e;

return 0;
}
```

For the following output listing, the printing function was modified to place braces around each component, to more clearly show the nested relationship. Also the whitespace has been rearranged:

``` mat_a:
( 0 1 2 3 4 5 ) = { 0.012345 } ( 0 1 2 3 4 6 ) = { 0.012346 } ( 0 1 2 3 5 5 ) = { 0.012355 } ( 0 1 2 3 5 6 ) = { 0.012356 }
( 0 1 2 4 4 5 ) = { 0.012445 } ( 0 1 2 4 4 6 ) = { 0.012446 } ( 0 1 2 4 5 5 ) = { 0.012455 } ( 0 1 2 4 5 6 ) = { 0.012456 }
( 0 1 3 3 4 5 ) = { 0.013345 } ( 0 1 3 3 4 6 ) = { 0.013346 } ( 0 1 3 3 5 5 ) = { 0.013355 } ( 0 1 3 3 5 6 ) = { 0.013356 }
( 0 1 3 4 4 5 ) = { 0.013445 } ( 0 1 3 4 4 6 ) = { 0.013446 } ( 0 1 3 4 5 5 ) = { 0.013455 } ( 0 1 3 4 5 6 ) = { 0.013456 }
( 0 2 2 3 4 5 ) = { 0.022345 } ( 0 2 2 3 4 6 ) = { 0.022346 } ( 0 2 2 3 5 5 ) = { 0.022355 } ( 0 2 2 3 5 6 ) = { 0.022356 }
( 0 2 2 4 4 5 ) = { 0.022445 } ( 0 2 2 4 4 6 ) = { 0.022446 } ( 0 2 2 4 5 5 ) = { 0.022455 } ( 0 2 2 4 5 6 ) = { 0.022456 }
( 0 2 3 3 4 5 ) = { 0.023345 } ( 0 2 3 3 4 6 ) = { 0.023346 } ( 0 2 3 3 5 5 ) = { 0.023355 } ( 0 2 3 3 5 6 ) = { 0.023356 }
( 0 2 3 4 4 5 ) = { 0.023445 } ( 0 2 3 4 4 6 ) = { 0.023446 } ( 0 2 3 4 5 5 ) = { 0.023455 } ( 0 2 3 4 5 6 ) = { 0.023456 }
( 1 1 2 3 4 5 ) = { 0.112345 } ( 1 1 2 3 4 6 ) = { 0.112346 } ( 1 1 2 3 5 5 ) = { 0.112355 } ( 1 1 2 3 5 6 ) = { 0.112356 }
( 1 1 2 4 4 5 ) = { 0.112445 } ( 1 1 2 4 4 6 ) = { 0.112446 } ( 1 1 2 4 5 5 ) = { 0.112455 } ( 1 1 2 4 5 6 ) = { 0.112456 }
( 1 1 3 3 4 5 ) = { 0.113345 } ( 1 1 3 3 4 6 ) = { 0.113346 } ( 1 1 3 3 5 5 ) = { 0.113355 } ( 1 1 3 3 5 6 ) = { 0.113356 }
( 1 1 3 4 4 5 ) = { 0.113445 } ( 1 1 3 4 4 6 ) = { 0.113446 } ( 1 1 3 4 5 5 ) = { 0.113455 } ( 1 1 3 4 5 6 ) = { 0.113456 }
( 1 2 2 3 4 5 ) = { 0.122345 } ( 1 2 2 3 4 6 ) = { 0.122346 } ( 1 2 2 3 5 5 ) = { 0.122355 } ( 1 2 2 3 5 6 ) = { 0.122356 }
( 1 2 2 4 4 5 ) = { 0.122445 } ( 1 2 2 4 4 6 ) = { 0.122446 } ( 1 2 2 4 5 5 ) = { 0.122455 } ( 1 2 2 4 5 6 ) = { 0.122456 }
( 1 2 3 3 4 5 ) = { 0.123345 } ( 1 2 3 3 4 6 ) = { 0.123346 } ( 1 2 3 3 5 5 ) = { 0.123355 } ( 1 2 3 3 5 6 ) = { 0.123356 }
( 1 2 3 4 4 5 ) = { 0.123445 } ( 1 2 3 4 4 6 ) = { 0.123446 } ( 1 2 3 4 5 5 ) = { 0.123455 } ( 1 2 3 4 5 6 ) = { 0.123456 }

mat_b:
( 0 1 2 3 ) = { ( 4 5 ) = { 0.012345 } ( 4 6 ) = { 0.012346 } ( 5 5 ) = { 0.012355 } ( 5 6 ) = { 0.012356 } }
( 0 1 2 4 ) = { ( 4 5 ) = { 0.012445 } ( 4 6 ) = { 0.012446 } ( 5 5 ) = { 0.012455 } ( 5 6 ) = { 0.012456 } }
( 0 1 3 3 ) = { ( 4 5 ) = { 0.013345 } ( 4 6 ) = { 0.013346 } ( 5 5 ) = { 0.013355 } ( 5 6 ) = { 0.013356 } }
( 0 1 3 4 ) = { ( 4 5 ) = { 0.013445 } ( 4 6 ) = { 0.013446 } ( 5 5 ) = { 0.013455 } ( 5 6 ) = { 0.013456 } }
( 0 2 2 3 ) = { ( 4 5 ) = { 0.022345 } ( 4 6 ) = { 0.022346 } ( 5 5 ) = { 0.022355 } ( 5 6 ) = { 0.022356 } }
( 0 2 2 4 ) = { ( 4 5 ) = { 0.022445 } ( 4 6 ) = { 0.022446 } ( 5 5 ) = { 0.022455 } ( 5 6 ) = { 0.022456 } }
( 0 2 3 3 ) = { ( 4 5 ) = { 0.023345 } ( 4 6 ) = { 0.023346 } ( 5 5 ) = { 0.023355 } ( 5 6 ) = { 0.023356 } }
( 0 2 3 4 ) = { ( 4 5 ) = { 0.023445 } ( 4 6 ) = { 0.023446 } ( 5 5 ) = { 0.023455 } ( 5 6 ) = { 0.023456 } }
( 1 1 2 3 ) = { ( 4 5 ) = { 0.112345 } ( 4 6 ) = { 0.112346 } ( 5 5 ) = { 0.112355 } ( 5 6 ) = { 0.112356 } }
( 1 1 2 4 ) = { ( 4 5 ) = { 0.112445 } ( 4 6 ) = { 0.112446 } ( 5 5 ) = { 0.112455 } ( 5 6 ) = { 0.112456 } }
( 1 1 3 3 ) = { ( 4 5 ) = { 0.113345 } ( 4 6 ) = { 0.113346 } ( 5 5 ) = { 0.113355 } ( 5 6 ) = { 0.113356 } }
( 1 1 3 4 ) = { ( 4 5 ) = { 0.113445 } ( 4 6 ) = { 0.113446 } ( 5 5 ) = { 0.113455 } ( 5 6 ) = { 0.113456 } }
( 1 2 2 3 ) = { ( 4 5 ) = { 0.122345 } ( 4 6 ) = { 0.122346 } ( 5 5 ) = { 0.122355 } ( 5 6 ) = { 0.122356 } }
( 1 2 2 4 ) = { ( 4 5 ) = { 0.122445 } ( 4 6 ) = { 0.122446 } ( 5 5 ) = { 0.122455 } ( 5 6 ) = { 0.122456 } }
( 1 2 3 3 ) = { ( 4 5 ) = { 0.123345 } ( 4 6 ) = { 0.123346 } ( 5 5 ) = { 0.123355 } ( 5 6 ) = { 0.123356 } }
( 1 2 3 4 ) = { ( 4 5 ) = { 0.123445 } ( 4 6 ) = { 0.123446 } ( 5 5 ) = { 0.123455 } ( 5 6 ) = { 0.123456 } }

mat_c:
( 0 1 ) = {
( 2 3 ) = { ( 4 5 ) = { 0.012345 } ( 4 6 ) = { 0.012346 } ( 5 5 ) = { 0.012355 } ( 5 6 ) = { 0.012356 } }
( 2 4 ) = { ( 4 5 ) = { 0.012445 } ( 4 6 ) = { 0.012446 } ( 5 5 ) = { 0.012455 } ( 5 6 ) = { 0.012456 } }
( 3 3 ) = { ( 4 5 ) = { 0.013345 } ( 4 6 ) = { 0.013346 } ( 5 5 ) = { 0.013355 } ( 5 6 ) = { 0.013356 } }
( 3 4 ) = { ( 4 5 ) = { 0.013445 } ( 4 6 ) = { 0.013446 } ( 5 5 ) = { 0.013455 } ( 5 6 ) = { 0.013456 } }
}
( 0 2 ) = {
( 2 3 ) = { ( 4 5 ) = { 0.022345 } ( 4 6 ) = { 0.022346 } ( 5 5 ) = { 0.022355 } ( 5 6 ) = { 0.022356 } }
( 2 4 ) = { ( 4 5 ) = { 0.022445 } ( 4 6 ) = { 0.022446 } ( 5 5 ) = { 0.022455 } ( 5 6 ) = { 0.022456 } }
( 3 3 ) = { ( 4 5 ) = { 0.023345 } ( 4 6 ) = { 0.023346 } ( 5 5 ) = { 0.023355 } ( 5 6 ) = { 0.023356 } }
( 3 4 ) = { ( 4 5 ) = { 0.023445 } ( 4 6 ) = { 0.023446 } ( 5 5 ) = { 0.023455 } ( 5 6 ) = { 0.023456 } }
}
( 1 1 ) = {
( 2 3 ) = { ( 4 5 ) = { 0.112345 } ( 4 6 ) = { 0.112346 } ( 5 5 ) = { 0.112355 } ( 5 6 ) = { 0.112356 } }
( 2 4 ) = { ( 4 5 ) = { 0.112445 } ( 4 6 ) = { 0.112446 } ( 5 5 ) = { 0.112455 } ( 5 6 ) = { 0.112456 } }
( 3 3 ) = { ( 4 5 ) = { 0.113345 } ( 4 6 ) = { 0.113346 } ( 5 5 ) = { 0.113355 } ( 5 6 ) = { 0.113356 } }
( 3 4 ) = { ( 4 5 ) = { 0.113445 } ( 4 6 ) = { 0.113446 } ( 5 5 ) = { 0.113455 } ( 5 6 ) = { 0.113456 } }
}
( 1 2 ) = {
( 2 3 ) = { ( 4 5 ) = { 0.122345 } ( 4 6 ) = { 0.122346 } ( 5 5 ) = { 0.122355 } ( 5 6 ) = { 0.122356 } }
( 2 4 ) = { ( 4 5 ) = { 0.122445 } ( 4 6 ) = { 0.122446 } ( 5 5 ) = { 0.122455 } ( 5 6 ) = { 0.122456 } }
( 3 3 ) = { ( 4 5 ) = { 0.123345 } ( 4 6 ) = { 0.123346 } ( 5 5 ) = { 0.123355 } ( 5 6 ) = { 0.123356 } }
( 3 4 ) = { ( 4 5 ) = { 0.123445 } ( 4 6 ) = { 0.123446 } ( 5 5 ) = { 0.123455 } ( 5 6 ) = { 0.123456 } }
}

mat_d:
( 0 1 2 3 ) = { ( 4 5 ) = { 0.012345 } ( 4 6 ) = { 0.012346 } ( 5 5 ) = { 0.012355 } ( 5 6 ) = { 0.012356 } }
( 0 1 2 4 ) = { ( 4 5 ) = { 0.012445 } ( 4 6 ) = { 0.012446 } ( 5 5 ) = { 0.012455 } ( 5 6 ) = { 0.012456 } }
( 0 1 3 3 ) = { ( 4 5 ) = { 0.013345 } ( 4 6 ) = { 0.013346 } ( 5 5 ) = { 0.013355 } ( 5 6 ) = { 0.013356 } }
( 0 1 3 4 ) = { ( 4 5 ) = { 0.013445 } ( 4 6 ) = { 0.013446 } ( 5 5 ) = { 0.013455 } ( 5 6 ) = { 0.013456 } }
( 0 2 2 3 ) = { ( 4 5 ) = { 0.022345 } ( 4 6 ) = { 0.022346 } ( 5 5 ) = { 0.022355 } ( 5 6 ) = { 0.022356 } }
( 0 2 2 4 ) = { ( 4 5 ) = { 0.022445 } ( 4 6 ) = { 0.022446 } ( 5 5 ) = { 0.022455 } ( 5 6 ) = { 0.022456 } }
( 0 2 3 3 ) = { ( 4 5 ) = { 0.023345 } ( 4 6 ) = { 0.023346 } ( 5 5 ) = { 0.023355 } ( 5 6 ) = { 0.023356 } }
( 0 2 3 4 ) = { ( 4 5 ) = { 0.023445 } ( 4 6 ) = { 0.023446 } ( 5 5 ) = { 0.023455 } ( 5 6 ) = { 0.023456 } }
( 1 1 2 3 ) = { ( 4 5 ) = { 0.112345 } ( 4 6 ) = { 0.112346 } ( 5 5 ) = { 0.112355 } ( 5 6 ) = { 0.112356 } }
( 1 1 2 4 ) = { ( 4 5 ) = { 0.112445 } ( 4 6 ) = { 0.112446 } ( 5 5 ) = { 0.112455 } ( 5 6 ) = { 0.112456 } }
( 1 1 3 3 ) = { ( 4 5 ) = { 0.113345 } ( 4 6 ) = { 0.113346 } ( 5 5 ) = { 0.113355 } ( 5 6 ) = { 0.113356 } }
( 1 1 3 4 ) = { ( 4 5 ) = { 0.113445 } ( 4 6 ) = { 0.113446 } ( 5 5 ) = { 0.113455 } ( 5 6 ) = { 0.113456 } }
( 1 2 2 3 ) = { ( 4 5 ) = { 0.122345 } ( 4 6 ) = { 0.122346 } ( 5 5 ) = { 0.122355 } ( 5 6 ) = { 0.122356 } }
( 1 2 2 4 ) = { ( 4 5 ) = { 0.122445 } ( 4 6 ) = { 0.122446 } ( 5 5 ) = { 0.122455 } ( 5 6 ) = { 0.122456 } }
( 1 2 3 3 ) = { ( 4 5 ) = { 0.123345 } ( 4 6 ) = { 0.123346 } ( 5 5 ) = { 0.123355 } ( 5 6 ) = { 0.123356 } }
( 1 2 3 4 ) = { ( 4 5 ) = { 0.123445 } ( 4 6 ) = { 0.123446 } ( 5 5 ) = { 0.123455 } ( 5 6 ) = { 0.123456 } }

mat_e:
( 0 1 2 3 4 5 ) = { 0.012345 } ( 0 1 2 3 4 6 ) = { 0.012346 } ( 0 1 2 3 5 5 ) = { 0.012355 } ( 0 1 2 3 5 6 ) = { 0.012356 }
( 0 1 2 4 4 5 ) = { 0.012445 } ( 0 1 2 4 4 6 ) = { 0.012446 } ( 0 1 2 4 5 5 ) = { 0.012455 } ( 0 1 2 4 5 6 ) = { 0.012456 }
( 0 1 3 3 4 5 ) = { 0.013345 } ( 0 1 3 3 4 6 ) = { 0.013346 } ( 0 1 3 3 5 5 ) = { 0.013355 } ( 0 1 3 3 5 6 ) = { 0.013356 }
( 0 1 3 4 4 5 ) = { 0.013445 } ( 0 1 3 4 4 6 ) = { 0.013446 } ( 0 1 3 4 5 5 ) = { 0.013455 } ( 0 1 3 4 5 6 ) = { 0.013456 }
( 0 2 2 3 4 5 ) = { 0.022345 } ( 0 2 2 3 4 6 ) = { 0.022346 } ( 0 2 2 3 5 5 ) = { 0.022355 } ( 0 2 2 3 5 6 ) = { 0.022356 }
( 0 2 2 4 4 5 ) = { 0.022445 } ( 0 2 2 4 4 6 ) = { 0.022446 } ( 0 2 2 4 5 5 ) = { 0.022455 } ( 0 2 2 4 5 6 ) = { 0.022456 }
( 0 2 3 3 4 5 ) = { 0.023345 } ( 0 2 3 3 4 6 ) = { 0.023346 } ( 0 2 3 3 5 5 ) = { 0.023355 } ( 0 2 3 3 5 6 ) = { 0.023356 }
( 0 2 3 4 4 5 ) = { 0.023445 } ( 0 2 3 4 4 6 ) = { 0.023446 } ( 0 2 3 4 5 5 ) = { 0.023455 } ( 0 2 3 4 5 6 ) = { 0.023456 }
( 1 1 2 3 4 5 ) = { 0.112345 } ( 1 1 2 3 4 6 ) = { 0.112346 } ( 1 1 2 3 5 5 ) = { 0.112355 } ( 1 1 2 3 5 6 ) = { 0.112356 }
( 1 1 2 4 4 5 ) = { 0.112445 } ( 1 1 2 4 4 6 ) = { 0.112446 } ( 1 1 2 4 5 5 ) = { 0.112455 } ( 1 1 2 4 5 6 ) = { 0.112456 }
( 1 1 3 3 4 5 ) = { 0.113345 } ( 1 1 3 3 4 6 ) = { 0.113346 } ( 1 1 3 3 5 5 ) = { 0.113355 } ( 1 1 3 3 5 6 ) = { 0.113356 }
( 1 1 3 4 4 5 ) = { 0.113445 } ( 1 1 3 4 4 6 ) = { 0.113446 } ( 1 1 3 4 5 5 ) = { 0.113455 } ( 1 1 3 4 5 6 ) = { 0.113456 }
( 1 2 2 3 4 5 ) = { 0.122345 } ( 1 2 2 3 4 6 ) = { 0.122346 } ( 1 2 2 3 5 5 ) = { 0.122355 } ( 1 2 2 3 5 6 ) = { 0.122356 }
( 1 2 2 4 4 5 ) = { 0.122445 } ( 1 2 2 4 4 6 ) = { 0.122446 } ( 1 2 2 4 5 5 ) = { 0.122455 } ( 1 2 2 4 5 6 ) = { 0.122456 }
( 1 2 3 3 4 5 ) = { 0.123345 } ( 1 2 3 3 4 6 ) = { 0.123346 } ( 1 2 3 3 5 5 ) = { 0.123355 } ( 1 2 3 3 5 6 ) = { 0.123356 }
( 1 2 3 4 4 5 ) = { 0.123445 } ( 1 2 3 4 4 6 ) = { 0.123446 } ( 1 2 3 4 5 5 ) = { 0.123455 } ( 1 2 3 4 5 6 ) = { 0.123456 }
```