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
220 lines (178 loc) · 6.69 KB
/
mathsClass.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"""
class Matrix():
'''
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):
assert type(component) is list, str(type(component))+" type isn't supported by <class 'Matrix'>, must be <class 'list'>"
h = len(component)
assert h>0, "Empty matrix"
l = max([len(i) for i in component]) # Search the max row size
assert l>0, "Empty matrix"
for i in range(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
for i in range(self.h):
for j in range(self.l):
h=h^hash(self.comp[i][j])
return h
def __repr__(self):
return "\n".join(["\t".join([str(n) for n in i]) for i in self.comp])
## Other methods
def len(self):
return (self.h,self.l)
def scal(self, scal):
return Matrix([[scal*self.comp[i][j] for j in range(self.l)] for i in range(self.h)])
## magic methods
# Main magic methods
def __add__(self, other):
assert self.h==other.h and self.l==other.l, "Size error"
return Matrix([[self.comp[i][j]+other.comp[i][j] for j in range(self.l)] for i in range(self.h)])
def __sub__(self, other):
return self+(-other)
def __neg__(self):
return self.scal(-1)
def __mul__(self, other):
if type(other)==type(self) and type(self)==Matrix:
assert self.l==other.h, "Size error"
return Matrix([[sum([self.comp[rowN][i]*other.comp[i][colN] for i in range(self.l)]) for colN in range(other.l)] for rowN in range(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:
assert type(a)==Matrix and type(other)==type(0), 'nop, on fait Matrix()*n'
return Matrix.scal(self,other)
def __invert__(self):
return Matrix([[self.comp[j][i] for j in range(self.h)] for i in range(self.l)])
# Additional methods
transpose = __invert__ #Create an alias of ~self
def __contains__(self, item):
checklist = set()
for i in self.comp: # Create a set with one occurence of each item in the matrix
checklist = checklist | set(i)
return (item in checklist) #Check item is in the set (python know this)
def __iter__(self):
for j in range(self.h):
for i in range(self.l):
yield self.comp(i,j)
def __getitem__(self, key):
return self.comp[key[0]][key[1]]
def __setitem__(self, key, value): #use to redefine a value
assert type(key) is tuple and len(key)==2, "The key is badly set please use mat[(i,j)]"
self.comp[key[0]][key[1]] = value
return self
set = __setitem__
def __eq__(self,other):
return hash(a)==hash(b)
def MatNul(dim):
return funcMatrix(dim, lambda i,j : 0)
def MatId(n):
return Matrix([[1 if j==i else 0 for j in range(n)] for i in range(n)])
def funcMatrix(dim, func):
return Matrix([[func(i,j) for j in range(dim[1])] for i in range(dim[0])])
""" COMPLEX CLASS """
class Complex():
'''
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):
if self.r == 0:
return str(self.i) + 'i'
else:
if self.i==0:
return str(self.r)
else:
return str(self.r) + '+' + str(self.i) + 'i'
def real(self):return self.r
def imag(self):return self.i
def __neg__(self):
return Complex(self.r, -self.i)
def __eq__(self,other):
return self.r == other.r and self.i == other.i
def __add__(self, other):
return Complex(self.r+other.r, self.i+other.i)
def __mul__(self, other):
return Complex(self.r*other.r - self.i*other.i, self.r*other.i + other.r*self.i)
def __truediv__(self, other):
return Complex((-other*self).real()/(-other*other).real(),(-other*self).imag()/(-other*other).real())
""" RATIONAL CLASS"""
class Ratio():
'''
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
'''
from fractions import gcd
def __init__(self, num, den):
assert den != 0, 'Rationnel indéfini : den = 0'
self.den = den
self.num = num
def __repr__(self):
gc = gcd(self.num, self.den)
return str(self.num//gc) +'/' + str(self.den//gc)
def __hash__(self):
return hash(self.num)^hash(self.den)
def inv(self):
return Ratio(self.den, self.num)
#Classic ops
def __eq__(self, other):
return self.num * other.den == other.num * self.den
def __neg__(self):
return Ratio(-self.num, self.den)
def __add__(self, other):
return Ratio(self.num*other.den + other.num*self.den, other.den*self.den)
def __mul__(self, other):
return Ratio(self.num*other.num, self.den*other.den)
def __sub__(self, other):
return self + -other
def __truediv__(self, other):
return self * other.inv()
defined_class = [Matrix, Complex, Ratio] # Completed/functional classes