I have a list of numerical functions (interpolation functions) that I'd like to get the values of each one at different points. For example: the functions are {y1[t],y2[t]} and the points are {t1, t2}; I'd like to get the result {y1[t1], y2[t2]}.

I used Diagonal to get the answer:

Diagonal[{y1[t],y2[t]}/. t-> {t1,t2}]

I am wondering if there is a better way to avoid unnecessary replacements.


MapThread does the trick:

In[1]:= MapThread[#[#2] &, {{y1, y2}, {t1, t2}}]
Out[1]= {y1[t1], y2[t2]}
  • 1
    $\begingroup$ If you don't mind using deprecated/undocumented functions, you could do MapThread[Compose, {{y1, y2}, {t1, t2}}] instead. $\endgroup$
    – Carl Woll
    Aug 28 '17 at 16:10
  • $\begingroup$ And {y1,y2} can be obtained as {y1[t], y2[t]}[[All, 0]] using the OP's input. Or Head /@ {y1[t], y2[t]}. $\endgroup$
    – Szabolcs
    Aug 28 '17 at 16:28
  • $\begingroup$ Thank you. Both methods work great. I ended up using Mapthread with Compose. $\endgroup$ Aug 28 '17 at 16:31

MapThread is probably your best shot, and in particular this version most closely mirrors your original approach, which did not require extracting the heads or pure-function versions of the $y_i$:

MapThread[#1 /. t -> #2 &, {{y1[t], y2[t]}, {t1, t2}}]

You could do this directly using Thread:

Thread[f[{y1[t], y2[t]}, {t1, t2}]] /. f -> (#1 /. t -> #2 &)

By defining an auxially variable $t_i=\{t_1,t_2\}$, you could also do this using MapIndexed:

ti = {t1, t2};
MapIndexed[#1 /. t -> ti[[First@#2]] &, {y1[t], y2[t]}]
  • $\begingroup$ Thank you. very nice application of MapIndexed. $\endgroup$ Aug 28 '17 at 17:15

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.