Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

python - How can I solve system of linear equations in SymPy?

Sorry, I am pretty new to sympy and python in general.

I want to solve the following underdetermined linear system of equations:

x + y + z = 1 
x + y + 2z = 3
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SymPy recently got a new Linear system solver: linsolve in sympy.solvers.solveset, you can use that as follows:

In [38]: from sympy import *

In [39]: from sympy.solvers.solveset import linsolve

In [40]: x, y, z = symbols('x, y, z')

List of Equations Form:

In [41]: linsolve([x + y + z - 1, x + y + 2*z - 3 ], (x, y, z))
Out[41]: {(-y - 1, y, 2)}

Augmented Matrix Form:

In [59]: linsolve(Matrix(([1, 1, 1, 1], [1, 1, 2, 3])), (x, y, z))
Out[59]: {(-y - 1, y, 2)}

A*x = b Form

In [59]: M = Matrix(((1, 1, 1, 1), (1, 1, 2, 3)))

In [60]: system = A, b = M[:, :-1], M[:, -1]

In [61]: linsolve(system, x, y, z)
Out[61]: {(-y - 1, y, 2)}

Note: Order of solution corresponds the order of given symbols.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...