def test_diagonal_inverse_recovers_identity(self): M = Mat([2.0, 0.0], [0.0, 4.0]) Minv = M.inv() assert Minv.__origin__ is M.__origin__ assert type(Minv) is type(M) assert Minv * M == Mat([1.0, 0.0], [0.0, 1.0]) assert M * Minv == Mat([1.0, 0.0], [0.0, 1.0])
def __init__(self, matrix, vector): # Convert values if isinstance(matrix, Mat): matrix = matrix else: matrix = Mat(*matrix) # linearize inputs and save in the flat attribute self.shape = matrix.shape data = list(map(list, matrix)) for x, L in zip(vector, data): L.append(x) self.flat = sum(data, [])
def test_withrow_matrix_middle(): M1 = Mat([1, 2], [3, 4]) M2 = Mat([1, 2], [5, 6], [7, 8], [3, 4]) M3 = M1.append_row(Mat([5, 6], [7, 8]), index=1) assert M2 == M3
def test_withcol_vec_middle(): M1 = Mat([1, 2], [3, 4]) M2 = Mat([1, 5, 2], [3, 6, 4]) M3 = M1.append_col([5, 6], index=1) assert M2 == M3
def test_rows(): M1 = Mat([1, 2], [3, 4], [5, 6]) assert list(M1.rows()) == [Vec(1, 2), Vec(3, 4), Vec(5, 6)]
def test_withrow_matrix(): M1 = Mat([1, 2], [3, 4]) M2 = Mat([1, 2], [3, 4], [5, 6], [7, 8]) M3 = M1.append_row(Mat([5, 6], [7, 8])) assert M2 == M3
def test_cols(): M1 = Mat([1, 2], [3, 4], [5, 6]) assert list(M1.cols()) == [Vec(1, 3, 5), Vec(2, 4, 6)]
def test_swaprows(): M1 = mMat([1, 2], [3, 4]) M1.iswap_rows(0, 1) M2 = Mat([3, 4], [1, 2]) assert M1 == M2
def test_withcol_vec(): M1 = Mat([1, 2], [3, 4]) M2 = Mat([1, 2, 5], [3, 4, 6]) M3 = M1.append_col([5, 6]) assert M2 == M3
def test_transpose_sqr(): M1 = Mat([1, 2], [3, 4]) M2 = Mat([1, 3], [2, 4]) assert M1.T == M2 assert M1 == M2.T
def test_withrow_vec_middle(): M1 = Mat([1, 2], [3, 4]) M2 = Mat([1, 2], [5, 6], [3, 4]) M3 = M1.append_row([5, 6], index=1) assert M2 == M3
def test_withrow_vec(): M1 = Mat([1, 2], [3, 4]) M2 = Mat([1, 2], [3, 4], [5, 6]) M3 = M1.append_row([5, 6]) assert M2 == M3
def test_droppingcol(): M1 = Mat([1, 2], [3, 4], [5, 6]) M2 = Mat([1], [3], [5]) assert M1.drop_col(1) == (M2, Vec(2, 4, 6))
def test_selectcols(): M1 = Mat([1, 2, 3], [4, 5, 6], [7, 8, 9]) M2 = Mat([1, 3], [4, 6], [7, 9]) assert M1.select_cols(0, 2) == M2
def test_droppingrow(): M1 = Mat([1, 2], [3, 4], [5, 6]) M2 = Mat([1, 2], [5, 6]) assert M1.drop_row(1) == (M2, Vec(3, 4))
def test_eig2(): M1 = Mat([1, 0], [0, 2]) assert M1.eigenpairs() == [(2.0, (0.0, 1.0)), (1.0, (1.0, 0.0))]
def test_rowadd_sqr(): M1 = mMat([1, 2], [3, 4]) M1.isum_row(1, (1, 1)) M2 = Mat([1, 2], [4, 5]) assert M1 == M2
def test_selectrows(): M1 = Mat([1, 2], [3, 4], [5, 6]) M2 = Mat([1, 2], [5, 6]) assert M1.select_rows(0, 2) == M2
def test_inverse_of_diagonal_matrix(self): M = Mat([2, 0], [0, 4]) Minv = M.inv() assert Minv == Mat([0.5, 0], [0, 0.25])
def test_widthdiag(): M1 = Mat([1, 2], [3, 4]) M2 = Mat([0, 2], [3, 0]) assert M1.set_diag([0, 0]) == M2 assert M1.drop_diag() == M2
def test_det2(): M1 = Mat([1, 0], [0, 2]) M2 = Mat([1, 2], [3, 4]) assert M1.det() == 2 assert M2.det() == -2
def test_create_from_cols(self, M): cols = list(M.T) assert M == Mat.from_cols(cols)
def test_create_from_rows(self, M): rows = list(M) assert M == Mat.from_rows(rows)
def test_transpose(): M1 = Mat([1, 2, 3], [4, 5, 6]) M2 = Mat([1, 4], [2, 5], [3, 6]) assert M1.T == M2 assert M1 == M2.T
def test_rowmul(): M1 = mMat([1, 2], [3, 4]) M1.imul_row(1, 2) M2 = Mat([1, 2], [6, 8]) assert M1 == M2
def test_fromrows_sqr(): M1 = Mat([1, 2], [3, 4]) M2 = Mat.from_rows([[1, 2], [3, 4]]) assert M1 == M2
def test_colmul(): M1 = mMat([1, 2], [3, 4]) M1.imul_col(1, 2) M2 = Mat([1, 4], [3, 8]) assert M1 == M2
def test_get_item(): M1 = Mat([1, 2], [3, 4]) assert M1[0] == [1, 2] assert M1[1] == [3, 4]
def test_coladd(): M1 = mMat([1, 2], [3, 4]) M1.isum_col(1, (1, 1)) M2 = Mat([1, 3], [3, 5]) assert M1 == M2
def test_flat2x2(): M1 = Mat([1, 2], [3, 4]) assert list(M1.flat) == [1, 2, 3, 4]
def test_flat2x2_redo(): M1 = Mat([1, 2], [3, 4]) assert M1, Mat[2, 2].from_flat(M1.flat)
def test_swapcols(): M1 = mMat([1, 2], [3, 4]) M1.iswap_cols(0, 1) M2 = Mat([2, 1], [4, 3]) assert M1 == M2