RE: Function in a package
- To: mathgroup at smc.vnet.net
- Subject: [mg42508] RE: [mg42493] Function in a package
- From: "David Park" <djmp at earthlink.net>
- Date: Sat, 12 Jul 2003 05:19:08 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Roger, You are using Global names in the package, (norm, fst, max). When you move the code from the notebook, Global` context, to the package, FredNoEDA`Private` context, they lose their meaning. One method would be to attach Global` in the code. TestPkgNoEDA[dat_, opts___?OptionQ] := Module[ {n, Tow, Tow0}, n = Global`norm /. Flatten[{opts}] /. {Global`norm -> Global`fst}; Tow = dat; If[ n == Global`fst, Tow0 = First[Tow] ]; If[ n == Global`max, Tow0 = Tow[[ Position[Tow, Max[Tow] ][[1, 1]] ]] ]; Tow0 ] Probable a better method would be to write usage statements for norm, fst and max and thus export their names. Then add Options[TestPkgNoEDA]= {norm -> fst}; and n= norm/. {opts} /. Options[TestPkgNoEDA] David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Roger Mason [mailto:rmason at sparky2.esd.mun.ca] To: mathgroup at smc.vnet.net Hello, I have written and tested successfully a function in a notebook. On putting the function in a package it ceases to work. Here is the package: BeginPackage["FredNoEDA`"] TestPkgNoEDA::usage = "a test function."; Begin["`Private`"] TestPkgNoEDA[dat_, opts___?OptionQ] := Module[ {n, Tow, Tow0}, n = norm /. Flatten[{opts}] /. {norm -> fst}; Tow = dat; If[ n == fst, Tow0 = First[Tow] ]; If[ n == max, Tow0 = Tow[[ Position[Tow, Max[Tow] ][[1, 1]] ]] ]; Tow0 ] End[] Protect[TestPkgNoEDA] EndPackage[]; Here is the identical function (under a different name) from a notebook: TestPkg[dat_, opts___?OptionQ] := Module[ {n, Tow, Tow0}, n = norm /. Flatten[{opts}] /. {norm -> fst}; Tow = dat; If[ n == fst, Tow0 = First[Tow] ]; If[ n == max, Tow0 = Tow[[ Position[Tow, Max[Tow] ][[1, 1]] ]] ]; Tow0 ] Here are some test data: In[2]:= tst = Table[x, {x, 1, 10}] Out[2]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Here are the results of a test: Needs["FredNoEDA`"] In[6]:= TestPkgNoEDA[tst, norm -> max] Out[6]= 1 In[11]:= TestPkg[tst, norm -> max] Out[11]= 10 Can anyone explain why the version of the function in the package returns an incorrect result? Thanks, Roger Mason