Skip to content

Implement __pow__ in the coercion model #24247

@jdemeyer

Description

@jdemeyer

We implement powering in the coercion model. One important difference between powering and other operators is that the most common use case for powering is powering something to an integer exponent.

To deal with this integer powering, we implement an action IntegerPowAction. This action calls a special method _pow_int() on the element. In other words, x ^ n for an integer n becomes x._pow_int(n). We also provide a default implementation of _pow_int for MonoidElement and RingElement which uses the square-and-multiply algorithm implemented in generic_power().

For backward compatibility reasons, we also call this action for elements of IntegerModRing(m). In the future, we may rethink what to do here, see #15709.

Apart from this, powering behaves like other binary operators: coercion to a common parent is done if no action is defined.

Note that the 3-argument version of pow() is not supported in the coercion model. Only specific types like Integer implement it. See also #5082.

Fixing powering for specific parents is not within the scope of this ticket, except where it was needed to fix doctest failures. For example, we fix various serious bugs in powering for RDF such as:

sage: RDF(0) ^ RDF(-1)
0.0
sage: RDF(-1) ^ RDF(2)
NaN

Depends on #24467

Component: coercion

Author: Jeroen Demeyer

Branch/Commit: 74d6700

Reviewer: Travis Scrimshaw, Vincent Delecroix

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions