def __init__(self, weight_func=None, basis=None): """ Constructor """ if None is weight_func: self.weight_func = RadialFunction() else: self.weight_func = weight_func if None is basis: self.basis = MonomialBasis() else: self.basis = basis self.set_diff_order(0)
class MLS(object): """ classdocs """ def set_diff_order(self, order): if order > 1: raise ValueError("Only supports 0 or 1 differential order") # self.monomial_basis.set_diff_order(order) #nonsense about mls self.weight_func.set_diff_order(order) self._diff_order = order self.mat_As = [] base_dim = self.basis.size() self._t_mat = np.ndarray((base_dim, base_dim), dtype=np.double) for _i in xrange(output_length_2d(order)): self.mat_As.append(np.ndarray((base_dim, base_dim), dtype=np.double)) def _get_diff_order(self): return self._diff_order diff_order = property(_get_diff_order, set_diff_order) def __init__(self, weight_func=None, basis=None): """ Constructor """ if None is weight_func: self.weight_func = RadialFunction() else: self.weight_func = weight_func if None is basis: self.basis = MonomialBasis() else: self.basis = basis self.set_diff_order(0) def _zero_mat_As(self): for mat_A in self.mat_As: mat_A.fill(0) def _gen_mat_Bs(self, coords_lens): mat_Bs = [] for _i in xrange(output_length_2d(self.diff_order)): mat_Bs.append(np.zeros((coords_lens, self.basis.size()))) return mat_Bs def values(self, center, coords, influence_rads, dists=None): center = np.array(center, dtype=np.double) if None is dists: dists = _dists(center, coords, self.diff_order) weights = self.weight_func.values(dists, influence_rads) self._zero_mat_As() mat_As = self.mat_As t_mat = self._t_mat num_col = output_length_2d(self.diff_order) mat_Bs = self._gen_mat_Bs(len(coords)) self.basis.set_diff_order(0) for i in xrange(len(coords)): nd_crd = coords[i] p = self.basis.values(nd_crd - center) for diff_index in xrange(num_col): w = weights[i][diff_index] mat_B = mat_Bs[diff_index] mat_B[i] = p mat_B[i] *= w mat_A = mat_As[diff_index] np.multiply(p.reshape((-1, 1)), p, t_mat) t_mat *= w mat_A += t_mat self.basis.set_diff_order(self.diff_order) if self.diff_order > 0: self.basis.set_diff_order(self.diff_order) p = self.basis.values((0.0, 0.0)) g = np.linalg.solve(mat_As[0], p[0]) results = np.ndarray((len(coords), num_col), dtype=np.double) results[:, 0] = np.dot(mat_Bs[0], g) if self.diff_order > 0: g_x = np.linalg.solve(mat_As[0], p[1] - np.dot(mat_As[1], g)) g_y = np.linalg.solve(mat_As[0], p[2] - np.dot(mat_As[2], g)) results[:, 1] = np.dot(mat_Bs[1], g) + np.dot(mat_Bs[0], g_x) results[:, 2] = np.dot(mat_Bs[2], g) + np.dot(mat_Bs[0], g_y) return results