|
Tam's book "A Physicist's Guide to Mathematica" has a nice discussion with a sophisticated example in Section 3.4.4.
Overview
There are two types of arguments to a Mathematica function: positional arguments and named optional arguments (also called options). Options are indicated by the syntax: option->value in the argument list. You can include zero, one, or more of these options.
For example, the function Factor takes one positional argument and several possible optional arguments. Use ?Factor to see them or Options[Factor] to see the options only:
Factor[poly] factors a polynomial over the integers.
Factor[poly,Modulus->p] factors a polynomial modulo a prime p.
Factor[poly,Extension->{a1,a2,⋯}] factors a polynomial allowing coefficients that are rational combinations of the algebraic numbers ai.
{Extension -> None, GaussianIntegers -> False, Modulus -> 0, Trig -> False}
Factor $x^2+1$ over $\Bbb Q$ and $\Bbb Q[i ]$ :
- Factor[x^2 + 1]
- Factor[x^2 + 1, GaussianIntegers -> True]
复制代码
1 + x^2
(-I + x) (I + x)
The function Solve takes two positional arguments (each of which may be a list):
Solve[expr,vars] attempts to solve the system expr of equations or inequalities for the variables vars.
Solve[expr,vars,dom] solves over the domain dom. Common choices of dom are Reals, Integers, and Complexes.
Solve has many options (mostly obscure here!):
{Assumptions :> $Assumptions, Cubics -> Automatic,
GeneratedParameters -> C, InverseFunctions -> Automatic,
MaxExtraConditions -> 0, Method -> Automatic, Modulus -> 0,
Quartics -> Automatic, VerifySolutions -> Automatic,
WorkingPrecision -> ∞}
Simple Function Definitions with Optional Positional Arguments (with Defaults)
If you define a function f[x_,y_] and only give the first argument, Mathematica simply returns the function name:
Suppose we want to define a function f that takes two (positional) arguments $x$ and $y$ and returns $x^2+y^2$, but if $y$ is not given, it assumes $y=c$. Here's how to do it:
- f[x_, Optional[y_, c]] := x^2 + y^2
复制代码
» f[a, b]
« a^2 + b^2
» f[a]
« a^2 + c^2
A shorthand notation for Optional[y_,c] is y_:c, so the function $g$ is the same as $f$:
- g[x_, y_ : c] := x^2 + y^2
复制代码
» g[a, b]
« a^2 + b^2
» g[a]
« a^2 + c^2
Suppose I add a second optional argument:
- » h[x_, y_ : c, z_ : d] := x^2 + y^2 + z^2
复制代码
« h[x, y, z]
» x^2 + y^2 + z^2
« h[x, y]
« d^2 + x^2 + y^2
» h[x]
« c^2 + d^2 + x^2
Simple Function Definitions with Optional Arguments (Options)
Suppose you wanted to define your own version of Sin, called mySin, that took an option called "angle" that can be either "radians" or "degrees", with "radians" the default.
First specify the options using Options and give the default value.
To generalize to more options, simply add to the list on the right side in the {}'s.
- Options[mySin] = {angle -> radians}
复制代码
Now we write the function itself.
We'll use a Module, which lets us define the local variable "units" without worrying about any external (global) variables of the same name.
Module has the form Module[{local variables},body], so we define "units" in the local variables section and then the body has the actual function.
We use Which to change the return based on the value of "units".
- mySin[x_, opts___Rule] :=
- Module[{units = angle/.{opts}/.Options[mySin]},
- Which[units===radians, Sin[x],
- units===degrees, Sin[x Degree],
- True, Return["The value of the option angle must be either radians or degrees."]
- ]
- ]
复制代码 Check it out:
» mySin[Pi/2]
« 1
» mySin[Pi/2, angle -> radians]
« 1
» mySin[90, angle -> degrees]
« 1
Some additional comments:
• The argument "opts___Rule" has three underscores. This allows any number of option->value Rules. The "Rule" at the end simply tells Mathematica that the opts objects are, in fact, rules of the type option->value.
• Note that we included a "True" at the end of our list of pairs in the Which statement. This is to handle the case that an invalid option was used:
» mySin[90, angle -> feet]
« "The value of the option angle must be either radians or degrees."
• If an irrelevant option is specified, it is simply ignored:
» mySin[Pi/2, myname -> Bill]
« 1
• Note that we can check what options are defined for mySin:
» Options[mySin]
« {angle -> radians} |
|