Numpy 2d and 1d array to latex matrix

I am looking for a clean way to transfer numpy arrays to a latex matrix. It should work both for two-dimensional arrays, and for horizontal and vertical array 1d.

Example

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]) 

Of:

 [[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} 

Attempt to solve

 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 

This solution works for vertical and 2-dimensional arrays, however, it displays horizontal arrays as vertical.

 array_to_bmatrix(A[0]) 

Of:

 \begin{bmatrix} 12.000 & \\ 5.000 & \\ 2.000 & \\ \end{bmatrix} 
+4
source share
3 answers

The __str__ method of the numpy array already does most of the formatting for you. Let's use it;

 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' 

This returns:

 \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} 
+10
source

When you do this:

  for line in array: 

you iterate over the first size of the array . When the array is 1-D, you end up iterating over the values. You need to make sure that array really 2-D before doing this iteration. One way is to pass the argument through 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: 

and etc.

0
source

I am not happy with using print output from Python. The matrix may be too large to cause wrapping. This is the code for printing LaTeX text for a 2d matrix.

 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 

What gives this

 $\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 &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