Ask Your Question
2

Any way to timeout?

asked 2011-02-08 10:02:57 -0600

Shu gravatar image

If I run the bool(eq) with the equation as in the following, sometimes it returns False, sometimes it hangs. I was wondering is there anyway to put a timelimit when I call bool(eq).

sage: eq=1/2*((2*e^(2*x) + e^(4*x) + 1)*(e^(2*x) + 1)^(1/2*e^(-x))*2^(1/2*e^x)*e^(1/2*x*e^x)*log(2*e^x/(e^(2*x) + 1)) + (2*e^(2*x) - e^(4*x) - 1)*(e^(2*x) + 1)^(1/2*e^(-x))*2^(1/2*e^x)*e^(1/2*x*e^x))*e^(-1/2*x*e^(-x))/((e^(3*x) + e^x)*(e^(2*x) + 1)^(1/2*e^x)*2^(1/2*e^(-x))) == 1/2*(2*(2*e^(2*x) + e^(4*x) + 1)*e^(x^(-1/2*e^(-x) + 1/2*e^x))*log(2*e^x/(e^(2*x) + 1)) + (2*e^(2*x) - e^(4*x) - 1)*e^(2*x^(-1/2*e^(-x) + 1/2*e^x)) + 2*e^(2*x) - e^(4*x) - 1)/((e^(3*x) + e^x)*e^(2*x^(-1/2*e^(-x) + 1/2*e^x)) + e^(3*x) + e^x)
sage: bool(eq)
False
sage: bool(eq)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2011-02-08 12:24:11 -0600

DSM gravatar image

updated 2014-01-27 10:23:42 -0600

Updated: in modern Sage, alarm raises an AlarmInterrupt, not a KeyboardInterrupt, and it's probably a better idea to use cancel_alarm() instead of alarm(0), which was apparently never offically supported.


Yes, you can use the alarm function and catch the resulting KeyboardInterrupt

sage: alarm(3)
[3 seconds later..]
sage:  
KeyboardInterrupt

Here's an example use:

def long_calculation(sleepfor):
    alarm(3)
    try:
        print 'starting calculation..'
        sleep(sleepfor)
    except KeyboardInterrupt:
        print 'did not complete!'
    # if the computation finished early, though, the alarm is still ticking!
    # so let's turn it off..
    alarm(0) 
sage: long_calculation(6)
starting calculation..
did not complete!
sage: long_calculation(2)
starting calculation..
sage:

But this should simply work, you shouldn't need to play timing games. After interrupting one of the really long calculations, I managed to break an assert:

sage: eq=1/2*((2*e^(2*x) + e^(4*x) + 1)*(e^(2*x) + 1)^(1/2*e^(-x))*2^(1/2*e^x)*e^(1/2*x*e^x)*log(2*e^x/(e^(2*x) + 1)) + (2*e^(2*x) - e^(4*x) - 1)*(e^(2*x) + 1)^(1/2*e^(-x))*2^(1/2*e^x)*e^(1/2*x*e^x))*e^(-1/2*x*e^(-x))/((e^(3*x) + e^x)*(e^(2*x) + 1)^(1/2*e^x)*2^(1/2*e^(-x))) == 1/2*(2*(2*e^(2*x) + e^(4*x) + 1)*e^(x^(-1/2*e^(-x) + 1/2*e^x))*log(2*e^x/(e^(2*x) + 1)) + (2*e^(2*x) - e^(4*x) - 1)*e^(2*x^(-1/2*e^(-x) + 1/2*e^x)) + 2*e^(2*x) - e^(4*x) - 1)/((e^(3*x) + e^x)*e^(2*x^(-1/2*e^(-x) + 1/2*e^x)) + e^(3*x) + e^x)
sage: bool(eq)
get_z.c:37: MPFR assertion failed: (!(((r)->_mpfr_exp) == (((-9223372036854775807L - 1L))+2)) && !(((r)->_mpfr_exp) == (((-9223372036854775807L - 1L))+3)))
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)

so clearly things are getting themselves into a state.

UPDATE: I should also give the usual warning about using "bool(eq)", which is that it doesn't do what you might think it does. bool(eq) == False does not mean that the two sides aren't equal; it could mean only that it couldn't figure out how to prove they were equal. I disagree with this (maxima-based, I think) design decision, and would return True, False, or some Undecided result -- possibly the original expression.

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

Stats

Asked: 2011-02-08 10:02:57 -0600

Seen: 542 times

Last updated: Jan 27 '14