-
Notifications
You must be signed in to change notification settings - Fork 0
/
mypoly.py
248 lines (226 loc) · 8.7 KB
/
mypoly.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
from puiseuxPoly import puiseux
from fractions import Fraction
class mypoly(object):
"""
Represents a univariate polynomial with :py:class:`puiseuxPoly.puiseux`
coefficients.
"""
def __init__(self,poly,checkReduced=True):
"""
2x^2-3x+1 would be input as [[2,2],[-3,1],[1,0]]
and represented internally as {2:2,1:-3,0:1} i.e. the keys
are the exponents and the values are the coefficients
--OR-- can be input as a dictionary, in which case it will
just be copied to internal
Coefficients can be complex, int, long (if you really want to),
or (in the most usefull case) puiseux polynomials (using the
puiseuxPoly class).
**Maybe not**: they might need to be Puiseux objects?
checkReduced is a flag that determines if when setting
up the internal representation we check for like terms.
Be careful with it!!! If poly is a dict, checkReduced doesn't
matter since it can't have repeated keys (exponents)
"""
self.internal = {}
if type(poly)==str:
"""
This is bad. Uses eval.
"""
from sympy import poly as symp
from sympy.abc import x,y
s = poly
s = s.replace('^','**')
p = symp(eval(s),x,y,domain='CC')
print p
d = {item[0][1]:puiseux({item[0][0]:complex(item[1])}) for item in p.terms()}
for item in p.terms():
if item[0][1] in d.keys():
d[item[0][1]]+=puiseux({item[0][0]:complex(item[1])})
else: d[item[0][1]] = puiseux({item[0][0]:complex(item[1])})
poly = d
if type(poly)==dict:
for key in poly.keys():
if type(poly[key])!=puiseux:
self.internal[key] = puiseux({0:poly[key]})
else: self.internal[key] = poly[key]
elif checkReduced:
self.internal = {poly[0][1]:poly[0][0]}
for mon in poly[1:]:
coeff = mon[0]
exponent = mon[1]
if exponent in self.internal:
self.internal[exponent] += coeff
else: self.internal[exponent] = coeff
else:
self.internal = {}
for elt in poly:
self.internal[elt[1]]=elt[0]
for key in self.internal.keys():
if self.internal[key]==0:
self.internal.pop(key)
if self.internal=={}:self.internal = {0:0}
def __eq__(self,other):
if type(other)==mypoly:
if self.internal.keys() != other.internal.keys(): return False
equal = True
for key in self.internal.keys():
if self.internal[key]!=other.internal[key]:
equal = False
break
return equal
else:
l = self.internal.keys()
if len(l)==1 and l[0]==0:
return self.internal[l]==other
elif len(l)==0: return other==0
return False
def __add__(self,other):
if type(other) in [puiseux,int,float,Fraction,long,complex]:
mp = {}
for key in self.internal.keys():
mp[key] = self.internal[key]+other
return mypoly(mp)
elif type(other)==mypoly:
toReturn = {}
for key in self.internal.keys():
if key in other.internal.keys():
toReturn[key]=self.internal[key]+other.internal[key]
else:
toReturn[key]=self.internal[key]
for key in other.internal.keys():
if key not in toReturn:
toReturn[key]=other.internal[key]
return mypoly(toReturn)
else: raise TypeError("can't do that")
def __sub__(self, other):
if type(other) in [puiseux,int,float,Fraction,long,complex]:
mp = {}
for key in self.internal.keys():
mp[key] = self.internal[key]-other
return mypoly(mp)
elif type(other)==mypoly:
toReturn = {}
for key in self.internal.keys():
if key in other.internal.keys():
toReturn[key]=self.internal[key]-other.internal[key]
else:
toReturn[key]=self.internal[key]
for key in other.internal.keys():
if key not in toReturn:
toReturn[key]=(-other.internal[key])
return mypoly(toReturn)
else: raise TypeError("can't do that")
def __mul__(self, other):
if type(other) in [puiseux,int,float,Fraction,long,complex]:
mp = {}
for key in self.internal.keys():
mp[key] = self.internal[key]*other
return mypoly(mp)
elif type(other)==mypoly:
toReturn = {}
for key1 in other.internal.keys():
for key2 in self.internal.keys():
coeff = other.internal[key1]*self.internal[key2]
exponent = key1 + key2
if exponent in toReturn:
toReturn[exponent] += coeff
else: toReturn[exponent]=coeff
return mypoly(toReturn,checkReduced=False)
else:
raise TypeError("can't do that")
def __rmul__(self,other):return self*other
def __radd__(self,other):return self+other
def __rsub__(self,other):return (-self)+other
def __pow__(self,other):
if type(other)==int and other>=0:
if other==0:
return mypoly({0:puiseux({Fraction(0,1):1})})
else:
toReturn = mypoly({0:puiseux({Fraction(0,1):1})})
for i in xrange(other):
toReturn = self*toReturn
return toReturn
elif type(other)==int:
raise TypeError("don't have support for negative exponents")
else: raise TypeError("can't do that")
def __neg__(self):
mp = {}
for key in self.internal.keys():
mp[key] = -self.internal[key]
return mypoly(mp)
def __repr__(self):
listy = sorted(self.internal.keys())
if listy[0]==0:
ypart = ''
else: ypart = 'y^'+str(listy[0])
if type(self.internal[listy[0]])==puiseux:
toReturn = '('+str(self.internal[listy[0]])+')'+ ypart
else:
toReturn = str(self.internal[listy[0]])+ ypart
for elt in listy[1:]:
if type(self.internal[elt])==int and self.internal[elt]<0:
toReturn+=' - '+str(0-self.internal[elt])+'y^'+str(elt)
elif type(self.internal[elt])==puiseux:
toReturn += ' + ('+str(self.internal[elt])+')y^'+str(elt)
else:
toReturn+=' + '+str(self.internal[elt])+'y^'+str(elt)
return toReturn
def __str__(self):
return self.__repr__()
def __call__(self,value):
"""
Calls :py:func:`evaluate`
"""
return self.evaluate(value)
def evaluate(self,value):
"""
Evaluates self at value.
Use Horner's scheme to speed this up?
"""
toReturn = 0
for exponent in self.internal.keys():
toReturn = (value**exponent)*self.internal[exponent]+toReturn
return toReturn
def support(self):
"""
Returns a list of ordered pairs *(a,b)* where *a* is the degree
of a monomial in :py:data:`self` and *b* is the order of that monomial's
Puiseux coefficient.
"""
toReturn = []
for key in self.internal.keys():
coeff = self.internal[key]
if type(coeff)==puiseux:
toReturn.append([key,coeff.order()])
else:
toReturn.append([key,0])
return toReturn
def degree(self):
"""
Returns the degree of :py:data:`self`.
"""
return max(self.internal.keys())
def lowestDegree(self):
"""
Returns the degree of the lowest-degree monomial.
"""
return min(self.internal.keys())
def reduced(self):
"""
Returns ``self`` without any (y-0) factors.
"""
toReturn = {}
lowest = self.lowestDegree()
for i in self.internal.keys():
toReturn[i-lowest] = self.internal[i]
return mypoly(toReturn)
if __name__=='__main__':
poly = mypoly({0:puiseux({2:1,3:-1}),1:puiseux({1:-2}),2:puiseux({0:1})})
poly = mypoly({0:puiseux({4:2}),1:puiseux({2:1}),2:puiseux({1:4}),3:puiseux({0:4})})
print 'poly: ',poly
first = puiseux({2:-2})
print first
print poly(first)
print poly
print poly.support()
print poly.degree()