Skip to content

Sage Integers are incompatible with Python fraction.Fraction, statistics.mean, itertools.permutations #28234

@sagetrac-mcbell

Description

@sagetrac-mcbell

fraction.Fraction

In Sage 8.8 the code:

sage: from fractions import Fraction
sage: Fraction(1, 1)

raises the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-0c4690dff1ca> in <module>()
----> 1 Fraction(Integer(1), Integer(1))

/Applications/SageMath-8.8.app/Contents/Resources/sage/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'

This appears to be because of the fact that Python ints have numerator and denominator properties whereas for Sage integers these are methods. Therefore Fraction does not call these attributes leading to the crash

Note, turning off the pre-parser or doing:

sage: Fraction(int(1), int(1))
Fraction(1, 1)

works.

One solution to this would be to decorate sage.rings.integer.Integer.numerator() and sage.rings.integer.Integer.denominator() with the @property decorator, but this will likely cause a large number of systems to break.

Earlier report of this issue
in #10928, comment 14 (a ticket about getitem of numpy matrix using Sage integers).

statistics.mean (see #29662)

Although the as_integer_ratio mechanism has been added to Python, ​statistics._exact_ratio does not use as_integer_ratio because it sees the Sage numbers' numerator and denominator methods and tries to use them as properties.

itertools.permutations

┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 9.5.beta7, Release Date: 2021-11-18               │
│ Using Python 3.9.9. Type "help()" for help.                        │
└────────────────────────────────────────────────────────────────────┘
sage: import itertools
sage: list(itertools.permutations(range(3),int(2)))
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
sage: list(itertools.permutations(range(3),2))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-a9f97c1deeca> in <module>
----> 1 list(itertools.permutations(range(Integer(3)),Integer(2)))

TypeError: Expected int as r

see https://bugs.python.org/issue30537#msg295000

CC: @jdemeyer @sagetrac-mcbell @slel @videlec @simon-king-jena @posita @tscrim

Component: basic arithmetic

Keywords: days100

Author: Mark Bell

Branch/Commit: public/28234 @ 4977867

Issue created by migration from https://trac.sagemath.org/ticket/28234

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions