# Find transformations for two non-square matrices $A$ and $B$

Given two matrices $$A$$ and $$B$$:

What transformation needs to be applied to transform matrix $$A$$ into matrix $$B$$?

A = {{0, c, b, -c + b c, a c, -a + a b, c + b c, a c, a + a b}, {0, c,
b, c + b c, a c, a + a b, -c + b c, a c, -a + a b}, {0, -1, 0, b,
a, 0, b, a, 0}}

B = {{0, c, b, 0, c, b, 0, -1, 0}, {-c + b c, a c, -a + a b, c + b c,
a c, a + a b, b, a, 0}, {c + b c, a c, a + a b, -c + b c,
a c, -a + a b, b, a, 0}};


EDIT:

I found one method that only uses $$\frac{dQ}{d\boldsymbol{\theta}}$$, and the result is the same as direct differentiation $$\frac{dQ^T}{d\boldsymbol{\theta}}$$. The disadvantage of this method is the need to glue the matrix again. Maybe I can get around this with some kind of unified tensor operation ? To immediately receive the entire matrix, without additional gluing.

Rx = RotationMatrix[\[Phi][t], {1, 0, 0}];

Ry = RotationMatrix[\[Xi][t], {0, 1, 0}];

Rz = RotationMatrix[\[Psi][t], {0, 0, 1}];

Q = Rz.Ry.Rx;

v = {\[Phi][t], \[Xi][t], \[Psi][t]};

T1 = Flatten /@ D[Q, {v}];

T2 = Flatten /@ D[Transpose[Q], {v}];

P1 = {{1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0,
0, 0, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0,
1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 0, 0, 0,
0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1}};

A1 = T1.{{1, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 1, 0}, {0, 0, 0}, {0, 0,
0}, {0, 0, 1}, {0, 0, 0}, {0, 0, 0}};

A2 = T1.{{0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 1, 0}, {0, 0,
0}, {0, 0, 0}, {0, 0, 1}, {0, 0, 0}};

A3 = T1.{{0, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 1,
0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 1}};

Transpose[P1.ArrayFlatten[{{A1}, {A2}, {A3}}]] ==
Flatten /@ D[Transpose[Q], {v}] // MatrixForm;

• LinearSolve[A, B]? Jul 23 at 11:03
– dtn
Jul 23 at 11:05
• @LukasLang looking at the matrices I have an assumption about permutation matrices, am I right? if so, what will they look like?
– dtn
Jul 23 at 11:06

B == ArrayReshape[Transpose[ArrayReshape[A, {3, 3, 3}]], {3, 9}]


True

Also, in loop style:

result = ConstantArray[0, {3, 9}];
Do[
result[[i, 3 (j - 1) + k]] = A[[j, 3 (i - 1) + k]],
{i, 1, 3},
{j, 1, 3},
{k, 1, 3}
];
result == B


True

Or with a permutation matrix of size $$27 \times 27$$:

P = Block[{A, B, a},
A = Array[a, {3, 9}];
B = ArrayReshape[Transpose[ArrayReshape[A, {3, 3, 3}]], {3, 9}];
SparseArray[D[Flatten[B], {Flatten[A], 1}]]
];

ArrayReshape[P.Flatten[A], Dimensions[B]]

• Thank you! is it possible to formalize your proposed operation? Express it as a formula? As I assumed, you are using a permutation.
– dtn
Jul 23 at 12:17
• Well, if you treat both matrices as 3-tensors ArrayReshape[A, {3, 3, 3}] and ArrayReshape[B, {3, 3, 3}], then it is just about permuting the first two slots of the tensors. Jul 23 at 12:18
• I mean the following. Find such a matrix $X$, or a pair of matrix $x$ and $X$ for example (I don't know exactly how many are needed), so that either $AX=B$ or $xAX=B$.
– dtn
Jul 23 at 12:26
• Hm. You certainly cannot do that if you require that both x and X are permutation matrices; this can be seen when you compare the two columns in A and B that contain the -1. Jul 23 at 12:37
• Not sure what you need this for. Is it about coding? Just in case that you mean to code that in C, C++ or whatever: note that dense matrices and arrays are stored in flattened form. So the permutation matrix P from my post, applied to the raw buffer of A would do what you want. Jul 23 at 12:49

You can use LinearSolve to find one possible matrix such that A.X==B:

X = LinearSolve[A, B];
A.X == B // FullSimplify
(* True *)

• The result is a matrix $X$?
– dtn
Jul 23 at 11:08
• @dtn Yes, X is a 9x9 matrix. If you are looking for another kind of "transformation", you'll have to be a bit more specific with your requirements. (When talking about transformations in the context of matrixes, the usual thing that's meant is a transformation matrix such as the X from the answer) Jul 23 at 11:17
• Thank you for your answer. I understand what you are talking about. At the moment, for my tasks there is only one requirement that transformations lie in the class of vector-matrix operations. But I will happily explore possible alternatives if you point me to the sources.
– dtn
Jul 23 at 11:27
• I don't think this can be done purely using permutation matrices and dot products. You need to allow some other operations for this to be possible Jul 23 at 13:04
• @dtn As far as I can tell, no such permutation matrices exist (assuming a permutation matrix is a matrix where each row/column has exactly one 1 and the rest is 0). A matrix product with such a matrix can only ever permute rows/columns, but that is not sufficient to perform the permutation you are after. Jul 23 at 19:27

Here is a solution using Part

Flatten[A[[All, # ;; ;; 3]] & /@ Range@3 // Transpose, 1] // Transpose

• math.stackexchange.com/a/4201431/656085 Please see the very end of the answer. I need something similar, but I don't know the structure of the $P_{9\times9}$ and $P_{3\times3}$ matrices.
– dtn
Jul 23 at 15:32
• See my edit please. I found a variant of the way how to switch to the transposed matrix through the original one.
– dtn
Jul 23 at 19:45