示例#1
0
    def __init__(self, parent, x, bits=None, nterms=None):
        """
        EXAMPLES::

            sage: sage.rings.contfrac.ContinuedFraction(CFF,[1,2,3,4,1,2])
            [1, 2, 3, 4, 1, 2]
            sage: sage.rings.contfrac.ContinuedFraction(CFF,[1,2,3,4,-1,2])
            Traceback (most recent call last):
            ...
            ValueError: each entry except the first must be positive
        """
        FieldElement.__init__(self, parent)
        if isinstance(x, ContinuedFraction):
            self._x = list(x._x)
        elif isinstance(x, (list, tuple)):
            x = [ZZ(a) for a in x]
            for i in range(1, len(x)):
                if x[i] <= 0:
                    raise ValueError, "each entry except the first must be positive"
            self._x = list(x)
        else:
            self._x = [
                ZZ(a)
                for a in continued_fraction_list(x, bits=bits, nterms=nterms)
            ]
    def __init__(self, parent, value):
        r"""
        TESTS::

            sage: from pyeantic import RealEmbeddedNumberField
            sage: K = NumberField(x^2 - 2, 'a', embedding=sqrt(AA(2)))
            sage: K = RealEmbeddedNumberField(K)
            sage: a = K.gen()

            sage: from pyeantic.real_embedded_number_field import RealEmbeddedNumberFieldElement
            sage: isinstance(a, RealEmbeddedNumberFieldElement)
            True

        """
        if isinstance(value, cppyy.gbl.eantic.renf_elem_class):
            self.renf_elem = value
        else:
            value = parent.number_field(value)
            self.renf_elem = parent.renf.zero()

            gen_pow = parent.renf.one()
            for coeff in value.polynomial().coefficients(sparse=False):
                self.renf_elem = self.renf_elem + coeff * gen_pow
                gen_pow = gen_pow * parent.renf.gen()

        FieldElement.__init__(self, parent)
示例#3
0
    def __init__(self, parent, n, d=None):
        if (not isinstance(parent, LazyFracField)):
            raise TypeError(
                "The parent of a fraction must be a Fraction Field")

        B = parent.base()
        # B is the lazy domain
        ### Checking/Adapting numerator
        if (not (n in B)):
            raise TypeError("Element for numerator must be in %s" % (repr(B)))
        elif (not (isinstance(n, LazyIDElement))):
            n = B(n)

        ### Checking/Adapting denominator
        if (d is None):
            d = B.one()
        elif (not (d in B)):
            raise TypeError("Element for denominator must be in %s" %
                            (repr(B)))
        elif (d == B.zero()):
            raise ZeroDivisionError("The denominator can not be zero")
        elif (not (isinstance(d, LazyIDElement))):
            d = B(d)
        elif (isinstance(d, SumLIDElement)):
            #raise TypeError("No additions are allowed as denominators in a Lazy Fraction Field");
            #d = B(d.raw());
            pass

        self.__n = n
        self.__d = d
        FieldElement.__init__(self, parent)
示例#4
0
        def __init__(self, x1, x2=None):
            r"""
            INPUT:

            - ``parent`` - the parent

            - ``x`` - the quotients of the continued fraction

            TESTS::

                sage: TestSuite(CFF.an_element()).run()
            """
            ContinuedFraction_periodic.__init__(self, x1)
            FieldElement.__init__(self, parent=CFF)
示例#5
0
        def __init__(self, x1, x2=None):
            r"""
            INPUT:

            - ``parent`` - the parent

            - ``x`` - the quotients of the continued fraction

            TESTS::

                sage: TestSuite(CFF.an_element()).run()
            """
            ContinuedFraction_periodic.__init__(self, x1)
            FieldElement.__init__(self, parent=CFF)
示例#6
0
文件: contfrac.py 项目: drupel/sage
        def __init__(self, x1, x2=None):
            r"""
            INPUT:

            - ``parent`` - the parent

            - ``x`` - the quotients of the continued fraction

            TESTS::

                sage: TestSuite(CFF.an_element()).run()
            """
            deprecation(20012, "CFF (ContinuedFractionField) is deprecated, use QQ instead")
            ContinuedFraction_periodic.__init__(self, x1)
            FieldElement.__init__(self, parent=CFF)
示例#7
0
        def __init__(self, x1, x2=None):
            r"""
            INPUT:

            - ``parent`` - the parent

            - ``x`` - the quotients of the continued fraction

            TESTS::

                sage: TestSuite(CFF.an_element()).run()
            """
            deprecation(20012, "CFF (ContinuedFractionField) is deprecated, use QQ instead")
            ContinuedFraction_periodic.__init__(self, x1)
            FieldElement.__init__(self, parent=CFF)
 def __init__(self, parent, value, symbolic=None):
     FieldElement.__init__(
         self, parent)  ## this is so that canonical_coercion works.
     if not parent._mutable_values and value is not None:
         ## Test coercing the value to RR, so that we do not try to build a ParametricRealFieldElement
         ## from something like a tuple or vector or list or variable of a polynomial ring
         ## or something else that does not make any sense.
         from sage.interfaces.mathematica import MathematicaElement
         if not isinstance(value, MathematicaElement):
             RR(value)
         self._val = value
     if symbolic is None:
         self._sym = value  # changed to not coerce into SR. -mkoeppe
     else:
         self._sym = symbolic
示例#9
0
    def __init__(self, parent, obj):
        r"""
        INPUT:

        - ``parent`` -- a universal cyclotomic field

        - ``obj`` -- a libgap element (either an integer, a rational or a
          cyclotomic)

        TESTS::

            sage: UCF = UniversalCyclotomicField()
            sage: a = UCF.an_element()
            sage: TestSuite(a).run()
        """
        self._obj = obj
        FieldElement.__init__(self, parent)
    def __init__(self, parent, obj):
        r"""
        INPUT:

        - ``parent`` - a universal cyclotomic field

        - ``obj`` - a libgap element (either an integer, a rational or a
          cyclotomic)

        TESTS::

            sage: UCF = UniversalCyclotomicField()
            sage: a = UCF.an_element()
            sage: TestSuite(a).run()
        """
        self._obj = obj
        FieldElement.__init__(self, parent)
 def __init__(self, parent, value, symbolic=None):
     FieldElement.__init__(
         self, parent)  ## this is so that canonical_coercion works.
     if not parent._mutable_values and value is not None:
         ## Test coercing the value to RR, so that we do not try to build a ParametricRealFieldElement
         ## from something like a tuple or vector or list or variable of a polynomial ring
         ## or something else that does not make any sense.
         # FIXME: parent(value) caused SIGSEGV because of an infinite recursion
         from sage.structure.coerce import py_scalar_parent
         if hasattr(value, 'parent'):
             if not RR.has_coerce_map_from(value.parent()):
                 raise TypeError("Value is of wrong type")
         else:
             if not RR.has_coerce_map_from(py_scalar_parent(type(value))):
                 raise TypeError("Value is of wrong type")
         self._val = value
     if symbolic is None:
         self._sym = value  # changed to not coerce into SR. -mkoeppe
     else:
         self._sym = symbolic
示例#12
0
    def __init__(self, parent, x, bits=None, nterms=None):
        """
        EXAMPLES::

            sage: sage.rings.contfrac.ContinuedFraction(CFF,[1,2,3,4,1,2])
            [1, 2, 3, 4, 1, 2]
            sage: sage.rings.contfrac.ContinuedFraction(CFF,[1,2,3,4,-1,2])
            Traceback (most recent call last):
            ...
            ValueError: each entry except the first must be positive
        """
        FieldElement.__init__(self, parent)
        if isinstance(x, ContinuedFraction):
            self._x = list(x._x)
        elif isinstance(x, (list, tuple)):
            x = [ZZ(a) for a in x]
            for i in range(1,len(x)):
                if x[i] <= 0:
                    raise ValueError, "each entry except the first must be positive"
            self._x = list(x)
        else:
            self._x = [ZZ(a) for a in continued_fraction_list(x, bits=bits, nterms=nterms)]
示例#13
0
    def __init__(self, parent, value):
        """
        TESTS::

            sage: F = GF(3).algebraic_closure()
            sage: TestSuite(F.gen(2)).run(skip=['_test_pickling'])

        .. NOTE::

            The ``_test_pickling`` test has to be skipped because
            there is no coercion map between the parents of ``x``
            and ``loads(dumps(x))``.

        """
        if is_FiniteFieldElement(value):
            n = value.parent().degree()
        else:
            from sage.rings.integer import Integer
            n = Integer(1)
        self._value = parent._subfield(n).coerce(value)
        self._level = n
        FieldElement.__init__(self, parent)
    def __init__(self, parent, value):
        """
        TEST::

            sage: F = GF(3).algebraic_closure()
            sage: TestSuite(F.gen(2)).run(skip=['_test_pickling'])

        .. NOTE::

            The ``_test_pickling`` test has to be skipped because
            there is no coercion map between the parents of ``x``
            and ``loads(dumps(x))``.

        """
        if is_FiniteFieldElement(value):
            n = value.parent().degree()
        else:
            from sage.rings.integer import Integer
            n = Integer(1)
        self._value = parent._subfield(n).coerce(value)
        self._level = n
        FieldElement.__init__(self, parent)
示例#15
0
    def __init__(self, parent, ogf):
        r"""
        Initialize the C-Finite sequence.

        The ``__init__`` method can only be called by the :class:`CFiniteSequences`
        class. By Default, a class call reaches the ``__classcall_private__``
        which first creates a proper parent and then call the ``__init__``.

        INPUT:

        - ``ogf`` -- the ordinary generating function, a fraction of polynomials over the rationals
        - ``parent`` --  the parent of the C-Finite sequence, an occurrence of :class:`CFiniteSequences`

        OUTPUT:

        - A CFiniteSequence object

        TESTS::

            sage: C.<x> = CFiniteSequences(QQ)
            sage: C((2-x)/(1-x-x^2))            # indirect doctest
            C-finite sequence, generated by (x - 2)/(x^2 + x - 1)
        """

        br = parent.base_ring()
        ogf = parent.fraction_field()(ogf)
        P = parent.polynomial_ring()
        num = ogf.numerator()
        den = ogf.denominator()

        FieldElement.__init__(self, parent)

        if den == 1:
            self._c = []
            self._off = num.valuation()
            self._deg = 0
            if ogf == 0:
                self._a = [0]
            else:
                self._a = num.shift(-self._off).list()
        else:
            # Transform the ogf numerator and denominator to canonical form
            # to get the correct offset, degree, and recurrence coeffs and
            # start values.
            self._off = 0
            self._deg = 0
            if num.constant_coefficient() == 0:
                self._off = num.valuation()
                num = num.shift(-self._off)
            elif den.constant_coefficient() == 0:
                self._off = -den.valuation()
                den = den.shift(self._off)
            f = den.constant_coefficient()
            num = P(num / f)
            den = P(den / f)
            f = num.gcd(den)
            num = P(num / f)
            den = P(den / f)
            self._deg = den.degree()
            self._c = [-den[i] for i in range(1, self._deg + 1)]
            if self._off >= 0:
                num = num.shift(self._off)
            else:
                den = den.shift(-self._off)

            # determine start values (may be different from _get_item_ values)
            alen = max(self._deg, num.degree() + 1)
            R = LaurentSeriesRing(br,
                                  parent.variable_name(),
                                  default_prec=alen)
            rem = num % den
            if den != 1:
                self._a = R(num / den).list()
                self._aa = R(rem /
                             den).list()[:self._deg]  # needed for _get_item_
            else:
                self._a = num.list()
            if len(self._a) < alen:
                self._a.extend([0] * (alen - len(self._a)))

            ogf = num / den

        self._ogf = ogf
示例#16
0
    def __init__(self, parent, ogf):
        r"""
        Initialize the C-Finite sequence.

        The ``__init__`` method can only be called by the :class:`CFiniteSequences`
        class. By Default, a class call reaches the ``__classcall_private__``
        which first creates a proper parent and then call the ``__init__``.

        INPUT:

        - ``ogf`` -- the ordinary generating function, a fraction of polynomials over the rationals
        - ``parent`` --  the parent of the C-Finite sequence, an occurence of :class:`CFiniteSequences`

        OUTPUT:

        - A CFiniteSequence object

        TESTS::

            sage: C.<x> = CFiniteSequences(QQ);
            sage: C((2-x)/(1-x-x^2))            # indirect doctest
            C-finite sequence, generated by (-x + 2)/(-x^2 - x + 1)
        """

        br = parent.base_ring()
        ogf = parent.fraction_field()(ogf)
        P = parent.polynomial_ring()
        num = ogf.numerator()
        den = ogf.denominator()

        FieldElement.__init__(self, parent)

        if den == 1:
            self._c = []
            self._off = num.valuation()
            self._deg = 0
            if ogf == 0:
                self._a = [0]
            else:
                self._a = P((num / (P.gen()) ** self._off)).list()
        else:

            # Transform the ogf numerator and denominator to canonical form
            # to get the correct offset, degree, and recurrence coeffs and
            # start values.
            self._off = 0
            self._deg = 0
            x = P.gen()
            if num.constant_coefficient() == 0:
                self._off = num.valuation()
                num = P(num / x ** self._off)
            elif den.constant_coefficient() == 0:
                self._off = -den.valuation()
                den = P(den * x ** self._off)
            f = den.constant_coefficient()
            num = P(num / f)
            den = P(den / f)
            f = gcd(num, den)
            num = P(num / f)
            den = P(den / f)
            self._deg = den.degree()
            self._c = [-den.list()[i] for i in range(1, self._deg + 1)]
            if self._off >= 0:
                num = x ** self._off * num
            else:
                den = x ** (-self._off) * den

            # determine start values (may be different from _get_item_ values)
            alen = max(self._deg, num.degree() + 1)
            R = LaurentSeriesRing(br, parent.variable_name(), default_prec=alen)
            rem = num % den
            if den != 1:
                self._a = R(num / den).list()
                self._aa = R(rem / den).list()[: self._deg]  # needed for _get_item_
            else:
                self._a = num.list()
            if len(self._a) < alen:
                self._a.extend([0] * (alen - len(self._a)))

            ogf = num / den

        self._ogf = ogf