Skip to content

Commit

Permalink
Replace imports from sage.all (#123)
Browse files Browse the repository at this point in the history
* Replace imports from sage.all

* python/snap/polished_reps.py: Use try..except for imports from Sage

* src/snappy/verify/upper_halfspace/finite_point.py: Use try..except for imports from Sage

* python/snap/polished_reps.py: Move try..except for imports to sage_helper

* python/number.py: Go through .sage_helper instead of sage.all

* python/verify/upper_halfspace/finite_point.py: Go through sage_helper instead of using try..except for imports

* python/sage_helper.py: Fix for is_RealIntervalFieldElement

* python/verify/upper_halfspace/ideal_point.py: Go through sage_helper

* python/number.py: If SR cannot be imported, just do not register a coercion

* python/verify/hyperbolicity.py: Go through sage_helper

* python/cusps/maximal_cusp_area_matrix.py: Go through sage_helper

* python/hyperboloid/distances.py: Go through sage_helper

* python/tiling/tile.py: Go through sage_helper

* python/verify/complex_volume/adjust_torsion.py: Go through sage_helper

* python/verify/maximal_cusp_area_matrix/cusp_tiling_engine.py: Go through sage_helper

* python/verify/upper_halfspace/finite_point.py: Go through sage_helper

* python/pari.py: Use try..except for import of PariError

* python/number.py: If SR cannot be imported, just do not register a coercion (fixup)

* python/verify/complex_volume/extended_bloch.py: Go through sage_helper

* python/verify/complex_volume/cusped.py: Go through sage_helper

* python/upper_halfspace/__init__.py: Go through sage_helper for arccosh

* python/database.py: Go through sage_helper

* Use 'from sage.symbolic.constants import pi'

* python/verify/complex_volume/closed.py: Go through sage_helper

* cython/core/fundamental_group.pyx: Use import

* python/testing.py: Use import

* cython/core/abelian_group.pyx: Use import

* cython/core/basic.pyx: Remove .all import

* python/sage_helper.py: Fix import of RealNumber

* python/sage_helper.py: Import Complex* later

* python/verify/upper_halfspace/extended_matrix.py: Use try..except for import

* python/verify/upper_halfspace/ideal_point.py: Use try...except for import

* python/sage_helper.py: Import CIF, RIF

* python/verify/upper_halfspace/extended_matrix.py: Import from sage_helper

* python/sage_helper.py: Add imports from python/snap/find_field.py

* python/sage_helper.py: Add imports from python/exterior_to_link/rational_linear_algebra*.py

* python/sage_helper.py: Import RealField_class

* More imports via sage_helper

* python/sage_helper.py: Import xgcd

* More imports via sage_helper

* Even more imports via sage_helper

* python/sage_helper.py: Import ComplexBallField, exp, sin, block_matrix, prime_range

* python/sage_helper.py: Move imports from nsagetools here

* python/sage_helper.py: Move remaining imports from slice_obs_HKL here

* python/snap/peripheral: Import ChainComplex via sage_helper

---------

Co-authored-by: Matthias Goerner <[email protected]>
  • Loading branch information
mkoeppe and unhyperbolic authored Jan 14, 2025
1 parent 7833a9e commit 28b2932
Show file tree
Hide file tree
Showing 42 changed files with 172 additions and 108 deletions.
3 changes: 2 additions & 1 deletion cython/core/abelian_group.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ cdef class AbelianGroup():
'as a sequence.')
int_types = [int]
if _within_sage:
int_types += [sage.rings.integer.Integer]
from sage.rings.integer import Integer
int_types += [Integer]
for c in self.divisors:
assert type(c) in int_types and c >= 0,\
'Elementary divisors must be non-negative integers.\n'
Expand Down
1 change: 0 additions & 1 deletion cython/core/basic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ python_major_version = sys.version_info.major
from .sage_helper import _within_sage, SageNotAvailable
from .pari import pari as pari
try:
import sage.all
import sage.structure.sage_object
from sage.groups.perm_gps.permgroup_element import is_PermutationGroupElement
from sage.groups.perm_gps.permgroup import PermutationGroup
Expand Down
6 changes: 4 additions & 2 deletions cython/core/fundamental_group.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ class FundamentalGroup(CFundamentalGroup):


if _within_sage:
FundamentalGroup.__bases__ += (sage.structure.sage_object.SageObject,)
from sage.structure.sage_object import SageObject
FundamentalGroup.__bases__ += (SageObject,)


# Holonomy Groups
Expand Down Expand Up @@ -594,4 +595,5 @@ class HolonomyGroup(CHolonomyGroup):


if _within_sage:
HolonomyGroup.__bases__ += (sage.structure.sage_object.SageObject,)
from sage.structure.sage_object import SageObject
HolonomyGroup.__bases__ += (SageObject,)
2 changes: 1 addition & 1 deletion dev/drilling/smoke_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from snappy.drilling import exceptions
from snappy.drilling import perturb

from sage.all import pi
from sage.symbolic.constants import pi

# perturb._tube_developing_radius = 1

Expand Down
2 changes: 1 addition & 1 deletion dev/drilling/smoke_test2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from snappy.drilling import exceptions
from snappy.drilling import perturb

from sage.all import pi
from sage.symbolic.constants import pi

# perturb._tube_developing_radius = 1

Expand Down
6 changes: 3 additions & 3 deletions python/cusps/maximal_cusp_area_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..hyperboloid.distances import distance_r13_horoballs

if _within_sage:
import sage.all
from ..sage_helper import Infinity

def maximal_cusp_area_matrix(manifold, bits_prec, verified):
"""
Expand Down Expand Up @@ -63,7 +63,7 @@ def _diagonal_scale(mcomplex, i):
return e ** 2

if mcomplex.verified:
d = mcomplex.RF(sage.all.Infinity)
d = mcomplex.RF(Infinity)
else:
d = mcomplex.RF(1e20)

Expand Down Expand Up @@ -93,7 +93,7 @@ def _non_diagonal_scale(mcomplex, i, j):
return e ** 2

if mcomplex.verified:
d = mcomplex.RF(sage.all.Infinity)
d = mcomplex.RF(Infinity)
else:
d = mcomplex.RF(1e20)

Expand Down
10 changes: 5 additions & 5 deletions python/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
import collections

if _within_sage:
import sage.all
from .sage_helper import Integer, RealDoubleElement, RealNumber

def is_int(slice):
return isinstance(slice, (sage.all.Integer, int))
return isinstance(slice, (Integer, int))

def is_int_or_none(slice):
return isinstance(slice, (sage.all.Integer, int, type(None)))
return isinstance(slice, (Integer, int, type(None)))

def is_float_or_none(slice):
return isinstance(slice, (float, sage.all.RealDoubleElement,
sage.rings.real_mpfr.RealNumber, type(None)))
return isinstance(slice, (float, RealDoubleElement,
RealNumber, type(None)))
else:
def is_int(slice):
return isinstance(slice, int)
Expand Down
2 changes: 1 addition & 1 deletion python/exterior_to_link/rational_linear_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .. import sage_helper

if sage_helper._within_sage and not use_pari_even_inside_sage:
from sage.all import QQ, RR, vector, matrix, VectorSpace
from ..sage_helper import QQ, RR, vector, matrix, VectorSpace

def rational_sqrt(x):
"""
Expand Down
2 changes: 1 addition & 1 deletion python/exterior_to_link/rational_linear_algebra_wrapped.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
This file is not currently used by SnapPy, but is kept for possible
future reference.
"""
from sage.all import QQ, vector, matrix, VectorSpace
from ..sage_helper import QQ, vector, matrix, VectorSpace


class Vector:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ def _add_one_cusp_vertex_positions(self, cusp : t3m.Vertex):
visited.add((tet1.Index, vert1))

def _debug_show_horotriangles(self, cusp : int =0):
from sage.all import line, real, imag
from sage.plot.line import line
from sage.functions.other import real, imag

self.add_vertex_positions_to_horotriangles()

Expand All @@ -398,7 +399,8 @@ def _debug_show_horotriangles(self, cusp : int =0):
if tet.Class[V].Index == cusp ])

def _debug_show_lifted_horotriangles(self, cusp : int =0):
from sage.all import line, real, imag
from sage.plot.line import line
from sage.functions.other import real, imag

self.add_vertex_positions_to_horotriangles()

Expand Down
14 changes: 7 additions & 7 deletions python/hyperboloid/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..sage_helper import _within_sage # type: ignore

if _within_sage:
import sage.all # type: ignore
from ..sage_helper import Infinity

__all__ = ['distance_r13_lines',
'lower_bound_distance_r13_line_triangle']
Expand Down Expand Up @@ -183,7 +183,7 @@ def _safe_sqrt(p):

if is_RealIntervalFieldElement(p):
RIF = p.parent()
p = p.intersection(RIF(0, sage.all.Infinity))
p = p.intersection(RIF(0, Infinity))
else:
if p < 0:
RF = p.parent()
Expand All @@ -193,7 +193,7 @@ def _safe_sqrt(p):
def _safe_log(p):
if is_RealIntervalFieldElement(p):
RIF = p.parent()
p = p.intersection(RIF(0, sage.all.Infinity))
p = p.intersection(RIF(0, Infinity))
else:
if p <= 0:
RF = p.parent()
Expand All @@ -207,7 +207,7 @@ def _safe_log_non_neg(p):
if p == 0:
if is_RealIntervalFieldElement(p):
RIF = p.parent()
return RIF(-sage.all.Infinity)
return RIF(-Infinity)
else:
RF = p.parent()
return RF(-1e20)
Expand All @@ -217,7 +217,7 @@ def _safe_log_non_neg(p):
def _safe_arccosh(p):
if is_RealIntervalFieldElement(p):
RIF = p.parent()
p = p.intersection(RIF(1, sage.all.Infinity))
p = p.intersection(RIF(1, Infinity))
else:
if p < 1:
RF = p.parent()
Expand All @@ -233,9 +233,9 @@ def _safe_div(a, b):
if is_RealIntervalFieldElement(b):
RIF = b.parent()
if b == 0:
return RIF(sage.all.Infinity)
return RIF(Infinity)
else:
return a / b.intersection(RIF(0, sage.all.Infinity))
return a / b.intersection(RIF(0, Infinity))
else:
if b <= 0:
RIF = b.parent()
Expand Down
3 changes: 2 additions & 1 deletion python/math_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def is_Interval(x):
return is_RealIntervalFieldElement(x) or is_ComplexIntervalFieldElement(x)

if _within_sage:
from sage.all import prod, xgcd
from .sage_helper import prod, xgcd

from sage.rings.real_mpfi import RealIntervalFieldElement
from sage.rings.complex_interval import ComplexIntervalFieldElement

Expand Down
11 changes: 8 additions & 3 deletions python/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
precision_of_exact_GEN = pari(0).precision()

if _within_sage:
from sage.all import RealField, Integer, Rational, ZZ, QQ, RR, CC, SR
from .sage_helper import RealField, Integer, Rational, ZZ, QQ, RR, CC
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.categories.homset import Hom
Expand Down Expand Up @@ -66,8 +66,13 @@ def __init__(self, precision):
self._precision = precision
self.register_coercion(MorphismToSPN(ZZ, self, self._precision))
self.register_coercion(MorphismToSPN(QQ, self, self._precision))
to_SR = Hom(self, SR, Sets())(lambda x: SR(x.sage()))
SR.register_coercion(to_SR)
try:
from sage.symbolic.ring import SR
except ImportError:
pass
else:
to_SR = Hom(self, SR, Sets())(lambda x: SR(x.sage()))
SR.register_coercion(to_SR)

def _repr_(self):
return "SnapPy Numbers with %s bits precision" % self._precision
Expand Down
6 changes: 5 additions & 1 deletion python/pari.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
from cypari2.pari_instance import (prec_words_to_bits,
prec_bits_to_dec,
prec_dec_to_bits)
from sage.all import PariError
try:
from sage.all import PariError
except ImportError:
from sage.libs.pari.all import PariError

shut_up = lambda: None
speak_up = lambda: None

Expand Down
66 changes: 64 additions & 2 deletions python/sage_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

try:
import sage.all
import sage.structure.sage_object
_within_sage = True
except ImportError:
_within_sage = False
Expand All @@ -22,11 +22,73 @@ def sage_method(function):
function._sage_method = True
return function

try:
# Monolithic Sage library
from sage.all import RealField, RealDoubleElement, gcd, xgcd, prod, powerset
from sage.all import MatrixSpace, matrix, vector, ZZ
from sage.all import Integer, Rational, QQ, RR, CC
from sage.all import sqrt
from sage.all import I, Infinity
from sage.all import arccosh
from sage.all import RIF, CIF
from sage.all import (cached_method, real_part, imag_part, round, ceil, floor, log,
CDF, ComplexDoubleField, ComplexField, CyclotomicField, NumberField, PolynomialRing, identity_matrix)
from sage.all import FiniteField as GF
from sage.all import VectorSpace, ChainComplex
from sage.all import ComplexBallField, exp, sin, block_matrix, prime_range, det
from sage.all import LaurentPolynomialRing, AbelianGroup, GroupAlgebra
except ImportError:
# Modularized Sage library
from sage.algebras.group_algebra import GroupAlgebra
from sage.arith.misc import gcd, xgcd
from sage.combinat.subset import powerset
from sage.functions.hyperbolic import arccosh
from sage.functions.log import exp
from sage.functions.other import (real as real_part,
imag as imag_part,
ceil,
floor)
from sage.functions.trig import sin
from sage.groups.abelian_gps.abelian_group import AbelianGroup
from sage.homology.chain_complex import ChainComplex
from sage.matrix.constructor import Matrix as matrix
from sage.matrix.matrix_space import MatrixSpace
from sage.matrix.special import block_matrix, identity_matrix
from sage.misc.cachefunc import cached_method
from sage.misc.functional import det, log, round, sqrt
from sage.misc.misc_c import prod
from sage.modules.free_module import VectorSpace
from sage.modules.free_module_element import free_module_element as vector
from sage.rings.cc import CC
from sage.rings.cif import CIF
from sage.rings.complex_arb import ComplexBallField
from sage.rings.complex_double import CDF, ComplexDoubleField
from sage.rings.complex_mpfr import ComplexField
from sage.rings.fast_arith import prime_range
from sage.rings.finite_rings.finite_field_constructor import FiniteField as GF
from sage.rings.imaginary_unit import I
from sage.rings.infinity import Infinity
from sage.rings.integer import Integer
from sage.rings.integer_ring import ZZ
from sage.rings.polynomial.laurent_polynomial_ring import LaurentPolynomialRing
from sage.rings.number_field.number_field import CyclotomicField, NumberField
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.rational import Rational
from sage.rings.rational_field import QQ
from sage.rings.real_double import RealDoubleElement
from sage.rings.real_mpfi import RIF
from sage.rings.real_mpfr import RealField, RealNumber, RR

from sage.rings.complex_interval_field import ComplexIntervalField
from sage.rings.real_mpfi import is_RealIntervalFieldElement, RealIntervalField
from sage.rings.real_mpfr import RealNumber, RealField_class
from sage.structure.sage_object import SageObject

try: # Sage >= 9.3, see https://trac.sagemath.org/ticket/24483
from sage.rings.complex_mpfr import (ComplexField,
ComplexField_class,
create_ComplexNumber)
except ModuleNotFoundError:
except ImportError:
from sage.rings.complex_field import ComplexField, ComplexField_class
from sage.rings.complex_number import create_ComplexNumber

Expand Down
2 changes: 1 addition & 1 deletion python/snap/character_varieties.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def character_variety_ideal(gens, rels=None):
2
"""
presentation = character_variety(gens, rels)
from sage.all import PolynomialRing, QQ
from ..sage_helper import PolynomialRing, QQ
R = PolynomialRing(QQ, [repr(v) for v in presentation.gens])
return R.ideal([R(p) for p in presentation.rels])

Expand Down
7 changes: 4 additions & 3 deletions python/snap/find_field.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sage.all import (cached_method, real_part, imag_part, round, ceil, floor, log,
ZZ, QQ, CDF, ComplexField, NumberField, PolynomialRing,
matrix, identity_matrix)
import itertools

from ..sage_helper import (cached_method, real_part, imag_part, round, ceil, floor, log,
ZZ, QQ, CDF, ComplexField, NumberField, PolynomialRing,
matrix, identity_matrix)


def error(poly, z, a=ZZ(0)):
"""
Expand Down
2 changes: 1 addition & 1 deletion python/snap/fundamental_polyhedron.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from ..sage_helper import _within_sage
if _within_sage:
from sage.all import matrix
from ..sage_helper import matrix
else:
from .utilities import Matrix2x2 as matrix

Expand Down
8 changes: 3 additions & 5 deletions python/snap/nsagetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
from ..sage_helper import _within_sage, sage_method

if _within_sage:
import sage
from sage.all import (ZZ, vector, matrix, block_matrix, identity_matrix,
gcd, prod, det, MatrixSpace, AbelianGroup, GroupAlgebra,
SageObject, PolynomialRing, LaurentPolynomialRing)

from ..sage_helper import ZZ, vector, matrix, block_matrix, identity_matrix, gcd, prod, det
from ..sage_helper import MatrixSpace, PolynomialRing, LaurentPolynomialRing
from ..sage_helper import SageObject, AbelianGroup, GroupAlgebra
from .polished_reps import polished_holonomy, MatrixRepresentation
Id2 = MatrixSpace(ZZ, 2)(1)
else:
Expand Down
3 changes: 2 additions & 1 deletion python/snap/peripheral/dual_cellulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from .. import t3mlite as t3m

if sage_helper._within_sage:
from sage.all import (ZZ, matrix, vector, ChainComplex, Graph)
from ...sage_helper import ZZ, matrix, vector, ChainComplex
from sage.graphs.graph import Graph


class DualCell():
Expand Down
2 changes: 1 addition & 1 deletion python/snap/peripheral/peripheral.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from . import link, dual_cellulation

if sage_helper._within_sage:
from sage.all import matrix, vector, ZZ
from ...sage_helper import matrix, vector, ZZ


def peripheral_curve_from_snappy(dual_cell, snappy_data):
Expand Down
Loading

0 comments on commit 28b2932

Please sign in to comment.