ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 14 Jan 2020 21:34:19 +0100Generating and evaluating a polynomialhttps://ask.sagemath.org/question/49523/generating-and-evaluating-a-polynomial/I woud like to create a polynomial P(x) = 1 + x + x^2 + ...... + x^50
and evaluate it for different values of x.
What would be a good way to do this without entering it by hand.
Thank you!
I am very new to Sage. I would like to create a polynomial and evaluate it for a certain x say x=1.00001
The polynomial is:
P(x) = 1 + x + x^2 + ..... + x^50
Thank you for the help!
Mon, 13 Jan 2020 01:47:57 +0100https://ask.sagemath.org/question/49523/generating-and-evaluating-a-polynomial/Comment by Emmanuel Charpentier for <p>I woud like to create a polynomial P(x) = 1 + x + x^2 + ...... + x^50
and evaluate it for different values of x.</p>
<p>What would be a good way to do this without entering it by hand.</p>
<p>Thank you! </p>
<p>I am very new to Sage. I would like to create a polynomial and evaluate it for a certain x say x=1.00001 </p>
<p>The polynomial is:
P(x) = 1 + x + x^2 + ..... + x^50</p>
<p>Thank you for the help!</p>
https://ask.sagemath.org/question/49523/generating-and-evaluating-a-polynomial/?comment=49532#post-id-49532Homework ?Mon, 13 Jan 2020 14:34:00 +0100https://ask.sagemath.org/question/49523/generating-and-evaluating-a-polynomial/?comment=49532#post-id-49532Answer by Emmanuel Charpentier for <p>I woud like to create a polynomial P(x) = 1 + x + x^2 + ...... + x^50
and evaluate it for different values of x.</p>
<p>What would be a good way to do this without entering it by hand.</p>
<p>Thank you! </p>
<p>I am very new to Sage. I would like to create a polynomial and evaluate it for a certain x say x=1.00001 </p>
<p>The polynomial is:
P(x) = 1 + x + x^2 + ..... + x^50</p>
<p>Thank you for the help!</p>
https://ask.sagemath.org/question/49523/generating-and-evaluating-a-polynomial/?answer=49552#post-id-49552Okay. *Assuming* this is not homework, you have at least two ways:
- Symbolically:
This uses explicitly the symbolic `sum` function, this might be handy in situations where you want to keeep the algorithm explicit (e. g. the upper pound is a parameter).
sage: var("j, k", domain="integer")
(j, k)
sage: P(k)=sum(x^j,j,0,k); P
k |--> (x^(k + 1) - 1)/(x - 1)
Surprise : Sage has done an interesting simplification to your input...
sage: P1=P(50); P1
(x^51 - 1)/(x - 1)
sage: P1(x=1.00001)
51.0127520827452
- Pythonically:
If you care only about the (algebraically pristine) result:
sage: P2=sum([x^u for u in (0..50)])
sage: P2
x^50 + x^49 + x^48 + x^47 + x^46 + x^45 + x^44 + x^43 + x^42 + x^41 + x^40 + x^39 + x^38 + x^37 + x^36 + x^35 + x^34 + x^33 + x^32 + x^31 + x^30 + x^29 + x^28 + x^27 + x^26 + x^25 + x^24 + x^23 + x^22 + x^21 + x^20 + x^19 + x^18 + x^17 + x^16 + x^15 + x^14 + x^13 + x^12 + x^11 + x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
No nice surprise here...
sage: P2(x=1.00001)
51.0127520827500
Note that this floating-point approximation is different from the one obtained on the "simplified" formula above (the firs one involves less computations, hence less roundings).
But you can get an *exact* result:
sage: P1(x=1+1/100000)
510127520827499234924010617814679347440131881460455152630970806236978089841760065377401299770935099052810457413019360896761286548194142590173663162246727753486333322365757658529375175723866161447474229318717642077528009483490624990020825012750005100001/10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
sage: P1(x=1+1/100000).n()
51.0127520827499
which is again slightly different of the 17-digits numerical approximation given by default by the use of floating-point quantities...
Unless you have serious reasons to do otherwise, you should try to :
- take advantage of (valid) algebraic simplifications, and
- keep your calculations exact, approximating only to get manageable expressions in writing...
HTH,Tue, 14 Jan 2020 21:34:19 +0100https://ask.sagemath.org/question/49523/generating-and-evaluating-a-polynomial/?answer=49552#post-id-49552