Ask Your Question

Benjamin Young's profile - activity

2021-10-24 14:04:20 +0100 received badge  Famous Question (source)
2021-10-24 14:04:20 +0100 received badge  Notable Question (source)
2018-05-12 06:00:02 +0100 received badge  Taxonomist
2017-05-09 14:36:43 +0100 received badge  Popular Question (source)
2017-05-09 14:36:43 +0100 received badge  Notable Question (source)
2016-06-15 23:06:55 +0100 received badge  Popular Question (source)
2013-03-08 14:15:10 +0100 answered a question Laurent series, Rational Functions in sqrt(q)?

Hearing nothing, I just used the first workaround, made 500 mistakes typing in formulas as predicted, fixed them, and I was done. However, the surprise was that it was less painful than I expected, and it did have the advantage that I solved the problem with the tools I already knew, which I regard as virtuous in some sense.

So I'm going to sheepishly call this an answer to my own question. Probably there's a badge for that or something. But I'd still be curious to hear if there's a better way.

2013-03-08 14:11:40 +0100 answered a question Read sparse matrix in Harwell-Boeing Exchange Format

Actually it seems that scipy provides basic support for this format starting in version 0.10.0. According to fidelbc's comment, scipy already ships with sage. So I'd tentatively answer "yes", although I haven't tried it.

2013-03-08 06:01:34 +0100 received badge  Student (source)
2013-03-07 22:56:16 +0100 commented answer sage version of python's execfile

Thank you! I'm embarrassed I didn't know that, despite it being in the tutorial... I guess I never worked through it, since I already knew python. Oh, well.

2013-03-07 22:53:42 +0100 received badge  Scholar (source)
2013-03-07 22:53:42 +0100 marked best answer sage version of python's execfile

You can do load("file.sage") or attach("file.sage"). Make sure to use a ".sage" suffix, so Sage will know to preparse it. See the tutorial for a bit more information.

2013-03-07 20:24:57 +0100 asked a question sage version of python's execfile

What is the sage analogue of python's "execfile()"? How do I ask sage to preprocess and execute a text file full of sage commands?

Up till now I've just been using execfile() on the sage command line prompt, because that's what I do in python sometimes. However I just learned the hard way that this is the wrong thing to do; it misses a vital preprocessing step. For example, if I ask for the type of 1/2 on the sage command line, the following happens:

sage: print type(1/2)
<type 'sage.rings.rational.Rational'>

Great! that's what I want. However if I put the following into the file "":

print type(1/2)

and then I issue the following command on the sage command line:


then I get the output

<type 'int'>

Yes, the integer 1/2. I want the last hour of debugging time back.

Of course what's happening is that the text file is being interpreted in pure python, in which 1/2 really does represent an integer division. What is the right thing to do here, rather than execfile()?

2013-03-03 22:01:08 +0100 received badge  Nice Answer (source)
2013-03-03 16:16:51 +0100 commented answer nilpotent adjacency matrix

Got it. Fixed in the edit. What a great problem for learning all this stuff!

2013-03-03 15:49:40 +0100 commented answer nilpotent adjacency matrix

OK, now it starts from b_1 - I just used n+1 variables and made the diagonal matrix from the last n of them with a python list slice. As for the sum of the scalar coefficients, the answer is doubtless "yes" but the obvious things I tried haven't worked yet - I haven't worked with quotient rings a whole lot.

2013-03-03 15:49:01 +0100 received badge  Editor (source)
2013-03-03 13:46:48 +0100 received badge  Teacher (source)
2013-03-03 13:21:10 +0100 commented question p-part of a class group

Could you please post the code that calculates G?

2013-03-03 13:11:44 +0100 answered a question nilpotent adjacency matrix

Here's a naive answer, as I am somewhat naive (I just posted a question and, while waiting breathlessly for someone to answer it, I thought I'd just have a look at what other people are asking). No idea how efficient it is, but maybe it's a start.

You can construct the ring that you want to work in as a quotient ring of a multivariate polynomial ring. You can then construct the matrix B that you want by multiplying by a diagonal matrix whose entries are the variables you want.

n = 4
R = PolynomialRing(QQ, n+1, 'a')
squared_vars = [t**2 for t in R.gens()]
I = R.ideal(squared_vars)
S = R.quotient_ring(I, 'b')

def nil(A,k):
    B = A * diagonal_matrix(S, n, list(R.gens())[1:])
    return (B**k).trace()

Edit: It turns out that OP also wants to compute the sum of the coefficients. I found it kind of tricky to compute this, largely because the quotient ring classes seem not to have a lot of the methods you'd expect. However it's possible to lift an element of the quotient ring to the obvious preimage in the original ring, and since the original ring is a plain old multivariate polynomial ring, you can do all the things you'd normally do in it, like "subs". Naturally substituting 1 for all the variables computes the sum of the coefficients:

def sum_of_coeffs(x):
    return x.lift().subs({t:1 for t in R.gens()})

so, for instance, you could do


which hopefully should produce the answer 10 when you run it with OP's example, for instance. This was a great question - at least, I feel like I learned a lot... :)

2013-03-02 13:26:10 +0100 asked a question Laurent series, Rational Functions in sqrt(q)?

I need to construct the ring of formal Laurent series in q**(1/2) over the rational numbers. How would I do that in sage?

I realize that there's a perfectly good workaround, but I'd be nonetheless very happy if I didn't have to use it. I could just use Laurent series in another variable, like t,

R.<t> = LaurentSeriesRing(QQ)

I could then define q to be t**2, and use t as a formal square root of q. But then I still can't raise q to a non-integer power - sage complains that there's a non-integer in the exponent. I'd make about 500 mistakes just typing in formulas and it would be hard to read the output.

Another workaround which I'm not keen on is to just say


and just use symbolic expressions instead. I don't really want to do that either: I like having all the Laurent series methods available and I gather that working in an explicit ring is a lot faster? If I'm misinformed there, then please let me know.

Lastly, I'd also like to construct the rational functions in sqrt(q) - same basic problem, as far as I can see. Any help appreciated.