1
$\begingroup$

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.

$\endgroup$
6
  • 3
    $\begingroup$ Before anything else, have you already seen DifferenceQuotient[]? $\endgroup$ Aug 6 at 1:20
  • $\begingroup$ 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. $\endgroup$ Aug 6 at 3:07
  • 1
    $\begingroup$ 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). $\endgroup$
    – thorimur
    Aug 6 at 5:36
  • $\begingroup$ You might also be interested in how I implemented the product derivative in this answer, as a guide on how to implement operators. $\endgroup$ Aug 6 at 7:44
  • 1
    $\begingroup$ And thank you, @thorimur, for the great suggestion! $\endgroup$ Aug 7 at 5:33

1 Answer 1

0
$\begingroup$

Here is the solution:

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

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.