MathGroup Archive 2011

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

Search the Archive

Re: Options, OptionsPattern, and OptionsValues

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118879] Re: Options, OptionsPattern, and OptionsValues
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Sat, 14 May 2011 03:06:55 -0400 (EDT)

ClearAll[f, g, h]
Options[f] = {a -> "None", b -> "both", c -> "either", dog -> False};
f[x_, opts : OptionsPattern[f]] :=
  If[OptionValue[dog], g[x, opts], h[x, opts]]
g[x_, OptionsPattern[f]] := {3 x, OptionValue[a], OptionValue[b]}
h[x_, OptionsPattern[f]] := {4*x/7, OptionValue[a], OptionValue[c]}

f[2, c -> "left"]

{8/7, "None", "left"}

f[2, c -> "None"]

{8/7, "None", "None"}

Bobby

On Fri, 13 May 2011 05:26:36 -0500, blamm64 <blamm64 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.
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Maintaining the order of terms when adding symbolic expressions
  • Next by Date: Aw: Options, OptionsPattern, and OptionsValues
  • Previous by thread: Options, OptionsPattern, and OptionsValues
  • Next by thread: Re: Options, OptionsPattern, and OptionsValues