Skip to content

Commit 4cf1dee

Browse files
committed
Minor polish
1 parent 6a26f54 commit 4cf1dee

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/sage/libs/gmp/mpz.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from libc.stdio cimport FILE
66

77
from libc.stdint cimport intmax_t, uintmax_t
88

9-
cdef extern from "gmp.h":
9+
cdef extern from "gmp.h" nogil:
1010

1111
### Integer Functions ###
1212

src/sage/rings/integer.pyx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,18 +3022,25 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
30223022
raise ArithmeticError("self must be nonzero")
30233023
if not isinstance(m, Integer):
30243024
m = Integer(m)
3025-
if not m:
3025+
cdef Integer m_ = <Integer> m
3026+
if m_.is_zero():
30263027
return one
3028+
if m_.is_unit():
3029+
return self
30273030

30283031
cdef mpz_t mm, n
30293032
mpz_init(mm)
3030-
mpz_init(n)
3031-
mpz_set(n, self.value)
3032-
mpz_set(mm, (<Integer>m).value)
30333033
sig_on()
3034-
while mpz_cmp_ui(mm, 1):
3035-
mpz_gcd(mm, n, mm)
3036-
mpz_divexact(n, n, mm)
3034+
mpz_gcd(mm, self.value, m_.value)
3035+
if mpz_cmp_ui(mm, 1) == 0:
3036+
sig_off()
3037+
return self
3038+
else:
3039+
mpz_init(n)
3040+
mpz_divexact(n, self.value, mm)
3041+
while mpz_cmp_ui(mm, 1):
3042+
mpz_gcd(mm, n, mm)
3043+
mpz_divexact(n, n, mm)
30373044
sig_off()
30383045
mpz_clear(mm)
30393046
return move_integer_from_mpz(n)
@@ -3352,6 +3359,8 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
33523359
sage: abs(z) == abs(1)
33533360
True
33543361
"""
3362+
if mpz_sgn(self.value) >= 0:
3363+
return self
33553364
cdef Integer x = PY_NEW(Integer)
33563365
mpz_abs(x.value, self.value)
33573366
return x

0 commit comments

Comments
 (0)