Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Can you use a try/except block?

sage: def check_convergence(exp):
....:     try:
....:         print sum(exp,x,1,oo)
....:     except:
....:         print "Oops!"
....:         
sage: check_convergence(1/x^2)
1/6*pi^2
sage: check_convergence(1/x)
#0: simplify_sum(expr='sum(1/x,x,1,inf))
Oops!

I can't seem to get rid of that extra #0 thingie. Maxima sometimes prints out extra stuff now with our ECL library interface.

Can you use a try/except block?

sage: def check_convergence(exp):
....:     try:
....:         print sum(exp,x,1,oo)
....:  except:
....: except ValueError, msg:
        if 'divergent' in str(msg):
            print "Oops!"
....: "Diverges!"
 else:
            raise ValueError, msg

Then

sage: check_convergence(1/x^2)
1/6*pi^2
sage: check_convergence(1/x)
#0: simplify_sum(expr='sum(1/x,x,1,inf))
Oops!

I can't seem to get rid of that extra #0 thingie. Maxima sometimes prints out extra stuff now with our ECL library interface.

Notice also

sage: check_convergence(1/22)
+Infinity

because Maxima doesn't use the word diverge there, but evaluates it to infinity. Strange...