First time here? Check out the FAQ!

Ask Your Question
0

Symbolic to numerical array

asked 7 years ago

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 :)

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 7 years ago

fidbc gravatar image

updated 7 years ago

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.

Preview: (hide)
link
2

answered 7 years ago

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)
Preview: (hide)
link

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: 7 years ago

Seen: 1,080 times

Last updated: Sep 14 '17