如何在 sympy 中舍入矩阵元素? [英] How to round Matrix elements in sympy?
本文介绍了如何在 sympy 中舍入矩阵元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
众所周知
from sympy import *
x = sin(pi/4)
y = sin(pi/5)
A = Matrix([x, y])
print(x)
print(A.evalf())
显示
sqrt(2)/2
Matrix([[0.707106781186548], [0.587785252292473]])
所以
print(round(x.evalf(), 3))
print(round(y.evalf(), 3))
显示
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))
可以显示
Matrix([[0.707], [0.588]])
推荐答案
为什么不使用带有 evalf(3)
之类的 args 的方法 evalf?
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))
输出
sqrt(2)/2
Matrix([[0.707], [0.588]])
这篇关于如何在 sympy 中舍入矩阵元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文