Numpy 2d 和 1d 数组到乳胶 bmatrix

问题描述 投票:0回答:12

我正在寻找一种将 numpy 数组迁移到 Latex bmatrix 的干净方法。它应该适用于二维数组以及水平和垂直一维数组。

示例

A = array([[12, 5, 2],
           [20, 4, 8],
           [ 2, 4, 3],
           [ 7, 1,10]])

print A              #2d array
print A[0]           #horizontal array
print A[:,0, None]   #vertical array

array_to_bmatrix(A)
array_to_bmatrix(A[0])
array_to_bmatrix(A[:,0, None])

出:

[[12  5  2]
 [20  4  8]
 [ 2  4  3]
 [ 7  1 10]]

[12  5  2]

[[12]
 [20]
 [ 2]
 [ 7]]

\begin{bmatrix} 
 12.000 & 5.000 & 2.000 & \\
 20.000 & 4.000 & 8.000 & \\
 2.000 & 4.000 & 3.000 & \\
 7.000 & 1.000 & 10.000 & \\
\end{bmatrix}

\begin{bmatrix} 
 12.000 & 5.000 & 2.000
\end{bmatrix}

\begin{bmatrix} 
 12.000 & \\
 20.000 & \\
 2.000 & \\
 7.000 & \\
\end{bmatrix}

尝试解决方案

def array_to_bmatrix(array):
    begin = '\\begin{bmatrix} \n'
    data = ''
    for line in array:        
        if line.size == 1:
            data = data + ' %.3f &'%line
            data = data + r' \\'
            data = data + '\n'
            continue
        for element in line:
            data = data + ' %.3f &'%element

        data = data + r' \\'
        data = data + '\n'
    end = '\end{bmatrix}'
    print begin + data + end  

此解决方案适用于垂直和二维数组,但它将水平数组输出为垂直数组。

array_to_bmatrix(A[0])

出:

\begin{bmatrix} 
 12.000 & \\
 5.000 & \\
 2.000 & \\
\end{bmatrix}
python numpy latex
12个回答
35
投票

numpy 数组的

__str__
方法已经为您完成了大部分格式化。让我们利用它;

import numpy as np

def bmatrix(a):
    """Returns a LaTeX bmatrix

    :a: numpy array
    :returns: LaTeX bmatrix as a string
    """
    if len(a.shape) > 2:
        raise ValueError('bmatrix can at most display two dimensions')
    lines = str(a).replace('[', '').replace(']', '').splitlines()
    rv = [r'\begin{bmatrix}']
    rv += ['  ' + ' & '.join(l.split()) + r'\\' for l in lines]
    rv +=  [r'\end{bmatrix}']
    return '\n'.join(rv)

A = np.array([[12, 5, 2], [20, 4, 8], [ 2, 4, 3], [ 7, 1, 10]])
print bmatrix(A) + '\n'

B = np.array([[1.2], [3.7], [0.2]])
print bmatrix(B) + '\n'

C = np.array([1.2, 9.3, 0.6, -2.1])
print bmatrix(C) + '\n'

返回:

\begin{bmatrix}
  12 & 5 & 2\\
  20 & 4 & 8\\
  2 & 4 & 3\\
  7 & 1 & 10\\
\end{bmatrix}

\begin{bmatrix}
  1.2\\
  3.7\\
  0.2\\
\end{bmatrix}

\begin{bmatrix}
  1.2 & 9.3 & 0.6 & -2.1\\
\end{bmatrix}

5
投票

尝试

array_to_latex (pip install)
。我就是因为这个原因才写的。不足之处请提供您的反馈。

它具有默认值,但也允许您自定义格式(指数、小数位数)并处理复数,并且可以将结果直接“弹出”到剪贴板中(无需复制转储到屏幕上的文本)。

github 存储库中的一些示例。 https://github.com/josephcslater/array_to_latex


3
投票

我对使用 Python 的打印输出不满意。矩阵可能太大,导致缠绕。 这是打印 2d 矩阵的 LaTeX 文本的代码。

def bmatrix(a):
    text = r'$\left[\begin{array}{*{'
    text += str(len(a[0]))
    text += r'}c}'
    text += '\n'
    for x in range(len(a)):
        for y in range(len(a[x])):
            text += str(a[x][y])
            text += r' & '
        text = text[:-2]
        text += r'\\'
        text += '\n'
    text += r'\end{array}\right]$'

    print text

这给出了这个

$\left[\begin{array}{*{16}c}
2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 \\
0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 \\
0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 \\
-1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 \\
-1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 \\
0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 \\
0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 \\
\end{array}\right]$

2
投票

另一种选择是使用 sympy:首先将数组转换为 sympy.Matrix,然后使用 sympy.latex 函数。


1
投票

进一步的答案,受到罗兰史密斯答案的启发:

def matToTex(a, roundn=2, matrixType = "b",rowVector = False):
    if type(a) != np.ndarray:
        raise ValueError("Input must be np array")
    if len(a.shape) > 2:
        raise ValueError("matrix can at most display two dimensions")
    if matrixType not in ["b","p"]:
        raise ValueError("matrix can be either type \"b\" or type \"p\"")
    if rowVector:
        if not (len(a.shape) != 1 or a.shape[0] != 1):
            raise ValueError("Cannot rowVector this bad boi, it is not a vector!")
    lines = str(a).splitlines()
    ret = "\n\\begin{"+matrixType+"matrix}\n"
    for line in lines:
        line = re.sub("\s+",",",re.sub("\[|\]","",line).strip())
        nums = line.split(",");
        if roundn != -1:
            nums = [str(round(float(num),roundn)) for num in nums]
        if rowVector:
            ret += " \\\\\n".join(nums)
        else:
            ret += " & ".join(nums)+" \\\\ \n"
    ret += "\n\\end{"+matrixType+"matrix}\n"
    ret = re.sub("(\-){0,1}0.[0]* ","0 ",ret)
    print(ret)

1
投票

另一个,受到罗兰史密斯的回答的启发 支持科学记数格式

def bmatrix(a):
    """Returns a LaTeX bmatrix

    :a: numpy array
    :returns: LaTeX bmatrix as a string
    """
    if len(a.shape) > 2:
        raise ValueError('bmatrix can at most display two dimensions')
    temp_string = np.array2string(a, formatter={'float_kind':lambda x: "{:.2e}".format(x)})
    lines = temp_string.replace('[', '').replace(']', '').splitlines()
    rv = [r'\begin{bmatrix}']
    rv += ['  ' + ' & '.join(l.split()) + r'\\' for l in lines]
    rv +=  [r'\end{bmatrix}']
    return '\n'.join(rv)

结果:

\begin{bmatrix}
  7.53e-04 & -2.93e-04 & 2.04e-04 & 5.30e-05 & 1.84e-01 & -2.43e-05\\
  -2.93e-04 & 1.19e-01 & 2.96e-01 & 2.19e-01 & 1.98e+01 & 8.61e-03\\
  2.04e-04 & 2.96e-01 & 9.60e-01 & 7.42e-01 & 4.03e+01 & 2.45e-02\\
  5.30e-05 & 2.19e-01 & 7.42e-01 & 6.49e-01 & 2.82e+01 & 1.71e-02\\
  1.84e-01 & 1.98e+01 & 4.03e+01 & 2.82e+01 & 5.75e+03 & 1.61e+00\\
  -2.43e-05 & 8.61e-03 & 2.45e-02 & 1.71e-02 & 1.61e+00 & 7.04e-03\\
\end{bmatrix}

1
投票

只是为了详细说明macleginn 的答案。

import numpy as np
import sympy as sym
from IPython.display import display, Math
A = np.array([[12, 5, 2],
       [20, 4, 8],
       [ 2, 4, 3],
       [ 7, 1,10]])
A = sym.Matrix(A)
display(A)

0
投票

当你这样做时:

    for line in array:

您正在迭代

array
的第一个维度。当数组是一维时,您最终会迭代这些值。在进行此迭代之前,您需要确保
array
确实是二维的。一种方法是通过
numpy.atleast_2d
:

传递参数
import numpy as np

def array_to_bmatrix(array):
    array = np.atleast_2d(array)
    begin = '\\begin{bmatrix} \n'
    data = ''
    for line in array:

等等


0
投票

我尝试制定一个全面的解决方案,以便个人不需要编写哪怕是最小的脚本来完成它。我为浮点数、格式化、复杂数组和 Pandas 数组提供了灵活性。请使用 (array_to_latex)[https://pypi.org/project/array-to-latex/] .

并向其提供反馈

0
投票

如果你碰巧使用

qiskit
,你可以尝试来自
array_to_latex
的时尚方法
qiskit.visualization

请参阅此处

示例片段:

from qiskit.visualization import array_to_latex
import numpy as np

x = np.zeros(100).reshape(10,10)


# Max rows and cols = 24
array_to_latex(array=x, prefix='Output = ', max_size=(10,10)) # If max_size not set then matrix will have ellipses
# print latex source only: Source=True
latex_source = array_to_latex(array=x, source=True, max_size=(10,10))

# If you are using Jupyter Notebook: 
from IPython.display import display, Markdown
display(Markdown(latex_source))

输出示例:


0
投票

此外,对于之前的答案,您可以通过这种方式从数组生成乳胶

from IPython.display import *
from numpy import *
A = array([[12, 5, 2],
       [20, 4, 8],
       [ 2, 4, 3],
       [ 7, 1,10]])

latexA = '$$\n' + r'\begin{bmatrix}' + '\n' + (r'\\' + '\n').join('&'.join(str(x) for x in row) for row in A) + '\n' + r'\end{bmatrix}' + '\n' +'$$'
print(latexA)

display(Latex(latexA))

打印输出如下。

$$
\begin{bmatrix}
12&5&2\\
20&4&8\\
2&4&3\\
7&1&10
\end{bmatrix}
$$

0
投票

如果您有

sympy

>>> from sympy import Matrix, latex
>>> M = np.array([[1,2],[3,4]])
>>> print(latex(Matrix(M)))
\left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]

还有其他一些不错的实用程序可以帮助解决此问题:https://docs.sympy.org/latest/tutorials/intro-tutorial/printing.html

© www.soinside.com 2019 - 2024. All rights reserved.