Ask Your Question
0

Symbolic to numerical array

asked 2017-09-13 09:56:12 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I would like to get a number-valued array from a symbolic-valued array, for example:

( sin(1), cos(1), pi/4) to ( sin(1).n() , cos(1).n() , pi/4.n() )

WITHOUT having to convert one component at a time, but directly transforming the array. When I write

( sin(1), cos(1), pi/4 ).n()

I get 'Error'. I would greatly appreciate any help :)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2017-09-14 12:00:41 -0600

dan_fulea gravatar image

The answer of fidbc using list comprehension is all needed. This is a python native solution.

But just to see how versatile sage is, we can use a tacit conversion of the given tuple to a vector over a declared field. (We use the constructor for this class, which is vector.) The field may be RR, reals with default precision, or RealField(100) if we want to set our precision. For the purist, if we really need a tuple back, there is also a conversion back to tuple...

So the one-liner would be tuple( vector( RR, ( sin(1), cos(1), pi/4 ) ) ) . (With or without the conversion to tuple.

Sample code following the same idea:

sage: t = ( sin(1), cos(1), pi/4 ) 
sage: vector( RR, t )
(0.841470984807897, 0.540302305868140, 0.785398163397448)
sage: type( _ )
<type 'sage.modules.free_module_element.FreeModuleElement_generic_dense'>
sage: tuple( vector( RR, t ) )
(0.841470984807897, 0.540302305868140, 0.785398163397448)
sage: type( _ )
<type 'tuple'>
sage: IR = RealField( 100 )
sage: tuple( vector( IR, t ) )
(0.84147098480789650665250232163,
 0.54030230586813971740093660744,
 0.78539816339744830961566084582)
edit flag offensive delete link more
2

answered 2017-09-13 10:15:33 -0600

fidbc gravatar image

updated 2017-09-13 10:16:00 -0600

Would

[n(x) for x in ( sin(1), cos(1), pi/4 )]

count as a single command? If L is the list of numbers, say L=( sin(1), cos(1), pi/4), then map(n,L) should do the job.

edit flag offensive delete link more

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

Stats

Asked: 2017-09-13 09:56:12 -0600

Seen: 95 times

Last updated: Sep 14