Home python Build a reverse matrix by Gauss

# Build a reverse matrix by Gauss

Author

Date

Category

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, moving-up-up, 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))
``````

You have several errors in the code.

1. You design the right side as `np.matrix `, so after `np.hstack `you also get an object like `NP.MATRIX `. This is a very special type of arrays. In particular, the `M `iterator returns not one-dimensional lines of numbers, and the two-dimensional arrays of the `form>(1.6) `. As a result, `Row [NROW] `is not a number, but a one-dimensional array.

`NUMPY `There is a special function for constructing a single matrix: `np.eye (n) `builds a single matrix in size `n x n `. Therefore, you better design `M `as `m = np.hstack ((Matrix_origin, NP.Eye (Len (Matrix_origin))) `

2. You initialized `n `as the number of columns in `m `: `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. If `n `defines the number of rows, then you need to be assigned like this: `n = M.Shape [0] `

3. 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 two-dimensional array, each line of which is a string from `M `Starting from the `N `, that is, elements No. 3.4 and 5. Just the right side of the matrix `M `. The `copy () `method is called in order not to keep the pointer to `M `, otherwise `M `will hang in memory until ” Alive “Pointer to the result of the function `Inverse_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 10-15% faster due to the choice of indexing operations.

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.