vfg4341
vfg4341

Reputation: 13

Matrix operation errors in sympy

I am trying to write a sympy script, but facing problems at L1,L2. Any help is welcome.

At L1, I am trying to substitute values in matrix multiplication expression in earlier line. Can't I substitute in matrix object?

At L2, I am trying to evaluate value of the substitution in floating point numbers, but not getting it. Why eval() is not giving floating point value?

from sympy import *
init_printing()
x, x1, x2, x3 = symbols ('x x1 x2 x3')
N1 = ((x-x2)*(x-x3))/( (x1-x2) * (x1-x3) )
N2 = ((x-x1)*(x-x3))/( (x2-x1) * (x2-x3) )
N = Matrix([ [N1,N2] ] )
expr1 = N*Transpose(N)
print expr1.subs([ (x1,0.0), (x2,2.5), (x3,5) ]) #L1
N1.evalf (subs={x1:0.0, x2:2.5, x3:5}) #L2

EDIT:

After one answer, I am putting some extra lines of code. Lines `L3,L4, are not working. At L3,L4, I am trying to integrate f1, which is a matrix, and row of that matrix. But failing in both cases. How to integrate a matrix in sympy? Is it supported?

from sympy import *
init_printing()
x, x1, x2, x3 = symbols ('x x1 x2 x3')
N1 = ((x-x2)*(x-x3))/( (x1-x2) * (x1-x3) )
N2 = ((x-x1)*(x-x3))/( (x2-x1) * (x2-x3) )
N = Matrix([ [N1,N2] ] )
expr1 = N*Transpose(N)
print expr1.subs([ (x1,0.0), (x2,2.5), (x3,5) ]) #L1
N1s=N1.subs( [ (x1,0.0), (x2,2.5), (x3,5) ])
N2s=N2.subs( [ (x1,0.0), (x2,2.5), (x3,5) ])
Ns = Matrix([ [N1s,N2s] ] )
f1 = Ns*x**3
f2 = integrate(f1,(x,0,5)) #L3
f2 = integrate(f1.row(1),(x,0,5)) #L4

Upvotes: 1

Views: 199

Answers (1)

asmeurer
asmeurer

Reputation: 91450

The first line works for me. Make sure you are using the latest version of SymPy (1.0 at the time of this writing).

The second line does not work because evalf(subs={...}) doesn't work unless you substitute all the variables in an expression. See https://github.com/sympy/sympy/issues/6974. If you only want to substitute some of the variables, just use subs.

Upvotes: 1

Related Questions