5
$\begingroup$

I was wondering if there is an inbuilt Mathematica function that can take a vector $v$ whose components are sums of variables $x_1, x_2, \dots x_{n}$ and turn it into a matrix-vector product, where the matrix is just a coefficient matrix and vector is right and composed only of the variables? For example, is there a Mathematica function $F$ such that

$$\begin{pmatrix} x_{1} + 2x_{2} \\ 3x_{1} + 4x_{2}\end{pmatrix} \stackrel{F}{\to} \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \begin{pmatrix} x_{1} \\ x_{2} \end{pmatrix}$$

Essentially, I'm looking for a function that 'undoes' the matrix-vector multiplication. Apologies if this has been asked before or is very simple to implement, I'm not overly familiar with Mathematica and so I don't even know what to search for.

$\endgroup$
  • 6
    $\begingroup$ CoefficientArrays? $\endgroup$ – xzczd Apr 6 at 11:05
  • $\begingroup$ @xzczd Thanks for letting me know about this function, I'll give it a try. $\endgroup$ – mattos Apr 6 at 11:10
7
$\begingroup$

Just take the derivative with respect to the variables:

D[{x[1] + 2 x[2], 3 x[1] + 4 x[2]}, {{x[1], x[2]}, 1}]

{{1, 2}, {3, 4}}

| improve this answer | |
$\endgroup$
  • $\begingroup$ Didn't even think to do this. It worked a charm too, thanks for your help. $\endgroup$ – mattos Apr 6 at 11:11

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.