Assume that;

w[n_] :=Expand[Sum[Binomial[n - k - 1, k]*(-1)^k*A^(n - 2*k - 1), {k, 0, n - 1}]]
f[x_,y_, z_]:=PolynomialRemainder[(w[z] - 1)*(w[y] - 1), (w[x] - 1), A]

According to the above definitions, the function f depends on x,y,z and A. I want to have a new function only depends on A for any x,y,z. For example, when i put some values for x,y,z, i want a function such

g[A_]:=.........How can i obtain?



1 Answer 1


One possibility:

f[x_, y_, z_] := 
     Function @@ {A,PolynomialRemainder[(w[z] - 1)*(w[y] - 1), (w[x] - 1), A]}

When you call f, the result would be a pure function depending on A only, with the values x, y and z embedded into its body. I used Function @@ {} idiom to allow the body to evaluate, so that the evaluation of PolynomialRemainder and w is done only once, at definition-time.

For example:

f[4, 5, 16]

(* Function[A, 4 + 2 A - 2 A^2] *)

and you can now store this function in a variable and apply it many times. Technically, what you asked for is how one can create a closure - a function which carries some of the surrounding environment in its body. In this case, it was a little more tricky since I chose to perform some evaluation at definition-time as an optimization.

As a side note, it is best to not use capital letters for variables, since names starting with capital letters may collide with the system names.

  • $\begingroup$ Thank you for your response. In the case x=4, y=5, z=16 and in order to obtain a list for 0<A<10, what will i do? $\endgroup$
    Commented Apr 10, 2013 at 23:23
  • $\begingroup$ @Nurettinırmak myFun = f[4, 5, 16]; Map[myFun, Range[9]]. $\endgroup$ Commented Apr 10, 2013 at 23:27
  • $\begingroup$ Thank you. i am new user. Therefore, i can ask very easy questions :) $\endgroup$
    Commented Apr 10, 2013 at 23:32
  • $\begingroup$ @Nurettinırmak Glad I could help, and thanks for the accept. I would still suggest to spend some time and get familiar with the basics. The question you asked was not quite a basic one, so this does not apply to the case at hand, but be aware that very basic (RTFM - like) questions are frowned upon here. $\endgroup$ Commented Apr 10, 2013 at 23:35
  • $\begingroup$ +1 Leonid: Another "easy" question from the OP for you to enjoy mathematica.stackexchange.com/q/22807/193 $\endgroup$ Commented Apr 11, 2013 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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