/
utils.py
89 lines (58 loc) · 2.24 KB
/
utils.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
import numpy as np
from m2cgen import ast
def mul(left, right, to_reuse=False):
return ast.BinNumExpr(left, right, ast.BinNumOpType.MUL, to_reuse=to_reuse)
def div(left, right, to_reuse=False):
return ast.BinNumExpr(left, right, ast.BinNumOpType.DIV, to_reuse=to_reuse)
def add(left, right, to_reuse=False):
return ast.BinNumExpr(left, right, ast.BinNumOpType.ADD, to_reuse=to_reuse)
def sub(left, right, to_reuse=False):
return ast.BinNumExpr(left, right, ast.BinNumOpType.SUB, to_reuse=to_reuse)
def lt(left, right):
return ast.CompExpr(left, right, ast.CompOpType.LT)
def lte(left, right):
return ast.CompExpr(left, right, ast.CompOpType.LTE)
def gt(left, right):
return ast.CompExpr(left, right, ast.CompOpType.GT)
def eq(left, right):
return ast.CompExpr(left, right, ast.CompOpType.EQ)
BIN_EXPR_CLASSES = {
(False, False): ast.BinNumExpr,
(True, True): ast.BinVectorExpr,
(True, False): ast.BinVectorNumExpr,
}
def apply_bin_op(left, right, op):
"""
Finds binary expression class suitable for combination of left and right
expressions depending on whether their output is scalar or vector and
creates instance of this expression with specified operation.
"""
exr_class = BIN_EXPR_CLASSES.get(
(left.output_size > 1, right.output_size > 1))
if exr_class is None:
# change the positions of left and right
left, right = right, left
exr_class = ast.BinVectorNumExpr
return exr_class(left, right, op)
def apply_op_to_expressions(op, *exprs, to_reuse=False):
if len(exprs) < 1:
raise ValueError("At least one expression is required")
if len(exprs) == 1:
return exprs[0]
def _inner(current_expr, *rest_exprs):
if not rest_exprs:
return current_expr
return _inner(
apply_bin_op(current_expr, rest_exprs[0], op), *rest_exprs[1:])
result = _inner(apply_bin_op(exprs[0], exprs[1], op), *exprs[2:])
result.to_reuse = to_reuse
return result
def to_1d_array(var):
return np.ravel(np.asarray(var))
def to_2d_array(var):
shape = np.shape(var)
if len(shape) == 2:
x, y = shape
else:
x, y = 1, np.size(var)
return np.reshape(np.asarray(var), (x, y))