jupyter notebook看矩阵
时间: 2023-10-06 07:09:02 浏览: 92
Jupyter Notebook是一种交互式的编程环境,可以通过它来展示和编辑代码、运行程序、进行数据分析等。要在Jupyter Notebook中查看矩阵,可以使用以下代码:
```python
from IPython.display import display, Latex, Math
%matplotlib inline
from IPython.core.interactiveshell import InteractiveShell
sh = InteractiveShell.instance()
def matrix_to_latex(mat, style='bmatrix'):
if type(mat) == np.matrixlib.defmatrix.matrix:
mat = mat.A
head = r'\begin{' + style + '}'
tail = r'\end{' + style + '}'
if len(mat.shape) == 1:
body = r'\\'.join([str(el) for el in mat])
return head + body + tail
elif len(mat.shape) == 2:
lines = []
for row in mat:
lines.append('&'.join([number_to_str(el) for el in row]) + r'\\')
s = head + ' '.join(lines) + tail
return s
return None
sh.display_formatter.formatters['text/latex'].type_printers[np.ndarray] = matrix_to_latex
```
这段代码定义了一个函数`matrix_to_latex`,用于将矩阵转换为Latex格式,然后使用IPython内置的`display`函数来将矩阵以Latex格式展示出来。