简体   繁体   English

用sympy设置矩阵麻烦

[英]Trouble setting up matrix with sympy

I would like to do some symbolic mathe withe some matrices. 我想用一些矩阵做一些符号数学。 But my code does not work. 但是我的代码不起作用。 Setting up my first matrices is no problem but calculating the wanted one from the start matrices does not work and I do not know why. 设置我的第一个矩阵没问题,但是从一开始就计算想要的矩阵是行不通的,我也不知道为什么。 Any suggestions? 有什么建议么?

from sympy import *
from sympy import Matrix , pprint, symbols



niso=4

MM = Matrix(niso-1,1, lambda i,j:var('MM_%s' % (i+1) ))
MA = Matrix (niso-1,1, lambda i,j:var('m_%s%s' % ('A', chr(66+i)) ) )
MX = Matrix (niso-1,1, lambda i,j:var('m_%s%s'% (chr(66+i), 'A')))
Rb = Matrix(niso-1,1, lambda i,j:var('Rb_%s%d' % ( chr(65)+chr(66+i)+',', i+2)))
R = Matrix (niso, niso-1, lambda i,j: var('R_%s%d' % (chr(65+i)+',' , j+1 )))



A = Matrix(niso-1,niso-1, lambda i,j:var('A_%d' % i))

for i in range(0,niso-1):
        for j in range(0,niso-1):
            A[i,j]=Rb[i,0]*MM[i,0] + MM[i,0]

My code only leads to this error message. 我的代码仅导致此错误消息。

TypeError: Can't multiply sequence by non-integer of type '' TypeError:无法将序列乘以类型“”的非整数

The problem is that I thought that my matrices already contain only symbols so that multiplying should work. 问题是我以为我的矩阵已经只包含符号了,所以乘法应该起作用。

I have solved by myselfe! 我自己解决了! The Rb constructor was indeed the problem! Rb构造器确实是问题所在! Changing it to 更改为

R = Matrix (niso, niso-1, lambda i,j: var('R_%s%d' % (chr(65+i) , j+2 )))

helped! 帮助了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM