Ask Your Question
0

Generating and evaluating a polynomial

asked 2020-01-12 18:47:57 -0600

updated 2020-01-13 06:00:59 -0600

Iguananaut gravatar image

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!

edit retag flag offensive close merge delete

Comments

Homework ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-01-13 07:34:00 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-01-14 14:34:19 -0600

Emmanuel Charpentier gravatar image

updated 2020-01-14 21:41:41 -0600

Okay. 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,

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: 2020-01-12 18:47:57 -0600

Seen: 67 times

Last updated: Jan 14