Mathematica Asked on December 22, 2020
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!
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] &];
Correct answer by Michael E2 on December 22, 2020
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}] *)
```
Answered by Anton Antonov on December 22, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP