Ejemplo n.º 1
0
    def flatten_test(self):
        a = Matrix(2, 2, [2, 2, 2, 2])
        b = Matrix(2, 2, [a, a, a, a])

        result = Matrix(4, 4, [2] * 16)
        calculated = b.flatten()
        self.assertTrue(calculated, result)
Ejemplo n.º 2
0
    def blockwise_with_zero_expansion_test(self):
        a = Matrix(4, 4, [1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0, 0, 0, 0, 0])
        b = Matrix(4, 4, [1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0, 0, 0, 0, 0])

        calculated = a.matrix_multiplication_blockwise(b, 2)
        result = Matrix(4, 4, [
            30.000, 36.000, 42.000, 0, 66.000, 81.000, 96.000, 0, 102.000,
            126.000, 150.000, 0, 0, 0, 0, 0
        ])
        self.assertEqual(result, calculated)
Ejemplo n.º 3
0
 def matrix_multiplication_square_test(self):
     """Test the matrix_multiplication with a square matrix"""
     size = 33
     array = [[1.0 for i in range(size)] for j in range(size)]
     matrix = Matrix(size, size)
     matrix2 = Matrix(size, size)
     matrix.initialize(array, rowBased=True)
     matrix2.initialize(array, rowBased=True)
     result = matrix * matrix2
     expRes = [[size for i in range(size)] for j in range(size)]
     self.assertEqual(expRes, result.matrix)
Ejemplo n.º 4
0
 def matrix_multiplication_test(self):
     """Test the matrixmultplication of two matrices."""
     rows1 = 2
     cols1 = 3
     data1 = [[1, -2, 3], [-4, 5, 6]]
     data2 = [[3, 4], [5, -6], [7, 8]]
     exRes = [[14.0, 55.0], [40.0, 2.0]]
     mtrx1 = Matrix(cols1, rows1)
     mtrx2 = Matrix(rows1, cols1)
     mtrx1.initialize(data1, rowBased=True)
     mtrx2.initialize(data2, rowBased=True)
     res = mtrx1.matrix_multiplication(mtrx2)
     self.assertEqual(res.matrix, exRes)
Ejemplo n.º 5
0
 def add_matrix_test(self):
     """Test addition of two matrices."""
     rows = 2
     cols = 3
     data1 = [[1, -2, 3], [-4, 5, 6]]
     data2 = [[2, 4, -3], [5, -7, 3]]
     mtrx1 = Matrix(cols, rows)
     mtrx2 = Matrix(cols, rows)
     mtrx1.initialize(data1, rowBased=True)
     mtrx2.initialize(data2, rowBased=True)
     expRes = [[3, 1], [2, -2], [0, 9]]
     res = mtrx1 + mtrx2
     self.assertEqual(res.matrix, expRes)
Ejemplo n.º 6
0
    def blockwise_multiplication_test(self):
        data = range(1, 33)
        a = Matrix(4, 2, data[:8])
        b = Matrix(6, 4, data[8:])

        result = [
            210.000, 220.000, 230.000, 240.000, 250.000, 260.000, 498.000,
            524.000, 550.000, 576.000, 602.000, 628.000
        ]
        resultMatrix = Matrix(6, 2, result)

        calculated = a.matrix_multiplication_blockwise(b, 2)
        self.assertEqual(resultMatrix, calculated)
Ejemplo n.º 7
0
 def sub_matrix_test(self):
     """Test subtraction of two matrices."""
     rows = 2
     cols = 3
     data1 = [[1, -2, 3], [-4, 5, 6]]
     data2 = [[2, 4, -3], [5, -7, 3]]
     mtrx1 = Matrix(cols, rows)
     mtrx2 = Matrix(cols, rows)
     mtrx1.initialize(data1, rowBased=True)
     mtrx2.initialize(data2, rowBased=True)
     expRes = [[-1, -9], [-6, 12], [6, 3]]
     res = mtrx1 - mtrx2
     self.assertEqual(res.matrix, expRes)
Ejemplo n.º 8
0
 def lstsq_wrong_input_size_test(self):
     """Test for value error in lstsq method, if height of input matrices, does not match"""
     # Initialize input matrices
     volumes = [[24], [20], [20], [20], [21], [30]]
     promoted = [[1, 0, 0, 0, 1], [1, 0, 0, 0, 0]]
     volMatrix = Matrix(1, 6)
     volMatrix.initialize(volumes)
     proMatrix = Matrix(5, 2)
     proMatrix.initialize(promoted)
     # Least Square method should raise error, since number of rows
     # of proMatrix and volMatrix does not match
     self.assertRaises(ValueError, LinearRegression.lstsq, proMatrix,
                       volMatrix)
Ejemplo n.º 9
0
 def is_matrix_mult_possible_true_test(self):
     """Test if matrix_mult_possible() returns True, if matrices can be multiplied."""
     rows1 = 2
     cols1 = 3
     rows2 = 3
     cols2 = 2
     mtrx1 = Matrix(cols1, rows1)
     mtrx2 = Matrix(cols2, rows2)
     data1 = [[1, -2, 3], [-4, 5, 6]]
     data2 = [[3, 4], [5, -6], [7, 8]]
     mtrx1.initialize(data1, rowBased=True)
     mtrx2.initialize(data2, rowBased=True)
     res = mtrx1.is_matrix_mult_possible(mtrx2)
     self.assertTrue(res)
Ejemplo n.º 10
0
    def lstsq_value_error_test(self):
        """Test for the value error, if Matrix with dependent variable has more than 1 column"""
        volumes = [[23, 34], [12, 34], [14, 54]]
        promoted = [
            [1, 0, 0, 0, 1],
            [1, 0, 0, 0, 0],
            [1, 0, 0, 0, 0],
        ]
        volMatrix = Matrix(2, 3)
        volMatrix.initialize(volumes)
        proMatrix = Matrix(5, 3)
        proMatrix.initialize(promoted)

        self.assertRaises(ValueError, LinearRegression.lstsq, proMatrix,
                          volMatrix)
Ejemplo n.º 11
0
    def matrix_string_representation_with_precision_test(self):
        """Test if the precision is set correctly and used when printing a Matrix.
        """
        size = 2
        data = [[1.0123343, -2.012341234123], [3.04674567566, 4.012341234120]]
        mtrx = Matrix(size, size)
        mtrx.initialize(data, rowBased=True)
        mtrx.set_string_precision(4)
        rep = mtrx.__str__()
        # should print the number with 4 digits after decimal point
        self.assertTrue(rep.find(" 3.0467 ") >= 0)
        self.assertTrue(rep.find(" -2.0123") >= 0)

        # but should not print the full number
        self.assertFalse(rep.find(" 3.04674567566") >= 0)
        self.assertFalse(rep.find(" -2.012341234123 ") >= 0)

        # change precision
        mtrx.set_string_precision(2)
        rep = mtrx.__str__()
        print mtrx
        # should print the number with 2 digits after decimal point
        # numbers should be rounded
        self.assertTrue(rep.find(" 3.05 ") >= 0)
        self.assertTrue(rep.find(" -2.01") >= 0)
Ejemplo n.º 12
0
 def initialization_wrong_cols_test(self):
     """Test for :py:exc:`ValueError` in initialize() if data array has different number of columns."""
     rows = 2
     cols = 3
     data = [[2, 3], [1, 2, 4]]
     matrix = Matrix(cols, rows)
     self.assertRaises(ValueError, matrix.initialize, data, True)
Ejemplo n.º 13
0
 def column_based_initialization_with_wrong_columns_test(self):
     """Test for ValueError in initialize() if number of columns does not match."""
     rows = 2
     cols = 3
     mtrx = Matrix(cols, rows)
     data = [[1, 2], [3, 4], [5, 6], [7, 9]]
     self.assertRaises(ValueError, mtrx.initialize, data, False)
Ejemplo n.º 14
0
 def mul_matrix_test(self):
     """Test numeric multiplication on Matrices."""
     # Only column of first matrix does match rows of second matrix
     rows1 = 3
     cols1 = 2
     rows2 = 2
     cols2 = 4
     mtrx1 = Matrix(cols1, rows1)
     mtrx2 = Matrix(cols2, rows2)
     data1 = [[1, -2], [-4, 5], [3, 6]]
     data2 = [[3, 4, 5, 6], [5, -6, 4, 5]]
     exRes = [[-7, 13, 39], [16, -46, -24], [-3, 0, 39], [-4, 1, 48]]
     mtrx1.initialize(data1, rowBased=True)
     mtrx2.initialize(data2, rowBased=True)
     res = mtrx1 * mtrx2
     self.assertEqual(res.matrix, exRes)
Ejemplo n.º 15
0
 def svd_unitary_test(self):
     """Test if matrices u and v are unitary."""
     a = [[22., 10., 2., 3., 7.], [14., 7., 10., 0., 8.],
          [-1., 13., -1., -11., 3.], [-3., -2., 13., -2., 4.],
          [9., 8., 1., -2., 4.], [9., 1., -7., 5., -1.],
          [2., -6., 6., 5., 1.], [4., 5., 0., -2., 2.]]
     matrix = Matrix(5, 8)
     matrix.initialize(a, rowBased=True)
     u, diag, v = matrix.svd()
     # u and v should be unitary matrices. Matrixmultiplication withs its
     # transformation should be the identity Matrix.
     res = u.transform() * u
     res1 = v * v.transform()
     for row in range(res.get_height()):
         for col in range(res.get_width()):
             if row == col:
                 # value should be 1 at diagonal
                 self.assertAlmostEqual(res.get_value(col, row), 1,
                                        PRECISION)
                 self.assertAlmostEqual(res1.get_value(col, row), 1,
                                        PRECISION)
             else:
                 # value should be 0 otherwise.
                 self.assertAlmostEqual(res.get_value(col, row), 0,
                                        PRECISION)
                 self.assertAlmostEqual(res1.get_value(col, row), 0,
                                        PRECISION)
Ejemplo n.º 16
0
    def row_based_initialization_wrong_rows_test(self):
        """Test for :py:exc:`ValueError` if datalist has less rows than the Matrix."""
        rows = 4
        cols = 5
        matrix = Matrix(cols, rows)
        data = [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [3, 5, 4, 8, 6]]

        self.assertRaises(ValueError, matrix.initialize, data, True)
Ejemplo n.º 17
0
    def set_string_precision_error_value_test(self):
        """Test for :py:exc:`ValueError` when trying to set the precision to a negative value."""
        size = 2
        data = [[1, 2], [3, 4]]
        mtrx = Matrix(size, size)
        mtrx.initialize(data, rowBased=True)

        self.assertRaises(ValueError, mtrx.set_string_precision, -2)
Ejemplo n.º 18
0
    def init_with_one_dimensional_row_based_array_test(self):
        """Test the initialization with a one dimensional row based array."""
        data = [1, 2, 3, 4]
        mtrx = Matrix(2, 2, data, rowBased=True)
        exRes = [[1, 3], [2, 4]]

        res = mtrx.matrix
        self.assertEqual(exRes, res)
Ejemplo n.º 19
0
    def init_with_one_dimensional_column_based_array_test(self):
        """Test the initialization with a one dimensional column based array."""
        data = [1, 2, 3, 4]
        mtrx = Matrix(2, 2, data, rowBased=False)
        exRes = [[1, 2], [3, 4]]

        res = mtrx.matrix
        self.assertEqual(exRes, res)
Ejemplo n.º 20
0
    def column_based_initialization_with_wrong_rows_test(self):
        """Test for ValueError if number of rows does not match."""
        rows = 4
        cols = 3
        mtrx = Matrix(cols, rows)
        data = [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [3, 5, 4, 8, 6]]

        self.assertRaises(ValueError, mtrx.initialize, data, False)
Ejemplo n.º 21
0
 def invers_value_error_test(self):
     """Test if a :py:exc:`ValueError` is raised if Matrix is not regular"""
     rows = 2
     cols = 3
     data = [[1, 2, 3], [4, 5, 6]]
     matrix = Matrix(cols, rows)
     matrix.initialize(data, rowBased=True)
     self.assertRaises(ValueError, matrix.invers)
Ejemplo n.º 22
0
 def get_matrix_from_list_test(self):
     """Test to create a Matrix from a one dimensional list."""
     rows = 2
     cols = 3
     mtrx = Matrix(cols, rows)
     data = [1, 2, 3, 4, 5, 6]
     exRes = [[1, 2, 3], [4, 5, 6]]
     newMtrx = mtrx.get_matrix_from_list(rows, cols, data, rowBased=True)
     self.assertEqual(newMtrx.get_array(rowBased=True), exRes)
Ejemplo n.º 23
0
 def sub_matrix_value_error_test(self):
     """Test for ValueError, when subtracting matrices of different size."""
     rows1 = 2
     cols1 = 3
     rows2 = 3
     cols2 = 3
     data1 = [[1, -2, 3], [-4, 5, 6]]
     data2 = [[2, 4, -3], [5, -7, 3], [5, -7, 3]]
     mtrx1 = Matrix(cols1, rows1)
     mtrx2 = Matrix(cols2, rows2)
     mtrx1.initialize(data1, rowBased=True)
     mtrx2.initialize(data2, rowBased=True)
     try:
         mtrx1 - mtrx2
     except ValueError:
         pass
     else:
         raise AssertionError  # pragma: no cover
Ejemplo n.º 24
0
 def invers_test(self):
     """Test the calculation of the inverse."""
     size = 2
     data = [[1.0, 2.0], [3.0, 4.0]]
     exRes = [[-2.0, 1.5], [1.0, -0.5]]
     matrix = Matrix(size, size)
     matrix.initialize(data, rowBased=True)
     res = matrix.invers()
     self.assertEqual(res.matrix, exRes)
Ejemplo n.º 25
0
    def gauss_jordan_non_singular_matrix_test(self):
        """Test for ValueError, if the Matrix is not invertible."""
        rows = 3
        cols = 6
        data = [[0, 2, 0, 1, 0, 0], [0, 3, 0, 0, 1, 0], [3, 4, 1, 0, 0, 1]]
        mtrx = Matrix(cols, rows)
        mtrx.initialize(data, rowBased=True)

        self.assertRaises(ValueError, mtrx.gauss_jordan)
Ejemplo n.º 26
0
    def gauss_jordan_value_error_test(self):
        """Test for ValueError in gauss_jordan(), if matrix has wrong size."""
        rows = 3
        cols = 3
        data = [[0, 2, 0], [2, 3, 0], [3, 4, 1]]
        mtrx = Matrix(cols, rows)
        mtrx.initialize(data, rowBased=False)

        self.assertRaises(ValueError, mtrx.gauss_jordan)
Ejemplo n.º 27
0
 def init_test(self):
     """Test the initialization of a matrix."""
     rows = random.randint(1, 1000)
     cols = random.randint(1, 1000)
     matrix = Matrix(cols, rows)
     if not matrix.get_height() == rows:
         raise AssertionError  # pragma: no cover
     if not matrix.get_width() == cols:
         raise AssertionError  # pragma: no cover
Ejemplo n.º 28
0
    def initialization_with_column_based_list_test(self):
        """Test setting the matrix values using a row based list."""
        rows = 5
        cols = 4
        mtrx = Matrix(cols, rows)
        data = [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [3, 5, 4, 8, 6],
                [1, 6, 4, 3, 9]]
        mtrx.initialize(data, rowBased=False)

        self.assertEqual(data, mtrx.matrix)
Ejemplo n.º 29
0
 def transform_test(self):
     """Test matrix transformation."""
     rows = 3
     cols = 2
     data = [[1, -2, 3], [-4, 5, 6]]
     exRes = [[1, -4], [-2, 5], [3, 6]]
     mtrx = Matrix(cols, rows)
     mtrx.initialize(data, rowBased=False)
     res = mtrx.transform()
     self.assertEqual(res.matrix, exRes)
Ejemplo n.º 30
0
    def svd_value_error_test(self):
        """Test for ValueError in svd(), if Matrix has more columns than rows.

        May be removed if algorithm also works with these matrices.
        """
        rows = 2
        cols = 4
        data = [[-11, 2, -5.0, 7.0], [2, -4, 3.4, 5.4]]
        mtrx = Matrix(cols, rows)
        mtrx.initialize(data, rowBased=True)
        self.assertRaises(ValueError, mtrx.svd)