def test_mul(): X = Matrix( [ [12, 7, 3], [4, 5, 6], [7, 8, 9], ] ) Y = Matrix( [ [5, 8, 1, 2], [6, 7, 3, 0], [4, 5, 9, 1], ] ) R = Matrix( [ [114, 160, 60, 27], [74, 97, 73, 14], [119, 157, 112, 23], ] ) assert X * Y == R
def tridiag(): m = Matrix(TRI_DIAGONAL) a = [0] # a0 is not used but must be present a.extend(m.diag(-1)) b = m.diag(0) c = m.diag(+1) return a, b, c
def test_iadd(X): Y = X Y += Y assert Y == Matrix([[24, 14], [8, 10], [6, 16]]) assert Y[0, 0] != X[0, 0] Z = Y Z += 10 assert Z == Matrix([[34, 24], [18, 20], [16, 26]]) assert Y[0, 0] != Z[0, 0]
def test_detect_banded_matrix(): m1, m2 = detect_banded_matrix(BANDED_MATRIX) assert (m1, m2) == (2, 1) m1, m2 = detect_banded_matrix(TRICKY, check_all=False) assert (m1, m2) == (2, 1) m1, m2 = detect_banded_matrix(TRICKY, check_all=True) assert (m1, m2) == (5, 6) assert detect_banded_matrix(Matrix(shape=(10, 10))) == (0, 0) identity = Matrix.identity(shape=(10, 10)) assert detect_banded_matrix(identity) == (0, 0)
def test_imul(): X = Matrix([ [12, 7, 3], [4, 5, 6], [7, 8, 9], ]) Y = X Y *= 10 assert Y == Matrix([ [120, 70, 30], [40, 50, 60], [70, 80, 90], ]) assert X[0, 0] != Y[0, 0]
def matrix_init(X): Y = Matrix([12, 7, 4, 5, 3, 8], shape=(3, 2)) assert X.shape == X.nrows, X.ncols assert Y.shape == (3, 2) assert X == Y Y = Matrix(shape=(3, 3)) assert Y == Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) Y = Matrix(X) assert Y == X Y[0, 0] = -1 assert Y != X, "should not share the same list objects" Y = Matrix(X, shape=(2, 3)) assert Y.rows() == [[12, 7, 4], [5, 3, 8]]
def test_transpose(X): R = Matrix([ (12, 4, 3), (7, 5, 8), ]) T = X.transpose() assert T == R # is T mutable? T[0, 0] = 99 assert T[0, 0] == 99
def test_diag(): m = Matrix(DIAG) assert m.diag(0) == [1, 2, 3, 4] assert m.diag(1) == [2, 3, 4] assert m.diag(2) == [5, 6] assert m.diag(3) == [7] assert m.diag(4) == [] assert m.diag(-1) == [5, 4, 3] assert m.diag(-2) == [7, 6] assert m.diag(-3) == [8] assert m.diag(-4) == []
import pytest import math from ezdxf.math import ( Matrix, detect_banded_matrix, compact_banded_matrix, BandedMatrixLU, gauss_vector_solver, banded_matrix, ) BANDED_MATRIX = Matrix(matrix=[ [3, 1, 0, 0, 0, 0, 0], [4, 1, 5, 0, 0, 0, 0], [9, 2, 6, 5, 0, 0, 0], [0, 3, 5, 8, 9, 0, 0], [0, 0, 7, 9, 3, 2, 0], [0, 0, 0, 3, 8, 4, 6], [0, 0, 0, 0, 2, 4, 4], ]) TRICKY = Matrix(matrix=[ [3, 1, 0, 0, 0, 0, 1], [4, 1, 5, 0, 0, 0, 0], [9, 2, 6, 5, 0, 0, 0], [0, 3, 5, 8, 9, 0, 0], [0, 0, 7, 9, 3, 2, 0], [0, 0, 0, 3, 8, 4, 6], [0, 1, 0, 0, 2, 4, 4], ])
def test_LU_decomposition_inverse(): m = Matrix(matrix=EXPECTED_INVERSE) assert LUDecomposition(A).inverse() == m
def test_gauss_jordan_inverse(): result = gauss_jordan_inverse(A) assert result.nrows == len(A) assert result.ncols == len(A[0]) m = Matrix(matrix=EXPECTED_INVERSE) assert result == m
def test_gauss_jordan_vector_solver(): B = Matrix(items=B1, shape=(5, 1)) result_A, result_B = gauss_jordan_solver(A, B) are_close_vectors(result_B.col(0), SOLUTION_B1)
def test_identity(): m = Matrix.identity((3, 4)) for i in range(3): assert m[i, i] == 1.0
def test_set_diag_iterable(): m = Matrix(shape=(4, 4)) m.set_diag(0, range(5)) for i in range(4): assert m[i, i] == i
def test_set_diag_below(): m = Matrix(shape=(4, 4)) m.set_diag(-1, 2) for i in range(3): assert m[i + 1, i] == 2.0
def test_set_diag_above(): m = Matrix(shape=(4, 4)) m.set_diag(1, 2) for i in range(3): assert m[i, i + 1] == 2.0
def test_set_diag_float(): m = Matrix(shape=(4, 4)) m.set_diag(0, 2) for i in range(4): assert m[i, i] == 2.0
def test_build_matrix_by_cols(): m = Matrix() m.append_col([1, 2, 3]) assert m.nrows == 3 assert m.ncols == 1
def test_sub(X): R = X - X assert R == Matrix([[0, 0], [0, 0], [0, 0]]) R = X - 10 assert R == Matrix([[2, -3], [-6, -5], [-7, -2]])
def X(): return Matrix([[12, 7], [4, 5], [3, 8]])
def test_add(X): R = X + X assert R == Matrix([[24, 14], [8, 10], [6, 16]]) R = R + 10 assert R == Matrix([[34, 24], [18, 20], [16, 26]])