class hmat(LinearOperator): def __init__(self, mat, r=10, leaf_side=16): LinearOperator.__init__(self, dtype=mat.dtype, shape=mat.shape, matvec=self._matvec, rmatvec=self._rmatvec) self.mat = BlackBox(mat) self.r = r self.leaf_side = leaf_side self.leaf_size = leaf_side**2 N = int(np.sqrt(self.mat.shape[0])) self.pattern = distance_matrix(N, format='coo') perm = hilbert_traverse(N) conjugate_sparse(self.pattern, perm) self.pattern = self.pattern.tocsr() self.mat.permutate(perm) self.root = hmat_node(self, tuple(zip((0, 0), self.mat.shape))) return def full_matrix(self): mat = np.zeros(self.mat.shape) self.root.full_part(mat) conjugate(mat, self.mat.perm.argsort()) return mat def _matvec(self, vec): assert len(vec.shape) == 1, 'vec must be vector' assert self.mat.shape[1] == vec.shape[0],\ 'mat of shape {shp}, cannot matvec on vec of shape {vshp}'.format(shp=self.mat.shape, vshp=vec.shape) if self.mat.perm is not None: vec = vec[self.mat.perm] result = np.zeros(self.mat.shape[0]) self.root.matvec_part(vec, result) if self.mat.perm is not None: result = result[self.mat.perm.argsort()] return result def _rmatvec(self, vec): assert len(vec.shape) == 1, 'vec must be vector' assert self.mat.shape[0] == vec.shape[0],\ 'mat of shape {shp}, cannot matvec on vec of shape {vshp}'.format(shp=self.mat.shape, vshp=vec.shape) if self.mat.perm is not None: vec = vec[self.mat.perm] result = np.zeros(self.mat.shape[1]) self.root.rmatvec_part(vec, result) if self.mat.perm is not None: result = result[self.mat.perm.argsort()] return result def count_params(self): return self.root.count_params_part() def check(self, orig): self.root.check_part(orig)
def main_func(N=64): # create fbox and hmat from our function fbox = BlackBox(gen_func(N), shape=(N**2, N**2)) hm = hmat(fbox, r=3, leaf_side=16) #orig = gen_mat(N) # get original matrix #approx = hm.full_matrix() #print('approximation accuracy: {}'.format(rel_error(orig, approx))) #print hm.count_params(), int(np.prod(orig.shape)) x = np.ones((N**2)) #rel_err_list_full = [] rel_err_list_hmat = [] x_hmat = scipy.sparse.linalg.cg(hm, hm.matvec(x), x0=None, tol=1e-6, maxiter=50, callback=lambda xk: rel_err_list_hmat.append(rel_error(x, xk))) #x_full = scipy.sparse.linalg.cg(fbox[:, :], np.dot(fbox[:, :], x), x0=None, tol=1e-6, maxiter=50, # callback=lambda xk: rel_err_list_full.append(rel_error(x, xk))) #print(rel_error(np.dot(fbox[:, :], x), hm.matvec(x))) #print(x_full[1], x_hmat[1]) #print(rel_error(x, x_full[0]), rel_error(x, x_hmat[0])) #for res, res_n in zip(rel_err_list_full, rel_err_list_hmat): # print(res, res_n) print(x_hmat[1]) print('rel_error: {}'.format(rel_error(x, x_hmat[0]))) for res in rel_err_list_hmat: print(res) hmat_params = hm.count_params() full_params = int(np.prod(fbox.shape)) print('hmat params: {}, full params: {}'.format(hmat_params, full_params)) print('Compression_rate: {}'.format(1. - 1.*hmat_params/full_params)) return
def from_array(cls, data, eps=1e-7, decompose='skeleton'): accepted_types = ['svd', 'skeleton'] if decompose not in accepted_types: raise Exception( 'decompose must be one of the following: {types}'.format( types=accepted_types)) if isinstance(data, np.ndarray): black_box = BlackBox.from_array(data) cls(black_box, decompose, eps)
def __init__(self, mat, r=10, leaf_side=16): LinearOperator.__init__(self, dtype=mat.dtype, shape=mat.shape, matvec=self._matvec, rmatvec=self._rmatvec) self.mat = BlackBox(mat) self.r = r self.leaf_side = leaf_side self.leaf_size = leaf_side**2 N = int(np.sqrt(self.mat.shape[0])) self.pattern = distance_matrix(N, format='coo') perm = hilbert_traverse(N) conjugate_sparse(self.pattern, perm) self.pattern = self.pattern.tocsr() self.mat.permutate(perm) self.root = hmat_node(self, tuple(zip((0, 0), self.mat.shape))) return
def run(self): try: print('getting ready...') throttle = [0.0, 0.0, 0.0, 0.0] for n in range(4): print(4 - n) time.sleep(1) print("running...") t0 = time.time() t_stop = t0 + 3.0 # max duration of flight b = BlackBox(size=(1000, 7)) data = [0, 0, 0, 0, 0, 0, 0] while time.time() < t_stop and self.altimeter.get_altitude( )[0][2] < 0.30: throttle = [0.10, 0.10, 0.10, 0.10] throttle = np.add(throttle, self.pid_angular.update()) self.escs.set_throttle(throttle) for n in range(3): data[n] = self.ahrs.xyz[n] data[n + 3] = self.ahrs.xyz_dot[n] data[6] = self.ahrs.dt b.record(data) b.write() print(self.pid_angular) print( '============THROTTLE=========================================' ) print(self.escs) finally: self.stop()
def matvec_test(r=3): ns = np.arange(10, 32, 1) times_full = [] times_hmat = [] for n in ns: fbox = BlackBox(gen_func(n), shape=(n**2, n**2)) hm = hmat(fbox, r=r, leaf_side=16) x = np.ones((n**2)) orig = gen_mat(n) times_full.append(timeit.timeit(lambda: np.dot(orig, x))) times_hmat.append(timeit.timeit(lambda: hm.matvec(x))) plt.plot(ns**2, times_full, 'r', ns**2, times_hmat, 'g') plt.show() return times_full, times_hmat