forked from EnzDev/PythonMath
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathsClass.py
More file actions
Latest commit
220 lines (178 loc) · 6.69 KB
/
Copy pathmathsClass.py
File metadata and controls
220 lines (178 loc) · 6.69 KB
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
# coding=utf8
''' Display the doc of all the defined classes
for i in [("\n".join(c)) for c in [[i for i in p.__doc__.split("\n")] for p in defined_class]]: print(i.encode('utf-8').decode('unicode-escape'))
'''
""" MATRIX CLASS"""
classMatrix():
'''
Class name : Matrix
Class type : Math class
Matrix implementation with methods :
* initialisation
* representation
* addition
* substraction
* negation
* multiplication
* transpose (__invert__)
* contain (python "in" funtion)
* setitem (via matrix[(i,j)]=value)
* len (return a tuple (i,j))
'''
def__init__(self, component):
asserttype(component) islist, str(type(component))+" type isn't supported by <class 'Matrix'>, must be <class 'list'>"
h=len(component)
asserth>0, "Empty matrix"
l=max([len(i) foriincomponent]) # Search the max row size
assertl>0, "Empty matrix"
foriinrange(len(component)): # Look in the entire matrix
component[i]=component[i]+[0]*(l-len(component[i])) # Add missing 0
self.comp=component# Correctly shaped matrix (fill w missing 0)
self.l=l# Usefull because its often called in the Matrix class
self.h=h# Same
def__hash__(self):
h=0
foriinrange(self.h):
forjinrange(self.l):
h=h^hash(self.comp[i][j])
returnh
def__repr__(self):
return"\n".join(["\t".join([str(n) fornini]) foriinself.comp])
## Other methods
deflen(self):
return (self.h,self.l)
defscal(self, scal):
returnMatrix([[scal*self.comp[i][j] forjinrange(self.l)] foriinrange(self.h)])
## magic methods
# Main magic methods
def__add__(self, other):
assertself.h==other.handself.l==other.l, "Size error"
returnMatrix([[self.comp[i][j]+other.comp[i][j] forjinrange(self.l)] foriinrange(self.h)])
def__sub__(self, other):
returnself+(-other)
def__neg__(self):
returnself.scal(-1)
def__mul__(self, other):
iftype(other)==type(self) andtype(self)==Matrix:
assertself.l==other.h, "Size error"
returnMatrix([[sum([self.comp[rowN][i]*other.comp[i][colN] foriinrange(self.l)]) forcolNinrange(other.l)] forrowNinrange(self.h)])
'''
The line above is a little bit too long so i split it here just for reader
for rowN in range(self.h):
for colN in range(other.l): #Change the name just for readability (cuz it's same as self.h)
for i in range(self.l) #or range(other.h)
self.comp[rowN][i]*other.comp[i][colN]
'''
else:
asserttype(a)==Matrixandtype(other)==type(0), 'nop, on fait Matrix()*n'
returnMatrix.scal(self,other)
def__invert__(self):
returnMatrix([[self.comp[j][i] forjinrange(self.h)] foriinrange(self.l)])
# Additional methods
transpose=__invert__#Create an alias of ~self
def__contains__(self, item):
checklist=set()
foriinself.comp: # Create a set with one occurence of each item in the matrix
checklist=checklist|set(i)
return (iteminchecklist) #Check item is in the set (python know this)
def__iter__(self):
forjinrange(self.h):
foriinrange(self.l):
yieldself.comp(i,j)
def__getitem__(self, key):
returnself.comp[key[0]][key[1]]
def__setitem__(self, key, value): #use to redefine a value
asserttype(key) istupleandlen(key)==2, "The key is badly set please use mat[(i,j)]"
self.comp[key[0]][key[1]] =value
returnself
set=__setitem__
def__eq__(self,other):
returnhash(a)==hash(b)
defMatNul(dim):
returnfuncMatrix(dim, lambdai,j : 0)
defMatId(n):
returnMatrix([[1ifj==ielse0forjinrange(n)] foriinrange(n)])
deffuncMatrix(dim, func):
returnMatrix([[func(i,j) forjinrange(dim[1])] foriinrange(dim[0])])
""" COMPLEX CLASS """
classComplex():
'''
Class name : Complex
Class type : Math class
Complex implementation with methods :
* initialisation
* representation
* real part
* imaginary part
* negation
* equality
* addition
* multiplication
* division
'''
def__init__(self, real, imag):
self.r=real
self.i=imag
def__repr__(self):
ifself.r==0:
returnstr(self.i) +'i'
else:
ifself.i==0:
returnstr(self.r)
else:
returnstr(self.r) +'+'+str(self.i) +'i'
defreal(self):returnself.r
defimag(self):returnself.i
def__neg__(self):
returnComplex(self.r, -self.i)
def__eq__(self,other):
returnself.r==other.randself.i==other.i
def__add__(self, other):
returnComplex(self.r+other.r, self.i+other.i)
def__mul__(self, other):
returnComplex(self.r*other.r-self.i*other.i, self.r*other.i+other.r*self.i)
def__truediv__(self, other):
returnComplex((-other*self).real()/(-other*other).real(),(-other*self).imag()/(-other*other).real())
""" RATIONAL CLASS"""
classRatio():
'''
Class name : Rational
Class type : Math class
Rational implementation with methods :
* initialisation
* representation
* equality (__eq__)
* hash (for set purposes)
* invert
* negation
* addition
* multiplication
* substraction
* division
'''
fromfractionsimportgcd
def__init__(self, num, den):
assertden!=0, 'Rationnel indéfini : den = 0'
self.den=den
self.num=num
def__repr__(self):
gc=gcd(self.num, self.den)
returnstr(self.num//gc) +'/'+str(self.den//gc)
def__hash__(self):
returnhash(self.num)^hash(self.den)
definv(self):
returnRatio(self.den, self.num)
#Classic ops
def__eq__(self, other):
returnself.num*other.den==other.num*self.den
def__neg__(self):
returnRatio(-self.num, self.den)
def__add__(self, other):
returnRatio(self.num*other.den+other.num*self.den, other.den*self.den)
def__mul__(self, other):
returnRatio(self.num*other.num, self.den*other.den)
def__sub__(self, other):
returnself+-other
def__truediv__(self, other):
returnself*other.inv()
defined_class= [Matrix, Complex, Ratio] # Completed/functional classes