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.Thu, 14 Sep 2017 12:00:41 -0500Symbolic to numerical arrayhttps://ask.sagemath.org/question/38811/symbolic-to-numerical-array/ 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 :)Wed, 13 Sep 2017 09:56:12 -0500https://ask.sagemath.org/question/38811/symbolic-to-numerical-array/Answer by dan_fulea for <p>I would like to get a number-valued array from a symbolic-valued array, for example:</p>
<p>( sin(1), cos(1), pi/4) to ( sin(1).n() , cos(1).n() , pi/4.n() )</p>
<p>WITHOUT having to convert one component at a time, but directly transforming the array. When I write</p>
<p>( sin(1), cos(1), pi/4 ).n()</p>
<p>I get 'Error'. I would greatly appreciate any help :)</p>
https://ask.sagemath.org/question/38811/symbolic-to-numerical-array/?answer=38818#post-id-38818The answer of [fidbc](https://ask.sagemath.org/users/382/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)
Thu, 14 Sep 2017 12:00:41 -0500https://ask.sagemath.org/question/38811/symbolic-to-numerical-array/?answer=38818#post-id-38818Answer by fidbc for <p>I would like to get a number-valued array from a symbolic-valued array, for example:</p>
<p>( sin(1), cos(1), pi/4) to ( sin(1).n() , cos(1).n() , pi/4.n() )</p>
<p>WITHOUT having to convert one component at a time, but directly transforming the array. When I write</p>
<p>( sin(1), cos(1), pi/4 ).n()</p>
<p>I get 'Error'. I would greatly appreciate any help :)</p>
https://ask.sagemath.org/question/38811/symbolic-to-numerical-array/?answer=38812#post-id-38812Would
[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.Wed, 13 Sep 2017 10:15:33 -0500https://ask.sagemath.org/question/38811/symbolic-to-numerical-array/?answer=38812#post-id-38812