Python 如何在 SymPy 中求解线性方程组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31547657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:12:50  来源:igfitidea点击:

How can I solve system of linear equations in SymPy?

pythonmathsympy

提问by Aniket Vij

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

抱歉,总的来说,我对 sympy 和 python 还很陌生。

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

我想解决以下欠定线性方程组:

x + y + z = 1 
x + y + 2z = 3

采纳答案by Amit Kumar

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

SymPy 最近获得了一个新的线性系统求解器:linsolve在 中sympy.solvers.solveset,您可以按如下方式使用它:

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

A*x = b 形式

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.

注意:解的顺序对应于给定符号的顺序。

回答by Scott

You can solve in matrix form Ax=b(in this case an underdetermined system but we can use solve_linear_system):

您可以以矩阵形式求解Ax=b(在本例中为欠定系统,但我们可以使用solve_linear_system):

from sympy import Matrix, solve_linear_system

x, y, z = symbols('x, y, z')
A = Matrix(( (1, 1, 1, 1), (1, 1, 2, 3) ))
solve_linear_system(A, x, y, z)

{x: -y - 1, z: 2}

Or rewrite as (my editing, not sympy):

或重写为(我的编辑,而不是同情):

[x]=  [-1]   [-1]
[y]= y[1]  + [0]
[z]=  [0]    [2]

In the case of a square Awe could define band use A.LUsolve(b).

在正方形的情况下,A我们可以定义b和使用A.LUsolve(b).

回答by PaulDong

In addition to the great answers given by @AMiT Kumar and @Scott, SymPy 1.0 has added even further functionalities. For the underdetermined linear system of equations, I tried below and get it to work without going deeper into sympy.solvers.solveset. That being said, do go there if curiosity leads you.

除了@AMiT Kumar 和@Scott 给出的出色答案之外,SymPy 1.0 还添加了更多功能。对于欠定线性方程组,我在下面尝试并使其工作,而无需深入研究sympy.solvers.solveset. 话虽如此,如果好奇心引导你,那就去那里吧。

from sympy import *
x, y, z = symbols('x, y, z')
eq1 = x + y + z
eq2 = x + y + 2*z
solve([eq1-1, eq2-3], (x, y,z))

That gives me {z: 2, x: -y - 1}. Again, great package, SymPy developers!

这给了我{z: 2, x: -y - 1}。再次,伟大的包,SymPy 开发人员!

回答by Aziz Alto

Another example on matrix linear system equations, lets assume we are solving for this system:

矩阵线性系统方程的另一个例子,假设我们正在求解这个系统:

enter image description here

在此处输入图片说明

In SymPywe could do something like:

SymPy我们可以做这样的事情:

>>> import sympy as sy
... sy.init_printing()

>>> a, b, c, d = sy.symbols('a b c d')
... A = sy.Matrix([[a-b, b+c],[3*d + c, 2*a - 4*d]])
... A

? a - b     b + c  ?
?                  ?
?c + 3?d  2?a - 4?d?


>>> B = sy.Matrix([[8, 1],[7, 6]])
... B

?8  1?
?    ?
?7  6?


>>> A - B

? a - b - 8     b + c - 1  ?
?                          ?
?c + 3?d - 7  2?a - 4?d - 6?


>>> sy.solve(A - B, (a, b, c, d))
{a: 5, b: -3, c: 4, d: 1}

回答by Ali80

import sympy as sp
x, y, z = sp.symbols('x, y, z')
eq1 = sp.Eq(x + y + z, 1)             # x + y + z  = 1
eq2 = sp.Eq(x + y + 2 * z, 3)         # x + y + 2z = 3
ans = sp.solve((eq1, eq2), (x, y, z))

this is similar to @PaulDong answer with some minor changes

这类似于@PaulDong 的回答,但有一些细微的变化

  1. its a good practice getting used to not using import *(numpy has many similar functions)
  2. defining equations with sp.Eq()results in cleaner code later on
  1. 习惯不使用是一个好习惯import *(numpy 有很多类似的功能)
  2. 定义方程,sp.Eq()稍后会产生更清晰的代码