3
$\begingroup$

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)$$

$\endgroup$
  • $\begingroup$ I don't understand it. $\endgroup$ – Henrik Schumacher Oct 4 '18 at 12:40
  • $\begingroup$ @HenrikSchumacher Sorry, there was a mistake in my question. I fixed it and I am adding mor information. $\endgroup$ – james Oct 4 '18 at 12:42
  • $\begingroup$ @HenrikSchumacher Please have a look at my updated question. $\endgroup$ – james Oct 4 '18 at 12:46
  • $\begingroup$ But where are "equations"? ==0? $\endgroup$ – Αλέξανδρος Ζεγγ Oct 4 '18 at 13:33
2
$\begingroup$
 vars = {A1, B1, C1, A2, B2, C2};
 vars # & /@ CoefficientArrays[m, vars][[2]] // 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}

$\endgroup$
  • $\begingroup$ 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} $\endgroup$ – james Oct 4 '18 at 14:16
  • $\begingroup$ @james, you can do vars = Flatten@Transpose[ Outer[Symbol@StringJoin[##] &, CharacterRange["A", "Z"][[;;m]], ToString /@ Range[n]]] $\endgroup$ – kglr Oct 4 '18 at 14:21
  • $\begingroup$ Great ! Thanks a lot ! $\endgroup$ – james Oct 4 '18 at 14:26
  • $\begingroup$ Just a question: How can I get the coef. matrix in list form ? $\endgroup$ – james Oct 4 '18 at 14:56
  • $\begingroup$ @james, just use vars # & /@ CoefficientArrays[m, vars][[2]] (i.e. remove MatrixForm and TeXForm wrappers. $\endgroup$ – kglr Oct 4 '18 at 14:58
2
$\begingroup$

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}}
}
$\endgroup$
  • $\begingroup$ 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} $\endgroup$ – james Oct 4 '18 at 14:16
  • $\begingroup$ @james Do you mean that $ m $ is a multiple of 3? $\endgroup$ – Αλέξανδρος Ζεγγ Oct 4 '18 at 14:48
  • $\begingroup$ The question was already answered. Please see update of the accepted answer. Thanks ! :) $\endgroup$ – james Oct 4 '18 at 14:56

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.