示例#1
0
 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
示例#2
0
 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
示例#3
0
 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
示例#4
0
 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
示例#5
0
 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
示例#6
0
 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
示例#7
0
 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]
示例#8
0
    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
示例#9
0
 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
示例#10
0
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'
示例#11
0
 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
示例#12
0
 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()
示例#13
0
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"