Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit ddb7291

Browse files
committed
Add lift_centered method to more classes, deprecate centerlift
1 parent 8be52e6 commit ddb7291

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

src/sage/libs/ntl/ntl_ZZ_p.pyx

+28
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,34 @@ cdef class ntl_ZZ_p:
414414
ZZ_p_modulus( &r.x, &self.x )
415415
return r
416416

417+
def lift_centered(self):
418+
"""
419+
Compute a representative of ``self`` in `(-n/2 , n/2]` as an
420+
``ntl.ZZ object``.
421+
422+
OUTPUT:
423+
424+
- An ``ntl.ZZ`` object `r` such that `-n/2 < r <= n/2` and `Mod(r, n) == self`.
425+
426+
EXAMPLES::
427+
428+
sage: x = ntl.ZZ_p(8, 18)
429+
sage: x.lift_centered()
430+
8
431+
sage: type(x.lift())
432+
<type 'sage.libs.ntl.ntl_ZZ.ntl_ZZ'>
433+
sage: x = ntl.ZZ_p(12, 18)
434+
sage: x.lift_centered()
435+
-6
436+
sage: type(x.lift())
437+
<type 'sage.libs.ntl.ntl_ZZ.ntl_ZZ'>
438+
"""
439+
cdef ntl_ZZ r = self.lift()
440+
cdef ntl_ZZ m = self.modulus()
441+
if r*2 > m:
442+
r -= m
443+
return r
444+
417445
def _integer_(self, ZZ=None):
418446
"""
419447
Return a lift of self as a Sage integer.

src/sage/matrix/matrix2.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ cdef class Matrix(matrix1.Matrix):
11741174
d = R(self._pari_().matdet())
11751175
else:
11761176
# Lift to ZZ and compute there.
1177-
d = R(self.apply_map(lambda x : x.centerlift()).det())
1177+
d = R(self.apply_map(lambda x : x.lift_centered()).det())
11781178
self.cache('det', d)
11791179
return d
11801180

src/sage/rings/arith.py

+32
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,38 @@ def rational_reconstruction(a, m, algorithm='fast'):
21842184
else:
21852185
raise ValueError("unknown algorithm")
21862186

2187+
def lift_centered(p):
2188+
r"""
2189+
Compute a representative of the element `p` mod `n` in `(-n/2 , n/2]`
2190+
2191+
INPUT:
2192+
2193+
- ``p`` -- an integer mod `n`
2194+
2195+
OUTPUT:
2196+
2197+
- An integer `r` in `\ZZ` such that `-n/2 < r \leq n/2`
2198+
2199+
For specific classes, check the `p.lift_centered` attribute.
2200+
2201+
EXAMPLES::
2202+
2203+
sage: p = Mod(2,4)
2204+
sage: lift_centered(p)
2205+
2
2206+
sage: p = Mod(3,4)
2207+
sage: lift_centered(p)
2208+
-1
2209+
"""
2210+
try:
2211+
return ZZ(p.lift_centered())
2212+
except(AttributeError):
2213+
pass
2214+
r = p.lift()
2215+
if r*2 > p.modulus():
2216+
r -= p.modulus()
2217+
return ZZ(r)
2218+
21872219
def _rational_reconstruction_python(a,m):
21882220
"""
21892221
Internal fallback function for rational_reconstruction; see

src/sage/rings/finite_rings/integer_mod.pyx

+9-9
Original file line numberDiff line numberDiff line change
@@ -723,28 +723,28 @@ cdef class IntegerMod_abstract(FiniteRingElement):
723723
"""
724724
return self
725725

726-
def centerlift(self):
726+
def lift_centered(self):
727727
r"""
728728
Lift ``self`` to an integer `i` such that `n/2 < i <= n/2`
729729
(where `n` denotes the modulus).
730730
731731
EXAMPLES::
732732
733-
sage: Mod(0,5).centerlift()
733+
sage: Mod(0,5).lift_centered()
734734
0
735-
sage: Mod(1,5).centerlift()
735+
sage: Mod(1,5).lift_centered()
736736
1
737-
sage: Mod(2,5).centerlift()
737+
sage: Mod(2,5).lift_centered()
738738
2
739-
sage: Mod(3,5).centerlift()
739+
sage: Mod(3,5).lift_centered()
740740
-2
741-
sage: Mod(4,5).centerlift()
741+
sage: Mod(4,5).lift_centered()
742742
-1
743-
sage: Mod(50,100).centerlift()
743+
sage: Mod(50,100).lift_centered()
744744
50
745-
sage: Mod(51,100).centerlift()
745+
sage: Mod(51,100).lift_centered()
746746
-49
747-
sage: Mod(-1,3^100).centerlift()
747+
sage: Mod(-1,3^100).lift_centered()
748748
-1
749749
"""
750750
n = self.modulus()

0 commit comments

Comments
 (0)