I frequently encounter the situation where I have a function of two real variables defined, e.g.:
f[x_, y_] := 9 - x^2 - y^2
But then I need to feed into f
not just two numbers but rather a pair, i.e., a 2-element list, e.g.:
p = {3, 4};
f[p]
How might this be done simply -- without having to make a separate definition
f[{x_, y_}] := f[x, y]
Naively I used to expect that the following would work:
f[Sequence[p]]
But it doesn't. What does work, though, is:
f[Sequence @@ p]
Are there other, simpler techniques?
I bring this up as an advocate for relatively new users of Mathematica, because I think Sequence
along with Apply
is much too sophisticated for them!
Note: The root cause is the typical "abuse of notation" in math, where a "function of two real variables" is really a function of ordered pairs of reals but the notation $f(x, y)$ obscures that.
f@@p
:) $\endgroup$Sequence
along withApply
is much too sophisticated for them" - but here is a problem: what you request is not quite a trivial manipulation, andApply
andSequence
are exactly the tools. I can imagine that one may be able to find some twisted ways out, but I am sure those will be (much) harder to understand thanApply
orSequence
. Actually, I don't consider the latter two so hard to comprehend.Apply
"eats up" the head it operates on, replacing by another one, whileSequence
means "no head", and gives a bare sequence of function's arguments. Not hard, even for a newbie. $\endgroup$f@@p
goes against that mantra drilled into such beginners, "The argument(s) to a function are enclosed in square brackets." $\endgroup$