2
$\begingroup$

Say I have a matrix multiplication of the form

$$ B = A \cdot x $$

or

$$ \begin{align} \begin{pmatrix} a_{11} x_1 + a_{12} x_2 + a_{13} x_3 \\ a_{21} x_1 + a_{22} x_2 + a_{23} x_3 \\ a_{31} x_1 + a_{32} x_2 + a_{33} x_3 \end{pmatrix} = \begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{pmatrix} \cdot \begin{pmatrix} x_1 \\ x_2 \\ x_3 \end{pmatrix} \end{align} $$

Is there a way in Mathematica to factor $B$ in a way where I give it $x$ and it returns $A$?

$\endgroup$
2
  • $\begingroup$ @garej I noticed you tried to edit this question to give some Mathematica code. Although I understand that this was done in the best of intentions, I think it would be better to make a request to the OP directly from the comments section. $\endgroup$
    – QuantumDot
    Jan 2, 2016 at 15:30
  • $\begingroup$ You could use LinearSolve but the system is underdetermined, hence not uniquely solved. $\endgroup$ Jan 2, 2016 at 17:31

3 Answers 3

5
$\begingroup$

I would simply do the derivative. It's the shortest way to get the matrix from a linear expression, assuming the vector X consists of symbols as written in the question.

First define the matrices and vectors:

X = {x1, x2, x3};
A = Array[a, {3, 3}];

B = A.X

(*
==> {x1 a[1, 1] + x2 a[1, 2] + x3 a[1, 3], 
 x1 a[2, 1] + x2 a[2, 2] + x3 a[2, 3], 
 x1 a[3, 1] + x2 a[3, 2] + x3 a[3, 3]}
*)

Given these definitions, this is the only thing you need to do:

D[B, {X}]

(*
==> {{a[1, 1], a[1, 2], a[1, 3]}, {a[2, 1], a[2, 2], 
  a[2, 3]}, {a[3, 1], a[3, 2], a[3, 3]}}
*)
$\endgroup$
1
  • $\begingroup$ tensor derivative - that is very elegant! $\endgroup$
    – garej
    Jan 4, 2016 at 7:46
4
$\begingroup$

I found the solution. It's simply:

Table[Coefficient[B[[i]],#]&/@X,{i,Length[B]}]

This will go through each element of B, and check the coefficient of each element of X for that B element, creating a two dimensional array, which is basically A.

A short form of this that works for me is:

Coefficient[B,#]&/@X

Mathematica is smart enough to recognize that it has to apply Coefficient on each element of B. For non-symmetric matrixes Transpose is needed:

mat = {{-1, 2, 3}, {0, 2, 4}, {1, -1, 2}}
(Coefficient[mat.X, #] & /@ X) // Transpose

{{-1, 2, 3}, {0, 2, 4}, {1, -1, 2}}

$\endgroup$
3
  • $\begingroup$ @You forgot to Transpose the result, did not you? $\endgroup$
    – garej
    Jan 2, 2016 at 13:52
  • $\begingroup$ @garej Actually I'm not sure. I'm dealing with symmetric/Hermitian matrices so it doesn't really matter for me. If you're sure, please feel free to edit my answer. $\endgroup$ Jan 2, 2016 at 14:00
  • $\begingroup$ compare Array[a[##] &, {3, 3}] and Coefficient[Array[a[##] &, {3, 3}].{x1, x2, x3}, #] & /@ {x1, x2, x3}. I would add a sample a, say {{1, 3, -1}, {2, 4, 3}, {3, 5, 2}} in the question ;)) $\endgroup$
    – garej
    Jan 2, 2016 at 14:12
1
$\begingroup$

Using the CoefficientArrays[] function works as well.

Input:

mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
X = {x1, x2, x3};
Normal@CoefficientArrays[mat.X, X][[2]]

Output:

{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.