def convert_to_cpp_sparse(mat): assert type(mat) is ss.csr_matrix data = hm.double_vec() indices = hm.int_vec() indptr = hm.int_vec() data.extend(mat.data) indices.extend(mat.indices.astype('uint32')) indptr.extend(mat.indptr.astype('uint32')) size1, size2 = mat.shape[0], mat.shape[1] return hm.to_spmat(data, indices, indptr, size1, size2)
def convert_to_cpp_sparse(mat): if not isinstance(mat, ss.csr_matrix): raise ValueError("Invalid argument!") data = hm_cpp.double_vec() indices = hm_cpp.int_vec() indptr = hm_cpp.int_vec() data.extend(mat.data) indices.extend(mat.indices.astype('uint32')) indptr.extend(mat.indptr.astype('uint32')) size1, size2 = mat.shape[0], mat.shape[1] return hm_cpp.to_spmat(data, indices, indptr, size1, size2)
def project(inp, dim, directions, index_set="triangle"): if isinstance(directions, int): directions = [directions] inp = to_cpp(inp) directions_cpp = hm_cpp.int_vec() directions_cpp.extend(directions) return to_numpy(hm_cpp.project(inp, dim, directions_cpp, index_set))
def project(inp, dim, directions): if type(directions) is int: directions = [directions] inp = to_cpp(inp) directions = to_numeric(directions) directions_cpp = hm.int_vec() directions_cpp.extend(directions) return to_numpy(hm.project(inp, dim, directions_cpp))
def integrate(fgrid, nodes, weights, do_fourier=None): if do_fourier is None: do_fourier = [0]*len(nodes) do_fourier_cpp = hm_cpp.int_vec() do_fourier_cpp.extend(do_fourier) fgrid, nodes, weights = to_cpp_array(fgrid, nodes, weights) result = hm_cpp.integrate(fgrid, nodes, weights, do_fourier_cpp) return result
def varf(degree, fgrid, nodes, weights, sparse=False, do_fourier=None, index_set="triangle"): if do_fourier is None: do_fourier = [0]*len(nodes) do_fourier_cpp = hm_cpp.int_vec() do_fourier_cpp.extend(do_fourier) fgrid, nodes, weights = to_cpp_array(fgrid, nodes, weights) args = [degree, fgrid, nodes, weights, do_fourier_cpp, index_set] function = hm_cpp.varf_sp if sparse else hm_cpp.varf return to_numpy(log_stats()(function)(*args))
def transform(degree, fgrid, nodes, weights, forward, do_fourier=None, index_set="triangle"): if do_fourier is None: do_fourier = [0]*len(nodes) do_fourier_cpp = hm_cpp.int_vec() do_fourier_cpp.extend(do_fourier) degree = int(degree) fgrid, nodes, weights = to_cpp_array(fgrid, nodes, weights) return np.array(hm_cpp.transform(degree, fgrid, nodes, weights, do_fourier_cpp, forward, index_set))
def tensorize(inp, dim=None, direction=None, sparse=False, index_set="triangle"): if isinstance(inp, dict): any_elem = list(inp.values())[0] if isinstance(any_elem, (float, int)): return np.prod([inp[k] for k in inp]) assert isinstance(any_elem, (np.ndarray, ss.csr_matrix)) dim = len(any_elem.shape) assert dim == 1 or dim == 2 cpp_arrays = hm_cpp.double_mat() if dim == 1 else hm_cpp.double_cube() cpp_dirs_mat = hm_cpp.int_mat() for dirs, arg in inp.items(): cpp_dirs = hm_cpp.int_vec() cpp_dirs.extend(list(dirs)) cpp_dirs_mat.append(cpp_dirs) # Works only with dense arguments at the moment if isinstance(arg, ss.csr_matrix): arg = np.array(arg.todense()) arg = to_cpp_array(arg) cpp_arrays.append(arg) args = [cpp_arrays, cpp_dirs_mat, index_set] elif isinstance(inp, list): assert dim is None and direction is None is_scalar = isinstance(inp[0], (float, int)) if is_scalar and dim is None and direction is None: return np.prod(inp) if isinstance(inp[0], ss.csr_matrix): inp = [np.array(m.todense()) for m in inp] inp = to_cpp_array(inp) args = [inp, index_set] else: assert dim is not None and direction is not None if isinstance(inp, ss.csr_matrix): inp = np.array(inp.todense()) inp = to_cpp_array(inp) args = [inp, dim, direction, index_set] if sparse: tensorize_fun = hm_cpp.tensorize_sp convert_fun = to_csr else: tensorize_fun = hm_cpp.tensorize convert_fun = np.array return convert_fun(tensorize_fun(*args))
def iterator_index(mult_ind, index_set="triangle"): if isinstance(mult_ind, np.ndarray): mult_ind = list(mult_ind.astype('uint32')) ind_vec = hm_cpp.int_vec() ind_vec.extend(mult_ind) cpp_func = { "triangle": hm_cpp.triangle_index, "cube": hm_cpp.cube_index } if index_set not in cpp_func: raise ValueError("Unknown index set") return int(cpp_func[index_set](ind_vec))
def inner(s1, s2, d1, d2): s1, s2 = to_cpp_array(s1, s2) d1_cpp, d2_cpp = hm.int_vec(), hm.int_vec() d1_cpp.extend(d1) d2_cpp.extend(d2) return np.array(hm.inner(s1, s2, d1_cpp, d2_cpp))
def inner(s1, s2, d1, d2, index_set="triangle"): s1, s2 = to_cpp_array(s1, s2) d1_cpp, d2_cpp = hm_cpp.int_vec(), hm_cpp.int_vec() d1_cpp.extend(d1) d2_cpp.extend(d2) return np.array(hm_cpp.inner(s1, s2, d1_cpp, d2_cpp, index_set))