-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSymEigen.py
348 lines (292 loc) · 11.7 KB
/
SymEigen.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from sympy import *
from sympy.codegen import *
import sympy.printing.c as ccode
AuthorName = '''MuGdxy'''
AuthorGitHub = '''https://github.com/MuGdxy/SymEigen'''
AuthorEmail = '''[email protected]'''
__version__ = '0.1.0'
class Sym:
def Vectorize(M : Matrix, expand_dir : str = 'col'):
# if type(M) == EigenMatrix:
# raise ValueError("Don't call Vectorize on EigenMatrix, use EigenMatrix.Vectorize instead")
V = zeros(M.shape[0]*M.shape[1], 1)
if expand_dir == 'col':
i = 0
# expand in column major order
for j in range(M.shape[1]):
for k in range(M.shape[0]):
V[i] = M[k, j]
i += 1
elif expand_dir == 'row':
i = 0
# expand in row major order
for j in range(M.shape[0]):
for k in range(M.shape[1]):
V[i] = M[j, k]
i += 1
else:
raise ValueError('expand_dir must be either "col" or "row"')
return V
def VecDiff(VecF, VecX):
# if VecF has no shape attribute, it is a scalar
if not hasattr(VecF, 'shape'):
VecF = Matrix([VecF])
if VecF.shape[1] != 1 or VecX.shape[1] != 1:
raise ValueError(f'Inputs of VecDiff must be a column vector or a scalar, VecF[{VecF.shape[0]},{VecF.shape[1]}], VecX[{VecX.shape[0]},{VecX.shape[1]}]')
if(VecF.shape[0] == 1 and VecF.shape[1] == 1): # scalar
return VecF.jacobian(VecX).reshape(VecX.shape[0], 1)
else:
return VecF.jacobian(VecX).reshape(VecF.shape[0], VecX.shape[0])
class EigenMatrix(MutableDenseMatrix):
def __init__(self, *args, **kwargs):
self.name = None
self.to_origin_element_name = {}
self.from_origin_element_name = {}
self.origin_matrix = None
self.IS_EIGEN_MATRIX = True
def ValueType(self, Type = 'T'):
if self.shape[0] == 1 and self.shape[1] == 1:
return Type
elif self.shape[1] == 1:
return f'Eigen::Vector<{Type},{self.shape[0]}>'
elif self.shape[0] == 1:
return f'Eigen::RowVector<{Type},{self.shape[1]}>'
else:
return f'Eigen::Matrix<{Type},{self.shape[0]},{self.shape[1]}>'
def RefType(self, Type = 'T'):
return f'{self.ValueType(Type)}&'
def CRefType(self, Type = 'T'):
return f'const {self.ValueType(Type)}&'
# def [i, j]
def At(self, i, j):
'''
return the string representation of the element at (i, j)
e.g.
A.at(0, 0) -> 'A(0, 0)' for a matrix A
A.at(0, 0) -> 'A(0)' for a vector A
A.at(0, 0) -> 'A' for a scalar A
'''
if self.shape[0] == 1 and self.shape[1] == 1:
assert i == 0 and j == 0, 'Scalar has only one element'
return self.name
elif self.shape[1] == 1:
assert j == 0, 'Vector has only one column'
return self.name + f'({i})'
elif self.shape[0] == 1:
assert i == 0, 'Vector has only one row'
return self.name + f'({j})'
return self.name + f'({i},{j})'
def OriginMatrixName(self):
if self.origin_matrix is None:
return self.name
return self.origin_matrix.name
def MatrixName(self):
return self.name
def IsIndependent(self):
return self.origin_matrix is None
def Vectorize(self, Name, expand_dir : str = 'col'):
SymV = Sym.Vectorize(self, expand_dir)
Vector = EigenMatrix(SymV)
Vector.name = Name
Vector._build_remap(SymV)
Vector.origin_matrix = self
return Vector
def _build_remap(self, M : MutableDenseMatrix):
assert self.shape == M.shape, 'Shape mismatch'
for i in range(M.shape[0]):
for j in range(M.shape[1]):
if(not M[i, j].is_number): # if it is a symbol
self.to_origin_element_name[self.At(i, j)] = str(M[i, j])
self.from_origin_element_name[str(M[i, j])] = self.At(i, j)
def __rtruediv__(self, other):
if(self.shape[0]==1 and self.shape[1]==1):
return other / self[0, 0]
else:
raise ValueError('Only scalar is supported for right division')
class Eigen:
def Matrix(Name, M, N):
EMat = EigenMatrix(zeros(M,N))
if(M == 1 and N == 1):
EMat[0, 0] = Symbol(Name, real=True)
elif(N == 1):
for i in range(M):
EMat[i, 0] = Symbol(f'{Name}({i})', real=True)
elif(M == 1):
for i in range(N):
EMat[0, i] = Symbol(f'{Name}({i})', real=True)
else:
for i in range(M):
for j in range(N):
EMat[i, j] = Symbol(f'{Name}({i},{j})', real=True)
EMat.name = Name
EMat.origin_matrix = EMat
return EMat
def FromSympy(Name, M : MutableDenseMatrix):
if hasattr(M, 'shape'):
EMat = EigenMatrix(M)
EMat.name = Name
EMat._build_remap(M)
EMat.origin_matrix = None
else:
EMat = Eigen.Scalar(Name)
return EMat
def Vector(Name, N):
return Eigen.Matrix(Name, N, 1)
def RowVector(Name, N):
return Eigen.Matrix(Name, 1, N)
def Scalar(Name):
return Eigen.Matrix(Name, 1, 1)
def Subs(Expr, SubsList:list[tuple[EigenMatrix, Matrix]]):
new_dict = {}
for key, value in SubsList:
for i in range(value.shape[0]):
for j in range(value.shape[1]):
new_dict[key[i, j]] = value[i, j]
return Expr.subs(new_dict)
def Normalize(Expr):
return Expr / sqrt(Expr.dot(Expr))
def Cross(Expr):
return Expr.cross(Expr)
class EigenPrinter(ccode.C11CodePrinter):
def _print_Pow(self, expr):
base = f'{self._print(expr.base)}'
exp = f'{self._print(expr.exp)}'
return f'std::pow({base}, {exp})'
def _print_not_supported(self, expr):
print(f'Not supported: {expr}')
class EigenFunctionInputClosure:
def __init__(self, printer : EigenPrinter, option_dict : dict, *args : EigenMatrix):
Vars = [ var for var in args]
for i in range(len(Vars)):
if not hasattr(Vars[i], 'IS_EIGEN_MATRIX'):
Vars[i] = Eigen.FromSympy(Vars[i].name, Vars[i])
self.Args = Vars
self.printer = printer
self.option_dict = option_dict
def __call__(self, FunctionName : str, expr : Expr, return_value_name:str = 'R'):
# if expr has no shape attribute, it is a scalar
if(not hasattr(expr, 'shape')):
R = Eigen.Scalar(return_value_name)
else:
R = Eigen.Matrix(return_value_name, expr.shape[0], expr.shape[1])
FunctionDef = self._make_function_def(FunctionName, R, expr)
MaxLineLen = 120
# MaxLineLen = len(FunctionDef)
Comment = self._make_comment(R, expr)
CommentStr = '\n'.join(Comment)
# MaxLineLen = max([len(line) for line in Comment] + [MaxLineLen])
Content = self._make_content(R, expr)
ContentStr = '\n'.join(Content)
# MaxLineLen = max([len(line) for line in Content] + [MaxLineLen])
SepLine = '*' * (MaxLineLen + 4)
return f'''{FunctionDef}
{{
/*{SepLine}
Function generated by SymEigen.py
Author: {AuthorName}
GitHub: {AuthorGitHub}
E-Mail: {AuthorEmail}
**{SepLine}
{CommentStr}
{SepLine}*/
{ContentStr}
}}'''
def _make_function_def(self, FunctionName, R, expr):
Vars = self.Args
T = 'T'
OutputT = R.RefType(T)
Args = []
for i in range(len(Vars)):
Args.append(Vars[i].CRefType(T) + f' {Vars[i].MatrixName()}')
ArgStr = ', '.join(Args)
FOWARD_MACRO = self.option_dict['MacroBeforeFunction']
if len(FOWARD_MACRO) > 0:
FOWARD_MACRO = f'{FOWARD_MACRO} '
return f'''template <typename T>
{FOWARD_MACRO}void {FunctionName}({OutputT} {R.MatrixName()}, {ArgStr})'''
def _make_comment(self, R, expr):
Vars = self.Args
Comment = []
if self.option_dict['LatexComment']:
Comment.append(f'''LaTeX expression:
//tex:$${R.MatrixName()} = {latex(expr)}$$\n''')
Comment.append(f'''Symbol Name Mapping:''')
for var in Vars:
Comment.append(f'''{var.name}:
-> {var.to_origin_element_name}
-> {var}''')
return Comment
def _make_content(self, R, expr):
Vars = self.Args
Content = []
if self.option_dict['CommonSubExpression']:
sub_exprs, simplified = cse(expr)
cse_prefix = self.option_dict['CommonSubExprPrefix']
Content.append('/* Sub Exprs */')
for i in range(len(sub_exprs)):
E = sub_exprs[i][1]
EStr = self.printer._print(E)
EStr = self._replace_symbol(EStr, Vars)
Content.append(f'auto {cse_prefix}{sub_exprs[i][0]} = {EStr};')
Content.append('/* Simplified Expr */')
for S in simplified:
# S has shape
for i in range(R.shape[0]):
for j in range(R.shape[1]):
if S.is_Matrix:
E = S[i,j]
else:
E = S
EStr = self.printer._print(E)
EStr = self._replace_symbol(EStr, Vars)
Content.append(f'{R.At(i, j)} = {EStr};')
else:
for i in range(R.shape[0]):
for j in range(R.shape[1]):
if(hasattr(expr, 'shape')):
E = expr[i, j]
else:
E = expr
EStr = self.printer._print(E)
EStr = self._replace_symbol(EStr, Vars)
Content.append(f'{R.At(i, j)} = {EStr};')
return Content
def _replace_symbol(self, Str, Vars):
for var in Vars:
for key, value in var.from_origin_element_name.items():
Str = Str.replace(key, value)
return Str
class EigenFunctionGenerator:
def __init__(self, printer = EigenPrinter()):
self.printer = printer
self.option_dict = {
'MacroBeforeFunction': '',
'CommonSubExpression': True,
'LatexComment': True,
'CommonSubExprPrefix': ''
}
def MacroBeforeFunction(self, macro: str):
self.option_dict['MacroBeforeFunction'] = macro
def DisableCommonSubExpression(self):
self.option_dict['CommonSubExpression'] = False
def DisableLatexComment(self):
self.option_dict['LatexComment'] = False
def CommonSubExprPrefix(self, prefix: str):
self.option_dict['CommonSubExprPrefix'] = prefix
def Closure(self, *args : EigenMatrix):
return EigenFunctionInputClosure(self.printer, self.option_dict, *args)
def EigenVectorizeCode(self):
return '''template <typename T, int M, int N>
void Vectorize(Eigen::Vector<T, M * N>& Vec, const Eigen::Matrix<T, M, N>& Mat)
{
/******************************************************************************
Function generated by SymEigen.py
Author: %s
GitHub: %s
E-Mail: %s
******************************************************************************/
static_assert(M > 0 && N > 0, "Invalid Input Matrix");
for(int j = 0; j < N; ++j)
Vec.template segment<M>(M * j) = Mat.template block<M,1>(0, j);
}
''' % (AuthorName, AuthorGitHub, AuthorEmail)