def test_tools(self): self.mat_int = np.zeros((100, 100), dtype=int) # fill bands of pentadiagonal matrix self.mat_int[pp.diag_indices(100, 0)] = self.rand.randint(1, 1000, size=100) self.mat_int[pp.diag_indices(100, 1)] = self.rand.randint(1, 1000, size=99) self.mat_int[pp.diag_indices(100, 2)] = self.rand.randint(1, 1000, size=98) self.mat_int[pp.diag_indices(100, -1)] = self.rand.randint(1, 1000, size=99) self.mat_int[pp.diag_indices(100, -2)] = self.rand.randint(1, 1000, size=98) # create banded self.mat_int_col = pp.create_banded(self.mat_int) self.mat_int_row = pp.create_banded(self.mat_int, col_wise=False) # create full self.mat_int_col_ful = pp.create_full(self.mat_int_col, col_wise=True) self.mat_int_row_ful = pp.create_full(self.mat_int_row, col_wise=False) # shifting self.mat_shift_cr = pp.shift_banded(self.mat_int_col) self.mat_shift_rc = pp.shift_banded(self.mat_int_row, col_to_row=False) # in place shifting self.mat_int_col_ip = pp.create_banded(self.mat_int) self.mat_int_row_ip = pp.create_banded(self.mat_int, col_wise=False) pp.shift_banded(self.mat_int_col_ip, copy=False) pp.shift_banded(self.mat_int_row_ip, copy=False, col_to_row=False) # checking self.assertEqual(np.sum(self.mat_int > 0), 494) self.assertTrue(np.array_equal(self.mat_int_col, self.mat_shift_rc)) self.assertTrue(np.array_equal(self.mat_int_row, self.mat_shift_cr)) self.assertTrue(np.array_equal(self.mat_int_col, self.mat_int_row_ip)) self.assertTrue(np.array_equal(self.mat_int_row, self.mat_int_col_ip)) self.assertTrue(np.array_equal(self.mat_int, self.mat_int_col_ful)) self.assertTrue(np.array_equal(self.mat_int, self.mat_int_row_ful))
def test_solve2(self): self.mat_col = pp.shift_banded(self.mat, col_to_row=False) self.mat_ful = pp.create_full(self.mat, col_wise=False) sol_row = pp.solve(self.mat, self.rhs, is_flat=True, solver=2) sol_col = pp.solve( self.mat_col, self.rhs, is_flat=True, index_row_wise=False, solver=2, ) sol_ful = pp.solve(self.mat_ful, self.rhs, solver=2) diff_row = np.max(np.abs(np.dot(self.mat_ful, sol_row) - self.rhs)) diff_col = np.max(np.abs(np.dot(self.mat_ful, sol_col) - self.rhs)) diff_ful = np.max(np.abs(np.dot(self.mat_ful, sol_ful) - self.rhs)) diff_row_col = np.max( np.abs(self.mat_ful - pp.create_full(self.mat_col))) self.assertAlmostEqual(diff_row * 1e-5, 0.0) self.assertAlmostEqual(diff_col * 1e-5, 0.0) self.assertAlmostEqual(diff_ful * 1e-5, 0.0) self.assertAlmostEqual(diff_row_col * 1e5, 0.0)
Author: Mohammad Afzal Shadab Email : [email protected] Date : 10/15/2020 ''' import numpy as np import pentapy as pp from scipy.sparse import csr_matrix from pentapy_solve import pentapy_solve from scipy.sparse.linalg import spsolve import time as timer size = 10000 # create a flattened pentadiagonal matrix M_flat = (np.random.random((5, size)) - 0.5) * 1e-5 M = pp.create_full(M_flat, col_wise=False) V = np.random.random(size) * 1e5 Msparse = csr_matrix(M) penta_start = timer.clock() X_new = pentapy_solve(Msparse,V) penta_end = timer.clock() print('Xnew',X_new) #sparse solve no magic spsolve_start = timer.clock() X_spsolve = spsolve(Msparse,V) spsolve_end = timer.clock() print('Xspsolve',X_spsolve)