使用 sympy 求解线性系统 6x6 矩阵

我正在尝试解决一个具有 6x6 矩阵的矩阵作为它的条目(元素)


我尝试将 gen 的倒数乘以解决方案矩阵,但我不相信得到的答案的正确性。


from sympy import Eq, solve_linear_system, Matrix,count_ops,Mul,horner

import sympy as sp


a, b, c, d, e,f = sp.symbols('a b c d e f')


ad = Matrix(([43.4,26.5,115,-40.5,52.4,0.921],

  [3.78,62.9,127,-67.6,110,4.80],

  [41.25,75.0,213,-88.9, 131, 5.88],

  [-10.6,-68.4,-120,64.6,-132,-8.49],

  [6.5,74.3,121,-72.8,179,29.7],

  [1.2,30.7,49.7,-28.7,91,29.9]))

fb= Matrix(([1,0,0,0,0,0],

        [0,1,0,0,0,0],

        [0,0,1,0,0,0],

        [0,0,0,1,0,0],

        [0,0,0,0,1,0],

        [0,0,0,0,0,1]))


ab = Matrix(([-0.0057],

        [0.0006],

        [-0.0037],

        [0.0009],

        [0.0025],

        [0.0042]))

az = sp.symbols('az')

bz = sp.symbols('bz')

fz = sp.symbols('fz')


gen = Matrix(([az, fz, 0, 0, 0, 0,bz],

            [fz,az,fz,0,0,0,bz],

            [0,fz,az,fz,0,0,bz],

                [0,0,fz,az,fz,0,bz],

                [0,0,0,fz,az,fz,bz],

                [0,0,0,0,fz,az,bz]))


answer = solve_linear_system(gen,a,b,c,d,e,f)

first_solution = answer[a]

df = count_ops(first_solution)

print(df,first_solution)

disolved = zip(first_solution.simplify().as_numer_denom(),(1,-1))

dft = Mul(*[horner(b)**e for b,e in disolved])

dff = count_ops(dft)

print(dff,dft)

_1st_solution = dft.subs({az:ad,fz:fb,bz:ab},simultaneous = True).doit()

print(_1st_solution)

当我运行我的代码时,它引发了 sympy.matrices.common.ShapeError


缥缈止盈
浏览 147回答 1
1回答

慕姐4208626

horner在使用包含实际上不可交换的可交换符号的表达式时必须小心(在您的情况下,因为它们表示矩阵)。你的dft表情是(az**2*bz - bz*fz**2)/(az*(az*(az + fz) - 2*fz**2) - fz**3)但也许应该是(az**2 - fz**2)*(az*(az*(az + fz) - 2*fz**2) - fz**3)**(-1)*bz如果您将符号创建为不可交换的,您将收到正确的表达式(如下所示)。但是你不能使用horner非交换符号,所以我只是手动重新排列了表达式;您将必须检查订单是否正确。作为手动进行分解的替代方法,您也可以尝试使用factor_nc来帮助您——但它不会horner像表达式分解那样处理:>>> ax, bz, fz = symbols('az bz fz, commutative=False)>>> (az**2*bz - fz**2*bz)az**2*bz - fz**2*bz>>> factor_nc(_)(az**2 - fz**2)*bz
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python