10

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?

rm -rf
  • 88,781
  • 21
  • 293
  • 472
ssch
  • 16,590
  • 2
  • 53
  • 88
  • 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? – Sjoerd C. de Vries Nov 12 '12 at 21:13
  • Started rewriting my code to split everything up like that, but was hoping for some Hold magic :) – ssch Nov 12 '12 at 21:23

1 Answers1

6

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

Sasha
  • 7,373
  • 36
  • 47
  • 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

    – ssch Nov 12 '12 at 21:31
  • 2
    (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...) – J. M.'s missing motivation Nov 12 '12 at 22:02
  • @J.M. ...and there's one of those features I didn't know about. What other "properties" of InterpolatingFunction are there? – Mr.Wizard Nov 12 '12 at 22:04
  • 3
    It seems the answer to my question is: "Coordinates", "DerivativeOrder", "Domain", "Evaluate", "Grid", "InterpolationOrder", "MethodInformation", "Methods", "Properties", "ValuesOnGrid" – Mr.Wizard Nov 12 '12 at 22:11
  • 1
    @Mr.Wizard, for reference, y["Methods"] will return that list of properties supported by an InterpolatingFunction[]. – J. M.'s missing motivation Nov 13 '12 at 16:16