Ask Your Question

Polonius's profile - activity

2023-08-04 12:29:55 +0200 received badge  Notable Question (source)
2022-03-12 17:24:01 +0200 received badge  Popular Question (source)
2022-03-07 14:15:17 +0200 received badge  Notable Question (source)
2021-03-26 19:22:41 +0200 received badge  Notable Question (source)
2018-08-06 00:43:48 +0200 received badge  Nice Question (source)
2018-08-04 17:44:37 +0200 received badge  Commentator
2018-08-04 17:44:37 +0200 commented answer Words avoiding patterns

Thanks. This module looks very useful. I was searching for this kind of thing under "Combinatorics". There some kind of cross-reference in the documentation.

2018-08-04 17:42:50 +0200 commented answer Words avoiding patterns

Indeed. Many thanks.

2018-08-04 15:30:43 +0200 commented answer Words avoiding patterns

Thanks. Calling words inside of non_rep_words was a mistake. But fixing that still doesn't produce the expected output. Rather, I get this:

sage: list(non_rep_words(['a','b'],1))
[['a', 'b']]
sage: list(non_rep_words(['a','b'],2))
[['a', 'b', 'a']]
sage: list(non_rep_words(['a','b'],3))
[['a', 'b', 'a', 'b']]
2018-08-04 11:05:49 +0200 asked a question Words avoiding patterns

I'm trying to write variations on the "words" generator from The code there is

def words(alphabet,l):
    if l == 0:
        yield []
        for word in words(alphabet, l-1):
           for a in alphabet:
              yield word + [a]

which produces words in "alphabet" of length l. (Parenthetical remark: Where I've written "for a in alphabet", the original has "for l in alphabet", which looks confusing given that l is also the name of the input length.)

I'd like to modify this generator in various ways, for example to produce words in which the same "letter" does not appear twice in a row. What I've tried is the following:

def non_rep_words(alphabet,l):
    if l == 0:
        yield []
    elif l==1:
        yield alphabet
        for word in words(alphabet, l-1):
            for a in alphabet:
                if word[-1] != a:
                    yield w+[a]

The idea is to take a word w of length l-1 and for each element a of the alphabet, test if it agrees with the end of w, and if it doesn't, then tack it on to w.

This seems to work for l=0,1,2, but fails at l=3. Here's what my terminal looks like:

sage: list(non_rep_words(['a','b'],2))

[['a', 'b'], ['b', 'a']]

sage: list(non_rep_words(['a','b'],3))

[['a', 'a', 'b'], ['a', 'b', 'a'], ['b', 'a', 'b'], ['b', 'b', 'a']]

Clearly I am misunderstanding something basic, and I would appreciate any advice.

Eventually, I'd like to do other kinds of pattern avoidance. For example, elements of my "alphabet" might come in pairs, a yin and a yang version, and I might want to make words in which a_yin does not follow a_yang, and conversely.

2018-06-06 00:13:10 +0200 received badge  Nice Question (source)
2018-06-05 23:17:39 +0200 received badge  Popular Question (source)
2018-06-05 23:17:16 +0200 received badge  Nice Question (source)
2018-06-05 23:16:50 +0200 received badge  Popular Question (source)
2014-11-06 19:55:35 +0200 received badge  Scholar (source)
2014-11-06 19:54:13 +0200 commented answer preparser(False) doesn't work with attach()

OK. Thanks. Now if I start writing .py files which mostly consist of sage, I suppose I'll have lots more questions for this website.

2014-11-06 19:51:01 +0200 commented answer preparser(False) doesn't work with attach()

OK. Thank you for the clarification. It seems the ultimate answer to my question is 'don't write .sage files if you want to use some pure python along the way'.

2014-11-06 10:52:46 +0200 commented question preparser(False) doesn't work with attach()

Thanks. How did you put the grey boxes around the 'code'?

2014-11-06 10:02:46 +0200 commented answer preparser(False) doesn't work with attach()

Thanks. Then there is something I really don't understand. I thought that the following are equivalent: entering a bunch of lines into the sage interpreter, hitting return after each one, or putting those same lines into a .sage file and then attaching that .sage file in the interpreter. Doesn't attach read the lines one by one? Anyhow, about .sage versus .py. I've been doing that. I write some .sage file that produces a bunch of matrices, makes them into numpy arrays, and the saves those numpy arrays into a file. Then I write another .py file which loads those numpy arrays and applies some python modules to them. But this is rather awkward and (at least the first time) time consuming. Maybe it is the situation not your answer that is unsatisfactory.

2014-11-06 10:01:06 +0200 received badge  Supporter (source)
2014-11-05 08:57:54 +0200 asked a question preparser(False) doesn't work with attach()

This is a more basic version of my previous question, which had distracting details.


In William Stein's Sage for Power Users (, end of Section 2.2 and top of page 13, there is an example of how to turn the preparser off in the sage interpreter. The example works as claimed and here it is copied from my own computer:

sage: preparser(False)

sage: N=2/3 + 2^3

sage: N


sage: preparser(True)

sage: M=2/3 + 2^3

sage: M


But now suppose that I want to put (almost) exactly the same lines into a .sage file called 'preparsertest.sage', close sage, reopen it, and then type attach('preparsertest.sage') into the interpreter. Naively I expect the same behaviour but that's not what I get.

Here is the content of 'preparsertest.sage':


N=2/3 + 2^3



Here is the output from the interpreter:

sage: attach('preparsertest.sage')

sage: N


sage: M


So it seems that when I attach 'preparsertest.sage', the line preparser(False) is not being read.

What gives?

P.S. I tried to figure out how to attach a screen shot but it didn't work, and I didn't find instructions on

2014-11-04 09:00:38 +0200 asked a question Converting complex sage matrices to numpy arrays

I have a list of matrices with entries in K.=QuadraticField(-1) or K.=NumberField(x^2+1) (whichever is more convenient). I'd like to convert this to a list of numpy arrays with floating point complex numbers. Just applying np.array to my matrices defined over K doesn't seem to work. Any suggestions? (Part of my reason for asking this is that I did not get a satisfactory answer to my question about using pure python in an attached .sage file:

2014-10-31 14:44:29 +0200 commented answer preparse(False) (still) not working

OK. It seems it is really a good idea to reset Integer as soon I'm done with the pure python code. But I don't see how to do that. Obviously, Integer=int followed by Integer=Integer isn't going to work. On the other hand, I could just leave Integer as Integer and wrap everything with int(), but that's exactly what I don't want to do.

2014-10-31 13:56:15 +0200 commented answer preparse(False) (still) not working

Many thanks. RealNumber=float and Integer=int seem to be my best best now, and I guess I can always define them back to usual later in the file. But this really seems to me a bug, given that %python works as advertised in a sage worksheet but preparser(False) doesn't in .sage file. Not to be ungrateful for your help, but I'll leave this answer unaccepted for now.

2014-10-30 13:09:49 +0200 received badge  Student (source)
2014-10-30 12:18:23 +0200 received badge  Editor (source)
2014-10-30 12:06:51 +0200 commented answer preparse(False) (still) not working

Thanks. I forgot to mention that I had already tried that and it doesn't work with attach in the above example, although it does work if you copy and paste the code. If I try to attach the above code (with preparse(False) changed to preparser(False)), I get 13 preparser(False) 14 ---> 15 c=cvx.matrix([_sage_const_1p ,-_sage_const_1p ,_sage_const_1p ]) 16 17 G=[cvx.matrix([[-_sage_const_7p , -_sage_const_11p , -_sage_const_11p , _sage_const_3p ],[ _sage_const_7p , -_sage_const_18p , -_sage_const_18p , _sage_const_8p ],[-_sage_const_2p , -_sage_const_8p , -_sage_const_8p , _sage_const_1p ]]) ] TypeError: invalid type in list So that means (I think) that the preparser is not getting turned off when I attach the .sage file.

2014-10-30 09:15:08 +0200 asked a question preparse(False) (still) not working

I write .sage files and then attach them in the interpreter (rather than using the notebook). Sometimes I want to have a section in .sage file consisting of pure python code (using for example the python module cvxopt), and of course this sometimes leads to type errors. I've tried including preparse(False) at the beginning of my code (as suggested here: but in at least one case it returns the error AttributeError: 'bool' object has no attribute 'lstrip'.

I would very much appreciate a natural solution to this problem that doesn't involve changing anything in python (by decorating it with int,r, and so on).

Here is my code (which works just fine as pure python or works fine if I type preparser(False) in the sage interpreter and then cut and paste the code into the interpreter instead of typing attach('code.sage')).

import cvxopt as cvx from cvxopt import solvers



G=[cvx.matrix([[-7., -11., -11., 3.],[ 7., -18., -18., 8.],[-2., -8., -8., 1.]]) ]

G+=[cvx.matrix([[-21., -11., 0., -11., 10., 8., 0., 8., 5.],[ 0., 10., 16., 10., -10., -10., 16., -10., 3.],[ -5., 2., -17., 2., -6., 8., -17., 8., 6.]]) ]

h =[cvx.matrix([[33., -9.], [-9., 26.]]) ]

h+=[cvx.matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]

sol = solvers.sdp(c, Gs=G, hs=h)


The expected output is

 pcost       dcost       gap    pres   dres   k/t

0: -1.2037e+00 -1.8539e+02 2e+02 2e-16 8e+00 1e+00 1: -1.2937e+00 -6.8551e+00 5e+00 5e-16 3e-01 3e-02 2: -2.8964e+00 -3.7331e+00 7e-01 1e-15 4e-02 5e-02 3: -3.0150e+00 -3.2556e+00 2e-01 7e-16 1e-02 2e-02 4: -3.1389e+00 -3.1932e+00 5e-02 4e-16 3e-03 5e-03 5: -3.1533e+00 -3.1547e+00 1e-03 3e-16 7e-05 1e-04 6: -3.1535e+00 -3.1536e+00 5e-05 6e-16 3e-06 6e-06 7: -3.1535e+00 -3.1535e+00 1e-06 3e-16 7e-08 2e-07 Optimal solution found. [-3.68e-01] [ 1.90e+00] [-8.88e-01]

EDIT: tmonteil suggested changing preparse(False) to preparser(False). This works if I cut and paste the code into the interpreter but doesn't work if I attach the .sage file in the interpreter. See tmonteil's answer and my comment.

EDIT2: It also works in sage worksheet if I replace preparser(False) with %python (but I very much don't want to use the sage worksheet). Is this a bug, or am I doing something wrong?