Functions with both optional arguments and options
This is a common problem when we want to allow both options (name -> value
) and optional arguments (arguments that take a default value when omitted) in a function.
The usual solution is to make the patterns for the optional arguments specific enough that they will not match any options.
I would do it like this:
plotContour[
func_,
xRange : {_?NumericQ, _?NumericQ} : {-2,2} , yRange : {_?NumericQ, _?NumericQ} : {-2,2},
opt : OptionsPattern[]
] := ...
Things to pay attention to:
When we add a name to a generic
Pattern
, and we also make the corresponding argument optional, we must use two colons like this:name : pattern : defaultValue
Make sure that
defaultValue
itself will match thepattern
(related)Keep in mind that
OptionsPattern[]
does not only match a sequence of options such asa -> 1, b :> 2
, but also a list of options such as{a -> 1, b :> 2}
. Thus when making thepattern
specific, it is not sufficient to require it to be a list. This is why I used_?NumericQ
instead of_
.
As already said in a comment, I would not treat what feels more like an option as an usual argument to the function. Instead you can use OptionsPattern
and friends.
For a generic function with both own arguments, and potential arguments supplied to functions used within you can use the following design pattern
Define your own function's options in one of the following fashions
with (protected) symbol (offers the advantage of auto-completion)
Protect[xRange, yRange]; Options[myContourPlot] = {xRange -> {-2, 2}, yRange -> {-2, 2}}
with Strings
Options[myContourPlot] = {"xRange" -> {-2, 2}, "yRange" -> {-2, 2}}
And define your function with
myContourPlot[func_, opts : OptionsPattern[{myContourPlot, ContourPlot}]] :=
Module[{x1, x2, y1, y2},
(* read in your options *)
{x1, x2} = OptionValue[xRange] ;
{y1, y2} = OptionValue[yRange];
ContourPlot[Re[func[x + I*y]], {x, x1, x2}, {y, y1, y2},
Evaluate@FilterRules[{opts}, Options@ContourPlot]]]
Now you can call your function with
myContourPlot[Exp, xRange -> {-3, 3}, ContourStyle -> Dashed]
or (depending on your choice concerning symbols or strings as option names)
myContourPlot[Exp, "xRange" -> {-3, 3}, ContourStyle -> Dashed]
As suggested in the comment by Sascha, you can use options for the ranges as well as the standard options of ContourPlot, like this:
Options[plotContour] = {xRange -> {-2, 2}, yRange -> {-2, 2}}~Join~Options[ContourPlot];
plotContour[func_, opts : OptionsPattern[]] :=
Block[{x, y},
With[{xr = {x}~Join~OptionValue[xRange],
yr = {y}~Join~OptionValue[yRange]},
ContourPlot[Re[func[x + I*y]], xr, yr,
Evaluate@FilterRules[{opts}, Options[ContourPlot]]]]]
And then for example:
plotContour[#^2 &, ContourStyle -> Red, xRange -> {-1, 1}]