MathGroup Archive 1995

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

Search the Archive

Re: Options in self-defined functions


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************************)

Test the package:

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

AnyF[t] 
Tan[2 Pi t]

The defaults work.  Now try it with options:

AnyF[w,Name->Log,ScaleFactor->7]
Tan[2 Pi w]

The options do NOT work!  
Ask Mma what it thinks the default options are:

Options[AnyF]
{ OptionsExample`Private`Name -> Tan, 
  OptionsExample`Private`ScaleFactor -> 2 Pi}

To make them work you have to give the full names:

AnyF[w,OptionsExample`Private`Name->Log,
       OptionsExample`Private`ScaleFactor->7]
Log[7 w]

It works, but these long names are not what one wants.
How do you make the short names work? 

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.
These just make things worse.

Can anybody spot the problem?

Thanks in advance-  Martin McClain

AnyF[t] 
Tan[2 Pi t]

The defaults work.  Now try it with options:

AnyF[w,Name->Log,ScaleFactor->7]
Tan[2 Pi w]

The options do NOT work!  
Ask Mma what it thinks the default options are:

Options[AnyF]
{ OptionsExample`Private`Name -> Tan, 
  OptionsExample`Private`ScaleFactor -> 2 Pi}

To make them work you have to give the full names:

AnyF[w,OptionsExample`Private`Name->Log,
       OptionsExample`Private`ScaleFactor->7]
Log[7 w]

It works, but these long names are not what one wants.
How do you make the short names work? 

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.
These just make things worse.

Thanks in advance-  Martin McClain



  • Prev by Date: Re: Base 10 to Base 2 conversion error ?
  • Previous by thread: Re: Options in self-defined functions
  • Next by thread: 1/f article reference