Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

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

Search the Archive

RE: Different Methods inside one package

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33729] RE: [mg33715] Different Methods inside one package
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Wed, 10 Apr 2002 00:49:12 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Guillermo,

I have to add a note (see below)

> -----Original Message-----
> From: Wolf, Hartmut 
To: mathgroup at smc.vnet.net
> Sent: Tuesday, April 09, 2002 11:23 AM
> To: 'J. Guillermo Sanchez'; mathgroup at smc.vnet.net
> Subject: [mg33729] RE: [mg33715] [mg33715] Different Methods inside one package
> 
> 
> 
> > -----Original Message-----
> > From: J. Guillermo Sanchez [mailto:guillerm at usal.es]
To: mathgroup at smc.vnet.net
> > Sent: Tuesday, April 09, 2002 7:03 AM
> > To: mathgroup at smc.vnet.net
> > Subject: [mg33729] [mg33715] [mg33715] Different Methods inside one package
> > 
> > 
> > 
> > (* Dear friends, I wish build a package where the user, as 
> one option,
> > can be choose one of three methods of evaluation, i.e: 
> > Automatic, LT or 
> > EV. Some thing like this silly example *)
> > 
> > BeginPackage["test`test`"]
> > 
> > methodTest::usage="methodTest. This is an example"
> > 
> > Options[methodTest]={Method\[Rule]Automatic};
> > 
> > Begin["`Private`"]
> > 
> > methodTest[matrixA_, incond_, opts___]:=
> >   If[Automatic == Method/.{opts}/.Options[methodTest],
> >     Module[{a, b, r},(a = matrixA; b = incond; r = a b)]]
> > 
> > methodTest[matrixA_, incond_, opts___]:=
> >   If[LT == Method/.{opts}/.Options[methodTest],
> >     Module[{a, b, r},(a = matrixA; b = incond; r = a /b)]]
> > 
> > methodTest[matrixA_, incond_, opts___]:=
> >   If[EV == Method/.{opts}/.Options[methodTest],
> >     Module[{a, b, r},(a = matrixA; b = incond; r = a + b)]]
> > 
> > End[]
> > 
> > Protect[methodTest];
> > 
> > EndPackage[]
> > 
> > list1 ={{a11, a12 },{a21,a22}}
> > 
> > methodTest[list1,list2]
> > 
> > methodTest[list1,list2, Method\[Rule]EV]
> > 
> > methodTest[list1,list2, Method\[Rule]LT]
> > 
> > 
> > (*How can I get that this example work?*)
> > 
> 
> Guillermo,
> 
> observe
> 
> In[15]:= BeginPackage["test`test`"]
> 
> methodTest::usage = "methodTest. This is an example";
> LT::usage = "read manual";
> EV::usage = "before operating";
> 
> Options[methodTest] = {Method -> Automatic};
> 
> Begin["`Private`"]
> 
> methodTest[matrixA_, incond_, opts___] := 
>   Module[{a, b, r, m = Method /. Flatten[{opts}] /. 
> Options[methodTest]},
>     a = matrixA; b = incond;
>     Which[
>       m === Automatic, r = a b, 
>       m === LT, r = a/b,
>       m === EV, r = a + b,
>       True, "something went wrong"]]
> 
> End[]
> 
> Protect[methodTest]
> 
> EndPackage[]
> 
> In[25]:= list1 = {{a11, a12}, {a21, a22}}
> 
> In[26]:= methodTest[list1, XXX]
> Out[26]= {{a11 XXX, a12 XXX}, {a21 XXX, a22 XXX}}
> 
> In[27]:= methodTest[list1, XXX, Method -> EV]
> Out[27]= {{a11 + XXX, a12 + XXX}, {a21 + XXX, a22 + XXX}}
> 
> In[28]:= methodTest[list1, XXX, Method -> LT]
> Out[28]= {{a11/XXX, a12/XXX}, {a21/XXX, a22/XXX}}
> 
> In[29]:= ?LT
> In[30]:= ?EV
> 
> 
> Two things: first, if you repeat a definition (i.e. the same 
> lhs of a transformation rule), only the last one will be 
> stored. Adding a condition wouldn't work in your case 
> however. Second, you must export your symbols (LT, EV), 
> otherwise global symbols will be inserted at call (and not be 
> recognized in the context "test`test`Private`")
> 
> In[33]:= Context[EV]
> Out[33]= "test`test`"
> 
> --
> Hartmut
> 

I must correct myself: adding a condition might help!

So you have this alternative:

...

Begin["`Private`"]

methodTest[matrixA_,incond_,opts___]:=
  With[{m=Method/.Flatten[{opts}]/.Options[methodTest]},
       Module[{a,b,r},
        a=matrixA;b=incond;r=a b] /; m===Automatic]

methodTest[matrixA_,incond_,opts___]:=
  With[{m=Method/.Flatten[{opts}]/.Options[methodTest]},
        Module[{a,b,r},
        a=matrixA;b=incond;r=a/ b] /; m===LT]

methodTest[matrixA_,incond_,opts___]:=
  With[{m=Method/.Flatten[{opts}]/.Options[methodTest]},
        Module[{a,b,r},
        a=matrixA;b=incond;r=a +b] /; m===EV]

methodTest[matrixA_,incond_,opts___]:="Something went wrong";

End[]

...


The Condition in With is effective for pattern matching. To understand this,
scan the archive for "Allan Hayes", "Michael Trott" and "Adam Strzebonski".

kind regards,
Hartmut Wolf



  • Prev by Date: Seminar on Computational VaR using Mathematica
  • Next by Date: RE: Question - Karmarkar's Algorithm
  • Previous by thread: RE: Different Methods inside one package
  • Next by thread: RE: Different Methods inside one package