Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: Trouble Loading Packages...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128304] Re: Trouble Loading Packages...
  • From: "djmpark" <djmpark at comcast.net>
  • Date: Sat, 6 Oct 2012 01:47:51 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <12366398.1246.1349420243702.JavaMail.root@m06>

Because so many people don't know where to put packages, or don't want to
put them in the place designed for them.

Evaluate $UserBaseDirectory in a Mathematica session. In that directory you
will find a pre-established Applications folder. Put your work there. This
is your private Applications folder.

Say you are doing extended work on TopicX. Create a TopicX folder in your
private Applications folder. You can create subsidiary folders that contain
your work and organize your notebooks on this topic.

Now make the BeginPackage statement in your package (which has the name
cellNeighbours.m and is placed in the TopicX folder):

BeginPackage["TopicX`cellNeighbours`"]

Now you can load the package from anywhere by using:

<< TopicX`cellNeighbours`

You could have more than one package in the TopicX folder.

WRI has implemented a feature such that if you load the package with the
following statement (omitting the package context)

<< TopicX`

then Mathematica looks for an init.m file within a Kernel folder within the
TopicX folder and evaluates all the Initiation cells in the file. So if you
had a second package, say Package2, you could include the statements:

Get["TopicX`cellNeighbours`"]
Get["TopicX`Package2`"]

in the init.m file. 

You could also create FrontEnd/Palettes and FrontEnd/StyleSheets folders
within TopicX and then any palettes or style sheets associated with the
project would appear on your Mathematica menus. If you had more than one
palette or style sheet you could create the structure
FrontEnd/Palettes/TopicX/palettes... Then all the palettes associated with
TopicX would be grouped together within one entry on the Mathematica
Palettes menu.

And now your packages will load from anywhere because Mathematica
automatically looks in the private Applications folders for packages,
palettes, style sheets and documentation (if you create that.)


David Park
djmpark at comcast.net 
http://home.comcast.net/~djmpark/index.html 



From: Richard Palmer [mailto:rhpalmer at gmail.com] 


...using the latest version of Mathematica on a 64 bit windows machine.

Here is the relevant code


In[1]:= Directory[]
Out[1]= "C:\\Users\\Richard\\Documents"

FileNames[{"*.m"}]
{"cellNeighbours.m", "myS.m"}

FileExistsQ["cellNeighbours.m"]
True

In[5]:= FindFile["cellNeighbours.m"]
Out[5]= "C:\\Users\\Richard\\Documents\\cellNeighbours.m"
In[6]:= << cellNeighbours`
In[7]:= ?cellNeighbours
During evaluation of In[7]:= Information::notfound: Symbol cellNeighbours
not found. >>

Needs["cellNeighbours`"]
Needs::nocont: Context cellNeighbours` was not created when Needs was
evaluated. >>


Same thing happens with the myS package.  Why can't if find and load the
package?

--------------------------------------------------------------------------

This is the cellNeighbours Package

BeginPackage["cellNeighbours`"];

cellNeighbours::usage =
  "cellNeighbours[r,c] sets up a function to quickly return a list of the n
\ nearest neighbours of a given cell in an r\[ScriptX]c matrix. By deafult,
a \ toroidal distance matrix is assumed."; neighbourhoodList::usage =
  "neighbourhoodList[rI,cI,k] returns a list of the k nearest cells to \
\!\(\*SubscriptBox[\(cell\), \(r1, c1\)]\) in an r\[ScriptX]c matrix. By \
deafult, a toroidal distance matrix is assumed";

toroidalDistance::usage =
  "teroidalDistance[{r1,c1},{r2.c2}] returns a \!\(\*
  StyleBox[\"wrap\",\nFontSlant->\"Italic\"]\)\!\(\*
  StyleBox[\" \",\nFontSlant->\"Italic\"]\)\!\(\*
  StyleBox[\"around\",\nFontSlant->\"Italic\"]\)\!\(\*
  StyleBox[\" \",\nFontSlant->\"Italic\"]\)distance between two points";

distancefn::usage = "distance[a,b,len] computes a \!\(\*
  StyleBox[\"wrap\",\nFontSlant->\"Italic\"]\)\!\(\*
  StyleBox[\" \",\nFontSlant->\"Italic\"]\)\!\(\*
  StyleBox[\"around\",\nFontSlant->\"Italic\"]\) distance from a to b
between \ two cells on the same row or column.  \!\(\*
  StyleBox[\"len\",\nFontSlant->\"Italic\"]\) is the length of the \
row/column.";


Begin["Private`"];
rows = -1;
cols = -1;
cellNeighbourhood = -1;
cellIndices = -1;
cellNeighbours[r_Integer, c_Integer] := Module[{},
    rows = r;
    cols = c;
    cellIndices = Table[{i, j}, {i, 1, r}, {j, 1, c}];
    cellNeighbourhood = Nearest[Flatten[cellIndices, 1]
        DistanceFunction -> toroidalDistance]] /; r > 1 && c > 1; distancefn
=
  Compile[{{a, _Integer}, {b, _Integer}, {len, _Integer}},
   Module[{mn, mx, result},
    If[a == b, Return[0]];
    mx = Max[a, b];
    mn = Min[a, b];
    result = Min[mx - mn, mn + len - mx];
    If[result < 0, Print[{"distance error", a, b, len, result}]];
    Return[result]]];
toroidalDistance[p1_, p2_] :=
  Module[{r, c}, r = distancefn[First[p1], First[p2], rows];
   c = distancefn[Last[p1], Last[p2], cols]; r^2 + c^2];
neighbourhoodList[r_, c_, n_] :=
  cellNeighbourhood[{r, c}, n];
End[];
EndPackage[];

--
Richard Palmer

Home                            941 412 8828
Cell                               508 982-7266





  • Prev by Date: Re: Sum pattern
  • Next by Date: Re: Problem in solving nonlinear Differential Equation
  • Previous by thread: Trouble Loading Packages...
  • Next by thread: Re: Trouble Loading Packages...