# Convert List of Equations To Coefficient Matrix

I have the following list of 2 equations (but generally I have much more than 2 equations):

  m= {A1*x+B1*Sin[x]-C1*Cos[x],C2+A2*Cosh[x]}


How could I automatically convert it into a coefficient matrix, like that:

$$\left( \begin{array}{cccccc} \text{A1} \text{x4} & \text{B1} \sin (\text{x4}) & \text{-C1} \cos (\text{x4}) & 0 & 0 & 0 \\ 0 & 0 & 0 & \text{A2} \cosh (\text{x4}) & \text{0} & \text{C2} \\ \end{array} \right)$$

Each row of the coefficient matrix corresponds to an element of the m list and is sorted from A to Z.

Some more details:

Basically I have two equations:

(1) A1*x+B1*Sin[x]-C1*Cos[x]

(2) C2+ A2*Cosh[x]

Those equations are listed in the m list. I could rewrite those equations like that:

(1) A1*x + B1*Sin[x] - C1*Cos[x] + 0*A2 + 0*B2+0*C2

(2) 0*A1 + 0*B1 + 0*C1+A2*Cosh[x] + 0*B2+C2

Now I create the coefficient matrix:

$$\left( \begin{array}{cccccc} \text{A1} \text{x4} & \text{B1} \sin (\text{x4}) & \text{-C1} \cos (\text{x4}) & 0 & 0 & 0 \\ 0 & 0 & 0 & \text{A2} \cosh (\text{x4}) & \text{0} & \text{C2} \\ \end{array} \right)$$

• I don't understand it. Oct 4, 2018 at 12:40
• @HenrikSchumacher Sorry, there was a mistake in my question. I fixed it and I am adding mor information. Oct 4, 2018 at 12:42
• @HenrikSchumacher Please have a look at my updated question. Oct 4, 2018 at 12:46
• But where are "equations"? ==0? Oct 4, 2018 at 13:33

 vars = {A1, B1, C1, A2, B2, C2};
vars # & /@ CoefficientArrays[m, vars][] // MatrixForm // TeXForm


$$\left( \begin{array}{cccccc} \text{A1} x & \text{B1} \sin (x) & -\text{C1} \cos (x) & 0 & 0 & 0 \\ 0 & 0 & 0 & \text{A2} \cosh (x) & 0 & \text{C2} \\ \end{array} \right)$$

Update: to also automate vars:

ClearAll[f]
f[n_, m_] := Flatten@Transpose[Outer[Symbol@StringJoin[##] &,
CharacterRange["A", "Z"][[;; m]], ToString /@ Range[n]]]


Examples:

f[2, 3]


{A1, B1, C1, A2, B2, C2}

f[4, 4]


{A1, B1, C1, D1, A2, B2, C2, D2, A3, B3, C3, D3, A4, B4, C4, D4}

• Thanks a lot ! This works nicely. One question: Would it be possible to also automate vars ? I have n equation and m variables. ... a function that would take in the variable names and add the numbers: f(A,B,C) results in {A1,B1,C1,A2,B2,C2} Oct 4, 2018 at 14:16
• @james, you can do vars = Flatten@Transpose[ Outer[Symbol@StringJoin[##] &, CharacterRange["A", "Z"][[;;m]], ToString /@ Range[n]]]
– kglr
Oct 4, 2018 at 14:21
• Great ! Thanks a lot ! Oct 4, 2018 at 14:26
• Just a question: How can I get the coef. matrix in list form ? Oct 4, 2018 at 14:56
• @james, just use vars # & /@ CoefficientArrays[m, vars][] (i.e. remove MatrixForm and TeXForm wrappers.
– kglr
Oct 4, 2018 at 14:58

CoefficientArrays is quite suitable for this job.

m = {A1 x + B1 Sin[x] - C1 Cos[x], C2 + A2 Cosh[x]};
eqs = Thread[m + {a, b} == 0];
CoefficientArrays[eqs, {A1, B1, C1, A2, B2, C2}] // Normal

{{a, b},
{{x, Sin[x], -Cos[x], 0, 0, 0},
{0, 0, 0, Cosh[x], 0, 1}}
}

• Thanks a lot ! One question: Would it be possible to also automate vars ? I have n equation and m variables. ... a function that would take in the variable names and add the numbers: f(A,B,C) results in {A1,B1,C1,A2,B2,C2} Oct 4, 2018 at 14:16
• @james Do you mean that $m$ is a multiple of 3? Oct 4, 2018 at 14:48