def test_from_column_vector(self): m = 3 n = 1 a = [1, 2, 3] ia = [0, 1, 2, 3] ja = [0, 0, 0] input_vector = numpy.matrix([[1], [2], [3]]) expected_matrix = Matrix(m, n, a, ia, ja) output_matrix = Matrix.from_column_vector(input_vector) assert expected_matrix == output_matrix
def test_transpose(self): m = n = 3 a = [1, 2, 3, 4, 5, 6, 7, 8, 9] ia = [0, 3, 6, 9] ja = [0, 1, 2, 0, 1, 2, 0, 1, 2] initial_matrix = Matrix(m, n, a, ia, ja) a_trans = [1, 4, 7, 2, 5, 8, 3, 6, 9] expected_matrix = Matrix(m, n, a_trans, ia, ja) output_matrix = initial_matrix.transpose() assert expected_matrix == output_matrix
def test_eq_and_ne(self): m = 3 m1 = Matrix.tridiagonal(m) a = [2, -1, -1, 2, -1, -1, 2] ia = [0, 2, 5, 7] ja = [0, 1, 0, 1, 2, 1, 2] m2 = Matrix(m, m, a, ia, ja) assert m1 == m2 a = [1, 1, 1, 1, 1, 1, 1] m3 = Matrix(m, m, a, ia, ja) assert m2 != m3
def test_left_mult(self): A = Matrix.tridiagonal(3) a_inv = [0.75, 0.5, 0.25, 0.5, 1., 0.5, 0.25, 0.5, 0.75] ia_inv = [0, 3, 6, 9] ja_inv = [0, 1, 2, 0, 1, 2, 0, 1, 2] a = [1.0, 1.0, 1.0] ia = [0, 1, 2, 3] ja = [0, 1, 2] A_inv = Matrix(3, 3, a_inv, ia_inv, ja_inv) I = Matrix(3, 3, a, ia, ja) assert A * A_inv == I
def test_push_column_with_zeros(self): m = 3 n = 2 a = [1, 2, 5, 3] ia = [0, 1, 3, 4] ja = [0, 0, 1, 0] first_vector = numpy.matrix([[1], [2], [3]]) second_vector = numpy.matrix([[0], [5], [0]]) expected_matrix = Matrix(m, n, a, ia, ja) output_matrix = Matrix.from_column_vector(first_vector) output_matrix.push_column(second_vector) assert expected_matrix == output_matrix
def test_push_column(self): m = 3 n = 2 a = [1, 4, 2, 5, 3, 6] ia = [0, 2, 4, 6] ja = [0, 1, 0, 1, 0, 1] first_vector = numpy.matrix([[1], [2], [3]]) second_vector = numpy.matrix([[4], [5], [6]]) expected_matrix = Matrix(m, n, a, ia, ja) output_matrix = Matrix.from_column_vector(first_vector) output_matrix.push_column(second_vector) assert expected_matrix == output_matrix
def test_tridiagonal_generation(self): m = 3 a = Matrix.tridiagonal(m) assert len(a.ia) == m + 1 assert len(a) == 3 * m - 2 assert a.shape() == (m, m) assert a.a == [2, -1, -1, 2, -1, -1, 2] assert a.ia == [0, 2, 5, 7] assert a.ja == [0, 1, 0, 1, 2, 1, 2]
def test_symmetric(self): A = Matrix.tridiagonal(3) print A.a print A.ia print A.ja assert A.symmetric() == True a = [1, 2, 1, 1] ia = [0, 2, 3, 4] ja = [0, 1, 0, 0] A = Matrix(3, 3, a, ia, ja) assert A.symmetric() == False A = Matrix.from_mm_file('data/ash958.mtx') assert A.symmetric() == False
def test_gmres_tridiagonal(self): A = Matrix.tridiagonal(20) b = numpy.ones((A.shape()[0], 1)) gmres = Gmres(A, b) output_vector, error, metadata = gmres.solve() assert error < Gmres.EPSILON
from gmrescrs import Gmres from crsmatrix import Matrix import numpy max_matrix_size = 1000 max_iterations = 500 max_restart = 500 A = Matrix.tridiagonal(max_matrix_size) b = numpy.ones((A.m, 1)) x, error, metadata = Gmres(A, b, max_iterations=max_iterations, restart_after=max_restart).solve() error_time_values = [] delta = metadata[1] dur = metadata[2][1:] # discard the first to match siz of deltas for i in range(0, metadata[0] - 1): et = delta[i] / dur[i] error_time_values.append(et) handle = open('generated_data.csv', 'a') handle.write( 'matrix_size,iteration_norm,error_delta_normed,duration,error_time\n') for i in range(0, metadata[0] - 1): iteration_norm = float(i + 1) / max_matrix_size items = [ max_matrix_size, iteration_norm, delta[i], dur[i], error_time_values[i] ] line = ','.join([str(s) for s in items]) + '\n'
def test_from_mm_file(self): file_path = './data/ash958.mtx' ash958 = Matrix.from_mm_file(file_path) assert ash958.shape() == (958, 292) assert len(ash958) == 1916
def test_mult_vect_right(self): input_vector = numpy.matrix([1, 1, 1, 1, 1]).T expected_vector = numpy.matrix([1, 0, 0, 0, 1]).T test_matrix = Matrix.tridiagonal(5) output_vector = test_matrix.mult_left_vector(input_vector) assert (expected_vector == output_vector).all()
from gmrescrs import Gmres from crsmatrix import Matrix import numpy from datetime import datetime A = Matrix.from_mm_file('data/bcsstk18.mtx') b = numpy.ones((A.shape()[0], 1)) gmres = Gmres(A, b) x, error, metadata = gmres.solve() print error # times = 3 # durations = [] # for i in range(0, times): # gmres = Gmres(A, b, max_iterations=100000, restart_after=75) # print "starting" # start = datetime.now() # output_vector, error, metadata = gmres.solve() # end = datetime.now() # seconds = (end - start).seconds # decimal = float((end - start).microseconds) / 1e6 # print "ending" # durations.append(seconds + decimal) # # # print error # print "iterations: " + str(metadata[0]) # print "duration: " + str(sum(durations) / times) + " sec"