# Function as parameter in Wolfram Mathematica

I want to define some basic functions known from "discrete analysis":

$$I(f)(x):=f(x)$$ $$E(f)(x):=f(x+1)$$ $$\Delta(f)(x) := (E-I)(f)(x) = f(x+1)-f(x)$$ $$\nabla(f)(x) := (I-E^{-1})(f)(x) = f(x)-f(x-1)$$

And I know that I can define a function f[x_] := x^2 like that, but how can I take a function and evaluate it at the given position like E[fn_] := evaluate fn at x+1?

-

## 2 Answers

You can use the Apply command to make a definition such as Eval[f_,x_]:= Apply[f,{x}] (you can't use E as the name of your function, though - this is reserved by Mathematica for the number $e=2.71828\ldots$).

For example:

F[x_]:=x^2+1
Eval[f_,x_]:= Apply[f,{x}]

Eval[F,5]=26
Eval[F,x+1]=1+(1+x)^2
Eval[F,x]=1+x^2


Then you can define

Delta[g_]:=FullSimplify[Eval[g,x+1]-Eval[g,x]]
Delta[F]=1+2x

-
It does not work like expected! E.g. $E(f)(x)$ is now called TranslateEx. TranslateEx[fn_] := Apply[fn, {x + 1}] and then TranslateEx[x] leads to the result x[1 + x]. What does this mean? I expected x + 1 as the result. EDIT It seems i cannot enter a function directly and i need to specify it explicitly... – Christian Ivicevic Oct 8 '11 at 23:52
@Christian: x by itself is not a function. You need to define a function g[x_]:=x, and then you can do TranslateEx[g]=1+x. – Zev Chonoles Oct 8 '11 at 23:56
Is there any way, like in programming languages, to define an "anonymous function" (a.k.a. lambda-expression) the way i tried? – Christian Ivicevic Oct 8 '11 at 23:57
You might be able to do it with the Function command, but I'm not familiar with it myself. – Zev Chonoles Oct 9 '11 at 0:00
eval[k_][f_]:=f[k] – Gus Wiseman Oct 9 '11 at 0:08

Actually, for Mathematica 7 and later versions, you have the functions Identity[], DiscreteShift[], and DifferenceDelta[]:

Identity[f[x]]
f[x]

DiscreteShift[f[x], x]
f[1 + x]

DifferenceDelta[f[x], x]
-f[x] + f[1 + x]


The backward difference needs a bit more work:

DifferenceDelta[DiscreteShift[f[x], {x, 1, -1}], x]
-f[-1 + x] + f[x]


Otherwise:

bdf[f_, x_] := f - (f /. x -> x - 1)

bdf[f[x], x]
-f[-1 + x] + f[x]


In fact, Mathematica supports more traditional notation (see the manual for details):

-