9
$\begingroup$

For an InterpolatingFunction $y:\ \mathbb{R}\to\mathbb{R}^2$, Plus is unaware of this, so when I do any modifications to it in an unevaluated form, it breaks the result

y = Interpolation[Table[{i, RandomReal[{0, 1}, 2]}, {i, 1, 10}]];
y[t]
y[t] + {1, 2}

(* InterpolatingFunction[{{1.,10.}},<>][t]`
{1+InterpolatingFunction[{{1.,10.}},<>][t],2+InterpolatingFunction[{{1.,10.}},<>][t]} *)

I ran into it doing things like:

y = Interpolation[Table[{i, RandomReal[{0, 1}, 2]}, {i, 1, 10}]];
f[t_] := Piecewise[{{ {1, 2} + y[t], t < 5}}];
df[t_] := Evaluate[D[f[t], t]]
df[t]
df[3]

And I can't figure out how to prevent expressions like {1,2} + y[t] from being evaluated while still being able to differentiate them. If Plus just realized that y[t] is $\mathbb{R}^2$ valued, I think that would simplify everything for me. Is there some way to set this property?

$\endgroup$
  • $\begingroup$ I suppose y[t_] = {Interpolation[Table[{i, RandomReal[{0, 1}]}, {i, 1, 10}]][t], Interpolation[Table[{i, RandomReal[{0, 1}]}, {i, 1, 10}]][t]} is not what you're looking for? $\endgroup$ – Sjoerd C. de Vries Nov 12 '12 at 21:13
  • $\begingroup$ Started rewriting my code to split everything up like that, but was hoping for some Hold magic :) $\endgroup$ – ssch Nov 12 '12 at 21:23
5
$\begingroup$

Using the InterpolatingFunctionAnatomy package one can implement the threading explicitly.

y = Interpolation[Table[{i, RandomReal[{0, 1}, 2]}, {i, 1, 10}]];

<< DifferentialEquations`InterpolatingFunctionAnatomy`

yy = Interpolation[
  MapThread[
   List, {InterpolatingFunctionGrid[y], {1, 2} + # & /@ 
     InterpolatingFunctionValuesOnGrid[y]}], 
  InterpolationOrder -> 
   First[InterpolatingFunctionInterpolationOrder[y]]]

enter image description here

$\endgroup$
  • $\begingroup$ Nice! Trying to override Plus Unprotect[Plus]; Plus[c_List, f_InterpolatingFunction] := Interpolation[ MapThread[ List, {InterpolatingFunctionGrid[f], c + # & /@ InterpolatingFunctionValuesOnGrid[f]}], InterpolationOrder -> First[InterpolatingFunctionInterpolationOrder[f]]]; But I think I have to hunt the documentation a bit to see how to do it properly $\endgroup$ – ssch Nov 12 '12 at 21:31
  • 2
    $\begingroup$ (Of course, if you're aware of the direct way to access those properties (e.g. y["Grid"] and y["ValuesOnGrid"]), you don't need to load the package...) $\endgroup$ – J. M. will be back soon Nov 12 '12 at 22:02
  • $\begingroup$ @J.M. ...and there's one of those features I didn't know about. What other "properties" of InterpolatingFunction are there? $\endgroup$ – Mr.Wizard Nov 12 '12 at 22:04
  • 3
    $\begingroup$ It seems the answer to my question is: "Coordinates", "DerivativeOrder", "Domain", "Evaluate", "Grid", "InterpolationOrder", "MethodInformation", "Methods", "Properties", "ValuesOnGrid" $\endgroup$ – Mr.Wizard Nov 12 '12 at 22:11
  • 1
    $\begingroup$ @Mr.Wizard, for reference, y["Methods"] will return that list of properties supported by an InterpolatingFunction[]. $\endgroup$ – J. M. will be back soon Nov 13 '12 at 16:16

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.