Skip to content

Commit

Permalink
Move initialization of zero and one element to RationalField
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Feb 5, 2025
1 parent 3050d63 commit 270c4a1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
21 changes: 5 additions & 16 deletions src/sage/rings/rational.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ from cysignals.signals cimport sig_on, sig_off
import operator
import fractions

from sage.rings.rational_field import Q
from sage.rings.rational_field import QQ

from sage.arith.long cimport integer_check_long_py
from sage.categories.morphism cimport Morphism
Expand Down Expand Up @@ -181,16 +181,6 @@ cdef Rational_sub_(Rational self, Rational other):

return x

cdef Parent the_rational_ring = Q

# make sure zero/one elements are set
cdef set_zero_one_elements():
global the_rational_ring
the_rational_ring._zero_element = Rational(0)
the_rational_ring._one_element = Rational(1)

set_zero_one_elements()

cpdef Integer integer_rational_power(Integer a, Rational b):
"""
Compute `a^b` as an integer, if it is integral, or return ``None``.
Expand Down Expand Up @@ -506,9 +496,8 @@ cdef class Rational(sage.structure.element.FieldElement):
sage: p.parent()
Rational Field
"""
global the_rational_ring
mpq_init(self.value)
self._parent = the_rational_ring
self._parent = QQ

def __init__(self, x=None, unsigned int base=0):
"""
Expand Down Expand Up @@ -4124,10 +4113,10 @@ cdef class Z_to_Q(Morphism):
From: Integer Ring
To: Rational Field
"""
from sage.rings import integer_ring
from sage.rings import rational_field
from sage.rings.integer_ring import ZZ

import sage.categories.homset
Morphism.__init__(self, sage.categories.homset.Hom(integer_ring.ZZ, rational_field.QQ))
Morphism.__init__(self, sage.categories.homset.Hom(ZZ, QQ))

cpdef Element _call_(self, x):
"""
Expand Down
3 changes: 3 additions & 0 deletions src/sage/rings/rational_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ class RationalField(Singleton, number_field_base.NumberField):
sage: QQ(RealField(45)(t))
1/5
"""
_zero_element = Rational(0)
_one_element = Rational(1)

def __new__(cls):
"""
This method actually is not needed for using :class:`RationalField`.
Expand Down

0 comments on commit 270c4a1

Please sign in to comment.