Getting Sage to give symbolic output with Variables

51 views
Skip to first unread message

saad khalid

unread,
Jun 2, 2016, 2:29:34 PM6/2/16
to sage-support
Hey everyone:

So, there are some infinite sums that I would like to see computed out to around 20 terms, just so I can kind of see what form its taking. The problem is that it's a bit difficult to do by hand, and I'll be changing the parameters several times which means I would have to do it several times. I was wondering if there was any way to do it in sage. The type of thing I want would be like \sum_{k=1}^{20} (5^k)(y_k). So, the part that I don't know how to do is have it output y_k. I don't even necessarily need it to generate y_k as a variable that I can plug things in to, I just want it to output it so that I can see the form of the equation, if that makes sense. Thank you!


saad khalid

unread,
Jun 2, 2016, 3:22:37 PM6/2/16
to sage-support
As another example, here is something I found in Mathematica:
In: Sum[f[i], {i, 1, 5, 2}]
Out: f[1] + f[3] + f[5]

In this case, I believe it is treating f as a function, which is fine as I will eventually be writing y as a function. I just didn't know how I should define y in Sage so that it would let me do this type of thing.

Nils Bruin

unread,
Jun 2, 2016, 3:30:12 PM6/2/16
to sage-support
On Thursday, June 2, 2016 at 11:29:34 AM UTC-7, saad khalid wrote:
Hey everyone:

So, there are some infinite sums that I would like to see computed out to around 20 terms, just so I can kind of see what form its taking. The problem is that it's a bit difficult to do by hand, and I'll be changing the parameters several times which means I would have to do it several times. I was wondering if there was any way to do it in sage. The type of thing I want would be like \sum_{k=1}^{20} (5^k)(y_k). So, the part that I don't know how to do is have it output y_k. I don't even necessarily need it to generate y_k as a variable that I can plug things in to, I just want it to output it so that I can see the form of the equation, if that makes sense. Thank you!

You can get a list of symbols by just constructing them:

sage:  y = [SR("y%s"%i) for i in [0..20]]

or if you want to get fancy (and you know what kind of coefficients you have) you can use an "infinite polynomial ring"

sage: R.<y> = InfinitePolynomialRing(QQ)

in either case you can construct your sum by doing something along the lines of

sum([5^k*y[k] for k in [1..20]])

saad khalid

unread,
Jun 2, 2016, 4:19:21 PM6/2/16
to sage-support
Thanks for the quick reply! Could you explain, or tell me what to search, what exactly "SR("y%s"%i)" does? Is SR Symbolic Ring? Also, the output for the sum is close to perfect, though it gives me:
y1 + y10 + y11 + y12 + y13 + y14 + y15 + y16 + y17 + y18 + y19 + y2 + y20 + y3 + y4 + y5 + y6 + y7 + y8 + y9

So, the order is a bit messed up. I wasn't sure if there was an easy way to fix that.

Also, running it with
R.<y> = InfinitePolynomialRing(QQ)

sum
([5^k*y[k] for k in [1..20]])

Seems to run for a very long time

Ralf Stephan

unread,
Jun 7, 2016, 1:54:38 AM6/7/16
to sage-support
On Thursday, June 2, 2016 at 10:19:21 PM UTC+2, saad khalid wrote:
Thanks for the quick reply! Could you explain, or tell me what to search, what exactly "SR("y%s"%i)" does?

It's SR("y%s"%i) or better SR("y%s" % i). The % is Python format string substitution, like C's sprintf.
 
Is SR Symbolic Ring? Also, the output for the sum is close to perfect, though it gives me:
y1 + y10 + y11 + y12 + y13 + y14 + y15 + y16 + y17 + y18 + y19 + y2 + y20 + y3 + y4 + y5 + y6 + y7 + y8 + y9

Yes. This is a feature because any fancy string detection during ordering would slow symbolic calculations considerably.

So, the order is a bit messed up. I wasn't sure if there was an easy way to fix that. 

If you think we need a pretty print function then please open a ticket.
 
Also, running it with 
R.<y> = InfinitePolynomialRing(QQ)
sum
([5^k*y[k] for k in [1..20]])

Seems to run for a very long time

Tell us too please how long. I get with 7.3beta3:
sage: %timeit sum([5^k*y[k] for k in [1..20]])
The slowest run took 5.17 times longer than the fastest. This could mean that an i
ntermediate result is being cached.
1000 loops, best of 3: 264 µs per loop
sage: %timeit sum([5^k*y[k] for k in [1..20]])
1000 loops, best of 3: 268 µs per loop
sage: %timeit sum([5^k*y[k] for k in [1..20]])
1000 loops, best of 3: 263 µs per loop


Fast enough, I should say.

saad khalid

unread,
Jun 7, 2016, 2:27:29 PM6/7/16
to sage-support
Thank you for the information! Yeah, it's working just fine now, I have no idea why it wasn't working before. Maybe it was because I hadn't reset y before trying to define it as a ring, I'm not too sure. But, it's working now on a fresh worksheet, so thank you!
Reply all
Reply to author
Forward
0 new messages