Re: Re: How to use package without manually evaluating?
- To: mathgroup at smc.vnet.net
- Subject: [mg90580] Re: [mg90569] Re: How to use package without manually evaluating?
- From: DrMajorBob <drmajorbob at att.net>
- Date: Mon, 14 Jul 2008 05:22:21 -0400 (EDT)
- References: <g54oia$etg$1@smc.vnet.net> <g54s0e$hfd$1@smc.vnet.net>
- Reply-to: drmajorbob at longhorns.com
Rather than F[n_]:=Module[{k = 1}, Table[k++,{i,n},{j,n}] ] I'd use f[n_] := Partition[Range[n^2], n] Bobby On Sun, 13 Jul 2008 14:48:50 -0500, Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com> wrote: > jeremito wrote: > >> On Jul 10, 7:32 am, Jens-Peer Kuska <ku... at informatik.uni-leipzig.de> >> wrote: >>> Hi, >>> >>> you have to find *which* file. The Package *.m has no cells >>> and the *.nb file has cells but it is not a package. >>> >>> Regards >>> Jens >>> >> Sorry, I'm not exactly sure what you mean. Do I need a .m file as >> well as a .nb file? >> >> I have copied my .m file below for reference. All the cells are >> "Code". >> >> >> Thanks, >> Jeremy >> >> (* :Title: Matrices *) >> >> (* :Name: Matrices *) >> >> (* :Author: Jeremy L. Conlin *) >> >> (* :Context: Matricdes` *) >> >> (* :Summary: >> This package was created to have a centralized location for all my >> special matrix definitions. *) >> >> BeginPackage["Matrices`"]; >> >> UHessenberg::usage = "UHessenberg[n] will create a square, n by n, \ >> upper >> Hessenberg matrix with increasing elements." >> >> Begin["Private`"] >> >> UHessenberg[n_] := Module[{H}, >> (* UHessenberg makes a square upper Hessenberg matrix with increasing >> \ >> >> elements. *) >> k=1.0; >> H = Table[If[i<=j+1,k++,0],{i,n},{j,n}]; >> Return[H]; >> ] >> >> F[n_]:=Module[{A}, >> (* This creates a sqaure matrix whose elements are linearly \ >> increasing. >> All elements are non-zero.*) >> k = 1.0; >> A = Table[k++,{i,n},{j,n}]; >> Return[A]; >> ] >> >> (* Standard creates a square matrix that is diagonal with linearly \ >> increasing >> elements---except for the [[3,2]] entry which is 1.*) >> Standard[n_] := \ >> Module[{S}, >> S = DiagonalMatrix[Range[1.0,n]]; >> S[[3,2]] = 1.0; >> Return[S]; >> ] >> >> (* Simple matrix from Fundamentals of Matrix Computatations, by David >> \ >> S. Watkins >> pg. 357.*) >> Watkins[] := Module[{A}, >> A = {{8.,2.},{2.,5.}}; >> Return[A]; >> ] >> >> End[]; (* End Private` context. *) >> >> EndPackage[]; > > I could not help not noticing that many of the above functions could be > easily improved in terms of clarity and efficiency. > > For instance, the local variable A is usually useless, Return[] is not > required, indices such k should be localized. Also, if you do not have > any compelling reasons to use floating-point number arithmetic you > should use exact (infinite precision) numbers by default. > > (* This creates a square matrix whose elements are linearly > increasing. All elements are non-zero. *) > F[n_] := Module[{k = 1}, Table[k++, {i, n}, {j, n}]] > > (* Simple matrix from Fundamentals of Matrix Computations, > by David S. Watkins, pg. 357. *) > Watkins = {{8, 2}, {2, 5}}; > > > >> jeremito wrote: > >>> I just created a package called Matrices that has several functions > >>> that creates matrices that I use frequently. I placed the > Matrices.m > >>> file in my auto load directory so it will be loaded when I need it. > >>> The problem is I have to find the file and execute all the cells in > it > >>> before I can use any of the functions. I'm sure there is a way to > >>> avoid this. Can someone help? > > I may have failed to understand what the problem is: if Matrices.m is > auto-loaded, well, you have nothing else do do before being able to use > the functions defined in it. > > However, looking more closely at your code, I have noticed that you > export only one function, all the others are private. So, perhaps, > exporting the other functions will solve your problem. For instance, in > the following, all the functions but Standard[] are exported, i.e. > public, so ready to be use after the package has been loaded. > > BeginPackage["Matrices`"]; > > UHessenberg::usage = "UHessenberg[n] will create a square, n by n, \ > upper Hessenberg matrix with increasing elements." > > F::usage = "F[n] creates a square matrix whose elements are linearly \ > increasing. All elements are non-zero." > > Watkins::usage = "Simple matrix from Fundamentals of Matrix > Computations, by David > S. Watkins pg. 357." > > Begin["Private`"] > > UHessenberg[n_] := Module[{k = 1}, > (* UHessenberg makes a square upper Hessenberg matrix with increasing > elements. *) > Table[If[i<=j+1,k++,0],{i,n},{j,n}] > ] > > (* This creates a square matrix whose elements are linearly > increasing. All elements are non-zero.*) > F[n_]:=Module[{k = 1}, > Table[k++,{i,n},{j,n}] > ] > > (* Standard creates a square matrix that is diagonal with linearly \ > increasing > elements---except for the [[3,2]] entry which is 1.*) > Standard[n_] := \ > Module[{S}, > S = DiagonalMatrix[Range[1.0,n]]; > S[[3,2]] = 1.0; > Return[S]; > ] > > (* Simple matrix from Fundamentals of Matrix Computations, by David > S. Watkins pg. 357.*) > Watkins = {{8,2},{2,5}}; > > End[]; (* End Private` context. *) > > EndPackage[]; > > Regards, > -- Jean-Marc > > > > > -- DrMajorBob at longhorns.com