Beispiel #1
0
def mult(matrix1: list, matrix2: list) -> list:
    dims1 = mv.lengths(matrix1)
    dims2 = mv.lengths(matrix2)

    if dims1[1] != dims2[0] and not mv.is_scalar(matrix1):
        raise Exception("Matrices' row and column does not match! \n\n"
                        + str(mv.lengths(matrix1))
                        + str(mv.lengths(matrix2))
                        )

    result = initialize(dims1[0], dims2[1])
    # vector
    if mv.is_vector(matrix1) and mv.is_line_vector(matrix2):
        return mult_core_vectors_to_square_matrix(matrix1, matrix2, result)
    elif mv.is_line_vector(matrix1) and mv.is_vector(matrix2):
        return mult_core_vectors_to_scalar(matrix1, matrix2, result)
    # scalar
    elif mv.is_scalar(matrix1):
        return mult_core_scalar(matrix1, matrix2)
    # matrix
    else:
        return mult_core_matrices(matrix1, matrix2, result)
Beispiel #2
0
def determinant(matrix: list, row= 0) -> float:
    mv.validate_square_matrix(matrix)
    result: float = 0
    cofacs = []

    if mv.is_2d(matrix) and mv.is_scalar(matrix[0]):
        result = matrix[0][0]
    else:
        for det_col in range(len(matrix[row])):
            cofacs.append(cofactor_adj(matrix, row, det_col))

        for i in range(len(matrix[row])):
            el = matrix[row][i]
            result += el * cofacs[i]

    return result
 def test_is_not_scalar_matrix(self):
     m = [[1, 2], [3, 4]]
     self.assertEqual(mv.is_scalar(m), False)
 def test_is_not_scalar_float(self):
     e = 69
     self.assertRaises(Exception, lambda: mv.is_scalar(e))
 def test_is_scalar_nested(self):
     e = [[69]]
     self.assertEqual(mv.is_scalar(e), True)
 def test_is_scalar(self):
     e = [69]
     self.assertEqual(mv.is_scalar(e), True)