Re: Two different packages can not Need[] the same utility package?
- To: mathgroup at smc.vnet.net
- Subject: [mg79935] Re: [mg79867] Two different packages can not Need[] the same utility package?
- From: "Benedek Kovács" <benedek.kovacs.mr at gmail.com>
- Date: Thu, 9 Aug 2007 05:17:56 -0400 (EDT)
- References: <200708070532.BAA12076@smc.vnet.net> <46B881A5.3050204@wolfram.com>
Hi I have corrected the code according to your comments (the typo and the BeginPackage). The result is different than it was. (I actually don't understand why. According to the documentation <<BeginPackage<http://reference.wolfram.com/mathematica/ref/BeginPackage.html> ["context`", {"need1`", "need2`", ...}] calls Needs<http://reference.wolfram.com/mathematica/ref/Needs.html>on the needi. >> BeginPackage["f`",{"something`"}] and BeginPackage["f`"];Needs["something`"] has to result the same. Or am I misunderstanding something?) Anyway, with the modified code the function "f" didn't work either. I have browsed the documentation and now I have another problem in close connection to this. When I get Statistics`ContinuousDistributions`, a couple of packages also will be in the $ContextPath, like Statistics`NormalDistribution which I think has the definition for function: NormalDistribution[x_,y_]. However, when I call the package with BeginPackage["ff`",{"Statistics`ContinuousDistributions`"}], the $ContextPath will not contain Statistics`NormalDistribution thus it is not defined (not known by the kernel). How can this happen? I suppose that the packages does not recursively Need each other. Is that right? I have tried the following example: * BeginPackage["aa`"];Unprotect[a];Begin["Private`"];a[x_]:=x+1;End[];Protect[a];EndPackage; BeginPackage["bb`",{"aa`"}];Unprotect[b];Begin["Private`"];b[x_]:=10 x;End[];Protect[b];EndPackage; BeginPackage["cc`",{"bb`"}];Unprotect[c];Begin["Private`"];c[x_]:=x x;End[];Protect[c];EndPackage; $ContextPath * {cc*`,*bb`,System`} Altough I have "Need"-ed aa` for bb` clearly. What do you think? Regards Bence On 8/7/07, Adam Berry <adamb at wolfram.com> wrote: > > Hello, > > you should declare the packages dependencies using the second argument > of BeginPackage, for example; > > BeginPackage["foo`",{"Statistics`ContinuousDistributions`"}] > > This will solve the problem you are having, see the documentation for > BeginPackage at > > http://reference.wolfram.com/mathematica/ref/BeginPackage.html > > Adam Berry > Wolfram Research, Inc > > benedek.kovacs.mr at gmail.com wrote: > > Hi > > > > Here is a problem a I face with: > > I define two packages. Both needs the same > > Statistics`ContinuousDistibutions` package. However, the second one > > can not use it. Why? And how to resolve this problem? > > > > To make it more exact, in the following example I will define two > > packages: ff and gg with the corresponding f and g functions both > > using NormalDistribution. The first one evaluated works while the > > second does not. the problem is even more disappointing if we Get[] > > one packege twice and then are unable to use the function. More > > complicated verisions can appear using the Sytem` functions or using > > built-in packages where we does not even know what other packages are > > invited. > > > > Does anyone have a solution for this problem? > > > > Kind Regards > > Bence > > > > In[1]:=BeginPackage["ff`"] > > Out[1]=ff` > > In[2]:=Unprotect[f] > > Out[2]={} > > In[3]:=Begin["`Private`"] > > Out[3]=ff`Private` > > In[4]:=Needs["Statistics`ContinuousDistributions`"] > > In[5]:=Options[f]:={MyDistributionFunction\[Rule](NormalDistribution[#, > > 0.2 #]&)} > > f[x_,opts___]:=Random[(MyDistributionFunction/.{opts}/.Options[f])[x]] > > In[7]:=End[] > > Out[7]=ff`Private` > > In[8]:=Protect[f] > > Out[8]={f} > > In[9]:=EndPackage[] > > > > In[10]:=f[3] > > Out[10]=2.16487 > > In[11]:=ff`f[3] > > Out[11]=4.16217 > > > > In[12]:=BeginPackage["gg`"] > > Out[12]=gg` > > In[13]:=Unprotect[g] > > Out[13]={} > > In[14]:=Begin["`Private`"] > > Out[14]=gg`Private` > > In[15]:=Needs["Statistics`ContinuousDistributions`"] > > In[16]:=Options[g]:={MyDistributionFunction\[Rule] > > (NormalDistribution[#,0.2 #]&)} > > g[x_,opts___]:=Random[(MyDistributionFunction/.{opts}/.Options[f])[x]] > > In[18]:=End[] > > Out[18]=gg`Private` > > In[19]:=Protect[g] > > Out[19]={g} > > In[20]:=EndPackage[] > > > > In[21]:=f[3] > > Out[21]=2.88763 > > In[22]:=ff`f[3] > > Out[22]=3.09767 > > > > In[23]:=g[3] > > >From In[23]:=\!\(\* > > RowBox[{\(Random::"randt"\), \(\(:\)\(\ \)\), "\<\"Type > > specification \\!\\( > > gg`Private`MyDistributionFunction[3]\\) in \ > > \\!\\(Random[\\(\\(gg`Private`MyDistributionFunction[3]\\)\\)]\\) > > should be \ > > Real, Integer, or Complex. \\!\\(\\*ButtonBox[\\\"More...\\\", \ > > ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \ > > ButtonData:>\\\"Random::randt\\\"]\\)\"\>"}]\) > > Out[23]=Random[gg`Private`MyDistributionFunction[3]] > > In[24]:=gg`g[3] > > >From In[24]:=\!\(\* > > RowBox[{\(Random::"randt"\), \(\(:\)\(\ \)\), "\<\"Type > > specification \\!\\( > > gg`Private`MyDistributionFunction[3]\\) in \ > > \\!\\(Random[\\(\\(gg`Private`MyDistributionFunction[3]\\)\\)]\\) > > should be \ > > Real, Integer, or Complex. \\!\\(\\*ButtonBox[\\\"More...\\\", \ > > ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \ > > ButtonData:>\\\"Random::randt\\\"]\\)\"\>"}]\) > > Out[24]=Random[gg`Private`MyDistributionFunction[3]] > > > > >
- References:
- Two different packages can not Need[] the same utility package?
- From: "benedek.kovacs.mr@gmail.com" <benedek.kovacs.mr@gmail.com>
- Two different packages can not Need[] the same utility package?