1
$\begingroup$

What is the most elegant way of mapping an array of functions to an array of arguments of the same length? In practice I want to map {f1,f2,f3} and {x1,x2,x3} to {f[x1],f[x2],f[x3]}. Thanks!

EDIT

I should have mentioned that {f1,f2,f3} are the solutions of a differential equation:

fsol[t_] = Table[First[f[t] /. NDSolve[{f''[t] + m^2 f[t] == 0,
        f[0] == c[[i]], f'[0] == cp[[i]]}, f[t], {t,0,1}]], {i,1,3}]

Then the output is something like

{InterpolatingFunction[t],InterpolatingFunction[t],InterpolatingFunction[t]}

When I evaluate fsol[t] for some numerical t I get a numerical array as a result. With the proposed solutions the argument gets appended at the end, but the InterpolatingFunctions are not evaluated at that point.

EDIT 2:

Here is a minimal working example:

c = {1, 2, 3};
fsol = Table[NDSolveValue[{f''[t] + f[t] == 0, f[0] == c[[i]], f'[0] == 0}, f, {t, 0, 5}], {i, 1, 3}]
xtest = {1, 2, 3};

Then MapThread[#1[#2] &, {fsol, xtest}] indeed gives the desired result. The remaining question is, can the same be done with the derivative of fsol? MapThread[#1[#2] &, {fsol', xtest}] doesn't seem to work, since Mathematica doesn't interpret that as the derivative of the components of the array.

$\endgroup$
7
  • 5
    $\begingroup$ MapThread[#1[#2] &, {{f1, f2, f3}, {x1, x2, x3}}] $\endgroup$ Commented Mar 14 at 23:06
  • $\begingroup$ Very helpful, but see comment below. $\endgroup$
    – gabo_18
    Commented Mar 14 at 23:39
  • 3
    $\begingroup$ @gabo_18 You mention that {f1,f2,f3} are InterpolatingFunctions returned by NDSolve. In that case you should probably have a look at the alternative NDSolveValue. $\endgroup$ Commented Mar 15 at 0:12
  • 1
    $\begingroup$ A minimal working example would be helpful $\endgroup$
    – user1066
    Commented Mar 15 at 9:29
  • 1
    $\begingroup$ …As to EDIT 2: why not MapThread[#1'[#2] &, {fsol, xtest}]? $\endgroup$
    – xzczd
    Commented Mar 16 at 2:20

1 Answer 1

4
$\begingroup$
MapThread[Construct, {{f1, f2, f3}, {x1, x2, x3}}]
$\endgroup$
2
  • $\begingroup$ Very helpful, but see comment below. $\endgroup$
    – gabo_18
    Commented Mar 14 at 23:39
  • 4
    $\begingroup$ It should still work (all of the suggestions so far should still work). If it's not working, then you need to show more information about your interpolating functions. $\endgroup$
    – lericr
    Commented Mar 14 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.