# Is there an easy way to get the matrix of coefficients from a product of a matrix and a vector?

I have a matrix multiplication of the form

$$B = A x$$

or

$$\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}$$

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

Edited from a question posted by someone else at the Mathematica Stackexchange

edit retag close merge delete

$A$ is not determined by the vectors $B$ and $x$.

( 2019-02-09 03:43:26 +0200 )edit

Yes, perhaps you can give a more explicit example to see what exactly you are asking for. But @dan_fulea is right in general.

( 2019-02-09 04:59:52 +0200 )edit

Sorry, I wasn’t clear. I didn’t mean to ask to solve the equation in general. What I was wondering is if there is a simple function that just returns the matrix of coefficients ai,j in front of the xi’s. Just a purely formal exercise. In other words, just have the computer do the rewriting (splitting) B in the form of A x. Nothing else. Yes, I know, I could write a function myself, but maybe someone already did it. According to the answer on Stackexchange, it can be done easily in Mathematica.

( 2019-02-10 02:58:28 +0200 )edit

Ok, I should have used the word "rewrite" instead of "get" in my question.

( 2019-02-10 03:02:25 +0200 )edit

Sort by » oldest newest most voted

I beg to differ with the commenters.

Staying at high-school level, your system boils down to a system of three linear equations with nine unknowns. It has therefore a sextuple infinity of solutions. And sage knows that :

A=[]
B=[]
X=[]
for u in (1..3):
X.append(var("x_{}".format(u), latex_name="x_{{{}}}".format(u)))
B.append(var("b_{}".format(u), latex_name="x_{{{}}}".format(u)))
for v in (1..3):
A.append(var("a_{}_{}".format(u,v),
latex_name="a_{{{},{}}}".format(u,v)))
A=matrix(entries=A,nrows=3)
B=vector(B)
X=vector(X)
S=[B[u]==(A*X)[u] for u in range(3)]
Sol=solve(S,[u for u in A.variables()])
Sol
[[a_1_1 == -(r24*x_2 + r21*x_3 - b_1)/x_1, a_1_2 == r24, a_1_3 == r21, a_2_1 == -(r23*x_2 + r22*x_3 - b_2)/x_1, a_2_2 == r23, a_2_3 == r22, a_3_1 == -(r20*x_2 + r19*x_3 - b_3)/x_1, a_3_2 == r20, a_3_3 == r19]]


Or, more clearly, the only solution is:

$${a_{1,1}} = -\frac{r_{24} {x_{2}} + r_{21} {x_{3}} - {x_{1}}}{{x_{1}}}$$', '$${a_{1,2}} = r_{24}$$', '$${a_{1,3}} = r_{21}$$', '$${a_{2,1}} = -\frac{r_{23} {x_{2}} + r_{22} {x_{3}} - {x_{2}}}{{x_{1}}}$$', '$${a_{2,2}} = r_{23}$$', '$${a_{2,3}} = r_{22}$$', '$${a_{3,1}} = -\frac{r_{20} {x_{2}} + r_{19} {x_{3}} - {x_{3}}}{{x_{1}}}$$', '$${a_{3,2}} = r_{20}$$', '$${a_{3,3}} = r_{19}$$'

In other words, your solution is a vector space of dimension six. This means that you could pick six of the elements of your and sole for the three last. For example:

solve(S,[a_1_1, a_2_2, a_3_3])
[[a_1_1 == -(a_1_2*x_2 + a_1_3*x_3 - b_1)/x_1, a_2_2 == -(a_2_1*x_1 + a_2_3*x_3 - b_2)/x_2, a_3_3 == -(a_3_1*x_1 + a_3_2*x_2 - b_3)/x_3]]


'$${a_{1,1}} = -\frac{{a_{1,2}} {x_{2}} + {a_{1,3}} {x_{3}} - {x_{1}}}{{x_{1}}}$$', '$${a_{2,2}} = -\frac{{a_{2,1}} {x_{1}} + {a_{2,3}} {x_{3}} - {x_{2}}}{{x_{2}}}$$', '$${a_{3,3}} = -\frac{{a_{3,1}} {x_{1}} + {a_{3,2}} {x_{2}} - {x_{3}}}{{x_{3}}}$$'

Sage has other, higher level, methods for solving this kind of linear algebra problems, whose discovery is left to the reader as an exercise ;-).

more