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.
DifferenceQuotient[]
? $\endgroup$D[n^2, n]
so that Mathematica knows what to take the derivative with respect to. (Otherwise it might bePower
, or even2
.) 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). $\endgroup$