4

I have an equation which I try to plot and work with in Mathematica which is of the form

f(x,y) = (x^2 - a^2)^2 + x^2 y^2

Here x and y are independent variables and a is a constant. What is the standard way of defining a function such as this: should the constants/parameters be present in the list of arguments or should this list just contain the independent variables? Alternatively, should the parameters be present in the argument list but as optional arguments (with default values)?

Chris
  • 44,602
  • 16
  • 137
  • 156

2 Answers2

4

All of those options are possible, and each is reasonable in some circumstance.

Present in the list of arguments:

f[x_, y_, a_] := (x^2 - a^2)^2 + x^2 y^2

Or:

f[a_][x_, y_] := (x^2 - a^2)^2 + x^2 y^2

Only the independent variables:

Globally defined a value

a = 3.14;
f[x_, y_] := (x^2 - a^2)^2 + x^2 y^2

As optional argument

f[x_, y_, a_:3.14] := (x^2 - a^2)^2 + x^2 y^2

You'll need to be more specific regarding your use if I am to provide a more specific answer. The globally defined a value should be used with caution, but it is certainly not without its place.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • The function I am using defines a family of curves. In my notebook I thought I would specify the function and the parameters which define the shape of the curves. These parameters won't change throughout the notebook, so I thought that they should be global variables, but I wanted to ask if there was a standard/common way of defining such functions. – Chris Dec 07 '11 at 11:28
  • What is the difference between `f[a_][x_, y_]` and `f[x_, y_, a_]`? – Chris Dec 07 '11 at 11:29
  • 1
    If the `a` is truly a constant within the Notebook, then I believe it is fine to define it directly, i.e. `a = 3.14`. You may want to look into context [Unique to This Notebook](http://stackoverflow.com/questions/4896011/mathematica-separating-notebooks) if you start making a number of "global" assignments, so that they will not interfere with other Notebooks in the same session. – Mr.Wizard Dec 07 '11 at 11:34
  • 1
    the pattern `f[a_][x_, y_]` can be used to create a [pseudo-Currying function](http://stackoverflow.com/questions/5686494/currying-with-mathematica), where you might pass the arguments in two separate steps. This can handy if your are `Map`ing or `Apply`ing your function to a list or array. – Mr.Wizard Dec 07 '11 at 11:38
  • Thanks for the note on contexts - this seems very useful. – Chris Dec 07 '11 at 11:44
4

You could also create yourself a function that generates the the one that you need.

f[a_] := Function[{x, y}, Evaluate[(x^2 - a^2)^2 + x^2 y^2]]

and then use it to generate the function

f[23]

You then could use that

f[23][2, 3]

or store help = f[23] and use

help[2,3]

Hope this helps

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • Do you favor this over `f[a_][x_,y_]`? – Mr.Wizard Dec 07 '11 at 22:21
  • 2
    It depends on what you want to do: with a small change this approach for example allows you to do partial evaluation which leads to less computational complexity. –  Dec 07 '11 at 22:38