MathGroup Archive 2011

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

Search the Archive

Aw: Options, OptionsPattern, and OptionsValues

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118873] Aw: Options, OptionsPattern, and OptionsValues
  • From: Sebastian Hofer <sebhofer at gmail.com>
  • Date: Sat, 14 May 2011 03:05:50 -0400 (EDT)
  • Reply-to: comp.soft-sys.math.mathematica at googlegroups.com

Am Freitag, 13. Mai 2011 12:28:16 UTC+2 schrieb blamm64:
> 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.

I'm not completely sure what you are trying to achieve, but if you are looking for a solution which results in

In[5]:= f[2,c->"left"]
Out[5]= {8/7,None,left}

then you should try this:

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

HTH
Sebastian


  • Prev by Date: Re: Options, OptionsPattern, and OptionsValues
  • Next by Date: Re: Complex arithmetic identity question
  • Previous by thread: Re: Maintaining the order of terms when adding symbolic expressions
  • Next by thread: Re: Equation numbering