-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyNumbers.py
249 lines (209 loc) · 6.98 KB
/
MyNumbers.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
class Fra: #Fra is the shortage for Fraction
def __init__(self,top=1,bottom=1):
self.num = top
self.den = bottom
if bottom<=0:
print("Error")
def __str__(self):
return str(self.num)+"/"+str(self.den)
def gcd(m,n):
while m%n!=0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
def __add__(self,otherfra):
newnum = self.num*otherfra.den+self.den*otherfra.num
newden = self.den*otherfra.den
common = Fra.gcd(newnum,newden)
if newnum!=0:
return Fra(newnum//common, newden//common)
else:
return 0
def __sub__(self,otherfra):
newnum = self.num*otherfra.den-self.den*otherfra.num
newden = self.den*otherfra.den
common = Fra.gcd(newnum,newden)
if newnum!=0:
return Fra(newnum//common, newden//common)
else:
return 0
def __mul__(self,otherfra):
newnum = self.num*otherfra.num
newden = self.den*otherfra.den
common = Fra.gcd(newnum,newden)
if newnum!=1:
return Fra(newnum//common, newden//common)
else:
return 1
def __truediv__(self,otherfra):
newnum = self.num*otherfra.den
newden = self.den*otherfra.num
common = Fra.gcd(newnum,newden)
if newnum!=1:
return Fra(newnum//common, newden//common)
else:
return 1
def __eq__(self,other):
firstnum = self.num*other.den
secondnum = self.den*other.num
return firstnum==secondnum
class Vec:
def __init__(self,lis):
if type(lis)==list:
self.detail = lis
self.dimension = len(lis)
else:
print("A list obj is needed.")
def __str__(self):
return str(self.detail)
def show(self):
print("{},事{}维向量".format(self.detail, self.dimension))
def __mul__(self,other): #内积数乘采用同一个方法
newdet = []
ipro = 0
if type(other)==int: #只能把数放在后边
for i in self.detail:
newdet.append(other*i)
return Vec(newdet)
elif type(other)==Vec and self.dimension==other.dimension:
for i in range(self.dimension):
ipro += (self.detail[i]*other.detail[i])
return ipro
else:
print("MarkErrorVecMul")
def moudle(self):
ms = 0
for i in self.detail:
ms += i**2
return pow(ms,0.5)
def __add__(self,other):
addet = []
if type(other)==Vec and self.dimension==other.dimension:
for i in range(self.dimension):
addet.append(self.detail[i]+other.detail[i])
return Vec(addet)
else:
print("Error")
def __truediv__(self,other):
divdet = []
for i in self.detail:
divdet.append(i/other)
return Vec(divdet)
def outP(vec1,vec2):
outl=[]
if vec1.dimension==3 and vec2.dimension==3:
outl.append(vec1.detail[1]*vec2.detail[2]-vec1.detail[2]*vec2.detail[1])
outl.append(vec1.detail[2]*vec2.detail[0]-vec1.detail[0]*vec2.detail[2])
outl.append(vec1.detail[0]*vec2.detail[1]-vec1.detail[1]*vec2.detail[0])
else:
print("Error")
return Vec(outl)
class Mat:
def __init__(self,matr): #猜测对继承有偏见,那就不用继承了
#Vec.__init__(self,lis) #输入的是一行一行的行向量,,,
if type(matr)==list and type(matr[0])==Vec:
self.detail = matr
self.slen = len(matr)
self.nlen = len(matr[0].detail)
else:
print("Element is Vec,input is list.")
def __sum__(self, other):
nMats = []
if self.slen==other.slen and self.nlen==other.nlen:
for i in range(self.slen):
nVec = self.detail[i]+other.detail[i]
nMats.append(nVec)
return Mat(nMats)
else:
print("Error")
return None
def nVofMat(self, n):
pvec = []
for i in range(self.slen):
pvec.append(self.detail[i].detail[n])
return Vec(pvec)
def __mul__(self, other):
nMatm = []
pVec = []
if type(other)==int:
for item in self.detail:
nMatm.append(item*other)
return Mat(nMatm)
elif type(other)==Vec:
for item in self.detail:
pVec.append(item*other)
return Vec(pVec)
elif type(other)==Mat:
if self.slen==other.nlen and self.nlen==other.slen:
for i in range(self.slen):
pVec = [self.detail[i]*other.nVofMat(j) for j in range(other.nlen)]
nMatm.append(Vec(pVec))
pVec = []
continue
return Mat(nMatm)
else:
print("MarkErrorMatMul")
else:
print("Error")
'''def __str__(self):
for i in self.detail:
return str(i)'''
def alcomMat(self,i,j): #i,j仍是直接数的那种
amm = []
vecAm = []
for k in range(self.slen):
if k!=(i-1):
vecAm = [self.detail[k].detail[l] for l in range(self.nlen) if l!=(j-1)]
amm.append(Vec(vecAm))
vecAm = []
continue
return Mat(amm)
def transSet(self):
trmat = []
for i in range(self.nlen):
trmat.append(self.nVofMat(i))
return Mat(trmat)
'''def assistSum():
d = 0
for i in range(self.nlen):
d += self.detail[0].detail[i]*((-1)**i)*Mat.det(self.alcomMat(0,i))
return d
def det(self):
if type(self)==Mat and self.slen==self.nlen:
if self.slen==1:
return det.detail[0].detail[0]
else:
d = 0
for i in range(self.nlen):
d += self.detail[0].detail[i]*((-1)**i)*Mat.det(self.alcomMat(0,i))
return d
else:
print("Input 方阵,Plz")'''#听说算法很鸡肋,弃坑了弃坑了
def show(self):
for item in self.detail:
print(item)
print("\n")
'''v1 = Vec([1,1,4,5,1,4])
v2 = Vec([3,6,4,3,6,4])
v19 = Vec([1,9,1,9,8,1,0])
print(v1)
v1.show()
print(type(v1))
v3 = v1*2
print(v3)
v3.show()
v4=v1+v2
print(v4)
v4.show()
print(v1.moudle())
a = Vec(114514)'''
v1 = Vec([1,1,4])
v2 = Vec([5,1,4])
v3 = Vec([3,6,4])
m1 = Mat([v1,v2])
#print(m1.det())
m1.show()
m2 = m1.transSet()
m2.show()