Kkit.str2latex

This module is used to transfer between LaTex bmatrix and numpy array.

Example:

from Kkit import str2latex

bmatrix = r'''
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6
\end{bmatrix}
'''

array = str2latex.bmatrix2numpy(bmatrix)

print(array)
# [[1 2 3]
#  [4 5 6]]

bmatrix = str2latex.numpy2bmatrix(array)

print(bmatrix)

# egin{bmatrix}
# 1&2&3\
# 4&5&6
# \end{bmatrix}
  1"""
  2This module is used to transfer between LaTex bmatrix and numpy array.
  3
  4Example:
  5
  6```python
  7from Kkit import str2latex
  8
  9bmatrix = r'''
 10\\begin{bmatrix}
 111 & 2 & 3 \\\\
 124 & 5 & 6
 13\end{bmatrix}
 14'''
 15
 16array = str2latex.bmatrix2numpy(bmatrix)
 17
 18print(array)
 19# [[1 2 3]
 20#  [4 5 6]]
 21
 22bmatrix = str2latex.numpy2bmatrix(array)
 23
 24print(bmatrix)
 25
 26# \begin{bmatrix}
 27# 1&2&3\\
 28# 4&5&6
 29# \end{bmatrix}
 30```
 31"""
 32
 33# Transfer between string and object
 34# Author:walkureHHH
 35# Last modify:2020/10/14
 36import numpy
 37
 38class latex_str_error(Exception):
 39    pass
 40
 41def bmatrix2numpy(latex_str)->numpy.array:
 42    """
 43    Transfer a latex bmatrix to a numpy array
 44
 45    Parameters
 46    ----------
 47    latex_str : str
 48        The latex string
 49
 50    Returns
 51    -------
 52    numpy.array
 53        The numpy array
 54    """
 55    latex_str = latex_str.strip()
 56    if latex_str.startswith('\\begin{bmatrix}') and latex_str.endswith('\\end{bmatrix}'):
 57        latex_str = latex_str.replace('\\begin{bmatrix}','').replace('\\end{bmatrix}','')
 58        rows = latex_str.split('\\\\')
 59        lis = [i.split('&') for i in rows]
 60        return numpy.array(lis)
 61    else:
 62        raise latex_str_error("this is not a bmatrix,please check it again")
 63
 64def numpy2bmatrix(array)->str:
 65    """
 66    Transfer a numpy array to a latex bmatrix
 67
 68    Parameters
 69    ----------
 70    array : numpy.array
 71        The numpy array
 72
 73    Returns
 74    -------
 75    str
 76        The latex string
 77    """
 78    s = '\\begin{bmatrix}\n'
 79    shape = numpy.shape(array)
 80    str_list = array.tolist()
 81    for i in range(0,shape[0]):
 82        for j in range(0,shape[1]):
 83            if j < shape[1]-1:
 84                s += (str(str_list[i][j])+'&')
 85            else:
 86                s += str(str_list[i][j])
 87        if i < shape[0]-1:
 88            s += '\\\\\n'
 89        else:
 90            s += '\n\\end{bmatrix}'
 91    return s
 92
 93# def str2list(list_str,mod_str = ',')->list:
 94#     list_str = list_str.strip().replace('\n',mod_str)
 95#     return list_str.split(mod_str)
 96
 97# def list2str(list_,mod_str=',')->str:
 98#     result = ''
 99#     for i in range(0,len(list_)):
100#         if i < len(list_)-1:
101#             result += (str(list_[i])+mod_str)
102#         else:
103#             result += str(list_[i])
104#     return "[%s]"%result
105
106# if __name__ == "__main__":
107#     lista = [1,2,3,4,5,6]
108#     print(lista)
109#     print(list2str(lista))
class latex_str_error(builtins.Exception):
39class latex_str_error(Exception):
40    pass

Common base class for all non-exit exceptions.

def bmatrix2numpy(latex_str) -> <built-in function array>:
42def bmatrix2numpy(latex_str)->numpy.array:
43    """
44    Transfer a latex bmatrix to a numpy array
45
46    Parameters
47    ----------
48    latex_str : str
49        The latex string
50
51    Returns
52    -------
53    numpy.array
54        The numpy array
55    """
56    latex_str = latex_str.strip()
57    if latex_str.startswith('\\begin{bmatrix}') and latex_str.endswith('\\end{bmatrix}'):
58        latex_str = latex_str.replace('\\begin{bmatrix}','').replace('\\end{bmatrix}','')
59        rows = latex_str.split('\\\\')
60        lis = [i.split('&') for i in rows]
61        return numpy.array(lis)
62    else:
63        raise latex_str_error("this is not a bmatrix,please check it again")

Transfer a latex bmatrix to a numpy array

Parameters

latex_str : str The latex string

Returns

numpy.array The numpy array

def numpy2bmatrix(array) -> str:
65def numpy2bmatrix(array)->str:
66    """
67    Transfer a numpy array to a latex bmatrix
68
69    Parameters
70    ----------
71    array : numpy.array
72        The numpy array
73
74    Returns
75    -------
76    str
77        The latex string
78    """
79    s = '\\begin{bmatrix}\n'
80    shape = numpy.shape(array)
81    str_list = array.tolist()
82    for i in range(0,shape[0]):
83        for j in range(0,shape[1]):
84            if j < shape[1]-1:
85                s += (str(str_list[i][j])+'&')
86            else:
87                s += str(str_list[i][j])
88        if i < shape[0]-1:
89            s += '\\\\\n'
90        else:
91            s += '\n\\end{bmatrix}'
92    return s

Transfer a numpy array to a latex bmatrix

Parameters

array : numpy.array The numpy array

Returns

str The latex string