Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

DSM gravatar image

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:

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.

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) design decision, and would return True, False, or some Undecided result -- possibly the original expression.

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) (maxima-based, I think) design decision, and would return True, False, or some Undecided result -- possibly the original expression.

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.