Making code for calculating such a matrix. It consists of two parts – a direct move on which I get a triangular matrix with units on the main diagonal and zeros below. In the second stage, movingupup, transforming elements underlying the main diagonal to zero, thereby seeking a single matrix as a result.
Thank you: Earlier here they suggested with the first part – the array gives a triangular matrix. However, trying to reverse, catch a mistake indicating problems with dimension: Shapes (1.6) and (1.6) not aligned: 6 (dim 1)! = 1 (dim 0)
import numpy as np
DEF INVERSE_MATRIX (Matrix_origin):
"" "
The function receives on the matrix input, then adds a single matrix to it,
Conducts elementary transformations on strings from the initial, seeking to obtain a single matrix.
In this case, the right will be the matrix, which is the reverse to the specified initial
"" "
# Glue 2 matrices: on the left  the original, right  single
m = np.hstack ((Matrix_origin,
np.matrix (np.diag ([1.0 for i in Range (Matrix_origin.shape [0])))))
# N = Matrix_origin.shape [1]
N = M.SHAPE [1]
For NROW, ROW in Enumerate (M):
# NROW is equal to line number
# Row contains the row of the matrix
Divider = Row [NROW] # diagonal element
# Delim on a diagonal element:
ROW / = Divider
# Now deduct the line of all underlying rows:
For Lower_ROW in M [Nrow + 1:]:
Factor = Lower_Row [NROU] # Row element in the NROW column
Lower_ROW  = Factor * Row # Remove to get zero in the nrow column
# Return:
For k in Range (n  1, 0, 1):
For Row_ in Range (K  1, 1, 1):
IF M [ROW_, K]:
# 1) all the elements above the main diagonal do equal to zero
M [ROW_,:]  = M [K,:] * M [ROW_, K]
Return np.hsplit (m, n // 2) [1]
Matrix = np.array ([[3.8, 6.7, 1.2],
[6.4, 1.3, 2.7],
[2.4, 4.5, 3.5]])
INVERSE_MATRIX (NP.COPY (MATRIX))
Answer 1
You have several errors in the code.

You design the right side as
np.matrix
, so afternp.hstack
you also get an object likeNP.MATRIX
. This is a very special type of arrays. In particular, theM
iterator returns not onedimensional lines of numbers, and the twodimensional arrays of theform>(1.6)
. As a result,Row [NROW]
is not a number, but a onedimensional array.NUMPY
There is a special function for constructing a single matrix:np.eye (n)
builds a single matrix in sizen x n
. Therefore, you better designM
asm = np.hstack ((Matrix_origin, NP.Eye (Len (Matrix_origin)))

You initialized
n
as the number of columns inm
:m = m.shape [1]
. But then this line is incorrect:for Row_ in Range (K  1, 1, 1)
– You have no rows with numbers 5,4.3. Ifn
defines the number of rows, then you need to be assigned like this:n = M.Shape [0]

But in this case, your item is broken by the right part of
np.hsplit (M, N // 2) [1]
. I propose to use indexing instead of Hsplit:M [:, n:]. Copy ()
There is a twodimensional array, each line of which is a string fromM
Starting from theN
, that is, elements No. 3.4 and 5. Just the right side of the matrixM
. Thecopy ()
method is called in order not to keep the pointer toM
, otherwiseM
will hang in memory until ” Alive “Pointer to the result of the functionInverse_Matrix
After fixing these errors, something like
def inverse_matrix (Matrix_origin):
"" "
The function receives on the matrix input, then adds a single matrix to it,
Conducts elementary transformations on strings from the initial, seeking to obtain a single matrix.
In this case, the right will be the matrix, which is the reverse to the specified initial
"" "
# Glue 2 matrices: on the left  the original, right  single
N = Matrix_origin.shape [0]
m = np.hstack ((Matrix_origin, NP.Eye (N)))
For NROW, ROW in Enumerate (M):
# NROW is equal to line number
# Row contains the row of the matrix
Divider = Row [NROW] # diagonal element
# Delim on a diagonal element:
ROW / = Divider
# Now deduct the line of all underlying rows:
For Lower_ROW in M [Nrow + 1:]:
Factor = Lower_Row [NROU] # Row element in the NROW column
Lower_ROW  = Factor * Row # Remove to get zero in the nrow column
# Return:
For k in Range (n  1, 0, 1):
For Row_ in Range (K  1, 1, 1):
IF M [ROW_, K]:
# 1) all the elements above the main diagonal do equal to zero
M [ROW_,:]  = M [K,:] * M [ROW_, K]
RETURN M [:, N:]. Copy ()
The result of inverting your matrix
array ([[0.04128819, 0.09805945, 0.08980182],
[0.15689513, 0.08790039, 0.01401625],
[0.1734104, 0.18025555, 0.206115]])
I added jupyter notebook with your option , It is 1015% faster due to the choice of indexing operations.