Reputation: 651
I am currently working on sympy symbolic expressions that involve Matrices.
I would like to know if it is possible to multiply one such matrix by another symbol. In other words, I would like all the lines of the matrix Bn_Ksi
in the following to be multiplied by gN
This is what I wanted to try with
B_Ksi = (1 / H_KsiKSi) * ( Bt + gN * Bn_Ksi )
The problem comes from:
ipdb> gN * Bn_Ksi
*** TypeError: can't multiply sequence by non-int of type 'Symbol'
I also tried using mul and got:
ipdb> gN.__mul__(Bn_Ksi[1])
NotImplemented
Here is the part of my code allowing to get the error:
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 )
I am a beginner in sympy and Python in a general way, so I hope that the answer is not pointless. Thanks in advance for your help
Upvotes: 1
Views: 1395
Reputation: 91580
The elements of your matrix Bn_Ksk
are matrices. SymPy Matrix
objects are designed to hold scalars, not other matrices. You should either flatten the matrix to contain scalars, or use sympy.BlockMatrix
if you want a block matrix (matrix of matrices).
Upvotes: 2