Creating a matrix of equation coefficients

Given the equations

eqn1 = 5 x1 + 2 x2 + 3 x3 == 8 eqn2 = 4 x1 + 7 x2 + 9 x3 == 5 eqn3 = 6 x1 + x2 + 9 x3 == 2 

how to extract the coefficients at x1, x2, x3 to form a matrix?

I tried using CoefficientArrays , but the result was set as SparseArray .

+6
source share
2 answers

Try Normal

 (Normal[CoefficientArrays[{eqn1, eqn2, eqn3}, {x1, x2, x3}]][[2]]) // MatrixForm 

enter image description here

+11
source

I do not like Normal

 Coefficient[# /. Equal[e_, _] -> e, {x1, x2, x3}] & /@ {eqn1, eqn2, eqn3} 

In short, but not so clear:

 Coefficient[ First@ #, {x1, x2, x3}] & /@ {eqn1, eqn2, eqn3} 
+4
source

Source: https://habr.com/ru/post/897460/


All Articles