(m)
invert an n x n matrix
invert an n x n matrix
invert an n x n matrix
def invert_matrix(m):
    """invert an n x n matrix"""
    # Error conditions
    if not array_is_matrix(m):
        print("Invalid matrix: array is not a matrix")
        return [[-1]]
    elif len(m) != len(m[0]):
        print("Invalid matrix: matrix is not square")
        return [[-2]]
    elif len(m) < 2:
        print("Invalid matrix: matrix is too small")
        return [[-3]]
    elif get_determinant(m) == 0:
        print("Invalid matrix: matrix is square, but singular (determinant = 0)")
        return [[-4]]

    # Calculation
    elif len(m) == 2:
        # simple case
        multiplier = 1 / get_determinant(m)
        inverted = [[multiplier] * len(m) for n in range(len(m))]
        inverted[0][1] = inverted[0][1] * -1 * m[0][1]
        inverted[1][0] = inverted[1][0] * -1 * m[1][0]
        inverted[0][0] = multiplier * m[1][1]
        inverted[1][1] = multiplier * m[0][0]
        return inverted
    else:
        """some steps combined in helpers to reduce traversals"""
        # get matrix of minors w/ "checkerboard" signs
        m_of_minors = get_matrix_of_minors(m)

        # calculate determinant (we need to know 1/det)
        multiplier = fractions.Fraction(1, get_determinant(m))

        # adjugate (swap on diagonals) and multiply by 1/det
        inverted = transpose_and_multiply(m_of_minors, multiplier)

        return inverted
def invert_matrix ( m ) : # Error conditions if not array_is_matrix ( m ) : print ( "Invalid matrix: array is not a matrix" ) return [ [ - 1 ] ] elif len ( m ) != len ( m [ 0 ] ) : print ( "Invalid matrix: matrix is not square" ) return [ [ - 2 ] ] elif len ( m ) < 2 : print ( "Invalid matrix: matrix is too small" ) return [ [ - 3 ] ] elif get_determinant ( m ) == 0 : print ( "Invalid matrix: matrix is square, but singular (determinant = 0)" ) return [ [ - 4 ] ] # Calculation elif len ( m ) == 2 : # simple case multiplier = 1 / get_determinant ( m ) inverted = [ [ multiplier ] * len ( m ) for n in range ( len ( m ) ) ] inverted [ 0 ] [ 1 ] = inverted [ 0 ] [ 1 ] * - 1 * m [ 0 ] [ 1 ] inverted [ 1 ] [ 0 ] = inverted [ 1 ] [ 0 ] * - 1 * m [ 1 ] [ 0 ] inverted [ 0 ] [ 0 ] = multiplier * m [ 1 ] [ 1 ] inverted [ 1 ] [ 1 ] = multiplier * m [ 0 ] [ 0 ] return inverted else : """some steps combined in helpers to reduce traversals""" # get matrix of minors w/ "checkerboard" signs m_of_minors = get_matrix_of_minors ( m ) # calculate determinant (we need to know 1/det) multiplier = fractions . Fraction ( 1 , get_determinant ( m ) ) # adjugate (swap on diagonals) and multiply by 1/det inverted = transpose_and_multiply ( m_of_minors , multiplier ) return inverted