MCPcopy Create free account
hub / github.com/bgrimstad/splinter / Function

Class Function

python/splinter/function.py:15–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14
15class Function:
16 def __init__(self):
17 self._handle = None
18 self._num_variables = None
19
20 def eval(self, x):
21 x = self._transform_input(x)
22
23 num_points = len(x) // self._num_variables
24 res = splinter._call(splinter._get_handle().splinter_bspline_eval_row_major, self._handle, (c_double * len(x))(*x), len(x))
25
26 return c_array_to_list(res, num_points)
27
28 def eval_jacobian(self, x):
29 x = self._transform_input(x)
30
31 num_points = len(x) // self._num_variables
32 jac = splinter._call(splinter._get_handle().splinter_bspline_eval_jacobian_row_major, self._handle, (c_double * len(x))(*x), len(x))
33
34 # Convert from ctypes array to Python list of lists
35 # jacobians is a list of the jacobians in all evaluated points
36 jacobians = []
37 for i in range(num_points):
38 jacobians.append([])
39 for j in range(self._num_variables):
40 jacobians[i].append(jac[i * self._num_variables + j])
41 return jacobians
42
43 def eval_hessian(self, x):
44 x = self._transform_input(x)
45
46 num_points = len(x) // self._num_variables
47 hes = splinter._call(splinter._get_handle().splinter_bspline_eval_hessian_row_major, self._handle, (c_double * len(x))(*x), len(x))
48
49 # Convert from ctypes array to Python list of list of lists
50 # hessians is a list of the hessians in all points
51 hessians = []
52 for i in range(num_points):
53 hessians.append([])
54 for j in range(self._num_variables):
55 hessians[i].append([])
56 for k in range(self._num_variables):
57 hessians[i][j].append(hes[i * self._num_variables * self._num_variables + j * self._num_variables + k])
58 return hessians
59
60 def get_num_variables(self):
61 return splinter._call(splinter._get_handle().splinter_bspline_get_num_variables, self._handle)
62
63 def save(self, filename):
64 splinter._call(splinter._get_handle().splinter_bspline_save, self._handle, get_c_string(filename))
65
66 def _transform_input(self, x):
67 if isinstance(x, np.ndarray):
68 x = x.tolist()
69
70 if not isinstance(x, list):
71 x = [x]
72

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected