# how to define multi index functions of form f_{i_1,i_2,i_3,i_4}(x,y,z) ?

I was trying to define a function of form f_{i_1,i_2,i_3,i_4}(x,y,z) using the command

function('f_{i_1,i_2,i_3,i_4}',x,y,z)

but sage gave me back the error that 'tuple' object is not callable. I can define it like

function('f_{i_1}_{i_2}_{i_3}_{i_4}',x,y,z)

but it is not a good way when the indices are many  or maybe even I should use

function('f_i_1_i_2_i_3_i_4',x,y,z)

edit retag close merge delete

Sort by » oldest newest most voted

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)
more

thank you for your response, That's a good hint, yes I shouldn't use (i_1, ) but (i1, ) as my indices. However, still I have a problem with show() command, I have many integration and differentiation so I use show() instead of e.g. print() to print my results, just for the sake of readability, however using f_i1_i2_ makes i_1 as subscript to f, i_2 as subscript to i_1 and so on, each subscript with a smaller font than its parent, however if the number of indices gets large then such a notation seems not to be proper  although not very bad though! I just wanted to know if a better way exists in sage t show such indexed functions! Thanks again for your nice answer

( 2012-07-25 05:23:49 +0200 )edit
1

@owari: you can pass function a latex_name argument, e.g. use something like function('f_%d_%d_%d' % (a,b,c), x, y, z, latex_name='f_{%d, %d, %d}' % (a,b,c)) or whatever. Then it should look a little better. [Type help(function) for the full details on what function takes.]

( 2012-07-25 10:40:23 +0200 )edit

thank you very much, exactly what I needed! Thank you both benjaminfjones and DSM !

( 2012-07-25 10:48:59 +0200 )edit