Ask Your Question

Revision history [back]

When you construct a symbolic function using function( string, *vars ) the name contained in string gets injected into the global namespace so that you can use it directly by name, e.g.

sage: function('myfunc', x)
myfunc(x)
sage: myfunc(x).diff(x)
D[0](myfunc)(x)

So the string you give has to be a valid name for a python expression. That's why your first two examples don't really work (the command runs OK, but fails when you try to use the name).

I would suggest trying something like the following: think of an indexing scheme that suits your purposes and so that each function name is a valid python expression, e.g. f_i1_i12_i3 where i1, i2, i3 are non-negative integers. Then, build a dictionary that maps tuples (i1, i2, i3) to symbolic functions f_i1_i2_i3. This will allow you to easily access the functions. How about:

sage: indices = [ (a,b,c) for a in range(10) for b in range(10) for c in range(10) ]
sage: funcs = {} # empty dictionary
sage: for a,b,c in indices:
....:     funcs[(a,b,c)] = function('f_%d_%d_%d' % (a,b,c), x, y, z)
....:

Now use them,

sage: expr = funcs[(0,1,0)]^2 + 3*funcs[(1,2,3)] - 4*funcs[(9,9,9)].diff(x)
sage: expr
f_0_1_0(x, y, z)^2 + 3*f_1_2_3(x, y, z) - 4*D[0](f_9_9_9)(x, y, z)