How to round Matrix elements in sympy?

本小妞迷上赌 提交于 2019-12-04 04:22:29

问题


As we know

from sympy import *

x = sin(pi/4)
y = sin(pi/5)

A = Matrix([x, y])

print(x)
print(A.evalf())

displays

sqrt(2)/2
Matrix([[0.707106781186548], [0.587785252292473]])

So

print(round(x.evalf(), 3))
print(round(y.evalf(), 3))

displays

0.707
0.588

But how can we round all the elements in a Matrix in a terse way, so that

print(roundMatrix(A, 3))

can displays

Matrix([[0.707], [0.588]])

回答1:


Why you do not use method evalf with args like evalf(3)?

from sympy import *

x = sin(pi/4)
y = sin(pi/5)

A = Matrix([x, y])

print(x)
print(A.evalf(3))

Output

sqrt(2)/2
Matrix([[0.707], [0.588]])


来源:https://stackoverflow.com/questions/53844884/how-to-round-matrix-elements-in-sympy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!