# How to correctly take an element of a vector-valued function?

I have a vector-valued function whose arguments, for some reasons, are forced to be numeric by adding ?NumericQ. I can correctly take a particular element of the vector value of the function if I input specific numberic arguments. For example,

f[x_?NumericQ, y_?NumericQ] := {-x, -y, -y - 1}

f[1, 2]
{-1, -2, -3} (* output *)

f[1, 2][[1]]
-1  (* output *)


So far so good. But sometimes I need to leave the arguments symbolic and specify them by rules. That creates mistakes. For example:

f[x, y][[1]] /. {x -> 1, y -> 2}
1 (* output *)


Mathematica executes Part before applying the rules, which is not what I want. The problem is not solved even if I use Evaluate:

Evaluate[f[x, y]][[1]] /. {x -> 1, y -> 2}
1 (* output *)


Is there a simple built-in function to correct the order (replacing x, y using the rules before evaluating the function and taking part)? Currently I need to define a new module function to take the particular element and then work with the new function. I'm wondering if there is a simpler way. Thank you!

• Unevaluated[f[x, y][[1]]] /. {x -> 1, y -> 2}?
– kglr
Sep 17, 2021 at 3:45

You can use Indexed for this purpose:
f[x_?NumericQ, y_?NumericQ] := {-x, -y, -y-1}