Skip to content

Commit 4362e54

Browse files
committed
Add Matrix
1 parent 9597693 commit 4362e54

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

learn/python/day3/part1.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from copy import deepcopy
2+
3+
class Matrix:
4+
def __init__(self,s):
5+
if type(s) == list:
6+
self.d = s
7+
elif type(s) == str:
8+
rows = s.split("\n")
9+
self.d = list(map(lambda x:list(map(int, x.split())),rows))
10+
else:
11+
raise ValueError("s should be list or str")
12+
13+
def __str__(self):
14+
a = map(lambda x:" ".join(map(str, x)),self.d)
15+
a = "\n".join(a)
16+
return a
17+
18+
@property
19+
def size(self):
20+
return (len(self.d),len(self.d[0]))
21+
22+
def transpose(self):
23+
a = self.d
24+
k = []
25+
for i in range(len(a[0])):
26+
t = []
27+
for j in range(len(a)):
28+
t.append(a[j][i])
29+
k.append(t)
30+
self.d = k
31+
32+
def reshape(self,r,c):
33+
rows,cols = self.size
34+
if r*c != rows*cols:
35+
raise ValueError
36+
a = self.d
37+
k = [[]]
38+
for i in range(len(a)):
39+
for j in range(len(a[0])):
40+
if len(k[-1]) < c:
41+
k[-1].append(a[i][j])
42+
else:
43+
k.append([a[i][j]])
44+
self.d = k
45+
46+
def __add__(self,other):
47+
if type(other) == int:
48+
a = deepcopy(self.d)
49+
for i in range(len(a)):
50+
for j in range(len(a[0])):
51+
a[i][j] += other
52+
return Matrix(a)
53+
elif type(other) == Matrix:
54+
if other.size != self.size:
55+
raise ValueError("size of matrixes are not matched")
56+
c = deepcopy(self.d)
57+
for i in range(len(c)):
58+
for j in range(len(c[0])):
59+
c[i][j] += other.d[i][j]
60+
return Matrix(c)
61+
else:
62+
raise ValueError
63+
64+
def __mul__(self,other):
65+
if type(other) == int:
66+
a = deepcopy(self.d)
67+
for i in range(len(a)):
68+
for j in range(len(a[0])):
69+
a[i][j] *= other
70+
return Matrix(a)
71+
elif type(other) == Matrix:
72+
m,n = self.size
73+
l = other.size[1]
74+
if other.size[0] != self.size[1]:
75+
raise ValueError("size of matrixes are not matched")
76+
d = [[0 for i in range(l)] for i in range(m)]
77+
for i in range(m):
78+
for j in range(l):
79+
s = 0
80+
for k in range(n):
81+
s += self.d[i][k]*other.d[k][j]
82+
d[i][j] = s
83+
return Matrix(d)
84+
85+
else:
86+
raise ValueError
87+
88+
89+
def solution(input):
90+
inputs = input.split("\n\n")
91+
mt = Matrix(inputs[0])
92+
mt1 = Matrix(inputs[1])
93+
print(mt.size)
94+
print(mt1.size)
95+
print(mt*mt1)

learn/python/day3/sample.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1 2
2+
3 4
3+
5 6
4+
5+
1 2 3
6+
4 5 6

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy