1+ import random
2+
3+ class Matrix :
4+ def __init__ (self , rows , cols ):
5+ self .rows = rows
6+ self .cols = cols
7+
8+ self .data = []
9+ for row in range (rows ):
10+ new_row = []
11+ for col in range (cols ):
12+ new_row .append (0.0 )
13+ self .data .append (new_row )
14+
15+ def from_2d_list (input_2d_list ):
16+ result = Matrix (len (input_2d_list ), len (input_2d_list [0 ]))
17+ result .data = input_2d_list
18+ return result
19+
20+ def fill_zeros (self ):
21+ for row in range (self .rows ):
22+ for col in range (self .cols ):
23+ self .data [row ][col ] = 0.0
24+
25+ def flatten (self ):
26+ result = []
27+
28+ for row in range (self .rows ):
29+ for col in range (self .cols ):
30+ result .append (self .data [row ][col ])
31+
32+ return result
33+
34+ def transpose (a ):
35+ result = Matrix (a .cols , a .rows )
36+
37+ for row in range (a .rows ):
38+ for col in range (a .cols ):
39+ result .data [col ][row ] = a .data [row ][col ]
40+
41+ return result
42+
43+ def random (rows , cols ):
44+ result = Matrix (rows , cols )
45+
46+ for row in range (rows ):
47+ for col in range (cols ):
48+ result .data [row ][col ] = random .uniform (- 0.8 , 0.8 )
49+
50+ return result
51+
52+ def add (a , b ):
53+ result = Matrix (a .rows , a .cols )
54+
55+ for row in range (a .rows ):
56+ for col in range (a .cols ):
57+ result .data [row ][col ] = a .data [row ][col ] + b .data [row ][col ]
58+
59+ return result
60+
61+ def subtract (a , b ):
62+ result = Matrix (a .rows , a .cols )
63+
64+ for row in range (a .rows ):
65+ for col in range (a .cols ):
66+ result .data [row ][col ] = a .data [row ][col ] - b .data [row ][col ]
67+
68+ return result
69+
70+ def multiply (a , b ):
71+ result = Matrix (a .rows , a .cols )
72+
73+ for row in range (a .rows ):
74+ for col in range (a .cols ):
75+ result .data [row ][col ] = a .data [row ][col ] * b .data [row ][col ]
76+
77+ return result
78+
79+ def divide (a , b ):
80+ result = Matrix (a .rows , a .cols )
81+
82+ for row in range (a .rows ):
83+ for col in range (a .cols ):
84+ result .data [row ][col ] = a .data [row ][col ] / b .data [row ][col ]
85+
86+ return result
87+
88+ def divide_by_num (mat , num ):
89+ result = Matrix (mat .rows , mat .cols )
90+
91+ for row in range (mat .rows ):
92+ for col in range (mat .cols ):
93+ result .data [row ][col ] = mat .data [row ][col ] / num
94+
95+ return result
96+
97+ def dot (a , b ):
98+ result = Matrix (a .rows , b .cols )
99+
100+ for row in range (result .rows ):
101+ for col in range (result .cols ):
102+ sum_of_column = 0
103+
104+ for offset in range (a .cols ):
105+ sum_of_column += a .data [row ][offset ] * b .data [offset ][col ]
106+
107+ result .data [row ][col ] = sum_of_column
108+
109+ return result
110+
111+ def scale (m , s ):
112+ result = Matrix (m .rows , m .cols )
113+
114+ for row in range (result .rows ):
115+ for col in range (result .cols ):
116+ result .data [row ][col ] = m .data [row ][col ] * s
117+
118+ return result
119+
120+ def pow (m , e ):
121+ result = Matrix (m .rows , m .cols )
122+
123+ for row in range (result .rows ):
124+ for col in range (result .cols ):
125+ result .data [row ][col ] = m .data [row ][col ] ** e
126+
127+ return result
128+
129+ def map (m , fn ):
130+ for row in range (m .rows ):
131+ for col in range (m .cols ):
132+ m .data [row ][col ] = fn (m .data [row ][col ])
133+ return m
0 commit comments