like your function-symbols, the symbols z1, z2, ... need
to be exported by giving them a usage.
Cheers
Patrick
> Date: Thu, 11 Mar 2010 06:37:35 -0500
> From: fgutie...@yahoo.com
> Subject: elementary questio about packages
> To: math...@smc.vnet.net
Depending on the circumstances, another possibility would be to create the
needed symbols (z1,z2,etc) in the context they will be used (Global`, say),
before the BeginPackage command, and import this context publicly into your
package in BeginPackage. This is not as clean a solution as Patrick's (since
it couples contexts and assumes a specific work context), but it may be
sometimes more convenient - you won't then see the annoying shadowing
messages every time when you create z1, z2 in Global` (or whatever is your
working context) before loading your package.
I guess you can also combine the two approaches: rather than explicitly
creating the symbols say in Global` before BeginPackage, you can simply
import Global` (or whatever you working context) in BeginPackage, but give
these symbols the usage messages in your package anyway. Then, if they don't
exist in the target one (Global` here), they will be created in your package
and exported just as Patrick suggested. If they exist, usage messages will
be added to them in your working context, which I think is also a good
thing.
Regards,
Leonid
On Thu, Mar 11, 2010 at 3:48 PM, Patrick Scheibe <
psch...@trm.uni-leipzig.de> wrote:
> Hi,
>
> like your function-symbols, the symbols z1, z2, ... need
> to be exported by giving them a usage.
>
> Cheers
> Patrick
>
> On Thu, 2010-03-11 at 06:37 -0500, Francisco Gutierrez wrote:
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
--- On Thu, 3/11/10, becko BECKO <beck...@hotmail.com> wrote:
From: becko BECKO <beck...@hotmail.com>
Subject: RE: elementary questio about packages
To: fgutie...@yahoo.com, math...@smc.vnet.net
Date: Thursday, March 11, 2010, 7:52 AM
Whenever you declare a function in a package that uses some temporary variables, you should use Module to declare those variables. If this does not help, perhaps you should post some code so that the guys here can see where the problem is.
> Date: Thu, 11 Mar 2010 06:37:35 -0500
> From: fgutie...@yahoo.com
> Subject: elementary questio about packages
> To: math...@smc.vnet.net
>
hth,
albert
two solutions:
1)quick=C2=B4n dirty: In your package code, replace "z1" with "Global`z1"
etc. in situ. Quite prone to errors, oversights etc.
2) make your private variable known to the global context by adding a
usage comment before entering the private context, e.g.
z1::usage="z1 is used for blabla..."
This has the advantage that you can ask about z1 with Information[z1],
which returns said usage message. It also encourages proper
documentation on the package level.
As always, have a look at any number of published packages to learn how
it's done...
Regards,
Yves
Am 11.03.2010 12:37, schrieb Francisco Gutierrez:
The best solution may be to supply the desired symbol names to the package
routine.
MyOptimize[parms, {z1,z2,z3}]
We assume that these appear in the parms expressions. This first method is
more general and also picks up the Context of the notebook calling the
routine (such as Help Function pages).
A second method (but much more restricted for the user) is to put the
relevant package code inside a With statement:
With[{z1=Global`z1, z2=Global`z2, z3=Global`z3},...]
A third method is to use a default argument:
MyOptimize[parms_, vars_List:{Global`z1,Global`z2,Global`z3}]
David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/
> 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]]]
I think this example is one of those where there is absolutely no gain
in providing the results as a list of rules. Although many Mathematica
functions do so, there are just as many which don't! Returning results
in the form of rules ony makes sense when there are symbol names
provided with the call of the function, e.g. as in Solve, NSolve,
DSolve, NMinimize and the like. Others, like e.g. LinearSolve,
LeastSquares, LinearProgramming and many more only return vectors and no
rules, since returning rules make no sense for them. For your example
there is also no benefit in returning rules, so I would strongly
recommend to just return the vector that minimizes the contructed
expression, which also solves your problems with the package. This is
how I would define the function:
trivialfunc[ind_, depend_] :=
Module[{letsee, ineq1, ineq2, func, result},
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};
result = NMinimize[Flatten[{func, ineq1, ineq2}], Flatten[letsee]];
{result[[1]], Flatten[letsee] /. result[[2]]}
]
hth,
albert
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:
fgutie...@yahoo.com
> Subject: Re:
elementary questio about packages
> To: math...@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]]]
>
>