Systems of linear equations to augmented matrix

64 views
Skip to first unread message

Peter Gragert

unread,
May 11, 2021, 1:54:49 PM5/11/21
to sympy
Got adain interested in a book LINEAR ALGEBRA  with APLICATIONS, very nice book, very early there are exercises to show the corresponding matrix of coeffients. Trivial, if one types by reading the ckeeficients to bild the riws Matrix needs. But now my question : How to generate thebmatrix by a def ....
ok lhs and rhs of an Eq is easy, using args too .  But what dimyoundo if there are equations with differen numbers of say x_i. Missing unknows have to create an 0 at the needed row.index. 
I translated an equation (lhs) with srepr with regular expression , replacing  Symbol ('x1) to 1 etc .

Questions , how would you trans form e.g  x1 + x3 - 2*x5   To
1 0 1 0 -2 ?

 Ok, I extract x1 x3 and x5 ==>!knowing now x2 and x4 is missing, knowing the 0 at  index 1 and 3 (zero order of Python) ... 
  
Have to learn to give code examples here ... Pictutes ? 
Newbie in this groep

Ideas and help very much apteciated  😉 , by the way i am an 祖父 is grandpa 

Chris Smith

unread,
May 11, 2021, 4:55:56 PM5/11/21
to sympy
The sympy.solvers.solveset.linear_eq_to_matrix is probably what you are needing:

```python
    >>> eqns = [c*x + z - 1 - c, y + z, x - y]
    >>> A, b = linear_eq_to_matrix(eqns, [x, y, z])
    >>> A
    Matrix([
    [c,  0, 1],
    [0,  1, 1],
    [1, -1, 0]])
    >>> b
    Matrix([
    [c + 1],
    [    0],
    [    0]])
```

Peter Gragert

unread,
May 13, 2021, 9:15:59 AM5/13/21
to sympy
Oh , thanks a lot , will try very soon, indeed, wonderful ;

thanks a lot,
Opa Peter


Op dinsdag 11 mei 2021 om 22:55:56 UTC+2 schreef smi...@gmail.com:
Reply all
Reply to author
Forward
0 new messages