(ctx, A, **kwargs)
return result
Calculate the inverse of a matrix.

        If you want to solve an equation system Ax = b, it's recommended to use
        solve(A, b) instead, it's about 3 times more efficient.
Calculate the inverse of a matrix.
Calculate the inverse of a matrix .
def inverse(ctx, A, **kwargs):
        """
        Calculate the inverse of a matrix.

        If you want to solve an equation system Ax = b, it's recommended to use
        solve(A, b) instead, it's about 3 times more efficient.
        """
        prec = ctx.prec
        try:
            ctx.prec += 10
            # do not overwrite A
            A = ctx.matrix(A, **kwargs).copy()
            n = A.rows
            # get LU factorisation
            A, p = ctx.LU_decomp(A)
            cols = []
            # calculate unit vectors and solve corresponding system to get columns
            for i in xrange(1, n + 1):
                e = ctx.unitvector(n, i)
                y = ctx.L_solve(A, e, p)
                cols.append(ctx.U_solve(A, y))
            # convert columns to matrix
            inv = []
            for i in xrange(n):
                row = []
                for j in xrange(n):
                    row.append(cols[j][i])
                inv.append(row)
            result = ctx.matrix(inv, **kwargs)
        finally:
            ctx.prec = prec
        return result
def inverse ( ctx , A , * * kwargs ) : prec = ctx . prec try : ctx . prec += 10 # do not overwrite A A = ctx . matrix ( A , * * kwargs ) . copy ( ) n = A . rows # get LU factorisation A , p = ctx . LU_decomp ( A ) cols = [ ] # calculate unit vectors and solve corresponding system to get columns for i in xrange ( 1 , n + 1 ) : e = ctx . unitvector ( n , i ) y = ctx . L_solve ( A , e , p ) cols . append ( ctx . U_solve ( A , y ) ) # convert columns to matrix inv = [ ] for i in xrange ( n ) : row = [ ] for j in xrange ( n ) : row . append ( cols [ j ] [ i ] ) inv . append ( row ) result = ctx . matrix ( inv , * * kwargs ) finally : ctx . prec = prec return result