Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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
sage: P2(x=1+1/100000)

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,

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
sage: P2(x=1+1/100000)

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

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,

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
sage: P2(x=1+1/100000)

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,