# Coerce actions¶

Coerce actions

class sage.structure.coerce_actions.ActOnAction

Class for actions defined via the _act_on_ method.

class sage.structure.coerce_actions.ActedUponAction

Class for actions defined via the _acted_upon_ method.

class sage.structure.coerce_actions.GenericAction

TESTS:

sage: sage.structure.coerce_actions.ActedUponAction(MatrixSpace(ZZ, 2), Cusps, True)
Left action by Full MatrixSpace of 2 by 2 dense matrices over Integer Ring on Set P^1(QQ) of all cusps

sage: sage.structure.coerce_actions.GenericAction(QQ, Zmod(6), True)
Traceback (most recent call last):
...
NotImplementedError: Action not implemented.


This will break if we tried to use it:

sage: sage.structure.coerce_actions.GenericAction(QQ, Zmod(6), True, check=False)
Left action by Rational Field on Ring of integers modulo 6

codomain()

Returns the “codomain” of this action, i.e. the Parent in which the result elements live. Typically, this should be the same as the acted upon set.

EXAMPLES:

sage: A = sage.structure.coerce_actions.ActedUponAction(MatrixSpace(ZZ, 2), Cusps, True)
sage: A.codomain()
Set P^1(QQ) of all cusps
sage: A = sage.structure.coerce_actions.ActOnAction(SymmetricGroup(3), QQ['x,y,z'], False)
sage: A.codomain()
Multivariate Polynomial Ring in x, y, z over Rational Field

class sage.structure.coerce_actions.IntegerMulAction

This class implements the action $$n \cdot a = a + a + \cdots + a$$ via repeated doubling.

Both addition and negation must be defined on the set $$M$$.

INPUT:

• An integer ring, ZZ
• A ZZ module M
• Optional: An element m of M

EXAMPLES:

sage: from sage.structure.coerce_actions import IntegerMulAction
sage: R.<x> = QQ['x']
sage: act = IntegerMulAction(ZZ, R)
sage: act(5, x)
5*x
sage: act(0, x)
0
sage: act(-3, x-1)
-3*x + 3

class sage.structure.coerce_actions.LAction
class sage.structure.coerce_actions.LeftModuleAction

This creates an action of an element of a module by an element of its base ring. The simplest example to keep in mind is R acting on the polynomial ring R[x].

EXAMPLES:

sage: from sage.structure.coerce_actions import LeftModuleAction
sage: LeftModuleAction(ZZ, ZZ['x'])
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Integer Ring
sage: LeftModuleAction(ZZ, QQ['x'])
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Rational Field
sage: LeftModuleAction(QQ, ZZ['x'])
Left scalar multiplication by Rational Field on Univariate Polynomial Ring in x over Integer Ring
sage: LeftModuleAction(QQ, ZZ['x']['y'])
Left scalar multiplication by Rational Field on Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Integer Ring


The following tests against a problem that was relevant during work on trac ticket #9944:

sage: R.<x> = PolynomialRing(ZZ)
sage: S.<x> = PolynomialRing(ZZ, sparse=True)
sage: 1/R.0
1/x
sage: 1/S.0
1/x

class sage.structure.coerce_actions.ModuleAction

Module action.

This is an abstract class, one must actually instantiate a LeftModuleAction or a RightModuleAction.

INPUT:

• G – the actor, an instance of Parent.
• S – the object that is acted upon.
• g – optional, an element of G.
• a – optional, an element of S.
• check – if True (default), then there will be no consistency tests performed on sample elements.

NOTE:

By default, the sample elements of S and G are obtained from an_element(), which relies on the implementation of an _an_element_() method. This is not always awailable. But usually, the action is only needed when one already has two elements. Hence, by trac ticket #14249, the coercion model will pass these two elements the the ModuleAction constructor.

The actual action is implemented by the _rmul_ or _lmul_ function on its elements. We must, however, be very particular about what we feed into these functions, because they operate under the assumption that the inputs lie exactly in the base ring and may segfault otherwise. Thus we handle all possible base extensions manually here.

codomain()

The codomain of self, which may or may not be equal to the domain.

EXAMPLES:

sage: from sage.structure.coerce_actions import LeftModuleAction
sage: A = LeftModuleAction(QQ, ZZ['x,y,z'])
sage: A.codomain()
Multivariate Polynomial Ring in x, y, z over Rational Field

domain()

The domain of self, which is the module that is being acted on.

EXAMPLES:

sage: from sage.structure.coerce_actions import LeftModuleAction
sage: A = LeftModuleAction(QQ, ZZ['x,y,z'])
sage: A.domain()
Multivariate Polynomial Ring in x, y, z over Integer Ring

class sage.structure.coerce_actions.PyScalarAction
class sage.structure.coerce_actions.RAction
class sage.structure.coerce_actions.RightModuleAction

This creates an action of an element of a module by an element of its base ring. The simplest example to keep in mind is R acting on the polynomial ring R[x].

EXAMPLES:

sage: from sage.structure.coerce_actions import LeftModuleAction
sage: LeftModuleAction(ZZ, ZZ['x'])
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Integer Ring
sage: LeftModuleAction(ZZ, QQ['x'])
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Rational Field
sage: LeftModuleAction(QQ, ZZ['x'])
Left scalar multiplication by Rational Field on Univariate Polynomial Ring in x over Integer Ring
sage: LeftModuleAction(QQ, ZZ['x']['y'])
Left scalar multiplication by Rational Field on Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Integer Ring


The following tests against a problem that was relevant during work on trac ticket #9944:

sage: R.<x> = PolynomialRing(ZZ)
sage: S.<x> = PolynomialRing(ZZ, sparse=True)
sage: 1/R.0
1/x
sage: 1/S.0
1/x

is_inplace
sage.structure.coerce_actions.detect_element_action(X, Y, X_on_left, X_el=None, Y_el=None)

Returns an action of X on Y or Y on X as defined by elements X, if any.

EXAMPLES:

sage: from sage.structure.coerce_actions import detect_element_action
sage: detect_element_action(ZZ['x'], ZZ, False)
Left scalar multiplication by Integer Ring on Univariate Polynomial Ring in x over Integer Ring
sage: detect_element_action(ZZ['x'], QQ, True)
Right scalar multiplication by Rational Field on Univariate Polynomial Ring in x over Integer Ring
sage: detect_element_action(Cusps, MatrixSpace(ZZ, 2), False)
Left action by Full MatrixSpace of 2 by 2 dense matrices over Integer Ring on Set P^1(QQ) of all cusps
sage: detect_element_action(Cusps, MatrixSpace(ZZ, 2), True),
(None,)
sage: detect_element_action(ZZ, QQ, True),
(None,)


TESTS:

This test checks that the issue in trac ticket #7718 has been fixed:

sage: class MyParent(Parent):
....:     def an_element(self):
....:         pass
....:
sage: A = MyParent()
sage: detect_element_action(A, ZZ, True)
Traceback (most recent call last):
...
RuntimeError: an_element() for <class '__main__.MyParent'> returned None

sage.structure.coerce_actions.parent(x)

#### Previous topic

The Coercion Model

Coerce maps