Options
- To: mathgroup at smc.vnet.net
- Subject: [mg88376] Options
- From: tommcd <TCMcDermott at gmail.com>
- Date: Sat, 3 May 2008 06:17:23 -0400 (EDT)
Hi, >From reading up on options in the documentation centre and this news group I think there are at least two ways for dealing with options given in the form of rules. (1) The current (documented) method for using options is to use OptionValue, so given a list of options for a function f[x_, opts:OptionsPattern[]] ; Options[f]={ optA-> A0, optB->B0, ....} and using f[x, optA->a] see function "new" below then within f the actual values {a,b,...} for the options can be given by {a, b, ...} = OptionValue[#] & /@ {optA, optB, ...}; This is probably an improvement over the previous methods, (2) {a, b, ...} = {optA, optB, ...} /. {opts} /. Options[f] where fis defined as f[x_, opts : ___] see function "old" below or the undocumented variant {a, b,...} = {optA, optB, ...} /. Flatten[{opts, Options[g]}]; with f[x_, opts : ___?OptionQ] see function "old2" below However I'm looking for advice on how to deal with more general options of the form optC->{C0,optD->D0} such as for example NMinimize[{Sin[x] + .5 x, -10 <= x <= 10}, {x}, Method -> {"RandomSearch", "PostProcess" -> "InteriorPoint"}] I'd like to see some example code demonstrating how such options are parsed. When an option such as this is passed to "new","old" & "old2" below it appears OptionValue and the other methods cannot be used to pick out these "interior" options automatically so I assume further parsing is required but am wondering if there are already standard ways of doing this. I considered something like FlattenRules[rules : OptionsPattern[]] := Flatten[ rules //. { (a_ -> {m : (_ -> _) ..}) -> m, (a_ -> {x_, m : (_ -> _) ..}) -> {a -> x, m} } ] used in the function "test" below, but this is not very general. I ask this because I've recently converted an implementation of Shor minimization, SolvOpt (The original author has provided Fortran,C& other code at http://www.uni-graz.at/imawww/kuntsevich/solvopt/ ) to Mathematica. I want to make this version publicly available but i like to give it a Mathematica look and feel first. Currently the options are passed via an array of real values in the style of C/Fortran but I'd like to make it behave in a similar way to NMinimize. Regards, Tom McDermott In[2]:= Options[new] = Options[old] = Options[old2] = Options[test] = {optA -> A0, optB -> B0, optC -> {C0, optD -> D0}} Out[2]= {optA -> A0, optB -> B0, optC -> {C0, optD -> D0}} In[3]:= FlattenRules[rules : OptionsPattern[]] := Flatten[ rules //. { (a_ -> {m : (_ -> _) ..}) -> m, (a_ -> {x_, m : (_ -> _) ..}) -> {a -> x, m} } ] In[4]:= FlattenRules[{optA -> A0, optB -> {B0, B1}, optC -> {optD -> D0, optE -> eeee}, optC -> {D0, optE -> eeee}}] Out[4]= {optA -> A0, optB -> {B0, B1}, optD -> D0, optE -> eeee, optC -> D0, optE -> eeee} In[5]:= new[x_, opts : OptionsPattern[]] := Module[{a, b, c, d}, {a, b, c, d} = OptionValue[#] & /@ {optA, optB, optC, optD}; Print["{a,b,c,d}=", {a, b, c, d}]; Print["opts = ", opts]; ] In[6]:= old[x_, opts : ___] := Module[{a, b, c, d}, {a, b, c, d} = {optA, optB, optC, optD} /. {opts} /. Options[old]; Print["{a,b,c,d}=", {a, b, c, d}]; Print["opts = ", opts]; ] In[7]:= old2[x_, opts : ___?OptionQ] := Module[{a, b, c, d}, {a, b, c, d} = {optA, optB, optC, optD} /. Flatten[{opts, Options[old2]}]; Print["{a,b,c,d}=", {a, b, c, d}]; Print["opts = ", opts]; Print["Flatten[{opts,Options[old2]}] = ", Flatten[{opts, Options[old2]}]]; ] In[8]:= test[x_, opts : ___?OptionQ] := Module[{a, b, c, d}, {a, b, c, d} = {optA, optB, optC, optD} /. FlattenRules[{opts, Options[test]}]; Print["{a,b,c,d}=", {a, b, c, d}]; Print["opts = ", opts]; Print[" Flatten[{opts,Options[test]}] = ", Flatten[{opts, Options[test]}]]; Print["FlattenRules[{opts,Options[test]}] = ", FlattenRules[{opts, Options[test]}]]; ] In[9]:= new[x, optA -> aaa, optC -> {ccc, optD -> ddd}] During evaluation of In[9]:= OptionValue::optnf: Option name optD not \ found in defaults for new. >> During evaluation of In[9]:= {a,b,c,d}={aaa,B0,{ccc,optD->ddd},optD} During evaluation of In[9]:= opts = optA->aaaoptC->{ccc,optD->ddd} In[10]:= old[x, A -> aaa, C -> {ccc, D -> ddd}] During evaluation of In[10]:= {a,b,c,d}={A0,B0,{C0,optD->D0},optD} During evaluation of In[10]:= opts = A->aaaC->{ccc,D->ddd} In[11]:= old2[x, optA -> aaa, optC -> {ccc, optD -> ddd}] During evaluation of In[11]:= {a,b,c,d}={aaa,B0,{ccc,optD->ddd},optD} During evaluation of In[11]:= opts = optA->aaaoptC->{ccc,optD->ddd} During evaluation of In[11]:= Flatten[{opts,Options[old2]}] = \ {optA->aaa,optC->{ccc,optD->ddd},optA->A0,optB->B0,optC->{C0,optD- >D0}\ } In[12]:= test[x, optA -> aaa, optC -> {ccc, optD -> ddd}] During evaluation of In[12]:= {a,b,c,d}={aaa,B0,ccc,ddd} During evaluation of In[12]:= opts = optA->aaaoptC->{ccc,optD->ddd} During evaluation of In[12]:= Flatten[{opts,Options[test]}] = \ {optA->aaa,optC->{ccc,optD->ddd},optA->A0,optB->B0,optC->{C0,optD- >D0}\ } During evaluation of In[12]:= FlattenRules[{opts,Options[test]}] = \ {optA->aaa,optC->ccc,optD->ddd,optA->A0,optB->B0,optC->C0,optD->D0}