def apply_stiffness_matrix(self,modes): from scipy.sparse.csr import csr_matrix from numpy import ndarray, dot if self.stiffness_matrix is None: self.make_stiffness_matrix() if isinstance(self.stiffness_matrix,csr_matrix): try: from pymbolic.algorithm import csr_matrix_multiply return csr_matrix_multiply(self.stiffness_matrix,modes) except: return self.stiffness_matrix*modes elif isinstance(self.stiffness_matrix,ndarray): return dot(self.stiffness_matrix,modes) else: raise TypeError("Unrecognized type %s for stiffness matrix" % \ type(self.stiffness_matrix))
def test_sparse_multiply(): numpy = pytest.importorskip("numpy") pytest.importorskip("scipy") import scipy.sparse as ss la = numpy.linalg mat = numpy.random.randn(10, 10) s_mat = ss.csr_matrix(mat) vec = numpy.random.randn(10) mat_vec = s_mat * vec from pymbolic.algorithm import csr_matrix_multiply mat_vec_2 = csr_matrix_multiply(s_mat, vec) assert la.norm(mat_vec - mat_vec_2) < 1e-14
def test_sparse_multiply(): numpy = pytest.importorskip("numpy") pytest.importorskip("scipy") import scipy.sparse as ss la = numpy.linalg mat = numpy.random.randn(10, 10) s_mat = ss.csr_matrix(mat) vec = numpy.random.randn(10) mat_vec = s_mat*vec from pymbolic.algorithm import csr_matrix_multiply mat_vec_2 = csr_matrix_multiply(s_mat, vec) assert la.norm(mat_vec-mat_vec_2) < 1e-14
def fft_nodal_differentiation(fx, **kwargs): """Given the nodal function values, returns the nodal approximations to the derivative computed using: 1.) the fft 2.) application of the sparse stiffness matrix to the nodes 3.) the ifft s must be an integer for all this to work. """ from spyctral.wiener.matrices import weighted_wiener_stiffness_matrix as stiffmat from spyctral.wiener.fft import fft_collocation as fft from spyctral.wiener.fft import ifft_collocation as ifft stiff = stiffmat(len(fx), **kwargs) if fx.dtype == object: from pymbolic.algorithm import csr_matrix_multiply temp = fft(fx, **kwargs) temp = csr_matrix_multiply(stiff, temp) return ifft(temp, **kwargs) else: return ifft(stiff * fft(fx, **kwargs), **kwargs)