MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Functions with variable number of arguments and options

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105899] Re: [mg105884] Functions with variable number of arguments and options
  • From: "David Park" <djmpark at comcast.net>
  • Date: Thu, 24 Dec 2009 00:15:34 -0500 (EST)
  • References: <8549963.1261555384218.JavaMail.root@n11>

One possible solution is to give patterns to the optional arguments so that
Rule will not match.

ClearAll[g]
Options[g] = {opt -> "DefaultOptionValue"};
SyntaxInformation[
   g] = {"ArgumentsPattern" -> {_, _., _., OptionsPattern[]}};
g[x_, y : (_?NumericQ) : 0, z : (_?NumericQ) : 0,
  OptionsPattern[]] := {"g", x, y, z, OptionValue[opt]}

Then the following all work.

g[1]
g[2, 2]
g[3, 3, 3]
g[4, 4, opt -> "SpecialValue"]
g[4, 0, 4, opt -> "SpecialValue"]

If several of the optional arguments have the same pattern, then the first
must always be entered in order to enter the second. If they all have
different patterns then any one can be omitted regardless of sequence.

Here is another possibility. The idea here is to have special headers for
each argument, have default values for each argument, and allow the user to
enter the arguments in any order. It uses the following helper routine.

ClearAll[setnamedvalue];
Attributes[setnamedvalue] = {HoldFirst};
setnamedvalue[var_, name_, vallist_List] :=
 Module[{value, work},
  work = Cases[vallist, name[_]];
  If[Length[work] > 0, var = Part[work, 1, 1]]
  ]

ClearAll[f]
Options[f] = {opt -> "DefaultOptionValue"};
SyntaxInformation[f] = {"ArgumentsPattern" -> {___, OptionsPattern[]}};
f[values : (_xval | _yval | _zval) ..., OptionsPattern[]] :=
 Module[{x = 1, y = 0, z = 0},
  setnamedvalue[x, xval, {values}];
  setnamedvalue[y, yval, {values}];
  setnamedvalue[z, zval, {values}];
  {"f", x, y, z, OptionValue[opt]}]


f[]
{"f", 1, 0, 0, "DefaultOptionValue"}

f[zval[3], yval[2], opt -> "SpecialValue"]
{"f", 1, 2, 3, "SpecialValue"}


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/ 



From: Thomas M=FCnch [mailto:thomas.muench at gmail.com]

Dear Mathgroup,

I can't figure out how to define a function properly that has a variable 
number of arguments and options. When I call the function with less than 
the full set of arguments, I want the missing arguments to take on
default values. Below are two toy examples, functions f and g, that both 
behave identical. How do I make the options work when I call the
function with less than the full set of arguments?

Thank you for your help,

thomas


Clear[f];
Options[f]={opt->"DefaultOptionValue"};
SyntaxInformation[f]={"ArgumentsPattern"->{{_,_.,_.},OptionsPattern[]}};
f[x_,y_,z_,OptionsPattern[]]:={"f",x,y,z,OptionValue[opt]}
f[x_,y_,OptionsPattern[]]:={"f",x,y,0,OptionValue[opt]}
f[x_,OptionsPattern[]]:={"f",x,0,0,OptionValue[opt]}

Clear[g];
Options[g]={opt->"DefaultOptionValue"};
SyntaxInformation[g]={"ArgumentsPattern"->{{_,_.,_.},OptionsPattern[]}};
g[x_,y_:0,z_:0,OptionsPattern[]]:={"g",x,y,z,OptionValue[opt]}

(* The following simple function calls work as expected, both for f and
for g *)

f[1]
f[2,2]
f[3,3,3]

g[1]
g[2,2]
g[3,3,3]

(* But if one wants to specify a different value for the option, it only 
works if the full number of arguments are supplied, in this case 3
arguments. Otherwise, the option is interpreted as the second or third
argument instead of an option: *)

f[1,opt->"SpecialValue"]
f[2,2,opt->"SpecialValue"]
f[3,3,3,opt->"SpecialValue"] (*only this works*)

g[1,opt->"SpecialValue"]
g[2,2,opt->"SpecialValue"]
g[3,3,3,opt->"SpecialValue"] (*only this works*)






  • Prev by Date: Re: Re: 3D Animations
  • Next by Date: Re: Functions with variable number of arguments and
  • Previous by thread: Re: Functions with variable number of arguments and options
  • Next by thread: complex numbers