Ask Your Question
0

Python's Fraction Incompatibility ?

asked 2017-11-22 03:12:39 -0600

candide gravatar image

updated 2017-11-22 06:30:17 -0600

tmonteil gravatar image

Hi

Sagemath doesn't seem to recognize Fraction from the standard module fractions :

$ sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 8.0, Release Date: 2017-07-21                     │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: from fractions import Fraction 
sage: Fraction
<class 'fractions.Fraction'>
sage: Fraction(2,3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-9755b3988eff> in <module>()
----> 1 Fraction(Integer(2),Integer(3))

 /sage8.0/SageMath/local/lib/python2.7/fractions.pyc in __new__(cls, numerator, denominator)
    152             isinstance(denominator, Rational)):
    153             numerator, denominator = (
--> 154                 numerator.numerator * denominator.denominator,
    155                 denominator.numerator * numerator.denominator
    156                 )

TypeError: unsupported operand type(s) for *: 'builtin_function_or_method' and 'builtin_function_or_method'

Any workaround ?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2017-11-22 03:32:04 -0600

tmonteil gravatar image

updated 2017-11-22 06:30:03 -0600

This is because 2 and 3 are preparsed by Sage to give Sage integers, not Python ints, and Fraction doe not know how to deal with them.

Here is the action of Sage preparser on your command:

sage: preparse("Fraction(2,3)")
'Fraction(Integer(2),Integer(3))'

If you want to disable it, you can do:

sage: Fraction(2r,3r)
Fraction(2, 3)

Indeed:

sage: preparse("Fraction(2r,3r)")
'Fraction(2,3)'

You can also transform the Sage integers back into Python ints:

sage: Fraction(int(2),int(3))
Fraction(2, 3)
sage: preparse("Fraction(int(2),int(3))")
'Fraction(int(Integer(2)),int(Integer(3)))'

EDIT

If you want to use Sage, you should use Sage rationals, which have much more features than Python Fractions, for this just write:

sage: 2/3
2/3

If you want to do a lot of manipulations with Python Fraction, you can turn Sage preparser off:

sage: preparser(False)
sage: Fraction(2,3)
Fraction(2, 3)
edit flag offensive delete link more

Comments

Thanks for your answer. The problem with f=Fraction(int(2), int(3)) is that now some operations are broken, for instance 1*f or f*f. On the other hand, referring to the traceback, I can't figure out why f=Fraction(2,3) causes isinstance(numerator, Rational) to be True since Rational represents an internal Python classe and numerator a Sage int.

candide gravatar imagecandide ( 2017-11-22 05:47:36 -0600 )edit

f*f should work. As for 1*f, this is the same issue as i explained, you could replace it with 1r*f or int(1)*f.

tmonteil gravatar imagetmonteil ( 2017-11-22 06:27:02 -0600 )edit

Indeed Python integers provide these numerator and denominator attributes

>>> a = 2
>>> a.numerator
2
>>> a.denominator
1

And this is what Fraction is expecting as behavior... perhaps Sage integers should implement these as fake attributes?

vdelecroix gravatar imagevdelecroix ( 2017-11-23 16:46:37 -0600 )edit

And note that the other way around works just fine

sage: from fractions import Fraction
sage: Fraction(2r, 3r)
Fraction(2, 3)
sage: QQ(Fraction(2r, 3r))
2/3
vdelecroix gravatar imagevdelecroix ( 2017-11-23 16:47:28 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-11-22 03:12:39 -0600

Seen: 37 times

Last updated: Nov 22