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:) – rm -rf Mar 12 '13 at 19:37Sequencealong withApplyis much too sophisticated for them" - but here is a problem: what you request is not quite a trivial manipulation, andApplyandSequenceare 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 thanApplyorSequence. Actually, I don't consider the latter two so hard to comprehend.Apply"eats up" the head it operates on, replacing by another one, whileSequencemeans "no head", and gives a bare sequence of function's arguments. Not hard, even for a newbie. – Leonid Shifrin Mar 12 '13 at 19:57f@@pgoes against that mantra drilled into such beginners, "The argument(s) to a function are enclosed in square brackets." – murray Mar 13 '13 at 01:06Sequence@@ptrick. However brief the explanation may be, to a beginner this still seem like magic (I know that from experience teaching). It may just be that for beginners the most straightforward approach is the additional definitionf[{x_, y_}] := f[x, y]. I was casting about for something else that's almost elementary. – murray Mar 13 '13 at 01:10f[{x_, y_}] := 9 - x^2 - y^2. Then how would you want to tell a beginner to evaluatef[3, 4]-- where there are just two scalar arguments rather than one list argument? – murray Mar 13 '13 at 01:13List. But, this is easier to explain thanApplyandSequence, so may be this is then the way to go. – Leonid Shifrin Mar 13 '13 at 01:34