Skip to content

Commit e3d5feb

Browse files
committed
Minor optimization
1 parent dbcb879 commit e3d5feb

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/sage/rings/integer.pyx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,9 +1782,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
17821782
cdef Integer temp
17831783

17841784
if mpz_sgn(self.value) == 0:
1785-
temp = PY_NEW(Integer)
1786-
mpz_set_ui(temp.value, 0)
1787-
return temp
1785+
return self
17881786

17891787
if mpz_sgn(self.value) > 0:
17901788
temp = self.exact_log(base)
@@ -1816,7 +1814,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
18161814
mpz_add(x.value, (<Integer>left).value, (<Integer>right).value)
18171815
return x
18181816
elif type(right) is Rational:
1819-
y = <Rational> Rational.__new__(Rational)
1817+
y = <Rational>PY_NEW(Rational)
18201818
mpq_add_z(y.value, (<Rational>right).value, (<Integer>left).value)
18211819
return y
18221820

@@ -1899,7 +1897,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
18991897
mpz_sub(x.value, (<Integer>left).value, (<Integer>right).value)
19001898
return x
19011899
elif type(right) is Rational:
1902-
y = <Rational> Rational.__new__(Rational)
1900+
y = <Rational>PY_NEW(Rational)
19031901
mpz_mul(mpq_numref(y.value), (<Integer>left).value,
19041902
mpq_denref((<Rational>right).value))
19051903
mpz_sub(mpq_numref(y.value), mpq_numref(y.value),
@@ -2011,7 +2009,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
20112009
mpz_mul(x.value, (<Integer>left).value, (<Integer>right).value)
20122010
return x
20132011
elif type(right) is Rational:
2014-
y = <Rational> Rational.__new__(Rational)
2012+
y = <Rational>PY_NEW(Rational)
20152013
mpq_mul_z(y.value, (<Rational>right).value, (<Integer>left).value)
20162014
return y
20172015

@@ -2074,14 +2072,14 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
20742072
if type(left) is type(right):
20752073
if mpz_sgn((<Integer>right).value) == 0:
20762074
raise ZeroDivisionError("rational division by zero")
2077-
x = <Rational> Rational.__new__(Rational)
2075+
x = <Rational>PY_NEW(Rational)
20782076
mpq_div_zz(x.value, (<Integer>left).value, (<Integer>right).value)
20792077
return x
20802078
elif type(right) is Rational:
20812079
if mpq_sgn((<Rational>right).value) == 0:
20822080
raise ZeroDivisionError("rational division by zero")
20832081
# left * den(right) / num(right)
2084-
y = <Rational> Rational.__new__(Rational)
2082+
y = <Rational>PY_NEW(Rational)
20852083
mpq_div_zz(y.value, (<Integer>left).value,
20862084
mpq_numref((<Rational>right).value))
20872085
mpz_mul(mpq_numref(y.value), mpq_numref(y.value),
@@ -2103,7 +2101,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
21032101
"""
21042102
if mpz_sgn((<Integer>right).value) == 0:
21052103
raise ZeroDivisionError("rational division by zero")
2106-
x = <Rational> Rational.__new__(Rational)
2104+
x = <Rational>PY_NEW(Rational)
21072105
mpq_div_zz(x.value, self.value, (<Integer>right).value)
21082106
return x
21092107

@@ -2346,7 +2344,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
23462344
else:
23472345
if mpz_sgn(self.value) == 0:
23482346
raise ZeroDivisionError("rational division by zero")
2349-
q = Rational.__new__(Rational)
2347+
q = <Rational>PY_NEW(Rational)
23502348
sig_on()
23512349
mpz_pow_ui(mpq_denref(q.value), self.value, -n)
23522350
if mpz_sgn(mpq_denref(q.value)) > 0:
@@ -3620,7 +3618,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
36203618
ZeroDivisionError: rational reconstruction with zero modulus
36213619
"""
36223620
cdef Integer a
3623-
cdef Rational x = <Rational>Rational.__new__(Rational)
3621+
cdef Rational x = <Rational>PY_NEW(Rational)
36243622
try:
36253623
mpq_rational_reconstruction(x.value, self.value, m.value)
36263624
except ValueError:
@@ -6924,8 +6922,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
69246922
"""
69256923
if mpz_sgn(self.value) == 0:
69266924
raise ZeroDivisionError("rational division by zero")
6927-
cdef Rational x
6928-
x = <Rational> Rational.__new__(Rational)
6925+
cdef Rational x = <Rational>PY_NEW(Rational)
69296926
mpz_set_ui(mpq_numref(x.value), 1)
69306927
mpz_set(mpq_denref(x.value), self.value)
69316928
if mpz_sgn(self.value) == -1:

0 commit comments

Comments
 (0)