我目前有以下代码段:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy
from numpy import linalg
A = [[1,2,47,11],[3,2,8,15],[0,0,3,1],[0,0,8,1]]
S = [[113,49,2,283],[-113,0,3,359],[0,5,0,6],[0,20,0,12]]
A = numpy.matrix(A)
S = numpy.matrix(S)
numpy.set_printoptions(precision=2, suppress=True, linewidth=120)
print("S^{-1} * A * S")
print(linalg.inv(S) * A * S)
产生这个输出:
是否有一种标准方法可以产生类似于以下的输出?我怎样才能得到这个输出?
[[ -1 -0.33 0 0]
[ 0 1 0 0]
[ 0 -648 4 0]
[ 0 6.67 0 5]]
有什么不同?
i
的最后一个字符和列 i+1
的第一个字符之间至少有两个空格,但如果需要更多,可能会更多(NumPy 输出两个空格)BetterPythonConsole
的字体设置搞砸了)-0
而是 0
0.
而是 0
edit:似乎是 Python 控制台,它从 gEdits BetterPythonConsole 开始当我从终端启动插件时,插件的作用与 Python 不同。
这是上面脚本的文本输出
moose@pc07:~/Desktop$ python matrixScript.py
S^{-1} * A * S
[[ -1. -0.33 0. -0. ]
[ 0. -1. -0. 0. ]
[ 0. -648. 4. -0. ]
[ 0. 6.67 0. 5. ]]
使用 pretty-print :
S^{-1} * A * S
matrix([[ -1. , -0.33, 0. , -0. ],
[ 0. , -1. , -0. , 0. ],
[ 0. , -648. , 4. , -0. ],
[ 0. , 6.67, 0. , 5. ]])
这肯定更糟,但值得一试。
最佳答案
如果您使用 numpy 1.8.x,您可以使用 formatter
参数自定义格式。
例如设置:
numpy.set_printoptions(formatter={'float': lambda x: 'float: ' + str(x)})
所有 float 都将打印为 float: 3.0
或 float: 12.6666666666
。
不幸的是,我仍然安装了 numpy 1.6.1 并且没有提供此选项, 所以我不能用它来获得你想要的输出。
https://stackoverflow.com/questions/12347797/