diff options
author | Bryan Bishop <kanzure@gmail.com> | 2011-05-13 13:42:38 -0500 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2011-05-13 13:42:38 -0500 |
commit | 1534a25f3ab10f5206214d0af19b62c2b005a1f9 (patch) | |
tree | fef60cac4c127b14714ba73b6ac27291be0e2748 | |
parent | d75f82cc4f5bfa88cc23191b3b2fd7ae5230fb09 (diff) | |
download | lolcad-1534a25f3ab10f5206214d0af19b62c2b005a1f9.tar.gz lolcad-1534a25f3ab10f5206214d0af19b62c2b005a1f9.zip |
implemented reduce_num_coeffs
Imported gcd from fractions module.
-rw-r--r-- | esolid/esolid.py | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/esolid/esolid.py b/esolid/esolid.py index f36daa4..2c5aba1 100644 --- a/esolid/esolid.py +++ b/esolid/esolid.py @@ -2,6 +2,7 @@ #author: Bryan Bishop <kanzure@gmail.com> #date: 2011-04-26 from copy import copy, deepcopy +from fractions import Fraction, gcd ###################### # NOTES @@ -19,6 +20,11 @@ def sgn(number): elif number < 0: return -1 raise Exception, "this should never happen" +def den(thing): + return mpq_denref(thing) +def num(thing): + return mpq_numref(thing) + class K_RATPOLY: def __init__(self, *args): if len(args) == 0: self.init_noargs() @@ -384,7 +390,42 @@ class K_RATPOLY: returns an upper bound on the size of the largest root of *this.""" raise NotImplementedError, bryan_message def reduce_num_coeffs(self): - raise NotImplementedError, bryan_message + i, j, g = None, None, None + d = 1 + + i = 0 + while i < self.num_coeffs: + if sgn(self.coeffs[i]): + d *= den(self.coeffs[i]) #might be self.den + i += 1 + + i = 0 + while i < self.num_coeffs: + if sgn(self.coeffs[i]): + self.coeffs[i] *= d + i += 1 + + i = 0 + while i < self.num_coeffs and not sgn(self.coeffs[i]): + i+= 1 + + g = 1 + if i < self.num_coeffs: + g = num(self.coeffs[i]) #might be self.num + + j = i + 1 + while j < self.num_coeffs and g != 1: + if sgn(self.coeffs[j]): + g = gcd(g, num(self.coeffs[j])) #might be self.gcd or self.num + j += 1 + + if g != 1: + j = 0 + while j < self.num_coeffs: + if sgn(self.coeffs[j]): + self.coeffs[j] /= g + j += 1 + return d != 1 def reduce_coeffs(self): raise NotImplementedError, bryan_message def conv_to_Bernstein(self, deg_s, deg_t): @@ -392,7 +433,7 @@ class K_RATPOLY: def transform_Impl(self, T): raise NotImplementedError, bryan_message -def gcd(P1, P2): +def x_gcd(P1, P2): """PROVIDED P1 and P2 are uni or bivariate polynomials, returns their gcd.""" raise NotImplementedError, bryan_message |