forked from JelteF/PyLaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.py
117 lines (78 loc) · 2.39 KB
/
math.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
"""
This module implements the classes that deal with math.
.. :copyright: (c) 2014 by Jelte Fennema.
:license: MIT, see License for more details.
"""
from .base_classes import LatexObject, Command, Container
from pylatex.package import Package
class Math(Container):
"""A class representing a math environment.
:param data:
:param inline:
:type data: list
:type inline: bool
"""
def __init__(self, data=None, inline=False):
self.inline = inline
super().__init__(data)
def dumps(self):
"""Return a LaTeX formatted string representing the object.
:rtype: str
"""
if self.inline:
string = '$' + super().dumps(token=' ') + '$'
else:
string = '$$' + super().dumps(token=' ') + '$$\n'
super().dumps()
return string
class VectorName(Command):
"""A class representing a named vector.
:param name:
:type name: str
"""
def __init__(self, name):
super().__init__('mathbf', arguments=name)
class Matrix(LatexObject):
"""A class representing a matrix.
:param matrix:
:param name:
:param mtype:
:param alignment:
:type matrix: :class:`numpy.ndarray` instance
:type name: str
:type mtype: str
:type alignment: str
"""
def __init__(self, matrix, name='', mtype='p', alignment=None):
import numpy as np
self._np = np
self.mtype = mtype
self.matrix = matrix
self.alignment = alignment
self.name = name
super().__init__(packages=[Package('amsmath')])
def dumps(self):
"""Return a string representin the matrix in LaTeX syntax.
:rtype: str
"""
string = r'\begin{'
mtype = self.mtype + 'matrix'
if self.alignment is not None:
mtype += '*'
alignment = '{' + self.alignment + '}'
else:
alignment = ''
string += mtype + '}' + alignment
string += '\n'
shape = self.matrix.shape
for (y, x), value in self._np.ndenumerate(self.matrix):
if x:
string += '&'
string += str(value)
if x == shape[1] - 1 and y != shape[0] - 1:
string += r'\\' + '\n'
string += '\n'
string += r'\end{' + mtype + '}'
super().dumps()
return string