MathGroup Archive 2011

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

Search the Archive

Re: Options, OptionsPattern, and OptionsValues

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118872] Re: Options, OptionsPattern, and OptionsValues
  • From: David Reiss <dbreiss at gmail.com>
  • Date: Sat, 14 May 2011 03:05:39 -0400 (EDT)
  • References: <iqj140$rfq$1@smc.vnet.net>

Try an approach which names the option pattern and then uses this
option variable to pass the options as in the following small
modification of your code:


Options[f] = {a -> "None", b -> "both", c -> "either", dog -> False};

f[x_, opts : OptionsPattern[]] :=
 If[OptionValue[dog], g[x, opts], h[x, opts]]

g[x_, opts : OptionsPattern[f]] := {3 x, OptionValue[a],
  OptionValue[b]}

h[x_, opts : OptionsPattern[f]] := {4*x/7, OptionValue[a],
  OptionValue[c]}


Then you will get, for example,


In[61]:= f[2, c -> "left"]

Out[61]= {8/7, "None", "left"}


Hope this helps,
David



On May 13, 6:28 am, blamm64 <blam... at charter.net> wrote:
> Hi,
>
> I am developing a package for implementing polynomial linear least
> squares time domain filter and ran across the subject of this post as
> an interesting alternative to what I was doing to pass options.  There
> is one exported function, so it gets all the user-supplied options,
> and it then handles which private functions actually do the work and
> how they go about by receiving options from the exported function.  So
> I tried what amounts to the following:
>
> In[1]:= Options[f]={a->"None",b->"both",c->"either",dog->False};
> In[2]:= f[x_,OptionsPattern[]]:=If[OptionValue[dog],g[x],h[x]]
> In[3]:= g[x_,OptionsPattern[f]]:={3 x,OptionValue[a],OptionValue[b]}
> In[4]:= h[x_,OptionsPattern[f]]:={4*x/7,OptionValue[a],OptionValue[c]}
>
> In[5]:= f[2,c->"left"]
> Out[5]= {8/7,None,either}
> In[6]:= f[2,c->"None"]//Trace
> Out[6]= {{c->None,c->None},f[2,c->None],If[OptionValue[f,{c->None},dog],g[2],h[2]],{OptionValue[f,{c-
> >None},dog],False},If[False,g[2],h[2]],h[2],{(4 2)/7,OptionValue[f,
>
> {},a],OptionValue[f,{},c]},{{{1/7,1/7},2/7,2/7},(4 2)/7,8/7},
> {OptionValue[f,{},a],None},{OptionValue[f,{},c],either},
> {8/7,None,either}}
>
> My hope was to pass options to the exported function, which used some
> of those options to determine which worker to call and then passed all
> those options to the workers, who decide what options they need and
> what to do with those options.  I was trying to implement 'find what
> varies and encapsulate it'.  I came up with this scheme by looking at
> Documentation and reading what I could find in this forum, but saw
> nothing in either place (perhaps because it is 'intuitively obvious'
> to some this cannot be done ... ).  If this had worked, it would have
> scaled with increasing option number beautifully.  I ended up using
> alternative of passing only what the workers needed in the calls to
> them in the main exported function using OptionValue in the main and
> using a pattern <opts___> in the workers, but that does not scale well
> at all with increasing number of options.
>
> So I think all I can hope to do is have someone please explain to me
> the Trace, which I still have a hard time decoding.  I got stuck on
> the very first 'list'.  But it is plain to see where the option 'got
> lost'.  What is not plain to me is why.  I'm probably missing some
> very fundamental Mathematica evaluation paradigm.
>
> Thanks to any and all who help me out.
>
> -Brian L.



  • Prev by Date: Re: Complex arithmetic identity question
  • Next by Date: Re: How to select all cells in a notebook with specific property?
  • Previous by thread: Re: Options, OptionsPattern, and OptionsValues
  • Next by thread: Re: Options, OptionsPattern, and OptionsValues