I would like to be able to define the gain function of a system from its parameters. Specifically, I'd like to define a function that accepts two inputs, call them $b$ and $w$, and returns a function that accepts a single parameter, $v$ and returns
$1/\sqrt{(w^2 - v^2)^2 + b^2 v^2}$ .

What's the right way to do this? Presumably I'd like to be able to write

gain[b,w][v]

to evaluate the function, with parameters b and w, at v. But I'd also like to be able to write

It really is that simple. Thanks. Syntactically, what is gain[b_,w_][v_]? (I'm talking about syntax trees here). Is gain[b_,w_] itself a function? I'm just trying to figure out how to conceptualize this - I've always been somewhat confused about the difference between functions and symbols in this language.
–
rogerlJul 6 '12 at 22:35

Well, gain[b_,w_][v_] and gain[b_,w_] are patterns that have definitions associated with them. As I started typing this, Mr. Wizard has posted a more in-depth discussion of where the actual function is stored. It's interesting to compare the output of SubValues applied either to gain or to a gain2 defined with the single gain2[b_, w_, v_] :=. The latter has its definition stored in DownValues[gain2].
–
JensJul 6 '12 at 22:52

One important use for this form is when you need the additional arguments (in your example v) to be held unevaluated with HoldAll, etc. The SubValues form cannot do this*, but the third argument of Function can:

This form is also superior if your function can be partially evaluated because the result of the evaluation can be incorporated into the Function, often using With.

Additional notes

Be mindful of the fact that you can accidentally blend these forms in a potentially confusing way. If you define a SubValue first (or later, if it is a form that does not evaluate on a pattern) and later a DownValue with the same pattern you may get unexpected results.

This could be particularly confusing (or powerful) if you use a constrained pattern like gain[x_Real, y_] := . . . as you may have one pattern match in one case and the other in another.

While the two forms I show above are the most common and arguably best it is worth understanding that they are not exclusive. Because heads evaluate first, including x[y] in x[y][z], any definition for x[y] that returns a function can act upon z. Here is stylePrint again, using neither SubVales nor Function:

"Pure functions" with machine-size arguments can often be auto-compiled in a way that pattern-based definitions (*Values) cannot. This is attempted by various functions such as Map, Table, and Fold as controlled by the values in SystemOptions["CompileOptions"].

Let us compare the performance of SubValues and Function using machine-size Real numbers:

* I said that SubValues definitions cannot handle Hold attributes for the additional arguments. Strictly this is true, but there are some clever workarounds, with limitations. Please see these Q&A's for examples and further discussion:

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.