-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprettyeqn.py
141 lines (122 loc) · 3.71 KB
/
prettyeqn.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import numpy.typing as npt
class Matrix:
def __init__(self, content: npt.NDArray):
self.content = content
def get_height(self):
return self.content.shape[0] + 2
def get_width(self):
return self.content.shape[1] * 9 + 3
def get_line(self, linenum, total_lines):
centre = total_lines // 2
top = centre - self.get_height() // 2
if linenum < top:
return " " * self.get_width()
linenum = linenum - top
lines = self.get_height()
if linenum >= lines:
return " " * self.get_width()
if linenum == 0:
return "\u250c{}\u2510".format(
" " * (self.get_width()-2)
)
if linenum == lines - 1:
return "\u2514{}\u2518".format(
" " * (self.get_width()-2)
)
format_string = "\u2502 {}\u2502".format(
"{:8.2f} " * self.content.shape[1]
)
return format_string.format(
*self.content[linenum-1,:]
)
class SymbolVector:
def __init__(self, content):
self.content = content
def get_height(self):
return len(self.content) + 2
def get_width(self):
return 5
def get_line(self, linenum, total_lines):
centre = total_lines // 2
top = centre - self.get_height() // 2
if linenum < top:
return " " * self.get_width()
linenum = linenum - top
lines = self.get_height()
if linenum >= lines:
return " " * self.get_width()
if linenum == 0:
return "\u250c{}\u2510".format(
" " * (self.get_width()-2)
)
if linenum == lines - 1:
return "\u2514{}\u2518".format(
" " * (self.get_width()-2)
)
return "\u2502 {} \u2502".format(
self.content[linenum-1]
)
class Symbol:
def __init__(self, symbol):
self.symbol = symbol
def get_height(self):
return 1
def get_width(self):
return 1
def get_line(self, linenum, total_lines):
centre = total_lines // 2
if linenum == centre:
return self.symbol
return " "
class Equation:
def __init__(self, elements):
self.elements = elements
def __str__(self):
line_strings = []
total_lines = max(map(lambda e: e.get_height(), self.elements))
for line_num in range(total_lines):
line_string = ""
for element in self.elements:
line_string += "{} ".format(element.get_line(line_num,total_lines))
line_strings.append(line_string)
return "\n".join(line_strings)
def prettyeqn():
import numpy
from prettyeqn import Equation, SymbolVector, Matrix, Symbol
result = SymbolVector([
'u\u0307',
'w\u0307',
'q\u0307',
'\u03b8\u0307',
'z\u0307',
])
a = Matrix(numpy.array([
[-0.84, 0.71, 0.20, -9.81, -0.00],
[-0.93, -17.34, 25.00, 0.08, -0.00],
[ 0.16, -2.49, -19.79, -0.00, -0.00],
[ 0.00, -0.00, 1.00, 0.00, -0.00],
[ 0.01, 1.00, -0.00, -25.00, -0.00],
]))
state = SymbolVector([
'u',
'w',
'q',
'\u03b8',
'z',
])
b = Matrix(numpy.array([
[ -0.00, 14.68],
[ -0.00, 0.00],
[ 385.54, -15.37],
[ -0.00, 0.00],
[ -0.00, 0.00],
]))
input = SymbolVector([
'\u03b7',
'\u03c4',
])
equation = Equation([result,Symbol('='),a,state,Symbol('+'),b,input])
print(equation)
if __name__ == "__main__":
print("prettyeqn demo!")
prettyeqn()