# Symbolic to numerical array 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 close merge delete

Sort by » oldest newest most voted 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.

more

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)

more