# Matrix-scalar and vector-scalar operations

Consider :

sage: b=vector([var("b_{}".format(u)) for u in (1..3)])
sage: B=matrix(b).transpose()*matrix(b)


Unsurprisingly :

sage: B*x
[  b_1^2*x b_1*b_2*x b_1*b_3*x]
[b_1*b_2*x   b_2^2*x b_2*b_3*x]
[b_1*b_3*x b_2*b_3*x   b_3^2*x]


This is less obvious (but intuitively compatible with associativity) :

sage: B+x
[b_1^2 + x   b_1*b_2   b_1*b_3]
[  b_1*b_2 b_2^2 + x   b_2*b_3]
[  b_1*b_3   b_2*b_3 b_3^2 + x]


This also is unsurprising :

sage: b*x
(b_1*x, b_2*x, b_3*x)


But there is nothing "obviously expectable" from this :

sage: b+x
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-58bd66da7aa5> in <module>
----> 1 b+x

1230         # Left and right are Sage elements => use coercion model
1231         if BOTH_ARE_ELEMENT(cl):
-> 1232             return coercion_model.bin_op(left, right, add)
1233
1234         cdef long value

/usr/local/sage-9/local/lib/python3.9/site-packages/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel.bin_op (build/cythonized/sage/structure/coerce.c:11708)()
1248         # We should really include the underlying error.
1249         # This causes so much headache.
-> 1250         raise bin_op_exception(op, x, y)
1251
1252     cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for +: 'Vector space of dimension 3 over Symbolic Ring' and 'Symbolic Ring'


However,

sage: vector([u+x for u in b])
(b_1 + x, b_2 + x, b_3 + x)


would be a possible candidate, but I have trouble visualizing the consequences in the rest of algebraic computation rules in Sage. Is this documented ?

edit retag close merge delete

Sort by » oldest newest most voted

This kind of coercion is documented:

On the other hand, Sage has the notion of a coercion, which is a canonical morphism (occasionally up to a conventional choice made by developers) between parents. A coercion from one parent to another must be defined on the whole domain, and always succeeds. As it may be invoked implicitly, it should be obvious and natural (in both the mathematically rigorous and colloquial sense of the word).

Symbolic expressions can be coerced to symbolic matrices of a fixed size:

sage: B.parent().has_coerce_map_from(SR)
True
sage: cm = sage.structure.element.get_coercion_model()
Coercion on right operand via
Coercion map:
From: Symbolic Ring
To:   Full MatrixSpace of 3 by 3 dense matrices over Symbolic Ring
Arithmetic performed after coercions.
Result lives in Full MatrixSpace of 3 by 3 dense matrices over Symbolic Ring
Full MatrixSpace of 3 by 3 dense matrices over Symbolic Ring


It is because there is a canonical map, mapping $1$ to the identity matrix, which is a morphism of algebras with basis.

Symbolic expressions cannot be coerced to vectors of a fixed size:

sage: b.parent().has_coerce_map_from(SR)
False


It is because there is no canonical map which is a morphism of modules with basis. Of course there exist morphisms, like the $1 \mapsto (1,1,1)$ you suggest, but that is no better than $1 \mapsto (1,0,0)$ or $1 \mapsto (0,0,1)$. There is no obvious natural choice, so there is no coercion.

more

Thank you ; that's what I was looking for ; I wasn't aware of this documentation.

( 2020-12-24 09:17:34 +0200 )edit