-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_wrapper.py
More file actions
50 lines (40 loc) · 1.65 KB
/
expression_wrapper.py
File metadata and controls
50 lines (40 loc) · 1.65 KB
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
import copy
import cexprtk
import numpy as np
import scipy as sp
class ExpressionWrapper:
def __init__(self, math_str, parameters):
symbols = {}
for param in parameters:
symbols[param] = 1.0
st = cexprtk.Symbol_Table(symbols, add_constants=True)
self.expression = cexprtk.Expression(math_str, st)
self._constant = 1
def _evaluate_for_parameters(self, parameters):
for key in parameters:
self.expression.symbol_table.variables[key] = parameters[key]
return self._constant * self.expression()
def _evaluate_for_x(self, x):
return self._evaluate_for_parameters({'x': x})
def _evaluate_with_list(self, values):
result = []
for x in values:
result.append(self._evaluate_for_x(x))
return result
def _get_minus_func(self):
minus_func = copy.copy(self)
minus_func._constant = -1
return minus_func
def get_minimum_for_partition(self, p):
return [sp.optimize.minimize_scalar(self, bounds=(p[i], p[i + 1]), method='bounded').fun for i in range(len(p)-1)]
def get_maximum_for_partition(self, p):
minus_func = self._get_minus_func()
min = minus_func.get_minimum_for_partition(p)
return [-x for x in min]
def __call__(self, values):
if type(values) is np.ndarray or type(values) is list:
return self._evaluate_with_list(values)
elif type(values) is np.float64 or type(values) is float:
return self._evaluate_for_x(values)
else:
return self._evaluate_for_parameters(values)