Question: How to resolve a matrix of matrices?

I want to create a matrix (B) from entries of other matrices (A) with a helper-function (helper). The helper function is defined such that it returns a certain matrix depending on the index variables. This is necessary because the inner matrices are constructed with another function.

Since the helper-function returns matrices, the big matrix is of datatype=matrix. Unfortunately, creating the big matrix with the correct size and forcing the datatype=float, does not yield the desired result. However, the manual definition using the constructor with a list of matrices does create the desired matrix.

How do I resolve a matrix of matrices?

Note: I know that I could write a convert function that copies the entries to a corresponding matrix, though this seems to be unnecessary effort to me.

This might not be minimal but shows the issue. (Compare B and test)

MWE_matrix_of_matrices.mw

restart;
with(LinearAlgebra):

size_A := 2;
size_B := 3;

2

 

3

(1)

helper2 := proc(i::integer,j::integer);
  if i=j then
    a;
  elif i=j-1 or i=j+1 then
    b;
  else
    c;
  end if;
end proc:

helper3 := proc(i::integer,j::integer);
  if i=j then
    Matrix(size_A,size_A,helper2);
  elif i=j-1 or i=j+1 then
    -IdentityMatrix(size_A);
  else
    Matrix(size_A);
  end if;
end proc:

A := Matrix(size_A, size_A, helper2);
B := Matrix(size_B, size_B, helper3);
B := Matrix(size_B,size_B, helper3, datatype = float);
B := Matrix(size_B*size_A, size_B*size_A,[Matrix(size_B,size_B,helper3)], datatype = float)

A := Matrix(2, 2, {(1, 1) = a, (1, 2) = b, (2, 1) = b, (2, 2) = a})

 

B := Matrix(3, 3, {(1, 1) = Matrix(2, 2, {(1, 1) = a, (1, 2) = b, (2, 1) = b, (2, 2) = a}), (1, 2) = Matrix(2, 2, {(1, 1) = -1, (1, 2) = 0, (2, 1) = 0, (2, 2) = -1}), (1, 3) = Matrix(2, 2, {(1, 1) = 0, (1, 2) = 0, (2, 1) = 0, (2, 2) = 0}), (2, 1) = Matrix(2, 2, {(1, 1) = -1, (1, 2) = 0, (2, 1) = 0, (2, 2) = -1}), (2, 2) = Matrix(2, 2, {(1, 1) = a, (1, 2) = b, (2, 1) = b, (2, 2) = a}), (2, 3) = Matrix(2, 2, {(1, 1) = -1, (1, 2) = 0, (2, 1) = 0, (2, 2) = -1}), (3, 1) = Matrix(2, 2, {(1, 1) = 0, (1, 2) = 0, (2, 1) = 0, (2, 2) = 0}), (3, 2) = Matrix(2, 2, {(1, 1) = -1, (1, 2) = 0, (2, 1) = 0, (2, 2) = -1}), (3, 3) = Matrix(2, 2, {(1, 1) = a, (1, 2) = b, (2, 1) = b, (2, 2) = a})})

 

Error, (in Matrix) unable to store 'Matrix(2, 2, {(1, 1) = a, (1, 2) = b, (2, 1) = b, (2, 2) = a})' when datatype=float[8]

 

Error, (in Matrix) unable to store 'Matrix(2, 2, {(1, 1) = a, (1, 2) = b, (2, 1) = b, (2, 2) = a})' when datatype=float[8]

 

test := Matrix(4, 4, [
                [Matrix([[1,2],[0,9]]), Matrix([[3,6],[0,9]])],
                [Matrix([[3,4],[7,8]]), Matrix([[7,6],[5,5]])]
               ]); # is converted to a matrix of floats

test := Matrix(4, 4, {(1, 1) = 1, (1, 2) = 2, (1, 3) = 3, (1, 4) = 6, (2, 1) = 0, (2, 2) = 9, (2, 3) = 0, (2, 4) = 9, (3, 1) = 3, (3, 2) = 4, (3, 3) = 7, (3, 4) = 6, (4, 1) = 7, (4, 2) = 8, (4, 3) = 5, (4, 4) = 5})

(2)


Please Wait...