def test_Matrix_toFile_fromFile(): matrix = Matrix([[1, 2.5], [4, 5.5], ]) matrix.toFile("output.txt") newMatrix = Matrix.fromFile("output.txt") assert newMatrix == matrix
def test_Matrix_LU(): list = [[2,3,1,5], [6,13,5,19], [2,19,10,23], [4,10,11,31], ] LU = [[2,3,1,5], [3,4,2,4], [1,4,1,2], [2,1,7,3], ] LTrue = [[1,0,0,0], [3,1,0,0], [1,4,1,0], [2,1,7,1], ] UTrue = [[2,3,1,5], [0,4,2,4], [0,0,1,2], [0,0,0,3], ] matrix = Matrix(list) L, U = Matrix.LU(matrix) assert matrix == Matrix(list) #assert matrix == Matrix(LU) assert L == Matrix(LTrue) assert U == Matrix(UTrue)
def test_Matrix_solveAxbWithLUP(Matrix120): b = Matrix([[0.1], [12.5], [10.3]]) x, y = Matrix.solveAxbWithLUP(Matrix120, b) assert y == Matrix([[10.3], [6.319999], [-5.571428]]) assert x == Matrix([[0.5], [-0.2], [3]])
def TryToSolveATask(path): A, b = CreateMatricies(path) if (path == "Tasks/Task_6Y.txt"): for i in range(0, A.n_rows): A._matrix[0][i] /= float(10**9) A._matrix[2][i] *= float(10**10) b._matrix[0][0] /= float(10**9) b._matrix[2][0] *= float(10**10) print "-.-.- Applied special transformations -.-.-" try: print "-.-.- solveAxbWithLU -.-.-" x, y = Matrix.solveAxbWithLU(A, b) PrintResults(x, y) except ZeroDivisionError: print u"Cannot solve with LU" print try: print "-.-.- solveAxbWithLUP -.-.-" x, y = Matrix.solveAxbWithLUP(A, b) PrintResults(x, y) except ZeroDivisionError: print u"Cannot solve with LUP" print
def test_Matrix_solveAxbWithLU(Matrix120): b = Matrix([[0.1], [12.5], [10.3]]) x, y = Matrix.solveAxbWithLU(Matrix120, b) assert y == Matrix([[0.1], [12.2], [-39]]) assert x == Matrix([[0.5], [-0.2], [3]])
def test_Matrix_LUP(Matrix120): list = [[1, 2, 0], [3, 5, 4], [5, 6, 3], ] LUP = [[5, 6, 3], [3/5., 7/5., 11/5.], [1/5., 4/7., -13/7.], ] LTrue = [[1, 0, 0], [3/5., 1, 0], [1/5., 4/7., 1], ] UTrue = [[5, 6, 3], [0, 7/5., 11/5.], [0, 0, -13/7.], ] PTrue = [[0, 0, 1], [0, 1, 0], [1, 0, 0], ] L, U, P = Matrix.LUP(Matrix120) assert Matrix120 == Matrix(list) #assert Matrix120 == Matrix(LUP) assert L == Matrix(LTrue) assert U == Matrix(UTrue) assert P == Matrix(PTrue)
def test_Matrix_inverse(): A = Matrix([[1, 2], [3, 4], ]) result = Matrix([[-2, 1], [3/2., -1/2.], ]) A.inverse() assert A == result
def test_Matrix_solveUxy(Matrix120): b = Matrix([[0.1], [12.5], [10.3]]) L, U, P = Matrix.LUP(Matrix120) y = Matrix120._solveLyPb(L, P, b) assert y == Matrix([[10.3], [6.319999], [-5.571428]]) x = Matrix120._solveUxy(U, y) assert x == Matrix([[0.5], [-0.2], [3]])
def test_Matrix_solveLyPb(Matrix120): b = Matrix([[0.1], [12.5], [10.3]]) L, U = Matrix.LU(Matrix120) P = Matrix120._CreateI() y = Matrix120._solveLyPb(L, P, b) assert y == Matrix([[0.1], [12.2], [-39]]) x = Matrix120._solveUxy(U, y) assert x == Matrix([[0.5], [-0.2], [3]])
def Matrix120(): matrix = Matrix([[1, 2, 0], [3, 5, 4], [5, 6, 3], ]) return matrix
def Matrix2468(): matrix = Matrix([[2, 4], [6, 8], ]) return matrix
def test_Matrix__init__Dimensions(): matrix = Matrix(3,3) assert matrix[(3,3)] == 0
def test_Matrix__eq__F(MatrixDull): newMatrix = Matrix([[1, 2], [3,4], [5,6] ]) assert (MatrixDull == newMatrix) == False
def test_Matrix__eq__(MatrixDull): newMatrix = Matrix([[1.000001, 22], [333, 4444], [55555,666666] ]) assert (MatrixDull == newMatrix) == True
def Matrix1234(): matrix = Matrix([[1, 2], [3, 4], ]) return matrix
def test_Matrix__mul__Huge(): matrixManyRows = Matrix([[1], [2], [3], [4], [5]]) matrixManyColumns = Matrix([[1,2,3,4,5]]) hugeMatrix = matrixManyRows * matrixManyColumns assert hugeMatrix[5,5] == 25
def test_Matrix__mul__SingleValue(): matrixManyRows = Matrix([[1], [2], [3], [4], [5]]) matrixManyColumns = Matrix([[1,2,3,4,5]]) singleValueMatrix = matrixManyColumns * matrixManyRows assert singleValueMatrix[(1,1)] == 55
def MatrixDull(): matrix = Matrix([[1, 22], [333, 4444], [55555,666666] ]) return matrix
def CreateMatricies(path): A = Matrix.fromFile(path.replace("Y", "A")) b = Matrix.fromFile(path.replace("Y", "b")) PrintLoaded(A, b) return A, b