Ask Your Question
1

Python's Fraction Incompatibility ?

asked 2017-11-22 10:12:39 +0100

candide gravatar image

updated 2017-11-22 13:30:17 +0100

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

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 ( 2019-08-02 19:19:15 +0100 )edit
slelievre gravatar imageslelievre ( 2020-08-16 06:12:26 +0100 )edit

1 Answer

Sort by » oldest newest most voted
3

answered 2017-11-22 10:32:04 +0100

tmonteil gravatar image

updated 2017-11-22 13:30:03 +0100

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 12:47:36 +0100 )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 13:27:02 +0100 )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 23:46:37 +0100 )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 23:47:28 +0100 )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 10:12:39 +0100

Seen: 1,135 times

Last updated: Nov 22 '17