Ask Your Question

How to assign matrix (vector) value to variable

asked 2013-10-08 14:14:44 -0500

chenming gravatar image

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!

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2013-10-08 15:21:40 -0500

kcrisman gravatar image

In programs like Numpy or R, this would be very reasonable behavior to expect. Those are data analysis tools.


> sin(c(1,2,3,4))
[1]  0.8414710  0.9092974  0.1411200 -0.7568025


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]
edit flag offensive delete link more


Thanks! this does the trick for me!

chenming gravatar imagechenming ( 2013-10-08 19:20:56 -0500 )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


Asked: 2013-10-08 14:14:44 -0500

Seen: 251 times

Last updated: Oct 08 '13