# Working with matrices in python

The photo shows a good example of rotation, this is a fairly simple operation that I implemented in two cycles.

```
def transpose_matrix(matrix: list[list]) -> list[list]:
transposed_matrix = [[0 for i in range(len(matrix))] for i in range(len(matrix[0]))]
for i in range(len(matrix)):
for j in range(len(matrix[0])):
transposed_matrix[j][i] = matrix[i][j]
return transposed_matrix
```

Matrices are nested lists, so the function takes a list within a list. Variable *transposed_matrix* stores a zero matrix with dimensions identical to ours only with a rotation of 90 degrees, then a double iteration occurs in which the elements are swapped, resulting in a rotation.

## Matrix rank

The rank of a matrix is the highest of the orders of all possible non-zero minors of this matrix.

```
def rank_of_matrix(matrix: list[list]) -> int:
rank = min(len(matrix), len(matrix[0]))
row_index = 0
for i in range(len(matrix[0])):
found_nonzero = False
for j in range(row_index, len(matrix)):
if matrix[j][i] != 0:
found_nonzero = True
matrix[row_index], matrix[j] = matrix[j], matrix[row_index]
break
if found_nonzero:
for j in range(row_index + 1, len(matrix)):
factor = matrix[j][i] / matrix[row_index][i]
for k in range(i, len(matrix[0])):
matrix[j][k] -= matrix[row_index][k] * factor
row_index += 1
return rank
```

At the very beginning in the variable *rank* initialized to the minimum value between the number of rows and the number of columns in the matrix. This is determined by the fact that the rank of a matrix cannot be greater than the number of rows or columns, *row_index* is zero, since this will be the index of the string that we will work with in each step. We also go through two cycles and look for the first non-zero element in the current column. If such an element is found, it is swapped. This is done in order to place a non-null element at position `(`

*row_index, i*`)`

. If a non-null element is found in the current column at position `(`

*row_index, i*`)`

, the process of bringing the matrix to a stepped form is performed. For this, the coefficient is calculated *factor*equal to *matrix[j][i] /matrix[row_index][i]*and then subtracted *matrix[row_index][k] * factor* from all elements of the string *j*starting from the position *i*. This results in nulling all elements below *matrix[row_index][i] *in a column* i*. We increase *row_index* by 1 to move to the next line and continue the stair step process. At the end of the algorithm, it returns *rank*which is the maximum number of linearly independent rows or columns in the matrix.

## Matrix inversion

An inverse matrix is a matrix that is multiplied by the original matrix in such a way that their product gives the identity matrix. In other words, if we have a matrix A and its inverse matrix is denoted as A^-1.

```
def inverse_matrix(matrix: list[list]) -> list[list]:
augmented_matrix = [
[
matrix[i][j] if j < len(matrix) else int(i == j - len(matrix))
for j in range(2 * len(matrix))
]
for i in range(len(matrix))
]
for i in range(len(matrix)):
pivot = augmented_matrix[i][i]
if pivot == 0:
raise ValueError("Matrix is not invertible")
for j in range(2 * len(matrix)):
augmented_matrix[i][j] /= pivot
for j in range(len(matrix)):
if i != j:
scalar = augmented_matrix[j][i]
for k in range(2 * len(matrix)):
augmented_matrix[j][k] -= scalar * augmented_matrix[i][k]
inverse = [
[augmented_matrix[i][j] for j in range(len(matrix), 2 * len(matrix))]
for i in range(len(matrix))
]
return inverse
```

Variable *augmented_matrix* is an augmented matrix by adding the identity matrix on the right.* *Iteration occurs, we get the value *pivot*, which is the current diagonal element. If *pivot* is 0, an exception is thrown because the matrix is not invertible. Next, we normalize the string, divide all elements of the string by *pivot*. This is done so that the current diagonal element becomes equal to 1. The inner loop iterates over all columns of the expanded matrix. Subtract other rows from the current row, multiplied by the value of the element *scaler*. After the completion of the inner loop, the matrix will be reduced to the upper triangular form. Matrix is created *inverse*which contains the elements to the right of the vertical bar in *augmented_matrix*. Matrix is returned *inverse*which is the inverse of the original matrix.

## Conclusion

That’s all, this is my first article in general, I don’t know what will come of it, but I really wanted to write and share the implementation. I have given three matrix processing algorithms, in my repository you can see others. So far, I’m just trying my hand at data science, in the future I want to produce various analyzes and share them on the site.

Thanks for your time! I hope it was interesting, write your thoughts, criticize, I will be glad to correct myself and write better articles.