Skip to content

Commit

Permalink
Trac #22273: py3 compatible imports in sage/rings
Browse files Browse the repository at this point in the history
URL: https://trac.sagemath.org/22273
Reported by: aapitzsch
Ticket author(s): André Apitzsch
Reviewer(s): Frédéric Chapoton
  • Loading branch information
Release Manager authored and vbraun committed Feb 2, 2017
2 parents 3a53dca + c61082d commit 6bc1a2d
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 89 deletions.
49 changes: 23 additions & 26 deletions src/sage/rings/complex_double.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ AUTHORS:
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import absolute_import
from __future__ import print_function

import operator
Expand Down Expand Up @@ -88,16 +89,16 @@ from sage.structure.coerce cimport is_numpy_type
from sage.libs.cypari2.gen cimport Gen as pari_gen
from sage.libs.cypari2.convert cimport new_gen_from_double, new_t_COMPLEX_from_double

import complex_number
from . import complex_number

import complex_field
cdef CC = complex_field.ComplexField()
from .complex_field import ComplexField
cdef CC = ComplexField()

import real_mpfr
cdef RR = real_mpfr.RealField()
from .real_mpfr import RealField
cdef RR = RealField()

from .real_double cimport RealDoubleElement, double_repr, double_str
from real_double import RDF
from .real_double import RDF
from sage.rings.integer_ring import ZZ


Expand Down Expand Up @@ -205,8 +206,8 @@ cdef class ComplexDoubleField_class(sage.rings.ring.Field):
sage: CDF.characteristic()
0
"""
import integer
return integer.Integer(0)
from .integer import Integer
return Integer(0)

def random_element(self, double xmin=-1, double xmax=1, double ymin=-1, double ymax=1):
"""
Expand Down Expand Up @@ -400,12 +401,10 @@ cdef class ComplexDoubleField_class(sage.rings.ring.Field):
"""
if S is int or S is float:
return FloatToCDF(S)
from rational_field import QQ
from real_lazy import RLF
from real_mpfr import RR, RealField_class
from complex_field import ComplexField, ComplexField_class
CC = ComplexField()
from complex_number import CCtoCDF
from .rational_field import QQ
from .real_lazy import RLF
from .real_mpfr import RR, RealField_class
from .complex_field import ComplexField_class
if S is ZZ or S is QQ or S is RDF or S is RLF:
return FloatToCDF(S)
if isinstance(S, RealField_class):
Expand All @@ -424,9 +423,9 @@ cdef class ComplexDoubleField_class(sage.rings.ring.Field):
elif RR.has_coerce_map_from(S):
return FloatToCDF(RR) * RR._internal_coerce_map_from(S)
elif isinstance(S, ComplexField_class) and S.prec() >= 53:
return CCtoCDF(S, self)
return complex_number.CCtoCDF(S, self)
elif CC.has_coerce_map_from(S):
return CCtoCDF(CC, self) * CC._internal_coerce_map_from(S)
return complex_number.CCtoCDF(CC, self) * CC._internal_coerce_map_from(S)

def _magma_init_(self, magma):
r"""
Expand Down Expand Up @@ -475,7 +474,6 @@ cdef class ComplexDoubleField_class(sage.rings.ring.Field):
if prec == 53:
return self
else:
from complex_field import ComplexField
return ComplexField(prec)


Expand Down Expand Up @@ -529,8 +527,7 @@ cdef class ComplexDoubleField_class(sage.rings.ring.Field):
sage: CDF.real_double_field()
Real Double Field
"""
import real_double
return real_double.RDF
return RDF

def pi(self):
r"""
Expand Down Expand Up @@ -595,7 +592,7 @@ cdef class ComplexDoubleField_class(sage.rings.ring.Field):
...
ValueError: n must be a positive integer
"""
from integer import Integer
from .integer import Integer
try:
n = Integer(n)
except TypeError:
Expand Down Expand Up @@ -2314,8 +2311,8 @@ cdef class ComplexDoubleElement(FieldElement):
"""
if self._complex.dat[1] == 0:
if self._complex.dat[0] == 0:
import infinity
return infinity.unsigned_infinity
from .infinity import unsigned_infinity
return unsigned_infinity
try:
from sage.rings.all import Integer, CC
if Integer(self._complex.dat[0]) < 0:
Expand Down Expand Up @@ -2354,8 +2351,8 @@ cdef class ComplexDoubleElement(FieldElement):
Infinity
"""
if self._complex.dat[0] == 1 and self._complex.dat[1] == 0:
import infinity
return infinity.unsigned_infinity
from .infinity import unsigned_infinity
return unsigned_infinity
return pari_to_cdf(self._pari_().zeta())

def algdep(self, long n):
Expand Down Expand Up @@ -2386,8 +2383,8 @@ cdef class ComplexDoubleElement(FieldElement):
sage: CDF(1,5).algdep(2)
x^2 - 2*x + 26
"""
from polynomial.polynomial_ring_constructor import PolynomialRing
from integer_ring import ZZ
from .polynomial.polynomial_ring_constructor import PolynomialRing
from .integer_ring import ZZ
R = PolynomialRing(ZZ ,'x')
return R(self._pari_().algdep(n))

Expand Down
8 changes: 4 additions & 4 deletions src/sage/rings/complex_number.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ AUTHORS:
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import absolute_import
from __future__ import print_function

import math
Expand All @@ -34,8 +35,8 @@ from .complex_double cimport ComplexDoubleElement
from .real_mpfr cimport RealNumber

import sage.misc.misc
import integer
import infinity
import sage.rings.integer as integer
import sage.rings.infinity as infinity

from sage.libs.mpmath.utils cimport mpfr_to_mpfval
from sage.rings.integer_ring import ZZ
Expand Down Expand Up @@ -2092,7 +2093,7 @@ cdef class ComplexNumber(sage.structure.element.FieldElement):
if base is None:
return ComplexNumber(self._parent, rho.log(), theta)
else:
from real_mpfr import RealField
from .real_mpfr import RealField
return ComplexNumber(self._parent, rho.log()/RealNumber(RealField(self.prec()),base).log(), theta/RealNumber(RealField(self.prec()),base).log())

def additive_order(self):
Expand Down Expand Up @@ -2356,7 +2357,6 @@ cdef class ComplexNumber(sage.structure.element.FieldElement):
Infinity
"""
if mpfr_zero_p(self.__im) and mpfr_cmp_ui(self.__re, 1) == 0:
import infinity
return infinity.unsigned_infinity
return self._parent(self._pari_().zeta())

Expand Down
5 changes: 3 additions & 2 deletions src/sage/rings/finite_rings/element_givaro.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ AUTHORS:
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import absolute_import
from __future__ import print_function

include "cysignals/signals.pxi"
Expand All @@ -58,13 +59,13 @@ from sage.libs.cypari2.paridecl cimport *
from sage.misc.randstate cimport randstate, current_randstate
from sage.rings.finite_rings.finite_field_base cimport FiniteField
from sage.rings.ring cimport Ring
from element_ext_pari import FiniteField_ext_pariElement
from .element_ext_pari import FiniteField_ext_pariElement
from .element_pari_ffelt cimport FiniteFieldElement_pari_ffelt
from sage.structure.sage_object cimport SageObject, richcmp
from sage.structure.element cimport Element, ModuleElement, RingElement
import operator
import sage.arith.all
import finite_field_constructor as finite_field
import sage.rings.finite_rings.finite_field_constructor as finite_field

import sage.interfaces.gap
from sage.libs.pari.all import pari
Expand Down
4 changes: 2 additions & 2 deletions src/sage/rings/finite_rings/hom_finite_field.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ AUTHOR:
#
# http://www.gnu.org/licenses/
#****************************************************************************

from __future__ import absolute_import

from sage.rings.integer cimport Integer

Expand Down Expand Up @@ -610,7 +610,7 @@ cdef class FrobeniusEndomorphism_finite_field(FrobeniusEndomorphism_generic):
"""
if self._degree_fixed == 1:
k = FiniteField(self.domain().characteristic())
from hom_prime_finite_field import FiniteFieldHomomorphism_prime
from .hom_prime_finite_field import FiniteFieldHomomorphism_prime
f = FiniteFieldHomomorphism_prime(Hom(k, self.domain()))
else:
k = FiniteField(self.domain().characteristic()**self._degree_fixed,
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/finite_rings/hom_prime_finite_field.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ AUTHOR:
#
# http://www.gnu.org/licenses/
#****************************************************************************

from __future__ import absolute_import

from sage.rings.integer cimport Integer

Expand Down
7 changes: 4 additions & 3 deletions src/sage/rings/integer_ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ other types will also coerce to the integers, when it makes sense.
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import absolute_import
from __future__ import print_function

include "sage/ext/cdefs.pxi"
Expand All @@ -68,10 +69,10 @@ from sage.misc.misc_c import prod
from sage.misc.randstate cimport randstate, current_randstate, SAGE_RAND_MAX
from sage.libs.ntl.convert cimport ZZ_to_mpz

cimport integer
cimport rational
cimport sage.rings.integer as integer
cimport sage.rings.rational as rational

import ring
from . import ring

arith = None
cdef void late_import():
Expand Down
34 changes: 20 additions & 14 deletions src/sage/rings/number_field/number_field_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ AUTHORS:
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import absolute_import
from __future__ import print_function

import operator
Expand Down Expand Up @@ -54,8 +55,7 @@ import sage.rings.integer

from sage.rings.real_mpfi cimport RealIntervalFieldElement

cimport number_field_base
import number_field
cimport sage.rings.number_field.number_field_base as number_field_base

from sage.rings.integer_ring cimport IntegerRing_class
from sage.rings.rational cimport Rational
Expand Down Expand Up @@ -366,8 +366,9 @@ cdef class NumberFieldElement(FieldElement):
quadratic field elements)
"""
if check:
if not isinstance(self.number_field(), number_field.NumberField_cyclotomic) \
or not isinstance(new_parent, number_field.NumberField_cyclotomic):
from .number_field import NumberField_cyclotomic
if not isinstance(self.number_field(), NumberField_cyclotomic) \
or not isinstance(new_parent, NumberField_cyclotomic):
raise TypeError("The field and the new parent field must both be cyclotomic fields.")

if rel == 0:
Expand Down Expand Up @@ -1900,7 +1901,7 @@ cdef class NumberFieldElement(FieldElement):
if R.is_field():
return R.one()

from order import is_NumberFieldOrder
from .order import is_NumberFieldOrder
if not is_NumberFieldOrder(R) or not R.is_maximal():
raise NotImplementedError("gcd() for %r is not implemented" % R)

Expand Down Expand Up @@ -2544,7 +2545,8 @@ cdef class NumberFieldElement(FieldElement):
raise ValueError("need a real or complex embedding to convert "
"a non rational element of a number field "
"into an algebraic number")
emb = number_field.refine_embedding(emb, infinity)
from .number_field import refine_embedding
emb = refine_embedding(emb, infinity)
return parent(emb(self))

def _symbolic_(self, SR):
Expand Down Expand Up @@ -2642,7 +2644,8 @@ cdef class NumberFieldElement(FieldElement):
if embedding is None:
raise TypeError("an embedding into RR or CC must be specified")

if isinstance(K, number_field.NumberField_cyclotomic):
from .number_field import NumberField_cyclotomic
if isinstance(K, NumberField_cyclotomic):
# solution by radicals may be difficult, but we have a closed form
from sage.all import exp, I, pi, ComplexField, RR
CC = ComplexField(53)
Expand All @@ -2651,8 +2654,9 @@ cdef class NumberFieldElement(FieldElement):
gen_image = exp(k*two_pi_i/K._n())
return self.polynomial()(gen_image)
else:
from .number_field import refine_embedding
# Convert the embedding to an embedding into AA or QQbar
embedding = number_field.refine_embedding(embedding, infinity)
embedding = refine_embedding(embedding, infinity)
a = embedding(self).radical_expression()
if a.parent() == SR:
return a
Expand Down Expand Up @@ -2929,6 +2933,7 @@ cdef class NumberFieldElement(FieldElement):
"""
if self.__multiplicative_order is None:
from .number_field import NumberField_cyclotomic
if self.is_rational():
if self.is_one():
self.__multiplicative_order = ZZ(1)
Expand All @@ -2938,7 +2943,7 @@ cdef class NumberFieldElement(FieldElement):
self.__multiplicative_order = sage.rings.infinity.infinity
elif not (self.is_integral() and self.norm().is_one()):
self.__multiplicative_order = sage.rings.infinity.infinity
elif isinstance(self.number_field(), number_field.NumberField_cyclotomic):
elif isinstance(self.number_field(), NumberField_cyclotomic):
t = self.number_field()._multiplicative_order_table()
f = self.polynomial()
if f in t:
Expand Down Expand Up @@ -3392,11 +3397,12 @@ cdef class NumberFieldElement(FieldElement):
sage: foo.matrix(R) == matrix(1,1,[foo])
True
"""
import sage.matrix.matrix_space
from sage.matrix.matrix_space import MatrixSpace
if base is self.parent():
return sage.matrix.matrix_space.MatrixSpace(base,1)([self])
return MatrixSpace(base,1)([self])
if base is not None and base is not self.base_ring():
if number_field.is_NumberField(base):
from sage.rings.number_field.number_field_base import is_NumberField
if is_NumberField(base):
return self._matrix_over_base(base)
else:
return self._matrix_over_base_morphism(base)
Expand All @@ -3413,7 +3419,7 @@ cdef class NumberFieldElement(FieldElement):
for n in range(d-1):
cur = cur * X
v += cur.list()
M = sage.matrix.matrix_space.MatrixSpace(K.base_ring(), d)
M = MatrixSpace(K.base_ring(), d)
self.__matrix = M(v)
self.__matrix.set_immutable()
return self.__matrix
Expand Down Expand Up @@ -3453,7 +3459,7 @@ cdef class NumberFieldElement(FieldElement):
sage: [L(6).valuation(P) for P in L.primes_above(3)]
[2, 2]
"""
from number_field_ideal import is_NumberFieldIdeal
from .number_field_ideal import is_NumberFieldIdeal
if not is_NumberFieldIdeal(P):
if is_NumberFieldElement(P):
P = self.number_field().fractional_ideal(P)
Expand Down

0 comments on commit 6bc1a2d

Please sign in to comment.