Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace imports from sage.all #123

Merged
merged 46 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5c96067
Replace imports from sage.all
mkoeppe Dec 30, 2024
b14f59d
python/snap/polished_reps.py: Use try..except for imports from Sage
mkoeppe Oct 31, 2024
801783d
src/snappy/verify/upper_halfspace/finite_point.py: Use try..except fo…
mkoeppe Nov 1, 2024
664ea76
python/snap/polished_reps.py: Move try..except for imports to sage_he…
mkoeppe Nov 1, 2024
27eed35
python/number.py: Go through .sage_helper instead of sage.all
mkoeppe Nov 1, 2024
3125594
python/verify/upper_halfspace/finite_point.py: Go through sage_helper…
mkoeppe Nov 1, 2024
5a43792
python/sage_helper.py: Fix for is_RealIntervalFieldElement
mkoeppe Nov 1, 2024
ca7c6d3
python/verify/upper_halfspace/ideal_point.py: Go through sage_helper
mkoeppe Nov 1, 2024
ccacbcc
python/number.py: If SR cannot be imported, just do not register a co…
mkoeppe Nov 1, 2024
54fbfd0
python/verify/hyperbolicity.py: Go through sage_helper
mkoeppe Nov 1, 2024
84898c9
python/cusps/maximal_cusp_area_matrix.py: Go through sage_helper
mkoeppe Nov 1, 2024
dc738d2
python/hyperboloid/distances.py: Go through sage_helper
mkoeppe Nov 1, 2024
ac081ab
python/tiling/tile.py: Go through sage_helper
mkoeppe Nov 1, 2024
8274cb5
python/verify/complex_volume/adjust_torsion.py: Go through sage_helper
mkoeppe Nov 1, 2024
4e31893
python/verify/maximal_cusp_area_matrix/cusp_tiling_engine.py: Go thro…
mkoeppe Nov 1, 2024
9a01d66
python/verify/upper_halfspace/finite_point.py: Go through sage_helper
mkoeppe Nov 1, 2024
1f63268
python/pari.py: Use try..except for import of PariError
mkoeppe Nov 1, 2024
64477f6
python/number.py: If SR cannot be imported, just do not register a co…
mkoeppe Nov 1, 2024
c9ccfb0
python/verify/complex_volume/extended_bloch.py: Go through sage_helper
mkoeppe Nov 1, 2024
bbcd6c8
python/verify/complex_volume/cusped.py: Go through sage_helper
mkoeppe Nov 1, 2024
5ce0eb0
python/upper_halfspace/__init__.py: Go through sage_helper for arccosh
mkoeppe Nov 1, 2024
80d3d61
python/database.py: Go through sage_helper
mkoeppe Nov 1, 2024
c320923
Use 'from sage.symbolic.constants import pi'
mkoeppe Nov 1, 2024
7a70363
python/verify/complex_volume/closed.py: Go through sage_helper
mkoeppe Nov 1, 2024
00c5b98
cython/core/fundamental_group.pyx: Use import
mkoeppe Nov 1, 2024
4e1689a
python/testing.py: Use import
mkoeppe Nov 1, 2024
0e0df34
cython/core/abelian_group.pyx: Use import
mkoeppe Nov 1, 2024
c6440f3
cython/core/basic.pyx: Remove .all import
mkoeppe Nov 1, 2024
5b332d8
python/sage_helper.py: Fix import of RealNumber
mkoeppe Dec 31, 2024
8d02aee
python/sage_helper.py: Import Complex* later
mkoeppe Dec 31, 2024
7d883d1
python/verify/upper_halfspace/extended_matrix.py: Use try..except for…
mkoeppe Dec 31, 2024
2ac51c3
python/verify/upper_halfspace/ideal_point.py: Use try...except for im…
mkoeppe Dec 31, 2024
f94a43b
python/sage_helper.py: Import CIF, RIF
mkoeppe Jan 1, 2025
1afb2d4
python/verify/upper_halfspace/extended_matrix.py: Import from sage_he…
mkoeppe Jan 1, 2025
9a434d9
python/sage_helper.py: Add imports from python/snap/find_field.py
mkoeppe Jan 1, 2025
b74ed93
python/sage_helper.py: Add imports from python/exterior_to_link/ratio…
mkoeppe Jan 1, 2025
dc0347f
python/sage_helper.py: Import RealField_class
mkoeppe Jan 1, 2025
7a6b0b0
More imports via sage_helper
mkoeppe Jan 1, 2025
1b40d50
python/sage_helper.py: Import xgcd
mkoeppe Jan 1, 2025
30bb154
More imports via sage_helper
mkoeppe Jan 1, 2025
b6c24c6
Even more imports via sage_helper
mkoeppe Jan 1, 2025
2ded24e
python/sage_helper.py: Import ComplexBallField, exp, sin, block_matri…
mkoeppe Jan 1, 2025
3bb486e
python/sage_helper.py: Move imports from nsagetools here
mkoeppe Jan 1, 2025
6739b66
python/sage_helper.py: Move remaining imports from slice_obs_HKL here
mkoeppe Jan 1, 2025
033bff7
python/snap/peripheral: Import ChainComplex via sage_helper
mkoeppe Jan 1, 2025
8e92b2c
Merge branch 'master' into sage.all-imports
unhyperbolic Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -18,7 +18,11 @@
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need this?

Note: I do use the name RIF and CIF for RealIntervalField and ComplexIntervalField instances with precisions different from RealIntervalField() and ComplexIntervalField().

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you've added a bunch of Sage imports to sage_helper.py, I think it makes sense to import those objects through it here and in analogous places throughout. E.g.

from ..sage_helper import ZZ, QQ, matrix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I think. Preview: https://github.com/mkoeppe/SnapPy/actions/runs/12575148681

Happy new year!

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
Loading