用 Sympy 求解线性方程组

编程入门 行业动态 更新时间:2024-10-10 09:14: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 求解线性方程组

本文发布于:2023-11-30 06:13:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线性方程组   Sympy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!