0
$\begingroup$

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.

$\endgroup$

2 Answers 2

6
$\begingroup$

MapThread does the trick:

In[1]:= MapThread[#[#2] &, {{y1, y2}, {t1, t2}}]
Out[1]= {y1[t1], y2[t2]}
$\endgroup$
3
  • 1
    $\begingroup$ If you don't mind using deprecated/undocumented functions, you could do MapThread[Compose, {{y1, y2}, {t1, t2}}] instead. $\endgroup$
    – Carl Woll
    Commented Aug 28, 2017 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
    Commented Aug 28, 2017 at 16:28
  • $\begingroup$ Thank you. Both methods work great. I ended up using Mapthread with Compose. $\endgroup$ Commented Aug 28, 2017 at 16:31
1
$\begingroup$

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]}]
$\endgroup$
1
  • $\begingroup$ Thank you. very nice application of MapIndexed. $\endgroup$ Commented Aug 28, 2017 at 17:15

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.