Actions used by the coercion model for matrix and vector multiplications¶
Warning
The class MatrixMulAction
and its descendants extends the class
Action
. As a consequence objects from these classes only keep weak
references to the underlying sets which are acted upon. This decision was
made in Issue #715 in order to allow garbage collection within the coercion
framework, where actions are mainly used, and avoid memory leaks.
To ensure that the underlying set of such an object does not get garbage collected, it is sufficient to explicitly create a strong reference to it before creating the action.
sage: MSQ = MatrixSpace(QQ, 2)
sage: MSZ = MatrixSpace(ZZ['x'], 2)
sage: A = MSQ.get_action(MSZ)
sage: A
Left action by Full MatrixSpace of 2 by 2 dense matrices over Rational Field
on Full MatrixSpace of 2 by 2 dense matrices
over Univariate Polynomial Ring in x over Integer Ring
sage: import gc
sage: _ = gc.collect()
sage: A
Left action by Full MatrixSpace of 2 by 2 dense matrices over Rational Field
on Full MatrixSpace of 2 by 2 dense matrices
over Univariate Polynomial Ring in x over Integer Ring
>>> from sage.all import *
>>> MSQ = MatrixSpace(QQ, Integer(2))
>>> MSZ = MatrixSpace(ZZ['x'], Integer(2))
>>> A = MSQ.get_action(MSZ)
>>> A
Left action by Full MatrixSpace of 2 by 2 dense matrices over Rational Field
on Full MatrixSpace of 2 by 2 dense matrices
over Univariate Polynomial Ring in x over Integer Ring
>>> import gc
>>> _ = gc.collect()
>>> A
Left action by Full MatrixSpace of 2 by 2 dense matrices over Rational Field
on Full MatrixSpace of 2 by 2 dense matrices
over Univariate Polynomial Ring in x over Integer Ring
Note
The MatrixSpace()
function caches the objects it creates. Therefore,
the underlying set MSZ
in the above example will not be garbage
collected, even if it is not strongly ref’ed. Nonetheless, there is no
guarantee that the set that is acted upon will always be cached in such a
way, so that following the above example is good practice.
EXAMPLES:
An action requires a common parent for the base rings, so the following doesn’t work (see Issue #17859):
sage: vector(QQ, [1]) * matrix(Zmod(2), [[1]])
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for *: 'Vector space of
dimension 1 over Rational Field' and 'Full MatrixSpace of 1 by 1
dense matrices over Ring of integers modulo 2'
>>> from sage.all import *
>>> vector(QQ, [Integer(1)]) * matrix(Zmod(Integer(2)), [[Integer(1)]])
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for *: 'Vector space of
dimension 1 over Rational Field' and 'Full MatrixSpace of 1 by 1
dense matrices over Ring of integers modulo 2'
AUTHOR:
Robert Bradshaw (2007-09): Initial version.
- class sage.matrix.action.MatrixMatrixAction[source]¶
Bases:
MatrixMulAction
Action of a matrix on another matrix.
This is always implemented as a left action.
EXAMPLES:
By Issue #715, there only is a weak reference on the underlying set, so that it can be garbage collected if only the action itself is explicitly referred to. Hence, we first assign the involved matrix spaces to a variable:
sage: R.<x> = ZZ[] sage: MSR = MatrixSpace(R, 3, 3) sage: MSQ = MatrixSpace(QQ, 3, 2) sage: from sage.matrix.action import MatrixMatrixAction sage: A = MatrixMatrixAction(MSR, MSQ); A Left action by Full MatrixSpace of 3 by 3 dense matrices over Univariate Polynomial Ring in x over Integer Ring on Full MatrixSpace of 3 by 2 dense matrices over Rational Field sage: A.codomain() Full MatrixSpace of 3 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field sage: A(matrix(R, 3, 3, x), matrix(QQ, 3, 2, range(6))) [ 0 x] [2*x 3*x] [4*x 5*x]
>>> from sage.all import * >>> R = ZZ['x']; (x,) = R._first_ngens(1) >>> MSR = MatrixSpace(R, Integer(3), Integer(3)) >>> MSQ = MatrixSpace(QQ, Integer(3), Integer(2)) >>> from sage.matrix.action import MatrixMatrixAction >>> A = MatrixMatrixAction(MSR, MSQ); A Left action by Full MatrixSpace of 3 by 3 dense matrices over Univariate Polynomial Ring in x over Integer Ring on Full MatrixSpace of 3 by 2 dense matrices over Rational Field >>> A.codomain() Full MatrixSpace of 3 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field >>> A(matrix(R, Integer(3), Integer(3), x), matrix(QQ, Integer(3), Integer(2), range(Integer(6)))) [ 0 x] [2*x 3*x] [4*x 5*x]
Note
The
MatrixSpace()
function caches the object it creates. Therefore, the underlying setMSZ
in the above example will not be garbage collected, even if it is not strongly ref’ed. Nonetheless, there is no guarantee that the set that is acted upon will always be cached in such a way, so that following the above example is good practice.
- class sage.matrix.action.MatrixMulAction[source]¶
Bases:
Action
Abstract base class for a matrix space acting on something.
EXAMPLES:
sage: MSQ = MatrixSpace(QQ, 2) sage: MSZ = MatrixSpace(ZZ['x'], 2) sage: A = MSQ.get_action(MSZ); A Left action by Full MatrixSpace of 2 by 2 dense matrices over Rational Field on Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Integer Ring sage: A.actor() Full MatrixSpace of 2 by 2 dense matrices over Rational Field sage: A.domain() Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Integer Ring sage: A.codomain() Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
>>> from sage.all import * >>> MSQ = MatrixSpace(QQ, Integer(2)) >>> MSZ = MatrixSpace(ZZ['x'], Integer(2)) >>> A = MSQ.get_action(MSZ); A Left action by Full MatrixSpace of 2 by 2 dense matrices over Rational Field on Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Integer Ring >>> A.actor() Full MatrixSpace of 2 by 2 dense matrices over Rational Field >>> A.domain() Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Integer Ring >>> A.codomain() Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
- class sage.matrix.action.MatrixPolymapAction[source]¶
Bases:
MatrixMulAction
Left action of a matrix on a scheme polynomial morphism
- class sage.matrix.action.MatrixSchemePointAction[source]¶
Bases:
MatrixMulAction
Action class for left multiplication of schemes points by matrices.
- class sage.matrix.action.MatrixVectorAction[source]¶
Bases:
MatrixMulAction
Left action of a matrix on a vector
- class sage.matrix.action.PolymapMatrixAction[source]¶
Bases:
MatrixMulAction
Right action of a matrix on a scheme polynomial morphism
- class sage.matrix.action.VectorMatrixAction[source]¶
Bases:
MatrixMulAction
Right action of a matrix on a vector