I am trying to create an expansion of the form

f[x,y]->Sum[Cn[x] y^n,{n,1,order}]

To replace the function f by the coefficients cn. Since the function f appears in a differential equation, it is useful to get this to work as a set of replacement rules for a pure function, such that the function f and all its derivatives are replaced. Something like this works:

f->((c0[#1]+c1[#1] #2+c2[#1]#2^2)&)

Curiously, this expression doesn't

f->(( Sum[c[[n]][#1] #2^n,{n,1,order}] )&)

(where c is a pre-defined vector of coefficients). The index n is not recognized in the context of defining the pure function (though the same expression defining f[x,y] would work).

Similarly, if I have several functions and I am trying to define a collection of these replacement rules, this also does not work:

Table[ f[[i]]->f->((c0[[i]][#1]+c1[[i]][#1] #2+c2[[i]][#1]#2^2)&), {i,1,whatever}]

Somehow defining and manipulating a collection of pure functions with an index needs a different syntax. I am curious as for the logic of why this does not work, maybe then I'll understand how to fix it.

Edit: Apologies for the vague question. I think the logic that I was missing is related to attributes of pure functions I was trying to use in replacement rules, as pointed out by acl below. This is related to this question: Function in Table

  • $\begingroup$ One problem is that you are doing the sum with iterators {n,0,order} which goes from n=0 up to n=order. But with n=0, C[[n]] is not defined. So maybe C[[n+1]] would fix it. $\endgroup$ – bill s Mar 23 '13 at 10:08

There's a number of reasons your code doesn't work (all related to attributes; basically, Function has attribute HoldAll). I think this does what you want:

   {f[n], With[{n = n}, c[[n]][#1] #2^n &]},
   {n, 1, order}

Mathematica graphics

How does this work? With[{n=n},body] is what fixes the problem, as it literally inserts n into the body of the Function, so the HoldAll attribute never gets to see the n; it just sees the explicit value.

To illustrate what With does here:

Block[{y = 2}, y*x &]
Block[{y = 2}, With[{y = y}, y*x &]]

Mathematica graphics

So, here, in the first case the y in the function doesn't evaluate to 2, then the block exits and we're done. In the second case, y gets literally replaced by 2. That is, you may think of With[{y=2},body] as a search-and-replace of y by 2 in the body before anything else happens (before, i.a., evalution of body).

Hope this clarifies things a bit.

  • $\begingroup$ Thanks, the attributes of the function is the point I was missing. Thanks for the link, I will take a look. $\endgroup$ – Moshe Mar 23 '13 at 20:06
  • $\begingroup$ note that I used f[i] instead of f[[i]], because if f is not defined, trying to take a part of it gives an error. replace [] with [[]] if this is what you need $\endgroup$ – acl Mar 23 '13 at 20:06
  • $\begingroup$ That was a point I caught (fixed by predefining the coefficient vector), but the attributes and the use of with is likely the point I missed. My question was vague because I didn't want to simply fix the code, I wanted to understand what I am missing since the syntax seemed to me like it ought to work. Thanks for pointing me the right direction. $\endgroup$ – Moshe Mar 23 '13 at 20:10
  • 1
    $\begingroup$ @Moshe I tried to explain it a bit more here, hope it's clearer. $\endgroup$ – acl Mar 23 '13 at 20:19

I'm not really sure about what are you trying to do with those rules, but the following works:

c = Table[#/n! &, {n, 0, 10}];
f[order_] := ((Sum[c[[n + 1]][#1] #2^n, {n, 0, order}]) &)

Plot3D[{f[5][x, y], f[10][x, y]}, {x, -10, 10}, {y, -10, 10}, 
       PlotStyle -> {{Opacity[0.6], Red}, {Opacity[0.6], Blue}}]

Please remember to start your symbols with lowercase chars, Cis a system's defined name.

Mathematica graphics

  • $\begingroup$ Thanks for your answer. I don't think I have been very clear, I tried to modify my question to emphasize the point I am not getting, how to define and use a collection of pure functions. $\endgroup$ – Moshe Mar 23 '13 at 19:03
  • $\begingroup$ @Moshe I don't understand your question because you seem to be defining RULES (->) and not pure functions $\endgroup$ – Dr. belisarius Mar 23 '13 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.