3
$\begingroup$

For a given Blackbox interpolation function f[x] examplary

f = Interpolation[Table[{x, Cos[x]}, {x, 0, 2 Pi, 2 Pi/20}]] (*f[x]*)

I would like to rescale the function

eps=0.9;
fn=eps f[#/eps]&; (*fn[x]:= eps f[x/eps]*)

which makes no problem if I use a new function name fn.

My question: Is it possible to easily overwrite the original functiondefinition f[x] in this way?

Thanks!

$\endgroup$
  • 3
    $\begingroup$ Do you mean something like f = Evaluate[eps f[#/eps]] &;? You can only do it once, or you get a repeated composition. $\endgroup$ – Michael E2 Nov 30 '20 at 18:44
  • $\begingroup$ @MichaelE2 Yes that's it. Thanks! I tried it without Evaluate and got recursion error... $\endgroup$ – Ulrich Neumann Nov 30 '20 at 18:54
4
$\begingroup$

Simple way that works with any function f, but should be done once only; otherwise, the rescalings will be compounded with each iteration:

f = Evaluate[eps f[#/eps]] &

Another way that may be executed repeatedly and does not have the compounding problem, but it assumes f is originally an InterpolatingFunction:

f /. if_InterpolatingFunction :> 
   RuleCondition[f = eps if[#/eps] &; if];
f

Update: Here is another way for the second alternative. The difference is that the replacement fails but still has the same side effect. TracePrint reveals no significant difference between them in this case, probably because the InterpolatingFunction is inert and/or Function (&) is HoldAll.

f /. if_InterpolatingFunction :>
   if /; TrueQ[f = eps if[#/eps] &];
$\endgroup$
  • 1
    $\begingroup$ do you think Once would work in this connection? $\endgroup$ – kglr Nov 30 '20 at 21:26
  • $\begingroup$ @kglr It might depend. If you change f (new interpolation) after Once[..] and need to rescale it, then I think Once won't rescale it for you. You could execute something like If[ValueQ[Once[..]], Unset[Once[..]]] first. But sometimes it should be appropriate. $\endgroup$ – Michael E2 Nov 30 '20 at 21:42
  • $\begingroup$ meant something like f = Interpolation[Table[{x, Cos[x]}, {x, 0, 2 Pi, 2 Pi/20}]];Once[f = Evaluate[eps f[#/eps]] &] $\endgroup$ – kglr Nov 30 '20 at 21:45
  • $\begingroup$ @kglr No, wait, I think it doesn't work f = Interpolation[Table[{x, Cos[x]}, {x, 0, 2 Pi, 2 Pi/20}]]; Once[ f = Evaluate[eps f[#/eps]] &]; f = Interpolation[Table[{x, Sin[x]^3}, {x, 0, 2 Pi, 2 Pi/20}]]; Once[ f = Evaluate[eps f[#/eps]] &]; f. $\endgroup$ – Michael E2 Nov 30 '20 at 21:55
  • $\begingroup$ @MichaelE2 Your second variant also creates an InterpolationFunction, but is only once executable! Why? Thanks. $\endgroup$ – Ulrich Neumann Dec 1 '20 at 10:09
4
$\begingroup$

Here is a way that can be used in further tweaking:

f = Interpolation[Table[{x, Cos[x]}, {x, 0, 2 Pi, 2 Pi/20}]]

Block[{eps = 0.9},
 f = Interpolation @
   Transpose[{
     First@Rescale[f["Coordinates"], First@f["Domain"], {0, eps}], 
     eps*f["ValuesOnGrid"]}
   ]
 ]
f // InputForm

(* InterpolatingFunction[{{0., 0.9}}, {5, 7, 0, {21}, {4}, 0, 0, 0, 0, Automatic, {}, {}, 
  False}, {{0., 0.045, 0.09, 0.135, 0.18, 0.225, 0.27, 0.315, 0.36, 0.405, 0.45, 
  0.49500000000000005, 0.54, 0.585, 0.63, 0.675, 0.72, 0.765, 0.81, 0.855, 0.9}}, 
 {Developer`PackedArrayForm, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
  17, 18, 19, 20, 21}, {0.9, 0.8559508646656382, 0.7281152949374528, 0.5290067270632258, 
  0.27811529493745274, 0., -0.27811529493745274, -0.5290067270632258, 
  -0.7281152949374528, -0.8559508646656382, -0.9, -0.8559508646656382, 
  -0.7281152949374528, -0.5290067270632258, -0.27811529493745274, 0., 
  0.27811529493745274, 0.5290067270632258, 0.7281152949374528, 0.8559508646656382, 
  0.9}}, {Automatic}] *)
```
$\endgroup$
  • 1
    $\begingroup$ Thanks, my version Interpolation[ Transpose[{f["Coordinates"][[1]]/eps , f["ValuesOnGrid"] eps}]] also works $\endgroup$ – Ulrich Neumann Nov 30 '20 at 19:04

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.