def compute_product(Srows_A, Scols_A, Snums_A, Srows_B, Scols_B, Snums_B):
    # make sure that entered values are numerical
    try:
        rows_A = int(Srows_A)
        cols_A = int(Scols_A)
        rows_B = int(Srows_B)
        cols_B = int(Scols_B)
        nums_A = list(map(float, Snums_A.split()))
        nums_B = list(map(float, Snums_B.split()))
    except ValueError:
        messagebox.showerror("ValueError", "Input Numerical Values Please")
    # catch all incorrect input types
    if cols_A != rows_B:
        messagebox.showerror(
            "Rows != Columns",
            "Matrix A columns do not equal Matrix B rows. Unable to multiply.")
    if len(nums_A) != (rows_A * cols_A):
        messagebox.showerror(
            "Incorrect # of Coefficients",
            "The number of entered values for Matrix A doesn't match the " +
            "number of rows and columns.")
    if len(nums_B) != (rows_B * cols_B):
        messagebox.showerror(
            "Incorrect # of Coefficients",
            "The number of entered values for Matrix B doesn't match the " +
            "number of rows and columns.")
    else:
        Mat_A = Matrix(rows_A, cols_A, nums_A)
        Mat_B = Matrix(rows_B, cols_B, nums_B)
        AB_product = Matrices.product(Mat_A, Mat_B)
        messagebox.showinfo("A * B", "{}".format(AB_product))
Example #2
0
    def test_11_inverse(self):

        #Note: I'm adding a threshold here in equals, since we are multiplying floats and may encounter small rounding
        # errors The threshold is saying "the numbers are equal, give-or-take this amount."
        self.assertTrue(A_inv_expected.equals(A.inverse(),threshold= 1E-4),f"A_inv is incorrect.")
        self.assertTrue(Matrix.identity(3).equals(A.times(A.inverse()),threshold= 1E-4),f"A•A^-1 should be I.")
        self.assertTrue(Matrix.identity(3).equals(A.inverse().times(A),threshold= 1E-4),f"A•A^-1 should be I.")

        self.assertIsNone(C.inverse())
Example #3
0
    def test_0_Mr_Howes_code(self):
        Mat_A = Matrix(A)
        Mat_Z = Matrix.zeros((2, 5))
        self.assertTrue(
            Mat_Z.equals(Matrix(((0, 0, 0, 0, 0), (0, 0, 0, 0, 0)))),
            "Zeros method not working correctly.")

        with self.assertRaises(AssertionError):
            Mat_Z2 = Matrix.zeros((0, 2))

        Mat_U = Matrix.ones((4, 4))
        self.assertTrue(
            Mat_U.equals(
                Matrix(
                    ((1, 1, 1, 1), (1, 1, 1, 1), (1, 1, 1, 1), (1, 1, 1, 1)))),
            "Ones method not working correctly.")
        with self.assertRaises(AssertionError):
            Mat_U2 = Matrix.ones((-1, 0))
        self.assertTupleEqual((4, 3), Mat_A.shape(
        ), f"Shape of A should be (4,3). You got a different shape: {Mat_A.shape()}."
                              )
        self.assertTupleEqual((2, 5), Mat_Z.shape(
        ), f"Shape of Z should be (2,5). You got a different shape: {Mat_Z.shape()}."
                              )
        self.assertTupleEqual((4, 4), Mat_U.shape(
        ), f"Shape of U should be (4,4). You got a different shape: {Mat_U.shape()}."
                              )
Example #4
0
    def test_2_transpose(self):
        A_T = A.transpose()
        self.assertTupleEqual(
            (3, 3), A_T.shape(),
            f"A transpose is wrong shape. You got {A_T.shape()}")
        self.assertTrue(A_T.equals(Matrix(((1, 4, 7), (2, 5, 8), (3, 6, 9)))),
                        f"A transpose is incorrect. You got {A_T}")

        # TODO: Write at least two more tests to check on other matrices, perhaps non-square ones.
        # self.assertTrue(False, "You didn't write this part, did you?")
        B_T = B.transpose()
        self.assertTupleEqual(
            (3, 2), B_T.shape(),
            f"B transpose is wrong shape. You got {B_T.shape()}")
        self.assertTrue(B_T.equals(Matrix(((0, 8), (4, -3), (6, 2)))),
                        f"B transpose is incorrect. You got {B_T}")
def compute_vect_reflection(w_vect, x_vect):
    # make sure values are numerical
    try:
        w2 = list(map(float, w_vect.split()))
        x2 = list(map(float, x_vect.split()))
    except ValueError:
        messagebox.showerror("ValueError", "Input Numerical Values Please")
    if len(w2) != 2:
        messagebox.showerror("Invalid w", "Vector w is not 2-Dimensional")
    if len(x2) != 2:
        messagebox.showerror("Invalid x", "Vector x is not 2-Dimensional")
    else:
        Mat_w2 = Matrix(2, 1, w2)
        Mat_x2 = Matrix(2, 1, x2)
        reflected_vector = Matrices.vector_reflection(Mat_w2, Mat_x2)
        messagebox.showinfo("Reflection(x) onto L",
                            "{}".format(reflected_vector))
Example #6
0
def main():
    mat1 = Matrix(mat=np.array([[1, 2, -3], [4, 0, -2]]))
    mat2 = Matrix(mat=np.array([[5, -4, 2, 0], [-1, 6, 3, 1], [7, 0, 5, 8]]))

    mat3 = Matrix(mat=np.array([[-1, 3, 1], [2, 5, 0], [3, 1, -2]]))
    mat4 = Matrix(mat=np.array([[5, -4, 2], [-1, 3, 1], [7, 0, 8]]))

    mat5 = Matrix(mat=[[2, 0], [-7, 5]])

    print("Printing out the content")
    print(mat1, "\n", mat2)

    print("Dot product - with real number directly")
    print(mat1 * 5)

    print("Dot product - with matrix directly")
    print(mat1 * mat2)
    print(mat3 * mat4)

    print("Determinant")
    print(abs(mat3))

    print("Identities")
    print(Matrix.identity(3))

    print("Zero")
    print(Matrix.zeros(3, 4))
Example #7
0
    def test_1_Identity(self):
        Mat_I2 = Matrix.identity(2)
        self.assertTrue(
            Mat_I2.equals(Matrix(((1, 0), (0, 1)))),
            f"identity of (2) incorrect. You got {Mat_I2.__repr__()}")

        Mat_I5 = Matrix.identity(5)
        self.assertTrue(
            Mat_I5.equals(
                Matrix(((1, 0, 0, 0, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 0),
                        (0, 0, 0, 1, 0), (0, 0, 0, 0, 1)))),
            f"identity of (5) incorrect. You got {Mat_I5.__repr__()}")

        Mat_I8 = Matrix.identity(8)
        self.assertTrue(
            Mat_I8.equals(
                Matrix(((1, 0, 0, 0, 0, 0, 0, 0), (0, 1, 0, 0, 0, 0, 0, 0),
                        (0, 0, 1, 0, 0, 0, 0, 0), (0, 0, 0, 1, 0, 0, 0, 0),
                        (0, 0, 0, 0, 1, 0, 0, 0), (0, 0, 0, 0, 0, 1, 0, 0),
                        (0, 0, 0, 0, 0, 0, 1, 0), (0, 0, 0, 0, 0, 0, 0, 1)))),
            f"identity of (8) incorrect. You got {Mat_I8.__repr__()}")
 def test_vector_reflection(self):
     w_vector = Matrix(2, 1, [1, 0])
     x_vector = Matrix(2, 1, [0, 1])
     self.assertEqual(Matrices.vector_reflection(w_vector, x_vector),
         Matrix(2, 1, [0, -1]))
 def test_vector_projection_1(self):
     w_vector = Matrix(2, 1, [1, 0]) 
     x_vector = Matrix(2, 1, [1, 1])
     self.assertEqual(Matrices.vector_projection(w_vector, x_vector),
             Matrix(2, 1, [1, 0]))
 def test_add_1(self):
     A = Matrix(2, 2, [1, 1, 1, 1])
     B = Matrix(2, 2, [1, 1, 1, 1])
     self.assertEqual(Matrices.add(A, B), Matrix(2, 2, [2, 2, 2, 2]))
 def test_product_1(self):
     B = Matrix(2, 2, [1, 2, 3, 4])
     A = Matrix(2, 2, [5, 6, 7, 8])
     B_x_A = Matrix(2, 2, [19, 22, 43, 50])
     self.assertEqual(B_x_A, Matrices.product(B, A))
 def test_column_list_2(self):   
     Mat = Matrix(3, 2, [1, 2, 3, 4, 5, 6])
     self.assertEqual(Mat.column_lists, [[1, 3, 5], [2, 4, 6]])
 def test_column_list_1(self):
     Mat = Matrix(2, 2, [1, 2, 3, 4])
     self.assertEqual(Mat.column_lists, [[1, 3], [2, 4]])
Example #14
0
def main():
    mat1 = Matrix(mat=np.array([[1, 2, -3], [4, 0, -2]]))
    mat2 = Matrix(mat=np.array([[5, -4, 2, 0], [-1, 6, 3, 1], [7, 0, 5, 8]]))

    mat3 = Matrix(mat=np.array([[-1, 3, 1], [2, 5, 0], [3, 1, -2]]))
    mat4 = Matrix(mat=np.array([[5, -4, 2], [-1, 3, 1], [7, 0, 8]]))

    mat5 = Matrix(mat=[[2, 0], [-7, 5]])

    m1 = Matrix(mat=[[3, -1, 0], [10, 0, 5], [2, 4, 7]])
    m1 = Matrix(mat=[[1, -1, 2], [2, 1, -1], [-1, 2, 3]])
    m1 = Matrix(
        mat=[[1, 2, 4, 8], [1, 3, 9, 27], [1, 4, 16, 64], [1, 5, 25, 125]])
    print(m1.det())
    print(m1.inv())
Example #15
0
 def positionVector(self):
     mat = Matrix((4, 1))
     mat.matrix = [[self.x, self.y, self.z, 1.0]]
     return mat
Example #16
0
import unittest
from Matrices import Matrix

A = Matrix(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
B = Matrix(((0, 4, 6), (8, -3, 2)))


class MyTestCase(unittest.TestCase):
    def test_2_transpose(self):
        A_T = A.transpose()
        self.assertTupleEqual(
            (3, 3), A_T.shape(),
            f"A transpose is wrong shape. You got {A_T.shape()}")
        self.assertTrue(A_T.equals(Matrix(((1, 4, 7), (2, 5, 8), (3, 6, 9)))),
                        f"A transpose is incorrect. You got {A_T}")

        # TODO: Write at least two more tests to check on other matrices, perhaps non-square ones.
        # self.assertTrue(False, "You didn't write this part, did you?")
        B_T = B.transpose()
        self.assertTupleEqual(
            (3, 2), B_T.shape(),
            f"B transpose is wrong shape. You got {B_T.shape()}")
        self.assertTrue(B_T.equals(Matrix(((0, 8), (4, -3), (6, 2)))),
                        f"B transpose is incorrect. You got {B_T}")

    def test_3_double_transpose(self):
        self.assertTrue(
            A.transpose().transpose().equals(A),
            f"A^T^T should be A. You got {A.transpose().transpose()}")

        #TODO: check that your other matrices work for this, as well.
 def test_repr_1(self):
     Mat = Matrix(2, 2, [1, 2, 3, 4])
     self.assertEqual(str(Mat), "1, 2, \n3, 4;")
Example #18
0
import unittest
from Matrices import Matrix

A = Matrix([[4,2,9],[-1,4,7],[8,3,0]])
A_inv_expected = Matrix([[21/287, -27/287, 22/287],
                  [-56/287, 72/287, 37/287],
                  [35/287, -4/287, -18/287]])

B = Matrix([[5],[-2], [9]])

C = Matrix([[2,4,6],
           [8,10,12],
           [14,16,18]])
class MyTestCase(unittest.TestCase):
    def test_11_inverse(self):

        #Note: I'm adding a threshold here in equals, since we are multiplying floats and may encounter small rounding
        # errors The threshold is saying "the numbers are equal, give-or-take this amount."
        self.assertTrue(A_inv_expected.equals(A.inverse(),threshold= 1E-4),f"A_inv is incorrect.")
        self.assertTrue(Matrix.identity(3).equals(A.times(A.inverse()),threshold= 1E-4),f"A•A^-1 should be I.")
        self.assertTrue(Matrix.identity(3).equals(A.inverse().times(A),threshold= 1E-4),f"A•A^-1 should be I.")

        self.assertIsNone(C.inverse())

    def test_12_solve_equation(self):
        # solve         A * x = B
        #        A^-1 * A * x = A^-1 * B      multiply A^-1 by each side
        #               I * x = A^-1 * B      simplify A^-1 * A = I
        #                   x = A^-1 * B      I is multiplicative identity

        x = A.inverse().times(B)
Example #19
0
import unittest
from Matrices import Matrix

A = Matrix(((2, 5, 8), (3, 5, 6), (9, 12, 15)))
B = Matrix.identity(3)
C = Matrix(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
D = Matrix.ones((3, 5))


class MyTestCase(unittest.TestCase):
    def test_10_determinant(self):
        self.assertEqual(-21, A.determinant(),
                         f"|A| should be -21. You got {A.determinant()}.")
        self.assertEqual(1, B.determinant(),
                         f"|B| should be 1. You got {B.determinant()}.")
        self.assertEqual(0, C.determinant(),
                         f"|C| should be 0. You got {C.determinant()}.")
        with self.assertRaises(AssertionError):
            d = D.determinant()


if __name__ == '__main__':
    unittest.main()
import unittest
from Matrices import Matrix

mat_A = Matrix(((3,4),(6,7)))
mat_B = Matrix.ones((2,2))
mat_A_plus_B_expected = Matrix(((4,5),(7,8)))
mat_4B_expected = Matrix(((4,4),(4,4)))
mat_C = Matrix(((3,5,7),(11,13,17)))
mat_A_times_B_expected = Matrix(((7,7),(13,13)))
mat_A_times_C_expected = Matrix(((53,67,89),(95,121,161)))
mat_D = Matrix(((3,4),))
mat_E = Matrix(((5,9),))
mat_F = Matrix(((1,2,6),))
mat_G = Matrix(((5,-2,8),))
mat_H = Matrix(((28,22,-12),))
mat_I = Matrix.identity(4)
mat_J = Matrix(((2,3,4,5),(12,14,16,18),(-4, 8, 0, -2)))
mat_K = Matrix(((3,4),(5,6),(12,0),(-3,8)))
mat_JK_expected = Matrix(((54,66),(244,276),(34,16)))

class MyTestCase(unittest.TestCase):
    def test_4_sum(self):
        self.assertTrue(mat_A_plus_B_expected.equals(mat_A.add(mat_B)),"A + B was incorrect.")
        self.assertTrue(mat_4B_expected.equals(mat_B.add(mat_B).add(mat_B).add(mat_B)),"B + B + B + B incorrect.")
        with self.assertRaises(AssertionError):
            mat_A.add(mat_C)


    def test_5_scalar_multiply(self):
        self.assertTrue(mat_4B_expected.equals(mat_B.times(4)), f"4B is incorrect. You got {mat_B.times(4)}")
 def test_row_list_1(self):
     Mat = Matrix(2, 2, [1, 2, 3, 4])
     self.assertEqual(Mat.row_lists, [[1, 2], [3, 4]])
 def test_row_list_2(self):
     Mat = Matrix(2, 3, [1, 2, 3, 4, 5, 6])
     self.assertEqual(Mat.row_lists, [[1, 2, 3], [4, 5, 6]])
 def test_row_list_3(self):
     Mat = Matrix(3, 2, [1, 2, 3, 4, 5, 6])
     self.assertEqual(Mat.row_lists, [[1, 2], [3, 4], [5, 6]])
Example #24
0
 def __init__(self, boneCount=0):
     super().__init__(lambda: Matrix([4, 4]), boneCount)
Example #25
0
import unittest
from Matrices import Matrix

A = Matrix(((1, 2, 3), (4, 5, 6), (7, 8, 9)))


class MyTestCase(unittest.TestCase):
    def test_2_transpose(self):
        A_T = A.transpose()
        self.assertTupleEqual(
            (3, 3), A_T.shape(),
            f"A transpose is wrong shape. You got {A_T.shape()}")
        self.assertTrue(A_T.equals(Matrix(((1, 4, 7), (2, 5, 8), (3, 6, 9)))),
                        f"A transpose is incorrect. You got {A_T}")

        # TODO: Write at least two more tests to check on other matrices, perhaps non-square ones.
        self.assertTrue(False, "You didn't write this part, did you?")

    def test_3_double_transpose(self):
        self.assertTrue(
            A.transpose().transpose().equals(A),
            f"A^T^T should be A. You got {A.transpose().transpose()}")

        #TODO: check that your other matrices work for this, as well.
        self.assertTrue(False, "You didn't write this part, either.")


if __name__ == '__main__':
    unittest.main()