# How do I make an operator that acts on a function with the function's argument explicitly given?

I am trying to make a finite difference operator such that it would behave similar to the partial derivative $$\partial_{x}f[x]$$.

$$\delta_n[f[n],m] := \frac{f[n+m]-f[n]}{m}\ .$$

I couldn't find a way to define it where I can just pass the function with its argument explicitly shown to the operator. Note that I don't want to give the operator just the name of the function, that's because it cannot act on the function twice if I define it that way, e.g.

$$\delta_n[\delta_n[f[n],m],m]\ .$$

The finite difference operator is just an example. My goal is to learn how to do it so I could apply this to my work in general.

Edit:

I found a partial solution with the following lines of code

SetAttributes[$$\delta$$, HoldAll]
$$\delta[f\_[n\_],m\_]:=\frac{f[n+m]-f[n]}{m}\ .$$

There is still a problem when I do the operation twice, or directly pass the expression of the function into the operator, i.e.

$$\delta[n^2,m]$$

or if the function has multiple variables,

$$\delta[f[n,l],m]\ .$$

It would be nice if it could just work like the Derivative operator.

• Before anything else, have you already seen DifferenceQuotient[]? Aug 6 at 1:20
• No, I have not, but I would like to learn how to implement it so that I can define any arbitrary operator on a function. It was just a simple example that I came up with. Aug 6 at 3:07
• One problem with wanting something like $\delta[n^2, m]$ to work is that you'd actually need to specify what the variables were somewhere. Note that when taking the derivative, you need to write D[n^2, n] so that Mathematica knows what to take the derivative with respect to. (Otherwise it might be Power, or even 2.) So you need to find a way to give Mathematica this info—either by having extra arguments/options, feeding in functions/bound variables instead (e.g. d[n |-> n^2, m |-> m]), marking them somehow, or declaring specific variables to be arguments globally (which might be odd). Aug 6 at 5:36
• You might also be interested in how I implemented the product derivative in this answer, as a guide on how to implement operators. Aug 6 at 7:44
• And thank you, @thorimur, for the great suggestion! Aug 7 at 5:33

FiniteD[f_,{n_,m_}] := (Function[n,f][n+m] - Function[n,f][n])/m