Solving system of linear equation with Sympy

给你一囗甜甜゛ 提交于 2020-06-01 06:53:30

问题


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 :

enter image description here


回答1:


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?



来源:https://stackoverflow.com/questions/61951623/solving-system-of-linear-equation-with-sympy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!