Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

"""Implementation of :class:`PythonRationalField` class. """ 

 

from __future__ import print_function, division 

 

from sympy.polys.domains.rationalfield import RationalField 

from sympy.polys.domains.groundtypes import PythonInteger, PythonRational, SymPyRational 

 

from sympy.polys.polyerrors import CoercionFailed 

from sympy.utilities import public 

 

@public 

class PythonRationalField(RationalField): 

"""Rational field based on Python rational number type. """ 

 

dtype = PythonRational 

zero = dtype(0) 

one = dtype(1) 

alias = 'QQ_python' 

 

def __init__(self): 

pass 

 

def get_ring(self): 

"""Returns ring associated with ``self``. """ 

from sympy.polys.domains import PythonIntegerRing 

return PythonIntegerRing() 

 

def to_sympy(self, a): 

"""Convert `a` to a SymPy object. """ 

return SymPyRational(a.numerator, a.denominator) 

 

def from_sympy(self, a): 

"""Convert SymPy's Rational to `dtype`. """ 

if a.is_Rational: 

return PythonRational(a.p, a.q) 

elif a.is_Float: 

from sympy.polys.domains import RR 

p, q = RR.to_rational(a) 

return PythonRational(int(p), int(q)) 

else: 

raise CoercionFailed("expected `Rational` object, got %s" % a) 

 

def from_ZZ_python(K1, a, K0): 

"""Convert a Python `int` object to `dtype`. """ 

return PythonRational(a) 

 

def from_QQ_python(K1, a, K0): 

"""Convert a Python `Fraction` object to `dtype`. """ 

return a 

 

def from_ZZ_gmpy(K1, a, K0): 

"""Convert a GMPY `mpz` object to `dtype`. """ 

return PythonRational(PythonInteger(a)) 

 

def from_QQ_gmpy(K1, a, K0): 

"""Convert a GMPY `mpq` object to `dtype`. """ 

return PythonRational(PythonInteger(a.numer()), 

PythonInteger(a.denom())) 

 

def from_RealField(K1, a, K0): 

"""Convert a mpmath `mpf` object to `dtype`. """ 

p, q = K0.to_rational(a) 

return PythonRational(int(p), int(q)) 

 

def numer(self, a): 

"""Returns numerator of `a`. """ 

return a.numerator 

 

def denom(self, a): 

"""Returns denominator of `a`. """ 

return a.denominator