Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2336 > unrolled thread

Options, OptionsPattern, and OptionsValues

Started byblamm64 <blamm64@charter.net>
First post2011-05-13 10:28 +0000
Last post2011-05-16 07:35 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Options, OptionsPattern, and OptionsValues blamm64 <blamm64@charter.net> - 2011-05-13 10:28 +0000
    Re: Options, OptionsPattern, and OptionsValues David Reiss <dbreiss@gmail.com> - 2011-05-14 07:10 +0000
    Re: Options, OptionsPattern, and OptionsValues blamm64 <blamm64@charter.net> - 2011-05-16 07:35 +0000

#2336 — Options, OptionsPattern, and OptionsValues

Fromblamm64 <blamm64@charter.net>
Date2011-05-13 10:28 +0000
SubjectOptions, OptionsPattern, and OptionsValues
Message-ID<iqj140$rfq$1@smc.vnet.net>
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.

[toc] | [next] | [standalone]


#2353

FromDavid Reiss <dbreiss@gmail.com>
Date2011-05-14 07:10 +0000
Message-ID<iql9u0$9te$1@smc.vnet.net>
In reply to#2336
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...@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.

[toc] | [prev] | [next] | [standalone]


#2417

Fromblamm64 <blamm64@charter.net>
Date2011-05-16 07:35 +0000
Message-ID<iqqk42$2dr$1@smc.vnet.net>
In reply to#2336
Thanks to you All, all variants (two) worked flawlessly.  As I wrote
in a private communication to DMB, on my part, Ouch!  I missed that g
and h never received anything but the default OptionsPattern[f].  Yes,
it *is* really simple when someone else points it out!

Thanks to you All again.

-Brian L.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web