ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 08 Oct 2013 19:20:56 -0500How to assign matrix (vector) value to variablehttps://ask.sagemath.org/question/10599/how-to-assign-matrix-vector-value-to-variable/when I tried to assign a matrix or a vector to variables, something wrong happens:
sage: sin([1,2,3,4]).n()
Traceback (click to the left of this block for traceback)
...
TypeError: cannot coerce arguments: no canonical coercion from <type
'list'> to Symbolic Ring
what I want to achieve is that the out put gives a vector that equals to [sin(1),sin(2),sin(3),sin(4)].
Thanks in advance!Tue, 08 Oct 2013 14:14:44 -0500https://ask.sagemath.org/question/10599/how-to-assign-matrix-vector-value-to-variable/Answer by kcrisman for <p>when I tried to assign a matrix or a vector to variables, something wrong happens:</p>
<pre><code>sage: sin([1,2,3,4]).n()
Traceback (click to the left of this block for traceback)
...
TypeError: cannot coerce arguments: no canonical coercion from <type
'list'> to Symbolic Ring
</code></pre>
<p>what I want to achieve is that the out put gives a vector that equals to [sin(1),sin(2),sin(3),sin(4)]. </p>
<p>Thanks in advance!</p>
https://ask.sagemath.org/question/10599/how-to-assign-matrix-vector-value-to-variable/?answer=15529#post-id-15529In programs like Numpy or R, this would be very reasonable behavior to expect. Those are data analysis tools.
R:
> sin(c(1,2,3,4))
[1] 0.8414710 0.9092974 0.1411200 -0.7568025
Numpy:
sage: import numpy as np
sage: sin(np.array([1,2,3,4]))
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 ])
Sage is mathematical, however. So you will have to explicitly apply the function to all entries. Python makes this easy using list comprehensions or the map function.
sage: [sin(l).n() for l in [1,2,3,4]]
[0.841470984807897, 0.909297426825682, 0.141120008059867, -0.756802495307928]
sage: map(sin,[1,2,3,4])
[sin(1), sin(2), sin(3), sin(4)]
sage: map( lambda x: sin(x).n(), [1,2,3,4])
[0.841470984807897, 0.909297426825682, 0.141120008059867, -0.756802495307928]Tue, 08 Oct 2013 15:21:40 -0500https://ask.sagemath.org/question/10599/how-to-assign-matrix-vector-value-to-variable/?answer=15529#post-id-15529Comment by chenming for <p>In programs like Numpy or R, this would be very reasonable behavior to expect. Those are data analysis tools. </p>
<p>R:</p>
<pre><code>> sin(c(1,2,3,4))
[1] 0.8414710 0.9092974 0.1411200 -0.7568025
</code></pre>
<p>Numpy:</p>
<pre><code>sage: import numpy as np
sage: sin(np.array([1,2,3,4]))
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 ])
</code></pre>
<p>Sage is mathematical, however. So you will have to explicitly apply the function to all entries. Python makes this easy using list comprehensions or the map function.</p>
<pre><code>sage: [sin(l).n() for l in [1,2,3,4]]
[0.841470984807897, 0.909297426825682, 0.141120008059867, -0.756802495307928]
sage: map(sin,[1,2,3,4])
[sin(1), sin(2), sin(3), sin(4)]
sage: map( lambda x: sin(x).n(), [1,2,3,4])
[0.841470984807897, 0.909297426825682, 0.141120008059867, -0.756802495307928]
</code></pre>
https://ask.sagemath.org/question/10599/how-to-assign-matrix-vector-value-to-variable/?comment=16947#post-id-16947Thanks! this does the trick for me!Tue, 08 Oct 2013 19:20:56 -0500https://ask.sagemath.org/question/10599/how-to-assign-matrix-vector-value-to-variable/?comment=16947#post-id-16947