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!


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


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.


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.

  • 5
    $\begingroup$ MapThread[#1[#2] &, {{f1, f2, f3}, {x1, x2, x3}}] $\endgroup$ Mar 14 at 23:06
  • $\begingroup$ Very helpful, but see comment below. $\endgroup$
    – gabo_18
    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$ Mar 15 at 0:12
  • 1
    $\begingroup$ A minimal working example would be helpful $\endgroup$
    – user1066
    Mar 15 at 9:29
  • 1
    $\begingroup$ …As to EDIT 2: why not MapThread[#1'[#2] &, {fsol, xtest}]? $\endgroup$
    – xzczd
    Mar 16 at 2:20

1 Answer 1

MapThread[Construct, {{f1, f2, f3}, {x1, x2, x3}}]
  • $\begingroup$ Very helpful, but see comment below. $\endgroup$
    – gabo_18
    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
    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.