Let's say I have an interpolated function f
, like I might get as the solution to an NDSolve
:
f[x_] = y[x] /. First[NDSolve[{y'[x] == Cos[x], y[0] == 0}, y, {x, 0, 1}]]
InterpolatingFunction[{{0., 1.}}, <>][x]
Now suppose I define some convoluted function in terms of f
:
g[x_] = f[x]^2 - 2 f[x]^4 + 3 x
3 x - 2 InterpolatingFunction[{{0., 1.}}, <>][x]^4 + InterpolatingFunction[{{0., 1.}}, <>][x]^2
This new function g
preserves all the structure of how I constructed it, and in order to compute a value for, say, g[0.5]
, it will perform the computation in terms of f
.
This allows me to do algebraic things like
Simplify[g[x] / f[x]]
(3 x) / InterpolatingFunction[{{0., 1.}}, <>][x] + InterpolatingFunction[{{0., 1.}}, <>][x] - 2 InterpolatingFunction[{{0., 1.}}, <>][x]^3
which works because Mathematica is remembering the details of g
's construction in terms of f
.
But for my own use case, once I've defined g
, I no longer care at all about f
. Is there a way to tell Mathematica to forget about all the complexity that is g
's definition with respect to f
, and collapse g
all into a single InterpolatingFunction
that produces the same values?
g
withSet
(=
, as you do), you can safelyRemove[f]
without affectingg
. $\endgroup$g[x]
still produces a linear combination of three terms including severalInterpolatingFunction
s. But I don't care, in my mindg
is just a function that takes a value ofx
between 0 and 1 and returns a particular value; I just want a singleInterpolatingFunction
object that does that exact job. $\endgroup$FunctionInterpolation
on the combined function, result will be a single interpolation function, though there's going to be some difference in result... $\endgroup$