-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.py
68 lines (60 loc) · 1.39 KB
/
matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
##
## EPITECH PROJECT, 2018
## 108trigo_2017
## File description:
## Matrix file
##
from math import *
def identity_mat(n):
tmp = []
for i in range(n):
ident = []
for j in range(n):
ident.append(1 if j == i else 0)
tmp.append(ident)
return tmp
def init_mat(n, k):
tmp = []
for i in range(n):
ident = []
for j in range(n):
ident.append(k)
tmp.append(ident)
return tmp
def mat_mult(mat1, mat2):
tmp = []
for i in range(len(mat1)):
mult = []
for j in range(len(mat2[0])):
a = 0
for k in range(len(mat1[0])):
a += mat1[i][k] * mat2[k][j]
mult.append(a)
tmp.append(mult)
return tmp
def pow_mat(mat, n):
tmp = mat
for i in range(n - 1):
tmp = mat_mult(tmp, mat)
return tmp
def div_mat(mat, k):
for i in range(len(mat)):
for j in range(len(mat[0])):
mat[i][j] /= k
return mat
def add_mat(mat1, mat2):
tmp = []
for i in range(len(mat1)):
c = []
for j in range(len(mat1[0])):
c.append(mat1[i][j] + mat2[i][j])
tmp.append(c)
return tmp
def sub_mat(mat1, mat2):
tmp = []
for i in range(len(mat1)):
c = []
for j in range(len(mat1[0])):
c.append(mat1[i][j] - mat2[i][j])
tmp.append(c)
return tmp