我们如何将Sympy符号矩阵的所有条目乘以另一个符号?

时间:2016-11-22 07:13:04

标签: python-2.7 sympy

我目前正致力于涉及矩阵的同情符号表达。 我想知道是否可以将一个这样的矩阵乘以另一个符号。换句话说,我希望下面的矩阵Bn_Ksi的所有行都乘以gN 这是我想尝试的

    B_Ksi  = (1 / H_KsiKSi) * ( Bt + gN * Bn_Ksi ) 

问题来自:

    ipdb> gN * Bn_Ksi
    *** TypeError: can't multiply sequence by non-int of type 'Symbol'

我也尝试使用 mul 并得到:

    ipdb> gN.__mul__(Bn_Ksi[1])
    NotImplemented

以下是我的代码中允许出错的部分:

from sympy import *


x, y , Alpha, Ksi = symbols("x y Alpha Ksi")
x1x, x1y, x2x, x2y, x3x, x3y  = symbols("x1x x1y x2x x2y x3x x3y")

N, nx, ny   =  symbols("N nx ny")

# gap function and penalty stiffness 
gN, kpenN = symbols("gN, kpenN")



S = Matrix([x, y])
x1 = Matrix([x1x, x1y])
x2 = Matrix([x2x, x2y])
x3 = Matrix([x3x, x3y])

N = Matrix([nx, ny])


# control points
b0 = x2 + 0.5 * (x1 - x2)
#b3
b3 = x2 + 0.5 * (x3 - x2)
#b1
b1 = b0 + (x2- b0) * Alpha
#b2
b2 = x2 + (b3 - x2) * ( 1 - Alpha)



# Berstein polynomials
# B1 
B0 =  (1./8.) * ((1 - Ksi)**3)
# B1
B1 = (3./8.) * (1 - Ksi)**2 *  (1 + Ksi)
# B3
B2 =  (3./8.) * (1 - Ksi) * (1 + Ksi)**2
#B4
B3 = (1./8.) *(1 + Ksi)**3

# Berstein polynomials first order derivative
B0_Ksi = diff(B0, Ksi)
B1_Ksi = diff(B1, Ksi)
B2_Ksi = diff(B2, Ksi)
B3_Ksi = diff(B3, Ksi)


x_interp = b0 * B0 +  b1 * B1 + b2 * B2 + b3 * B3


# first order derivative 
x_Ksi = diff(x_interp, Ksi)
#second order derivative
x_KsiKsi = diff(x_interp, Ksi, 2)


Bn  = Matrix([N, -B0 * N, -B1 * N, -B2 * N, -B3 * N])

Bn_Ksi  = Matrix([0, B0_Ksi * N, B1_Ksi * N, B2_Ksi * N, B3_Ksi * N])

Bt      = Matrix([x_Ksi, -B0_Ksi * x_Ksi, B1_Ksi * x_Ksi, B2_Ksi * x_Ksi, B3_Ksi * x_Ksi])


H_KsiKSi = x_Ksi.dot(x_Ksi) - gN * N.dot(x_KsiKsi)

B_Ksi  = (1 / H_KsiKSi) * ( Bt + gN * Bn_Ksi ) 

我是一个普通方式的同情和Python的初学者,所以我希望答案毫无意义。 在此先感谢您的帮助

1 个答案:

答案 0 :(得分:1)

矩阵Bn_Ksk的元素是矩阵。 SymPy Matrix对象旨在容纳标量,而不是其他矩阵。您应该将矩阵展平以包含标量,或者如果需要块矩阵(矩阵矩阵),则使用sympy.BlockMatrix