用 Sympy 求解线性方程组 [英] Solving system of linear equation with Sympy

查看:38
本文介绍了用 Sympy 求解线性方程组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了评估某种弹性属性,我想使用 sympy 来可视化方程组.我使用以下代码:

将 numpy 导入为 np将 sympy 导入为 symb1=sym.Array([[Rational(-1/2),sqrt(3)/2,0],[-sqrt(3)/2,Rational(-1/2),0],[0,0],1]])西格玛=[]对于范围内的 i (0,3):对于范围内的 j (0,3):对于范围内的 k (0,3):对于范围内的 l(0,3):x= 符号(('\sigma_{%d%d}')%(k+1,l+1),commutative=False)M=sym.Array([x])Sigmatotal_tmp=tensorproduct(b1[i][k],b1[j][l],M)Sigma.append(Sigma11)

我想将这九个方程的集合可视化如下:

手动使用类似的东西:

Sigma11 = Sigma[0][0] + Sigma[1][0] + Sigma[2][0] + Sigma[3][0] + Sigma[4][0] + Sigma[5][0] + 西格玛[6][0]

显示

+....

如何将其转换为一组方程并求解以找到自变量?

我手工完成的,它看起来像这样:

解决方案

看起来方程组是内部循环中生成的 9 个项的总和:

from sympy import *将 sympy 导入为 symb1=sym.Array([[Rational(-1/2),sqrt(3)/2,0],[-sqrt(3)/2,Rational(-1/2),0],[0,0],1]])西格玛=[]对于范围内的 i (0,3):对于范围内的 j (0,3):y = 符号(('\sigma_{%d%d}')%(i+1,j+1), 可交换=真)参数 = []对于范围内的 k (0,3):对于范围内的 l(0,3):x= 符号(('\sigma_{%d%d}')%(k+1,l+1), 可交换=真)M=sym.Array([x])Sigmatotal_tmp=tensorproduct(b1[i][k],b1[j][l],M)args.append(Sigmatotal_tmp[0])Sigma.append(y - Add(*args))pprint(Sigma[-1])

鉴于此,您只需使用 solve(Sigma) 即可获得解决方案:

<预><代码>>>>解决(西格玛){\sigma_{32}: 0, \sigma_{31}: 0, \sigma_{23}: 0, \sigma_{13}: 0,\sigma_{12}: -\sigma_{21}, \sigma_{11}: \sigma_{22}}

还要注意,交换性被设置为 True——它是否有必要为 False?

To evaluate a certain property of elasticity I would like to use sympy to visualize the set of equation. I use the following code :

import numpy as np
import sympy as sym
b1=sym.Array([[Rational(-1/2),sqrt(3)/2,0],[-sqrt(3)/2,Rational(-1/2),0],[0,0,1]])
Sigma=[]
for i in range(0,3):
    for j in range(0,3):
        for k in range(0,3):
            for l in range(0,3):
                x= symbols(('\sigma_{%d%d}')%(k+1,l+1),commutative=False)
                M=sym.Array([x])
                Sigmatotal_tmp=tensorproduct(b1[i][k],b1[j][l],M)
                Sigma.append(Sigma11)

I would like to visualize the set of this nine equation as follow :

Using something like this manually :

Sigma11 = Sigma[0][0] + Sigma[1][0] + Sigma[2][0] + Sigma[3][0] + Sigma[4][0] + Sigma[5][0] + Sigma[6][0]

displays

+....

How can I covert this to a set of equation and solve it to find the independent variables?

I did it by hand and it looks like this :

解决方案

It looks like the equation set is the sum of the 9 terms generated in the inner loops:

from sympy import *
import sympy as sym
b1=sym.Array([[Rational(-1/2),sqrt(3)/2,0],[-sqrt(3)/2,Rational(-1/2),0],[0,0,1]])
Sigma=[]
for i in range(0,3):
  for j in range(0,3):
    y = symbols(('\sigma_{%d%d}')%(i+1,j+1), commutative=True)
    args = []
    for k in range(0,3):
        for l in range(0,3):
            x= symbols(('\sigma_{%d%d}')%(k+1,l+1), commutative=True)
            M=sym.Array([x])
            Sigmatotal_tmp=tensorproduct(b1[i][k],b1[j][l],M)
            args.append(Sigmatotal_tmp[0])
    Sigma.append(y - Add(*args))
    pprint(Sigma[-1])

Given that, you just use solve(Sigma) to get the solution:

>>> solve(Sigma)
{\sigma_{32}: 0, \sigma_{31}: 0, \sigma_{23}: 0, \sigma_{13}: 0, 
 \sigma_{12}: -\sigma_{21}, \sigma_{11}: \sigma_{22}}

Note, too, that the commutativity is set to True -- is it necessary for it to be False?

这篇关于用 Sympy 求解线性方程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆