我准备了一个扩充矩阵,如下所示
A = np.round(10*np.random.randn(5,5))
I = np.eye(5)
AI = np.hstack((A, I))
AI = sy.Matrix(AI)
AI
现在执行行约形算法,求逆矩阵的结果有一个接一个的数字,有人能告诉我如何在SymPy中控制矩阵对象的精度吗?
AI_rref = AI.rref()
Ainv = AI_rref[0][:,5:]
Ainv
发布于 2020-03-22 10:33:48
您可以使用applyfunc
将四舍五入应用于给定的数字,也可以使用evalf
/n
方法提供一定数量的重要数字。
>>> from sympy import randMatrix
>>> m = (randMatrix(3,3).row_join(eye(3))).rref()[0]
>>> m.applyfunc(lambda x: round(x, 3))
Matrix([
[1, 0, 0, 0.004, -0.022, 0.016],
[0, 1, 0, 0.015, 0.001, -0.006],
[0, 0, 1, -0.022, 0.027, 0.0]])
>>> m.n(3)
Matrix([
[1.0, 0, 0, 0.00404, -0.0225, 0.016],
[ 0, 1.0, 0, 0.0149, 0.000538, -0.00604],
[ 0, 0, 1.0, -0.0216, 0.0268, 0.000228]])
https://stackoverflow.com/questions/60796812
复制相似问题