Ask Your Question

Matrix-scalar and vector-scalar operations

asked 2020-12-22 14:02:35 -0600

Emmanuel Charpentier gravatar image

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

/usr/local/sage-9/local/lib/python3.9/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__add__ (build/cythonized/sage/structure/element.c:10988)()
   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)
   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)
   1252     cpdef canonical_coercion(self, x, y):

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


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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-12-22 15:14:10 -0600

rburing gravatar image

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)
sage: cm = sage.structure.element.get_coercion_model()
sage: cm.explain(B, x, operator.add)
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)

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.

edit flag offensive delete link more


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

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-12-24 02:17:34 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-12-22 14:02:35 -0600

Seen: 23 times

Last updated: Dec 22 '20