Re: defining a function of functions

*To*: mathgroup at smc.vnet.net*Subject*: [mg110474] Re: defining a function of functions*From*: Leonid Shifrin <lshifr at gmail.com>*Date*: Sun, 20 Jun 2010 03:45:05 -0400 (EDT)

Hi, you don't need a lot more effort to accomplish what you want. Just pass a pure function as your first parameter: innerprod[f[2,#]&,g] I would not recommend embedding global parameters in a function in the fashion you did it in your code though,since you make your function implicitly dependent on values of global variables, and this is an invitation to trouble. I'd either make all parameters explicit: Clear[innerprod]; innerprod[f_, g_,mylist_List,l_Integer] := Sum[f[k]Conjugate[g[k]]Part[ > > mylist,k],{k,1,l}] (notice also that := (SetDelayed) is usually more appropriate to define functions than = (Set), and that the use of names starting with a capital letter is better avoided). Or, if the same length and list are used many times, I'd make a function generator: ClearAll[makeInner]; SetAttributes[makeInner,HoldFirst]; makeInner[innerName_Symbol, mylist_List,l_Integer]:= (ClearAll[Unevaluated[innerName]]; innerName[f_, g_] :=Sum[f[k]Conjugate[g[k]]Part[ > > mylist,k],{k,1,l}]); then you define your inner product function by calling makeInner, like here: In[4]:= Clear[a, b, myInnerProd]; makeInner[myInnerProd, Range[10], 5] In[6]:= ?myInnerProd Global`myInnerProd myInnerProd[f$_,g$_]:=\!\( \*UnderoverscriptBox[\(\[Sum]\), \(k = 1\), \(5\)]\(f$[k]\ Conjugate[g$[k]]\ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}[[k]]\)\) In[7]:= myInnerProd[a, b] Out[7]= a[1] Conjugate[b[1]] + 2 a[2] Conjugate[b[2]] + 3 a[3] Conjugate[b[3]] + 4 a[4] Conjugate[b[4]] + 5 a[5] Conjugate[b[5]] Both ways, you don't mess with global variables. Regards, Leonid On Sat, Jun 19, 2010 at 4:49 AM, J Davis <texasautiger at gmail.com> wrote: > I want to define an inner product function such as > > (here mylist is a specified list and L is a specified constant) > > innerprod[f_, g_] = Sum[f[k]Conjugate[g[k]]Part[mylist,k],{k,1,L}] > > This works fine but now I need to apply it in a situation where f is a > function of 2 variables while g is a function of only one variable, > i.e. I want to compute something like the inner product of f[2,n] and > g[n]. > > Of course, I want the ability to freely vary the first input of f. > > I have accomplish this before rather easily but I'm presently drawing > a blank. > > Thanks for your help. > > Best, > JD > >