def __init__(self, parent, x=None, check=True, is_gen=False, construct=False): Polynomial.__init__(self, parent, is_gen, construct)
def __init__(self, parent, x=None, check=True, is_gen=False, construct=False): """ TESTS:: sage: PolynomialRing(RIF, 'z', sparse=True)([RIF(-1, 1), RIF(-1,1)]) 0.?*z + 0.? sage: PolynomialRing(CIF, 'z', sparse=True)([CIF(RIF(-1,1), RIF(-1,1)), RIF(-1,1)]) 0.?*z + 0.? + 0.?*I """ Polynomial.__init__(self, parent, is_gen=is_gen) if x is None: self.__coeffs = {} return R = parent.base_ring() if isinstance(x, Polynomial): if x.parent() == self.parent(): x = dict(x.dict()) elif x.parent() == R: x = {0: x} else: w = {} for n, c in six.iteritems(x.dict()): w[n] = R(c) # The following line has been added in trac ticket #9944. # Apparently, the "else" case has never occured before. x = w elif isinstance(x, list): x = dict((i, c) for (i, c) in enumerate(x) if c) elif isinstance(x, pari_gen): y = {} for i in range(len(x)): y[i] = R(x[i]) x = y check = True elif not isinstance(x, dict): x = {0: x} # constant polynomials if check: self.__coeffs = {} for i, z in six.iteritems(x): self.__coeffs[i] = R(z) else: self.__coeffs = x if check: self.__normalize()
def __init__(self, parent, x=None, check=True, is_gen=False, construct=False): Polynomial.__init__(self, parent, is_gen=is_gen) if x is None: self.__coeffs = {} return R = parent.base_ring() if isinstance(x, Polynomial): if x.parent() == self.parent(): x = dict(x.dict()) elif x.parent() == R: x = {0: x} else: w = {} for n, c in x.dict().iteritems(): w[n] = R(c) # The following line has been added in trac ticket #9944. # Apparently, the "else" case has never occured before. x = w elif isinstance(x, list): y = {} for i in xrange(len(x)): if x[i] != 0: y[i] = x[i] x = y elif isinstance(x, pari_gen): y = {} for i in range(len(x)): y[i] = R(x[i]) x = y check = True elif not isinstance(x, dict): x = {0: x} # constant polynomials if check: self.__coeffs = {} for i, z in x.iteritems(): self.__coeffs[i] = R(z) else: self.__coeffs = x if check: self.__normalize()
def __init__(self, parent, x=None, check=True, is_gen=False, construct=False): """ TESTS:: sage: PolynomialRing(RIF, 'z', sparse=True)([RIF(-1, 1), RIF(-1,1)]) 0.?*z + 0.? sage: PolynomialRing(CIF, 'z', sparse=True)([CIF(RIF(-1,1), RIF(-1,1)), RIF(-1,1)]) 0.?*z + 0.? + 0.?*I """ Polynomial.__init__(self, parent, is_gen=is_gen) if x is None: self.__coeffs = {} return R = parent.base_ring() if isinstance(x, Polynomial): if x.parent() == self.parent(): x = dict(x.dict()) elif x.parent() == R: x = {0:x} else: w = {} for n, c in x.dict().iteritems(): w[n] = R(c) # The following line has been added in trac ticket #9944. # Apparently, the "else" case has never occured before. x = w elif isinstance(x, list): x = dict((i, c) for (i, c) in enumerate(x) if c) elif isinstance(x, pari_gen): y = {} for i in range(len(x)): y[i] = R(x[i]) x = y check = True elif not isinstance(x, dict): x = {0:x} # constant polynomials if check: self.__coeffs = {} for i, z in x.iteritems(): self.__coeffs[i] = R(z) else: self.__coeffs = x if check: self.__normalize()
def __init__(self, parent, x=None, check=True, is_gen=False, construct=False): Polynomial.__init__(self, parent, is_gen=is_gen) if x is None: self.__coeffs = {} return R = parent.base_ring() if isinstance(x, Polynomial): if x.parent() == self.parent(): x = dict(x.dict()) elif x.parent() == R: x = {0:x} else: w = {} for n, c in x.dict().iteritems(): w[n] = R(c) # The following line has been added in trac ticket #9944. # Apparently, the "else" case has never occured before. x = w elif isinstance(x, list): y = {} for i in xrange(len(x)): if x[i] != 0: y[i] = x[i] x = y elif isinstance(x, pari_gen): y = {} for i in range(len(x)): y[i] = R(x[i]) x = y check = True elif not isinstance(x, dict): x = {0:x} # constant polynomials if check: self.__coeffs = {} for i, z in x.iteritems(): self.__coeffs[i] = R(z) else: self.__coeffs = x if check: self.__normalize()
def __init__(self, parent, is_gen=False, construct=False): Polynomial.__init__(self, parent, is_gen=is_gen)
def gcd(self, other, algorithm=None): """ Return the gcd of this polynomial and ``other`` INPUT: - ``other`` -- a polynomial defined over the same ring as this polynomial. ALGORITHM: Two algorithms are provided: - ``generic``: Uses the generic implementation, which depends on the base ring being a UFD or a field. - ``dense``: The polynomials are converted to the dense representation, their gcd is computed and is converted back to the sparse representation. Default is ``dense`` for polynomials over ZZ and ``generic`` in the other cases. EXAMPLES:: sage: R.<x> = PolynomialRing(ZZ,sparse=True) sage: p = x^6 + 7*x^5 + 8*x^4 + 6*x^3 + 2*x^2 + x + 2 sage: q = 2*x^4 - x^3 - 2*x^2 - 4*x - 1 sage: gcd(p,q) x^2 + x + 1 sage: gcd(p, q, algorithm = "dense") x^2 + x + 1 sage: gcd(p, q, algorithm = "generic") x^2 + x + 1 sage: gcd(p, q, algorithm = "foobar") Traceback (most recent call last): ... ValueError: Unknown algorithm 'foobar' TESTS: Check that :trac:`19676` is fixed:: sage: S.<y> = R[] sage: x.gcd(y) 1 sage: (6*x).gcd(9) 3 """ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.arith.all import lcm if algorithm is None: if self.base_ring() == ZZ: algorithm = "dense" else: algorithm = "generic" if algorithm == "dense": S = self.parent() # FLINT is faster but a bug makes the conversion extremely slow, # so NTL is used in those cases where the conversion is too slow. Cf # <https://groups.google.com/d/msg/sage-devel/6qhW90dgd1k/Hoq3N7fWe4QJ> sd = self.degree() od = other.degree() if max(sd,od)<100 or \ min(len(self.__coeffs)/sd, len(other.__coeffs)/od)>.06: implementation = "FLINT" else: implementation = "NTL" D = PolynomialRing(S.base_ring(), 'x', implementation=implementation) g = D(self).gcd(D(other)) return S(g) elif algorithm == "generic": return Polynomial.gcd(self, other) else: raise ValueError("Unknown algorithm '%s'" % algorithm)
def __init__(self, parent, x=None, check=True, is_gen=False, construct=False, absprec=infinity, relprec=infinity): """ TESTS: sage: K = Qp(13,7) sage: R.<t> = K[] sage: R([K(13), K(1)]) (1 + O(13^7))*t + (13 + O(13^8)) sage: T.<t> = ZZ[] sage: R(t + 2) (1 + O(13^7))*t + (2 + O(13^7)) """ Polynomial.__init__(self, parent, is_gen=is_gen) parentbr = parent.base_ring() from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing if construct: (self._poly, self._valbase, self._relprecs, self._normalized, self._valaddeds, self._list) = x #the last two of these may be None return elif is_gen: self._poly = PolynomialRing(ZZ, parent.variable_name()).gen() self._valbase = 0 self._valaddeds = [infinity, 0] self._relprecs = [infinity, parentbr.precision_cap()] self._normalized = True self._list = None return #First we list the types that are turned into Polynomials if isinstance(x, ZZX): x = Polynomial_integer_dense(PolynomialRing( ZZ, parent.variable_name()), x, construct=True) elif isinstance(x, fraction_field_element.FractionFieldElement) and \ x.denominator() == 1: #Currently we ignore precision information in the denominator. This should be changed eventually x = x.numerator() #We now coerce various types into lists of coefficients. There are fast pathways for some types of polynomials if isinstance(x, Polynomial): if x.parent() is self.parent(): if not absprec is infinity or not relprec is infinity: x._normalize() self._poly = x._poly self._valbase = x._valbase self._valaddeds = x._valaddeds self._relprecs = x._relprecs self._normalized = x._normalized self._list = x._list if not absprec is infinity or not relprec is infinity: self._adjust_prec_info(absprec, relprec) return elif x.base_ring() is ZZ: self._poly = x self._valbase = Integer(0) p = parentbr.prime() self._relprecs = [ c.valuation(p) + parentbr.precision_cap() for c in x.list() ] self._comp_valaddeds() self._normalized = len(self._valaddeds) == 0 or (min( self._valaddeds) == 0) self._list = None if not absprec is infinity or not relprec is infinity: self._adjust_prec_info(absprec, relprec) return else: x = [parentbr(a) for a in x.list()] check = False elif isinstance(x, dict): zero = parentbr.zero_element() n = max(x.keys()) v = [zero for _ in xrange(n + 1)] for i, z in x.iteritems(): v[i] = z x = v elif isinstance(x, pari_gen): x = [parentbr(w) for w in x.Vecrev()] check = False #The default behavior if we haven't already figured out what the type is is to assume it coerces into the base_ring as a constant polynomial elif not isinstance(x, list): x = [x] # constant polynomial # In contrast to other polynomials, the zero element is not distinguished # by having its list empty. Instead, it has list [0] if not x: x = [parentbr.zero_element()] if check: x = [parentbr(z) for z in x] # Remove this -- for p-adics this is terrible, since it kills any non exact zero. #if len(x) == 1 and not x[0]: # x = [] self._list = x self._valaddeds = [a.valuation() for a in x] self._valbase = sage.rings.padics.misc.min(self._valaddeds) if self._valbase is infinity: self._valaddeds = [] self._relprecs = [] self._poly = PolynomialRing(ZZ, parent.variable_name())() self._normalized = True if not absprec is infinity or not relprec is infinity: self._adjust_prec_info(absprec, relprec) else: self._valaddeds = [c - self._valbase for c in self._valaddeds] self._relprecs = [ a.precision_absolute() - self._valbase for a in x ] self._poly = PolynomialRing( ZZ, parent.variable_name())([a >> self._valbase for a in x]) self._normalized = True if not absprec is infinity or not relprec is infinity: self._adjust_prec_info(absprec, relprec)
def __init__(self, parent, x=None, check=True, is_gen=False, construct = False, absprec = infinity, relprec = infinity): """ TESTS: sage: K = Qp(13,7) sage: R.<t> = K[] sage: R([K(13), K(1)]) (1 + O(13^7))*t + (13 + O(13^8)) sage: T.<t> = ZZ[] sage: R(t + 2) (1 + O(13^7))*t + (2 + O(13^7)) """ Polynomial.__init__(self, parent, is_gen=is_gen) parentbr = parent.base_ring() from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing if construct: (self._poly, self._valbase, self._relprecs, self._normalized, self._valaddeds, self._list) = x #the last two of these may be None return elif is_gen: self._poly = PolynomialRing(ZZ, parent.variable_name()).gen() self._valbase = 0 self._valaddeds = [infinity, 0] self._relprecs = [infinity, parentbr.precision_cap()] self._normalized = True self._list = None return #First we list the types that are turned into Polynomials if isinstance(x, ZZX): x = Polynomial_integer_dense(PolynomialRing(ZZ, parent.variable_name()), x, construct = True) elif isinstance(x, fraction_field_element.FractionFieldElement) and \ x.denominator() == 1: #Currently we ignore precision information in the denominator. This should be changed eventually x = x.numerator() #We now coerce various types into lists of coefficients. There are fast pathways for some types of polynomials if isinstance(x, Polynomial): if x.parent() is self.parent(): if not absprec is infinity or not relprec is infinity: x._normalize() self._poly = x._poly self._valbase = x._valbase self._valaddeds = x._valaddeds self._relprecs = x._relprecs self._normalized = x._normalized self._list = x._list if not absprec is infinity or not relprec is infinity: self._adjust_prec_info(absprec, relprec) return elif x.base_ring() is ZZ: self._poly = x self._valbase = Integer(0) p = parentbr.prime() self._relprecs = [c.valuation(p) + parentbr.precision_cap() for c in x.list()] self._comp_valaddeds() self._normalized = len(self._valaddeds) == 0 or (min(self._valaddeds) == 0) self._list = None if not absprec is infinity or not relprec is infinity: self._adjust_prec_info(absprec, relprec) return else: x = [parentbr(a) for a in x.list()] check = False elif isinstance(x, dict): zero = parentbr.zero_element() n = max(x.keys()) v = [zero for _ in xrange(n + 1)] for i, z in x.iteritems(): v[i] = z x = v elif isinstance(x, pari_gen): x = [parentbr(w) for w in x.list()] check = False #The default behavior if we haven't already figured out what the type is is to assume it coerces into the base_ring as a constant polynomial elif not isinstance(x, list): x = [x] # constant polynomial # In contrast to other polynomials, the zero element is not distinguished # by having its list empty. Instead, it has list [0] if not x: x = [parentbr.zero_element()] if check: x = [parentbr(z) for z in x] # Remove this -- for p-adics this is terrible, since it kills any non exact zero. #if len(x) == 1 and not x[0]: # x = [] self._list = x self._valaddeds = [a.valuation() for a in x] self._valbase = sage.rings.padics.misc.min(self._valaddeds) if self._valbase is infinity: self._valaddeds = [] self._relprecs = [] self._poly = PolynomialRing(ZZ, parent.variable_name())() self._normalized = True if not absprec is infinity or not relprec is infinity: self._adjust_prec_info(absprec, relprec) else: self._valaddeds = [c - self._valbase for c in self._valaddeds] self._relprecs = [a.precision_absolute() - self._valbase for a in x] self._poly = PolynomialRing(ZZ, parent.variable_name())([a >> self._valbase for a in x]) self._normalized = True if not absprec is infinity or not relprec is infinity: self._adjust_prec_info(absprec, relprec)
def gcd(self,other,algorithm=None): """ Return the gcd of this polynomial and ``other`` INPUT: - ``other`` -- a polynomial defined over the same ring as this polynomial. ALGORITHM: Two algorithms are provided: - ``generic``: Uses the generic implementation, which depends on the base ring being a UFD or a field. - ``dense``: The polynomials are converted to the dense representation, their gcd is computed and is converted back to the sparse representation. Default is ``dense`` for polynomials over ZZ and ``generic`` in the other cases. EXAMPLES:: sage: R.<x> = PolynomialRing(ZZ,sparse=True) sage: p = x^6 + 7*x^5 + 8*x^4 + 6*x^3 + 2*x^2 + x + 2 sage: q = 2*x^4 - x^3 - 2*x^2 - 4*x - 1 sage: gcd(p,q) x^2 + x + 1 sage: gcd(p, q, algorithm = "dense") x^2 + x + 1 sage: gcd(p, q, algorithm = "generic") x^2 + x + 1 sage: gcd(p, q, algorithm = "foobar") Traceback (most recent call last): ... ValueError: Unknown algorithm 'foobar' TESTS: Check that :trac:`19676` is fixed:: sage: S.<y> = R[] sage: x.gcd(y) 1 sage: (6*x).gcd(9) 3 """ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.arith.all import lcm if algorithm is None: if self.base_ring() == ZZ: algorithm = "dense" else: algorithm = "generic" if algorithm=="dense": S = self.parent() # FLINT is faster but a bug makes the conversion extremely slow, # so NTL is used in those cases where the conversion is too slow. Cf # <https://groups.google.com/d/msg/sage-devel/6qhW90dgd1k/Hoq3N7fWe4QJ> sd = self.degree() od = other.degree() if max(sd,od)<100 or \ min(len(self.__coeffs)/sd, len(other.__coeffs)/od)>.06: implementation="FLINT" else: implementation="NTL" D = PolynomialRing(S.base_ring(),'x',implementation=implementation) g = D(self).gcd(D(other)) return S(g) elif algorithm=="generic": return Polynomial.gcd(self,other) else: raise ValueError("Unknown algorithm '%s'" % algorithm)