mrrclb48z
mrrclb48z

Reputation: 207

sympy matrix element (fraction--> float)? how to

Thank you in advance and sorry for the bad English!

I want print(Float(x0.inv(),3)

from sympy import *
var('a11 a12 a21 a22')
X0=Matrix([[3,1],[5,5]])
print(X0.inv())
a11=float(X0.inv()[0,0])
a12=float(X0.inv()[0,1])
a21=float(X0.inv()[1,0])
a22=float(X0.inv()[1,1])
X0inv=Matrix([[a11,a12],[a21,a22]])
print(X0inv)
# Matrix([[1/2, -1/10], [-1/2, 3/10]])
# Matrix([[0.500000000000000, -0.100000000000000], [-0.500000000000000, 0.300000000000000]])

Upvotes: 0

Views: 378

Answers (1)

smichr
smichr

Reputation: 19047

Objects with numerical elements can be numerically evaluated with the evalf (or n) method:

>>> X0=Matrix([[3,1],[5,5]])
>>> X0.inv()
Matrix([
[ 1/2, -1/10],
[-1/2,  3/10]])
>>> _.n()
Matrix([
[ 0.5, -0.1],
[-0.5,  0.3]])
>>> from sympy import pi
>>> pi.n(3)
3.14

Or you can use the N function:

>>> from sympy import N
>>> N(pi)
3.14159265358979

Upvotes: 2

Related Questions