Ask Your Question
1

Python's Fraction Incompatibility ?

asked 7 years ago

candide gravatar image

updated 7 years ago

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 ?

Preview: (hide)

Comments

1

Why do you care about Python fractions? It's a serious question, since Guido van Rossum seems quite negative about them (see https://discuss.python.org/t/pep-3141-ratio-instead-of-numerator-denominator/2037/27?u=jdemeyer (https://discuss.python.org/t/pep-3141...))

jdemeyer gravatar imagejdemeyer ( 5 years ago )

1 Answer

Sort by » oldest newest most voted
3

answered 7 years ago

tmonteil gravatar image

updated 7 years ago

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)
Preview: (hide)
link

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 ( 7 years ago )

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 ( 7 years ago )

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 ( 7 years ago )

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 ( 7 years ago )

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: 7 years ago

Seen: 1,287 times

Last updated: Nov 22 '17