Ask Your Question

chaesloc2's profile - activity

2024-02-27 08:31:15 +0100 received badge  Famous Question (source)
2023-06-30 19:25:58 +0100 received badge  Popular Question (source)
2022-02-26 21:23:43 +0100 received badge  Famous Question (source)
2020-06-10 17:01:10 +0100 received badge  Famous Question (source)
2018-03-18 18:47:55 +0100 received badge  Notable Question (source)
2017-07-07 10:24:08 +0100 received badge  Famous Question (source)
2016-07-17 03:58:16 +0100 received badge  Popular Question (source)
2016-07-17 03:58:16 +0100 received badge  Famous Question (source)
2016-07-17 03:58:16 +0100 received badge  Notable Question (source)
2016-07-01 01:03:53 +0100 received badge  Popular Question (source)
2016-07-01 01:03:53 +0100 received badge  Famous Question (source)
2016-07-01 01:03:53 +0100 received badge  Notable Question (source)
2016-05-24 23:00:58 +0100 received badge  Famous Question (source)
2016-05-23 19:45:25 +0100 received badge  Notable Question (source)
2015-11-06 10:16:50 +0100 received badge  Popular Question (source)
2014-06-29 20:45:45 +0100 received badge  Popular Question (source)
2014-06-29 20:45:45 +0100 received badge  Famous Question (source)
2014-06-29 20:45:45 +0100 received badge  Notable Question (source)
2014-06-29 20:44:29 +0100 received badge  Notable Question (source)
2014-06-29 20:44:29 +0100 received badge  Popular Question (source)
2014-06-29 03:15:13 +0100 marked best answer Distinguish between alarm() and manual interrupt

I have a long computation that needs to be repeated many times with different input. I want to limit each repetition to, say, a minute. Using alarm means that if I try to manually interrupt the whole thing, it will just assume that one repetition timed out. Is there a solution that doesn't involve restarting the worksheet?

while True:
    alarm(60)
    try:
        do_long_computation(random())
    except KeyboardInterrupt:
        continue
    alarm(0)
2014-06-11 06:59:39 +0100 received badge  Notable Question (source)
2014-01-19 19:03:13 +0100 received badge  Popular Question (source)
2013-11-16 07:11:14 +0100 received badge  Famous Question (source)
2013-11-06 05:54:45 +0100 received badge  Notable Question (source)
2013-09-09 12:08:10 +0100 received badge  Notable Question (source)
2013-07-02 07:19:40 +0100 received badge  Notable Question (source)
2013-06-14 09:15:30 +0100 received badge  Popular Question (source)
2013-03-29 13:36:14 +0100 received badge  Popular Question (source)
2013-02-11 10:07:21 +0100 received badge  Popular Question (source)
2013-01-31 00:35:05 +0100 received badge  Popular Question (source)
2012-07-19 16:58:35 +0100 received badge  Good Answer (source)
2012-06-19 15:35:54 +0100 asked a question Best way to animate an empty chessboard with changing square colors

I have an n by n chessboard (without any pieces on it) and a rule that changes the colors of some of the squares. I want an animation showing the chessboard after each change.

My current way is to generate each frame using "list_plot([list of red squares],color=red)+list_plot([list of blue squares],color=blue)+...", then "animate([list of frames])". Example output: http://www.4shared.com/photo/JcjFymIV...

Issues with that method: - animating a list of plots takes ages - the squares are shown as discs

Can anyone recommend a better way?

2012-05-09 20:58:58 +0100 commented answer Comparing Sage and Mathematica for an Introductory Course

@jaia I wrote an ad-hoc javascript solution for the delay a while ago - see my answer to http://ask.sagemath.org/question/517/fast-show-for-cached-plots-for-interact . It isn't nearly good enough for distribution, but assuming you don't care about generality, it shouldn't take too much fiddling to make it do what you want.

2012-05-01 13:25:41 +0100 received badge  Nice Question (source)
2012-05-01 08:02:17 +0100 asked a question Notebook bug

Start a slow calculation. E.g. sleep(20). While it is running, go to another cell and try using autocomplete. E.g. type "Graph." and press tab. Ignore the fact that autocomplete is not working and type lots of code in that cell. When the slow calculation finishes, sage remembers the autocomplete request and restores the cell to its then state, erasing your new code. At least that is what happens on my computer.

2012-05-01 07:56:33 +0100 asked a question Make sage not recreate plot files for sagetex

When running sage on a .sagetex.sage file, it spends ages on "Initializing plots directory\n Plot 0". Most of the time I haven't changed the plots. How do I tell sage(tex) to use the files generated last time?

2012-05-01 07:50:38 +0100 commented question A question on symbolic Matrices - unexpected Decimals in algebraic entry

I don't have time to test this, but my guess is that mag.norm() returns something irrational, and every calculation involving it automatically converts to a real number, even if the irrational bit cancels later. In other words, you would expect 1.5/1.5 to be 1, but it is actually 1.0. And I wouldn't expect conversion to SR to convert reals to rationals; SR should support reals. But I am only guessing.

2012-05-01 07:39:23 +0100 commented question Resuming a running process in Sage

I suspect (but don't know) that you will need to write your own vertex_coloring function. Copy the source of the built-in function and modify it so that it saves whatever it has calculated every now and then.

2012-05-01 07:39:23 +0100 received badge  Commentator
2012-05-01 07:32:22 +0100 commented answer Notebook delete revision history

You are right, it doesn't work.

2012-04-29 18:34:34 +0100 asked a question Notebook delete revision history

How do I delete the revision history of a worksheet? I know I could copy the contents in text mode to a new worksheet; I am asking for a user-friendly way.

2012-04-29 14:03:35 +0100 received badge  Nice Answer (source)
2012-04-29 06:27:29 +0100 answered a question How do I find the subsets of given Set?

You don't have a set to begin with. CartesianProduct returns a special object. That object automatically converts to a list of lists when you pass it to Subsets. Then Subsets goes through that list of lists, and gets confused by the lists inside - it cannot make subsets out of them, because lists are not hashable. One way around is to use tuples instead of lists:

def Z(m,n):
    return CartesianProduct(IntegerRange(m),IntegerRange(n))
print Subsets(map(tuple,Z(2,2)))
2012-04-23 20:20:13 +0100 marked best answer Distinguish between alarm() and manual interrupt

Updated: modern Sage raises an AlarmInterrupt instead of a KeyboardInterrupt, so you can catch it explicitly instead. That's a much cleaner solution than having to dig around inside a KeyboardInterrupt message.

Also, it's probably a better idea to use cancel_alarm() than alarm(0), which was apparently never officially supported.


Sure. While I would have made it a little more explicit, the KeyboardInterrupt generated by alarm does contain information that a typical KeyboardInterrupt doesn't, in the .args and .message attributes. For example, control-C in the following

try:
    sleep(5)
except KeyboardInterrupt as KI:
    print 'args=',KI.args
    print 'message=', KI.message

produces

^Cargs= ()
message=

but adding alarm(2) before executing it produces:

args= ('computation timed out because alarm was set for 2 seconds',)
message= computation timed out because alarm was set for 2 seconds

So one way to take advantage of this would be through something like this:

while True:
    alarm(3)
    try:
        print 'sleeping..'
        sleep(5)
    except KeyboardInterrupt as KI:
        if KI.message.startswith("computation timed out"):
            print 'timed out!'
            continue
        else:
            # not a timeout, so break
            print 'manual exit!'
            alarm(0)
            break
    alarm(0)

which gives

sleeping..
timed out!
sleeping..
timed out!
sleeping..
timed out!
sleeping..
^Cmanual exit!

I tend to wrap this stuff up in decorators, but in this case mine is nested three deep so it's probably overkill for the problem, because it makes it look more complicated than it really is.