As the title says Im trying to build a function to compute divided difference for arbitrary list of points.
The divided difference of a set of points $\{(x_1,y_1),\ldots, (x_n,y_n)\}$ where $x_j<x_{j+1}$ is defined recursively by
$$ f[i,j]:=\frac{f[i+1,j]-f[i,j-1]}{x_j-x_i},\, f[i,i]:=y_i $$
for $j\ge i$.I tried to mimic this definition with the code
f[i_, j_, m_] := f[i, j, m] = (f[i + 1, j, m] - f[i, j - 1, m])/(
m[[j, 1]] - m[[i, 1]]) Boole[j > i] + m[[j, 2]] Boole[j == i]
where m
is an arbitrary $n\times 2$ matrix. However I tried to see if this works calling f[1,1,{{1,2},{3,4}}]
but it gives me a recursion limit error. How I can fix the above code? Thank you in advance.
f[0] = 1; f[n_] := n f[n - 1]
from the docs almost works, but better is something likef[0] = 1; f[n_Integer?Positive] := n f[n - 1]
orf[0] = 1; f[n_Integer] /; n > 0 := n f[n - 1]
$\endgroup$dividedDifference[x_, f_] := Module[{d}, With[{n = Length[(x)]}, d = f; (* div. diff. computed in place *) Do[ d[[j]] = (d[[j]] - d[[j - 1]])/(x[[j]] - x[[j - k + 1]]), {k, 2, n}, {j, n, k, -1}]; d ]]
--x
is a list of nodes andf
a list of values at the nodes. For inputm
, the call should bedividedDifference @@ Transpose@m
. $\endgroup$f[i_, j_, m_]
, which callsf[i + 1, j, m]
andf[i, j - 1, m]
, both of which in turn callf
on new arguments ad infinitum. Unless there are definition rules that you are not showing, there are no stops to this process. The calls tof
will occur even ifBoole[]
evaluates to zero, in case you thinkBoole[]
might stop the process. It would be possible to put the stops in using anIf[]
statement. $\endgroup$