Ask Your Question
1

How do I get an ordered list of a symbolic functions arguments?

asked 2011-11-10 08:41:49 -0500

rtrwalker gravatar image

How can I get a list/tuple of the variables in a symbolic function with the same ordering as when the function was defined? e.g. for the function below I would want (z,t) not the alphabetically ordered (t, z) I get with .variables() of .arguments(). The ordering has to be stored/used somewhere in sage because I can differentiate with respect to z and get D0(z,t) as an answer where the '0' corresponds to 'z'.

sage: var('z,t')
(z, t)
sage: f = function('u',z,t)
sage: print (f)
u(z, t)
sage: f.variables()
(t, z)
sage: f.arguments()
(t, z)
sage: f.diff(z)
D[0](u)(z, t)
sage: f.diff(t)
D[1](u)(z, t)
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
5

answered 2011-11-10 11:06:41 -0500

DSM gravatar image

updated 2011-11-10 12:51:58 -0500

I think the .operands() method should do what you want:

sage: var('z,t')
(z, t)
sage: f = function('u',z,t)
sage: f.variables()
(t, z)
sage: f.operands()
[z, t]

We should probably add a reference to operands() in variables()/arguments(). Interested in working up a documentation patch?

We can sanity-check this using a variant of jdc's suggestion:

import string
import random as rnd

def test_one(f):
    ops = f.operands()
    fops = list(str(op) for op in ops)
    sf = str(f)
    sops = sf[sf.find('(')+1:sf.find(')')].split(',')
    sops = list(sop.strip() for sop in sops)
    assert fops == sops

def test_lots():
    for c in range(50):
        for w in range(1,10):
            vv = tuple(var(rnd.choice(string.ascii_letters) + str(randint(0,10))) for _ in range(w))
            f = function(*(('u',) + vv))
            print f
            test_one(f)
    return 'hooray!'

produces

sage: test_lots()
[...]
u(J8)
u(H5, M5)
u(g7, H1, B5)
u(f5, I2, i0, A10)
u(c4, B0, i5, g4, T0)
u(e6, R9, B0, T9, i6, n8)
u(T0, V5, c10, T5, X7, B9, Y4)
u(F8, d9, j6, k10, U0, r6, I10, p3)
u(V5, Q10, z6, n10, V6, f3, J7, o4, F0)
'hooray!'
edit flag offensive delete link more

Comments

The .operands() method is indeed what I am looking for. Thanks

rtrwalker gravatar imagertrwalker ( 2011-11-14 13:39:53 -0500 )edit

What is involved in 'working up a document patch'?

rtrwalker gravatar imagertrwalker ( 2011-11-14 13:41:32 -0500 )edit
0

answered 2011-11-10 09:40:27 -0500

jdc gravatar image

updated 2011-11-10 09:42:04 -0500

How about converting f into a string, then stripping off the pieces you don't need? For instance,

s = str(f).split("(")[-1][:-1].split(",")

yields the list s = ['z', ' t']. Or if you want a list of symbolic variables, map(SR, s) = [z, t]. Ugly, but it might do what you need.

edit flag offensive delete link more

Comments

I prefer the .operand() method as it is less 'ugly'. Thanks for the answer though; I now know about the map(SR, s) approach.

rtrwalker gravatar imagertrwalker ( 2011-11-14 13:39:12 -0500 )edit

Sure -- I like .operands() better too, now that I know about it!

jdc gravatar imagejdc ( 2011-11-15 08:16:29 -0500 )edit

:-) Never underestimate quick-and-dirty solutions like this.. sometimes you're on the clock and need it to work Right Now(tm) and don't have time to wrestle with the documentation. Plus, if they're general like this one is, it can be used as a test.

DSM gravatar imageDSM ( 2011-11-16 03:06:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-11-10 08:41:49 -0500

Seen: 328 times

Last updated: Nov 10 '11