# 20190524 matrix algorithm, matrix addition, matrix multiplication, matrix transposition, etc

Keywords: Python

1. Transposition of two-dimensional matrix

```arrA = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
def turn(arr):
if not arr:
return []
result = []
for i in range(len(arr)):#Original column becomes row
temp =[]
for j in range(len(arr)):#Original row becomes column
temp.append(arr[j][i])
result.append(temp)
return result
print(turn(arrA))```

2. Matrix addition, matrix A and matrix B need to be a matrix of N*M, that is, the rows and columns of the addition matrix must be equal

```def matrix_add(arrA,arrB):
if not arrA and not arrB:
return []
if len(arrA)!=len(arrB)or len(arrA)!=len(arrA):
return 'Error'
arrC = [[None]*len(arrA) for row in range(len(arrA))]#First define the result matrix
for i in range(len(arrA)):
for j in range(len(arrA[i])):
arrC[i][j] = arrA[i][j] + arrB[i][j]
return arrC
A = [[1,3,5,4],[7,9,1,3],[13,15,17,42]]
B = [[9,8,7,1],[6,5,4,2],[3,2,1,3]]

3. Matrix multiplication, matrix A and matrix B need to satisfy the condition that matrix A is m*n, matrix B is n*p, and result C is m*p

`C11 = A11*B11+A12*B21+....+A1n*Bn1C1P = A11*B1p+A12*B2p+...+A1n*BnpCMP = Am1*B1p+Am2*B2p+...+Amn*BnpThe first index of arrA is equal to the first index of C, and the second index of arrA increases gradually each timeThe first index of arrB increases gradually each time, and the second index of arrB is equal to the second index of C. So, because C is a matrix of m*pFirst index of arrA = IThe second index of arrA = kFirst index of arrB = kThe second index of arrB = J`
```A = [[1,3,5],[7,9,11],[13,15,17]]
B = [[9,8],[6,5],[3,2]]

def MatrixMultiply(arrA,arrB):
if len(arrA)!=len(arrB):
return False
M = len(A)
N = len(A)
P = len(B)
arrC = [[None] * P for row in range(M)]
for i in range(len(arrA)):
for j in range(len(arrB)):
temp = 0
for k in range(len(arrB)):
#print(arrA[i][k],arrB[k][j],end =' ')
temp = temp+int(arrA[i][k])*int(arrB[k][j])#Realization C1P = A11*B1p+A12*B2p+...+A1n*Bnp
arrC[i][j] = temp
return arrC

print(MatrixMultiply(A,B))```

4. Write function and compress sparse matrix with trinomial
Sparse matrix: if most elements of a matrix are 0, it is a sparse matrix
Trinomial: nonzero terms are represented by (i, j, item value). If a sparse matrix has n nonzero terms, A(0:N,1:3) two-dimensional array can be used to store these nonzero terms
A(0,1) stores the number of rows of sparse matrix
A(0,2) stores the number of columns of sparse matrix
A(0,3) stores the nonzero term of sparse matrix
Each non-zero term is represented by (I, J, item value)

```def Sparse_Transfer2_Trinomial(sparse):
trinomial = []
print(trinomial)
if not sparse:
return trinomial
non_zero = 0
for i in range(len(sparse)):
for j in range(len(sparse[i])):
#print(sparse[i][j])
if sparse[i][j]:#sparse[i][j]Non 0
non_zero+=1
trinomial.append([i,j,sparse[i][j]])
trinomial.insert(0,[len(sparse),len(sparse),non_zero])
return trinomial
Sparse = [[15,0,0,22,0,-15],[0,11,3,0,0,0],[0,0,0,-6,0,0],[0,0,0,0,0,0,0],[91,0,0,0,0,0],[0,0,28,0,0,0]]
print(Sparse_Transfer2_Trinomial(Sparse))```

5. Using trinomial transpose sparse matrix

Define the sparse matrix first, exchange the rows and columns, and fill the other positions with 0

```def Turn_Sparse(trinomial):
sparse = [*trinomial for i in range(trinomial)]
for each in trinomial[1:]:
sparse[each][each] = each
return sparse
print(Turn_Sparse(Sparse_Transfer2_Trinomial(Sparse)))```

Posted by jameslynns on Tue, 05 Nov 2019 13:58:35 -0800