MathGroup Archive 1995

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

Search the Archive

Re: Re: Options in self-defined functions

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1831] Re: [mg1779] Re: [mg1612] Options in self-defined functions
  • From: John Fultz <jfultz>
  • Date: Thu, 3 Aug 1995 23:53:32 -0400

> Dear MathGroupers:
> 
> Here is a further question along the lines of one Allan Hayes 
> answered for Scott A. Hill [mg1612]. 
> 
> I have had trouble making a PACKAGE that contains a function with 
> options.  I used the WRI package Miscellaneous`Audio` as a model, 
> but I must have missed some essential detail, because it does not 
> work quite correctly.  Here is the full text of a didactic package 
> containing one fairly useless but simple function AnyF, described 
> below in its usage statement:
> 
> (**************cell begins*************************)
> BeginPackage["OptionsExample`"];
> 
> AnyF::usage = 
> "AnyF[x,Name->f,ScaleFactor->s] returns f[s*x], where
> the replacements are optional. The defaults are 
> Name->Tan and ScaleFactor->2 Pi.";
> 
> Begin["`Private`"];
> 
> Options[AnyF] = {Name->Tan, ScaleFactor->2Pi};
> 
> AnyF[x_,opts___](*public*) := iaf[x,opts](*private*);
> 
> iaf[x_,opts___]:=Module[{fn,sf},
> {fn,sf} = {Name,ScaleFactor}/.{opts}/.Options[AnyF];
> af[x,fn,sf]];
> 
> af[x_,fn_,sf_]:=fn[x*sf];
> 
> Protect[AnyF];
> 
> End[];
> 
> EndPackage[]
> (****************cell ends************************)
> 
:
:
> Ask Mma what it thinks the default options are:
> 
> Options[AnyF]
> { OptionsExample`Private`Name -> Tan, 
>   OptionsExample`Private`ScaleFactor -> 2 Pi}

This causes us to immediately know you have a context problem.  The fact that
these options are available only in the `Private` context tells us that we
need to redefine them in a publicly available context (specifically in
OptionsExample`).

> I have tried several things:
> (1) Put the Options statement in the outer part, not the Private` part.
> (2)  Make usage statements for Name and ScaleFactor.

Both should have worked.

> These just make things worse.

Since they didn't work, let's examine why.  Did you, perhaps, not quit
and restart your kernel?  If you had evaluated the program with the
wrong definitions, those spurious definitions could have a wide variety
of side effects until you get rid of them.  Note that using CleanSlate
(MathSource ref. #0204-310) is a perfectly agreeable alternative to
restarting the kernel as well.

I tried this program with a fresh kernel and it worked fine (note that
all I did was add usage statements for Name and ScaleFactor):

In[1]:=
BeginPackage["OptionsExample`"];

AnyF::usage = 
"AnyF[x,Name->f,ScaleFactor->s] returns f[s*x], where
the replacements are optional. The defaults are 
Name->Tan and ScaleFactor->2 Pi.";

Name::usage = "";
ScaleFactor::usage = "";

Begin["`Private`"];

Options[AnyF] = {Name->Tan, ScaleFactor->2Pi};

AnyF[x_,opts___](*public*) := iaf[x,opts](*private*);

iaf[x_,opts___]:=Module[{fn,sf},
{fn,sf} = {Name,ScaleFactor}/.{opts}/.Options[AnyF];
af[x,fn,sf]];

af[x_,fn_,sf_]:=fn[x*sf];

Protect[AnyF];

End[];

EndPackage[]
General::spell: 
   Possible spelling error: new symbol name "Name"
     is similar to existing symbols {NameQ, Names}.

In[13]:= AnyF[w,Name->Log,ScaleFactor->7]

Out[13]= Log[7 w]

In[14]:= Options[AnyF]

Out[14]= {Name -> Tan, ScaleFactor -> 2 Pi}

John Fultz
Wolfram Research, Inc.


  • Prev by Date: New Protocol for Mathematica when using Windows 95
  • Next by Date: Re: Functional programming puzzle
  • Previous by thread: Re: Re: Options in self-defined functions
  • Next by thread: Re: Exponetial Fit