ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 24 Dec 2020 09:17:34 +0100Matrix-scalar and vector-scalar operationshttps://ask.sagemath.org/question/54844/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
/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)
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 ?Tue, 22 Dec 2020 21:02:35 +0100https://ask.sagemath.org/question/54844/matrix-scalar-and-vector-scalar-operations/Answer by rburing for <p>Consider :</p>
<pre><code>sage: b=vector([var("b_{}".format(u)) for u in (1..3)])
sage: B=matrix(b).transpose()*matrix(b)
</code></pre>
<p>Unsurprisingly :</p>
<pre><code>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]
</code></pre>
<p>This is less obvious (but intuitively compatible with associativity) :</p>
<pre><code>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]
</code></pre>
<p>This also is unsurprising :</p>
<pre><code>sage: b*x
(b_1*x, b_2*x, b_3*x)
</code></pre>
<p>But there is nothing "obviously expectable" from this :</p>
<pre><code>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)
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'
</code></pre>
<p>However,</p>
<pre><code>sage: vector([u+x for u in b])
(b_1 + x, b_2 + x, b_3 + x)
</code></pre>
<p>would be a possible candidate, but I have trouble visualizing the consequences in the rest of algebraic computation rules in Sage. Is this documented ?</p>
https://ask.sagemath.org/question/54844/matrix-scalar-and-vector-scalar-operations/?answer=54850#post-id-54850This kind of [coercion](https://doc.sagemath.org/html/en/reference/coercion/index.html) 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()
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)
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.Tue, 22 Dec 2020 22:14:10 +0100https://ask.sagemath.org/question/54844/matrix-scalar-and-vector-scalar-operations/?answer=54850#post-id-54850Comment by Emmanuel Charpentier for <p>This kind of <a href="https://doc.sagemath.org/html/en/reference/coercion/index.html">coercion</a> is documented:</p>
<blockquote>
<p>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).</p>
</blockquote>
<p>Symbolic expressions can be coerced to symbolic matrices of a fixed size:</p>
<pre><code>sage: B.parent().has_coerce_map_from(SR)
True
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
</code></pre>
<p>It is because there is a canonical map, mapping $1$ to the identity matrix, which is a morphism of algebras with basis.</p>
<p>Symbolic expressions cannot be coerced to vectors of a fixed size:</p>
<pre><code>sage: b.parent().has_coerce_map_from(SR)
False
</code></pre>
<p>It is because there is no <em>canonical</em> 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.</p>
https://ask.sagemath.org/question/54844/matrix-scalar-and-vector-scalar-operations/?comment=54881#post-id-54881Thank you ; that's what I was looking for ; I wasn't aware of this documentation.Thu, 24 Dec 2020 09:17:34 +0100https://ask.sagemath.org/question/54844/matrix-scalar-and-vector-scalar-operations/?comment=54881#post-id-54881