-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.py
73 lines (58 loc) · 2.46 KB
/
polynomial.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
import abstractPolynomial
import monomial
import coefficient
import composite
class polynomial(composite.composite,abstractPolynomial.abstractPolynomial):
"""
File: polynomial.py
Author: Chris Campbell
Email: c (dot) j (dot) campbell (at) ed (dot) ac (dot) uk
Github: https://github.com/campbellC
Description: The polynomial class is the composite class for abstractPolynomials.
"""
##############################################################################
###### CONSTRUCTORS
##############################################################################
def __init__(self,monomials=()):
if isinstance(monomials, monomial.monomial):
monomials = (monomials,)
monomials = tuple(monomials)
composite.composite.__init__(self,monomials)
self.monomials = self.components
##############################################################################
###### MATHEMATICAL METHODS
##############################################################################
def __mul__(self, other):
if len(self) == 0:
return self
if isinstance(other,monomial.monomial) or (type(other) in [str,float,int]) or isinstance(other,coefficient.coefficient):
newMonos = []
for i in self:
newMonos.append(i * other)
return polynomial(tuple(newMonos)).clean()
if isinstance(other,polynomial):
if len(other.monomials) == 0:
return other
newMonos = []
for mono1 in self:
for mono2 in other.monomials:
newMonos.append(mono1 * mono2)
return polynomial(tuple(newMonos)).clean()
# From here on we know length of monomials > 0
def __rmul__(self,other):
if isinstance(other,monomial.monomial):
other = polynomial(other)
return (other * self).clean()
elif (type(other) in [str,float,int]) or isinstance(other,coefficient.coefficient):
return self * other
else:
return NotImplemented
def __add__(self,other):
if isinstance(other,abstractPolynomial.abstractPolynomial):
return composite.composite.__add__(self,other)
elif (type(other) in [str,float,int]) or isinstance(other,coefficient.coefficient):
return self + monomial.monomial(other)
else:
return NotImplemented
if __name__ == '__main__':
pass