Пример #1
0
    def pseudoinverse_with_more_columns_test(self):
        """Test to calculate the pseudoinverse of a Matrix with more columns than rows."""
        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)
        # Expected result calculated with scipy
        exRes = [
                    [-0.0541328,   0.02473614],
                    [ 0.00705413, -0.06480734],
                    [-0.02269591,  0.05255596],
                    [ 0.03956448,  0.09492743]
                ]

        res = mtrx.pseudoinverse()
        # Pseudoinverse of a m x n Matrix has to be a n x m Matrix
        self.assertEqual(res.get_width(), rows)
        self.assertEqual(res.get_height(), cols)
        for row in range(cols):
            for col in range(rows):
                self.assertAlmostEqual(exRes[row][col], res.get_value(col, row))
Пример #2
0
    def pseudoinverse_with_more_columns_test(self):
        """Test to calculate the pseudoinverse of a Matrix with more columns than rows."""
        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)
        # Expected result calculated with scipy
        exRes = [[-0.0541328, 0.02473614], [0.00705413, -0.06480734],
                 [-0.02269591, 0.05255596], [0.03956448, 0.09492743]]

        res = mtrx.pseudoinverse()
        # Pseudoinverse of a m x n Matrix has to be a n x m Matrix
        self.assertEqual(res.get_width(), rows)
        self.assertEqual(res.get_height(), cols)
        for row in range(cols):
            for col in range(rows):
                self.assertAlmostEqual(exRes[row][col],
                                       res.get_value(col, row))
Пример #3
0
    def pseudoinverse_test(self):
        """Test that the pseudoinverse is calculated correctly."""
        rows = 3
        cols = 2
        data = [[1, 2], [2, 4], [3, 6]]
        mtrx = Matrix(cols, rows)
        mtrx.initialize(data, rowBased=True)
        # Expected result calculated with scipy
        exRes = [[0.01428571, 0.02857143, 0.04285714],
                 [0.02857143, 0.05714286, 0.08571429]]

        res = mtrx.pseudoinverse()
        # Pseudoinverse of a m x n Matrix has to be a n x m Matrix
        self.assertEqual(res.get_width(), rows)
        self.assertEqual(res.get_height(), cols)
        for row in range(cols):
            for col in range(rows):
                self.assertAlmostEqual(exRes[row][col],
                                       res.get_value(col, row))
Пример #4
0
    def pseudoinverse_test(self):
        """Test that the pseudoinverse is calculated correctly."""
        rows = 3
        cols = 2
        data = [
                    [1, 2],
                    [2, 4],
                    [3, 6]
                ]
        mtrx = Matrix(cols, rows)
        mtrx.initialize(data, rowBased=True)
        # Expected result calculated with scipy
        exRes = [
                    [0.01428571,  0.02857143,  0.04285714],
                    [0.02857143,  0.05714286,  0.08571429]
                ]

        res = mtrx.pseudoinverse()
        # Pseudoinverse of a m x n Matrix has to be a n x m Matrix
        self.assertEqual(res.get_width(), rows)
        self.assertEqual(res.get_height(), cols)
        for row in range(cols):
            for col in range(rows):
                self.assertAlmostEqual(exRes[row][col], res.get_value(col, row))