forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.py
532 lines (453 loc) · 19.8 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# from __future__ import annotations
from fractions import Fraction
from typing import Dict, Union, Set, Iterable
from numbers import Rational
from functools import reduce
class Monomial:
"""
A simple Monomial class to
record the details of all variables
that a typical monomial is composed of.
"""
def __init__(self, variables: Dict[int, int], coeff: Union[int, float, Fraction, None]= None) -> None:
'''
Create a monomial in the given variables:
Examples:
Monomial({1:1}) = (a_1)^1
Monomial({
1:3,
2:2,
4:1,
5:0
}, 12) = 12(a_1)^3(a_2)^2(a_4)
Monomial({}) = 0
Monomial({2:3, 3:-1}, 1.5) = (3/2)(a_2)^3(a_3)^(-1)
'''
self.variables = dict()
if coeff is None:
if len(variables) == 0:
coeff = Fraction(0, 1)
else:
coeff = Fraction(1, 1)
elif coeff == 0:
self.coeff = Fraction(0, 1)
return
if len(variables) == 0:
self.coeff = Monomial._rationalize_if_possible(coeff)
return
for i in variables:
if variables[i] != 0:
self.variables[i] = variables[i]
self.coeff = Monomial._rationalize_if_possible(coeff)
@staticmethod
def _rationalize_if_possible(num):
'''
A helper for converting numbers
to Fraction only when possible.
'''
if isinstance(num, Rational):
res = Fraction(num, 1)
return Fraction(res.numerator, res.denominator)
else:
return num
# def equal_upto_scalar(self, other: Monomial) -> bool:
def equal_upto_scalar(self, other) -> bool:
"""
Return True if other is a monomial
and is equivalent to self up to a scalar
multiple.
"""
if not isinstance(other, Monomial):
raise ValueError('Can only compare monomials.')
return other.variables == self.variables
# def __add__(self, other: Union[int, float, Fraction, Monomial]):
def __add__(self, other: Union[int, float, Fraction]):
"""
Define the addition of two
monomials or the addition of
a monomial with an int, float, or a Fraction.
"""
if isinstance(other, int) or isinstance(other, float) or isinstance(other, Fraction):
return self.__add__(Monomial({}, Monomial._rationalize_if_possible(other)))
if not isinstance(other, Monomial):
raise ValueError('Can only add monomials, ints, floats, or Fractions.')
if self.variables == other.variables:
mono = {i: self.variables[i] for i in self.variables}
return Monomial(mono, Monomial._rationalize_if_possible(self.coeff + other.coeff)).clean()
# If they don't share same variables then by the definition,
# if they are added, the result becomes a polynomial and not a monomial.
# Thus, raise ValueError in that case.
raise ValueError(f'Cannot add {str(other)} to {self.__str__()} because they don\'t have same variables.')
# def __eq__(self, other: Monomial) -> bool:
def __eq__(self, other) -> bool:
"""
Return True if two monomials
are equal upto a scalar multiple.
"""
return self.equal_upto_scalar(other) and self.coeff == other.coeff
# def __mul__(self, other: Union[int, float, Fraction, Monomial]) -> Monomial:
def __mul__(self, other: Union[int, float, Fraction]):
"""
Multiply two monomials and merge the variables
in both of them.
Examples:
Monomial({1:1}) * Monomial({1: -3, 2: 1}) = (a_1)^(-2)(a_2)
Monomial({3:2}) * 2.5 = (5/2)(a_3)^2
"""
if isinstance(other, float) or isinstance(other, int) or isinstance(other, Fraction):
mono = {i: self.variables[i] for i in self.variables}
return Monomial(mono, Monomial._rationalize_if_possible(self.coeff * other)).clean()
if not isinstance(other, Monomial):
raise ValueError('Can only multiply monomials, ints, floats, or Fractions.')
else:
mono = {i: self.variables[i] for i in self.variables}
for i in other.variables:
if i in mono:
mono[i] += other.variables[i]
else:
mono[i] = other.variables[i]
temp = dict()
for k in mono:
if mono[k] != 0:
temp[k] = mono[k]
return Monomial(temp, Monomial._rationalize_if_possible(self.coeff * other.coeff)).clean()
# def inverse(self) -> Monomial:
def inverse(self):
"""
Compute the inverse of a monomial.
Examples:
Monomial({1:1, 2:-1, 3:2}, 2.5).inverse() = Monomial({1:-1, 2:1, 3:-2} ,2/5)
"""
mono = {i: self.variables[i] for i in self.variables if self.variables[i] != 0}
for i in mono:
mono[i] *= -1
if self.coeff == 0:
raise ValueError("Coefficient must not be 0.")
return Monomial(mono, Monomial._rationalize_if_possible(1/self.coeff)).clean()
# def __truediv__(self, other: Union[int, float, Fraction, Monomial]) -> Monomial:
def __truediv__(self, other: Union[int, float, Fraction]):
"""
Compute the division between two monomials
or a monomial and some other datatype
like int/float/Fraction.
"""
if isinstance(other, int) or isinstance(other, float) or isinstance(other, Fraction):
mono = {i: self.variables[i] for i in self.variables}
if other == 0:
raise ValueError('Cannot divide by 0.')
return Monomial(mono, Monomial._rationalize_if_possible(self.coeff / other)).clean()
o = other.inverse()
return self.__mul__(o)
# def __floordiv__(self, other: Union[int, float, Fraction, Monomial]) -> Monomial:
def __floordiv__(self, other: Union[int, float, Fraction]):
"""
For monomials,
floor div is the same as true div.
"""
return self.__truediv__(other)
# def clone(self) -> Monomial:
def clone(self):
"""
Clone the monomial.
"""
temp_variables = {i: self.variables[i] for i in self.variables}
return Monomial(temp_variables, Monomial._rationalize_if_possible(self.coeff)).clean()
# def clean(self) -> Monomial:
def clean(self):
"""
Clean the monomial by dropping any variables that have power 0.
"""
temp_variables = {i: self.variables[i] for i in self.variables if self.variables[i] != 0}
return Monomial(temp_variables, Monomial._rationalize_if_possible(self.coeff))
# def __sub__(self, other: Union[int, float, Fraction, Monomial]) -> Monomial:
def __sub__(self, other: Union[int, float, Fraction]):
"""
Compute the subtraction
of a monomial and a datatype
such as int, float, Fraction, or Monomial.
"""
if isinstance(other, int) or isinstance(other, float) or isinstance(other, Fraction):
mono = {i: self.variables[i] for i in self.variables if self.variables[i] != 0}
if len(mono) != 0:
raise ValueError('Can only subtract like monomials.')
other_term = Monomial(mono, Monomial._rationalize_if_possible(other))
return self.__sub__(other_term)
if not isinstance(other, Monomial):
raise ValueError('Can only subtract monomials')
return self.__add__(other.__mul__(Fraction(-1, 1)))
def __hash__(self) -> int:
"""
Define the hash of a monomial
by the underlying variables.
If hashing is implemented in O(v*log(v))
where v represents the number of
variables in the monomial,
then search queries for the
purposes of simplification of a
polynomial can be performed in
O(v*log(v)) as well; much better than
the length of the polynomial.
"""
arr = []
for i in sorted(self.variables):
if self.variables[i] > 0:
for _ in range(self.variables[i]):
arr.append(i)
return hash(tuple(arr))
def all_variables(self) -> Set:
"""
Get the set of all variables
present in the monomial.
"""
return set(sorted(self.variables.keys()))
def substitute(self, substitutions: Union[int, float, Fraction, Dict[int, Union[int, float, Fraction]]]) -> Fraction:
"""
Substitute the variables in the
monomial for values defined by
the substitutions dictionary.
"""
if isinstance(substitutions, int) or isinstance(substitutions, float) or isinstance(substitutions, Fraction):
substitutions = {v: Monomial._rationalize_if_possible(substitutions) for v in self.all_variables()}
else:
if not self.all_variables().issubset(set(substitutions.keys())):
raise ValueError('Some variables didn\'t receive their values.')
if self.coeff == 0:
return Fraction(0, 1)
ans = Monomial._rationalize_if_possible(self.coeff)
for k in self.variables:
ans *= Monomial._rationalize_if_possible(substitutions[k]**self.variables[k])
return Monomial._rationalize_if_possible(ans)
def __str__(self) -> str:
"""
Get a string representation of
the monomial.
"""
if len(self.variables) == 0:
return str(self.coeff)
result = str(self.coeff)
result += '('
for i in self.variables:
temp = 'a_{}'.format(str(i))
if self.variables[i] > 1:
temp = '(' + temp + ')**{}'.format(self.variables[i])
elif self.variables[i] < 0:
temp = '(' + temp + ')**(-{})'.format(-self.variables[i])
elif self.variables[i] == 0:
continue
else:
temp = '(' + temp + ')'
result += temp
return result + ')'
class Polynomial:
"""
A simple implementation
of a polynomial class that
records the details about two polynomials
that are potentially comprised of multiple
variables.
"""
def __init__(self, monomials: Iterable[Union[int, float, Fraction, Monomial]]) -> None:
'''
Create a polynomial in the given variables:
Examples:
Polynomial([
Monomial({1:1}, 2),
Monomial({2:3, 1:-1}, -1),
math.pi,
Fraction(-1, 2)
]) = (a_1)^2 + (-1)(a_2)^3(a_1)^(-1) + 2.6415926536
Polynomial([]) = 0
'''
self.monomials = set()
for m in monomials:
if any(map(lambda x: isinstance(m, x), [int, float, Fraction])):
self.monomials |= {Monomial({}, m)}
elif isinstance(m, Monomial):
self.monomials |= {m}
else:
raise ValueError('Iterable should have monomials, int, float, or Fraction.')
self.monomials -= {Monomial({}, 0)}
@staticmethod
def _rationalize_if_possible(num):
'''
A helper for converting numbers
to Fraction only when possible.
'''
if isinstance(num, Rational):
res = Fraction(num, 1)
return Fraction(res.numerator, res.denominator)
else:
return num
# def __add__(self, other: Union[int, float, Fraction, Monomial, Polynomial]) -> Polynomial:
def __add__(self, other: Union[int, float, Fraction, Monomial]):
"""
Add a given poylnomial to a copy of self.
"""
if isinstance(other, int) or isinstance(other, float) or isinstance(other, Fraction):
return self.__add__(Monomial({}, Polynomial._rationalize_if_possible(other)))
elif isinstance(other, Monomial):
monos = {m.clone() for m in self.monomials}
for _own_monos in monos:
if _own_monos.equal_upto_scalar(other):
scalar = _own_monos.coeff
monos -= {_own_monos}
temp_variables = {i: other.variables[i] for i in other.variables}
monos |= {Monomial(temp_variables, Polynomial._rationalize_if_possible(scalar + other.coeff))}
return Polynomial([z for z in monos])
monos |= {other.clone()}
return Polynomial([z for z in monos])
elif isinstance(other, Polynomial):
temp = list(z for z in {m.clone() for m in self.all_monomials()})
p = Polynomial(temp)
for o in other.all_monomials():
p = p.__add__(o.clone())
return p
else:
raise ValueError('Can only add int, float, Fraction, Monomials, or Polynomials to Polynomials.')
# def __sub__(self, other: Union[int, float, Fraction, Monomial, Polynomial]) -> Polynomial:
def __sub__(self, other: Union[int, float, Fraction, Monomial]):
"""
Subtract the given polynomial
from a copy of self.
"""
if isinstance(other, int) or isinstance(other, float) or isinstance(other, Fraction):
return self.__sub__(Monomial({}, Polynomial._rationalize_if_possible(other)))
elif isinstance(other, Monomial):
monos = {m.clone() for m in self.all_monomials()}
for _own_monos in monos:
if _own_monos.equal_upto_scalar(other):
scalar = _own_monos.coeff
monos -= {_own_monos}
temp_variables = {i: other.variables[i] for i in other.variables}
monos |= {Monomial(temp_variables, Polynomial._rationalize_if_possible(scalar - other.coeff))}
return Polynomial([z for z in monos])
to_insert = other.clone()
to_insert.coeff *= -1
monos |= {to_insert}
return Polynomial([z for z in monos])
elif isinstance(other, Polynomial):
p = Polynomial(list(z for z in {m.clone() for m in self.all_monomials()}))
for o in other.all_monomials():
p = p.__sub__(o.clone())
return p
else:
raise ValueError('Can only subtract int, float, Fraction, Monomials, or Polynomials from Polynomials.')
return
# def __mul__(self, other: Union[int, float, Fraction, Monomial, Polynomial]) -> Polynomial:
def __mul__(self, other: Union[int, float, Fraction, Monomial]):
"""
Multiply a given polynomial
to a copy of self.
"""
if isinstance(other, int) or isinstance(other, float) or isinstance(other, Fraction):
result = Polynomial([])
monos = {m.clone() for m in self.all_monomials()}
for m in monos:
result = result.__add__(m.clone()*other)
return result
elif isinstance(other, Monomial):
result = Polynomial([])
monos = {m.clone() for m in self.all_monomials()}
for m in monos:
result = result.__add__(m.clone() * other)
return result
elif isinstance(other, Polynomial):
temp_self = {m.clone() for m in self.all_monomials()}
temp_other = {m.clone() for m in other.all_monomials()}
result = Polynomial([])
for i in temp_self:
for j in temp_other:
result = result.__add__(i * j)
return result
else:
raise ValueError('Can only multiple int, float, Fraction, Monomials, or Polynomials with Polynomials.')
# def __floordiv__(self, other: Union[int, float, Fraction, Monomial, Polynomial]) -> Polynomial:
def __floordiv__(self, other: Union[int, float, Fraction, Monomial]):
"""
For Polynomials, floordiv is the same
as truediv.
"""
return self.__truediv__(other)
# def __truediv__(self, other: Union[int, float, Fraction, Monomial, Polynomial]) -> Polynomial:
def __truediv__(self, other: Union[int, float, Fraction, Monomial]):
"""
For Polynomials, only division by a monomial
is defined.
TODO: Implement polynomial / polynomial.
"""
if isinstance(other, int) or isinstance(other, float) or isinstance(other, Fraction):
return self.__truediv__( Monomial({}, other) )
elif isinstance(other, Monomial):
poly_temp = reduce(lambda acc, val: acc + val, map(lambda x: x / other, [z for z in self.all_monomials()]), Polynomial([Monomial({}, 0)]))
return poly_temp
elif isinstance(other, Polynomial):
if Monomial({}, 0) in other.all_monomials():
if len(other.all_monomials()) == 2:
temp_set = {x for x in other.all_monomials() if x != Monomial({}, 0)}
only = temp_set.pop()
return self.__truediv__(only)
elif len(other.all_monomials()) == 1:
temp_set = {x for x in other.all_monomials()}
only = temp_set.pop()
return self.__truediv__(only)
raise ValueError('Can only divide a polynomial by an int, float, Fraction, or a Monomial.')
return
# def clone(self) -> Polynomial:
def clone(self):
"""
Clone the polynomial.
"""
return Polynomial(list({m.clone() for m in self.all_monomials()}))
def variables(self) -> Set:
"""
Get all the variables present
in this polynomials.
"""
res = set()
for i in self.all_monomials():
res |= {j for j in i.variables}
res = list(res)
# res.sort()
return set(res)
def all_monomials(self) -> Iterable[Monomial]:
"""
Get the monomials of this polynomial.
"""
return {m for m in self.monomials if m != Monomial({}, 0)}
def __eq__(self, other) -> bool:
"""
Return True if the other polynomial is the same as
this.
"""
if isinstance(other, int) or isinstance(other, float) or isinstance(other, Fraction):
other_poly = Polynomial([Monomial({}, other)])
return self.__eq__(other_poly)
elif isinstance(other, Monomial):
return self.__eq__(Polynomial([other]))
elif isinstance(other, Polynomial):
return self.all_monomials() == other.all_monomials()
else:
raise ValueError('Can only compare a polynomial with an int, float, Fraction, Monomial, or another Polynomial.')
def subs(self, substitutions: Union[int, float, Fraction, Dict[int, Union[int, float, Fraction]]]) -> Union[int, float, Fraction]:
"""
Get the value after substituting
certain values for the variables
defined in substitutions.
"""
if isinstance(substitutions, int) or isinstance(substitutions, float) or isinstance(substitutions, Fraction):
substitutions = {i: Polynomial._rationalize_if_possible(substitutions) for i in set(self.variables())}
return self.subs(substitutions)
elif not isinstance(substitutions, dict):
raise ValueError('The substitutions should be a dictionary.')
if not self.variables().issubset(set(substitutions.keys())):
raise ValueError('Some variables didn\'t receive their values.')
ans = 0
for m in self.all_monomials():
ans += Polynomial._rationalize_if_possible(m.substitute(substitutions))
return Polynomial._rationalize_if_possible(ans)
def __str__(self) -> str:
"""
Get a string representation of
the polynomial.
"""
return ' + '.join(str(m) for m in self.all_monomials() if m.coeff != Fraction(0, 1))