Re: elementary questio about packages
- To: mathgroup at smc.vnet.net
- Subject: [mg108298] Re: elementary questio about packages
- From: becko BECKO <becko565 at hotmail.com>
- Date: Sat, 13 Mar 2010 07:55:00 -0500 (EST)
If you are going to return a symbol (L in this case), then perhaps you should let the user specify the symbol. Note that this is how functions like Solve,Root,NMinimize, etc that return symbols are implemented. The following would go in your package (the only change is putting L_ in the argument of the trivialfunc): BeginPackage["trivial`"] trivialfunc::usage="this function is very useful." Begin["`Private`"] trivialfunc[ind_,depend_,L_]:=Module[{letsee,ineq1,ineq2,func}, letsee=Array[L,Length[depend]]; ineq1=MapThread[GreaterEqual,{letsee,Table[0,{Length[letsee]}]}]; ineq2=MapThread[GreaterEqual,{ind-letsee,Table[0,{Length[letsee]}]}]; func={ind.letsee}; NMinimize[Flatten[{func,ineq1,ineq2}],Flatten[letsee]] ] End[] EndPackage[] Then when you call it from a notebook: In[1]:= Needs["trivial`", NotebookDirectory[] <> "trivial.m"] In[4]:= x = {1, 2, 3, 4, 5}; y = {6, 7, 8, 9, 10}; In[7]:= trivialfunc[x, y, myL] Out[7]= {0., {myL[1] -> 0., myL[2] -> 0., myL[3] -> 0., myL[4] -> 0., myL[5] -> 0.}} Hope this helps. > Date: Fri, 12 Mar 2010 07:09:30 -0500 > From: fgutiers2002 at yahoo.com > Subject: [mg108265] Re: [mg108238] elementary questio about packages > To: mathgroup at smc.vnet.net > > Right Becko. And thanks to Leonid, David, and Patrick for their answers. But maybe to make it very clear I should provide a an example. > > Take the following function: > trivialfunc[ind_, depend_] := > Module[{letsee, ineq1, ineq2, func}, letsee = Array[L, Length[depend]]; > ineq1 = MapThread[ > GreaterEqual, {letsee, Table[0, {Length[letsee]}]}]; > ineq2 = MapThread[ > GreaterEqual, {ind - letsee, Table[0, {Length[letsee]}]}]; > func = {ind.letsee}; > NMinimize[Flatten[{func, ineq1, ineq2}], Flatten[letsee]]] > > Of course, it should send all the L's to 0. Note that the L's vary in size, according to the length of the two variables. > > I make a package called trivial, and run the function with an x and a y. > x={1,2,3,4,5} > y={6,7,8,9,10} > > trivialfunc[y,y] > And the output is: > {0.,{trivial2`Private`L[1]->0.,trivial2`Private`L[2]->0.,trivial2`Private`L[3]->0.,trivial2`Private`L[4]->0.,trivial2`Private`L[5]->0.}} > If I eliminate the Private I get pure nonsense, and if a declare the Ls in the module I get something of the form L$something, which I do not want either. > > I would want it to be simply L[1]->0, etc, for a varying number of L's. > Thank you for any help > Fg >