This repository has been archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
206 lines (176 loc) · 5.89 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import math
import random
class Matrix:
'''
Matrix:
---
A matrix has rows and columns, wich contain values (default 0).
Matrixes can be modified with matrix methods.
Parameters:
---
rows (int): Rows
cols (int): Columns
data (Array): Matrix data
'''
# Initialize
def __init__(self, rows:int, cols:int):
self.rows = rows
self.cols = cols
self.data = []
for i in range(self.rows):
self.data.append([])
for _ in range(self.cols):
self.data[i].append(0)
@staticmethod
def fromArray(arr: list):
'''Create a single column matrix from an array.'''
m = Matrix(len(arr), 1)
for i in range(len(arr)):
m.data[i][0] = arr[i]
return m
def toArray(self):
'''Create an array of a matrix.'''
arr = []
for i in range(self.rows):
for j in range(self.cols):
arr.append(self.data[i][j])
return arr
# Print the matrix array
def print(self):
'''Print the matrix in a table form'''
print("")
print(f"Matrix ({self.rows} rows x {self.cols} columns)")
print("———————————————————————————")
for i in range(self.rows):
row = "r | "
for j in range(self.cols):
row = (f" {row} {self.data[i][j]} ")
print(row)
print("———————————————————————————")
print("")
# Randomize
def randomize(self):
'''Randomize tha data in a matrix floating between -1 and 1'''
for i in range(self.rows):
for j in range(self.cols):
self.data[i][j] = 1 * random.randint(0,100)/100
# Scalar Add
def add(self, n):
'''
Add:
---
Adds each matrix container with the value n.
Add multiple matrixes to each other.
'''
for i in range(self.rows):
for j in range(self.cols):
if (isinstance(n, Matrix)):
self.data[i][j] += n.data[i][j]
else:
self.data[i][j] += n
# Scalar Substract
# def substract(self, n):
# '''
# Substract:
# ---
# Substracts each matrix container with the value n.
# Substract multiple matrixes from each other.
# '''
# for i in range(self.rows):
# for j in range(self.cols):
# if (isinstance(n, Matrix)):
# self.data[i][j] -= n.data[i][j]
# else:
# self.data[i][j] -= n
@staticmethod
def subtract(a, b):
'''
Subtract:
---
Subtracts one matrix from another
'''
if (isinstance(a, Matrix) and isinstance(b, Matrix)):
# TODO: Check matrixes size
result = Matrix(a.rows, a.cols)
for i in range(result.rows):
for j in range(result.cols):
result.data[i][j] = a.data[i][j] - b.data[i][j]
return result
# Multiply (Static Matrix)
@staticmethod
def multiply(a, b):
'''
Calculate the matrix product of two matrixes.
Requirements:
---
* Parameters are both matrixes
* Rows of Matrix a have to be equal to columns of Matrix b
Returns:
---
Matrix(a.rows, b.cols)
'''
if (isinstance(a, Matrix) and isinstance(b, Matrix)):
# Matrix Product
# TODO: Rewrite code of checking matrix size
if (not a.cols == b.rows):
try:
raise Warning(f"The columns of the matrix ({a.cols}) don't match with the rows of the other matrix ({b.rows}).")
except:
raise
else:
result = Matrix(a.rows, b.cols)
for i in range(result.rows):
for j in range(result.cols):
sum = 0
for k in range(a.cols):
sum += a.data[i][k] * b.data[k][j]
result.data[i][j] = sum
return result
# Multiply (Non Static Scalar)
def s_multiply(self, n):
'''
Scalar Multiply:
---
Multiplies each matrix container with value n.
'''
# Scalar Product
for i in range(self.rows):
for j in range(self.cols):
self.data[i][j] *= n
# Transpose
def transpose(self):
'''
Transposes a matrix from (rows,cols) to (cols, rows)
'''
result = Matrix(self.cols, self.rows)
for i in range(self.rows):
for j in range(self.cols):
result.data[j][i] = self.data[i][j]
return result
# Non Static Map function
def map(self, fn):
'''
Executes function fn to all matrix container values individually and returns the values back in the matrix.
Updated values:
---
val = self.data[i][j]
self.data[i][j] = fn(val)
'''
for i in range(self.rows):
for j in range(self.cols):
val = self.data[i][j]
self.data[i][j] = fn(val)
@staticmethod
def s_map(matrix, fn):
'''
Executes function fn to all matrix container values individually and returns the values back in the matrix.
Updated values:
---
val = self.data[i][j]
self.data[i][j] = fn(val)
'''
for i in range(matrix.rows):
for j in range(matrix.cols):
val = matrix.data[i][j]
matrix.data[i][j] = fn(val)
return matrix