From: Albert Retey on
Am 11.03.2010 12:37, schrieb Francisco Gutierrez:
> Dear List: I made a package with a function that has a minimization.
> The minimization returns, as should be, the value of the
> optimization, and then the values of the variables, in the form
> {z1->10,z2->20,z3->50}.
>
> In the notebook, this works perfectly well. In the package, however,
> the function throws back the variables in the form
> {contextname`z1->10,contextname`z2->20}. This is obnoxious, and makes
> the result much harder to utilize. How can I avoid this? I tried
> deleting Begin["`Private`"], but then I got a completely crazy
> result. What should I do? Fg
>
It's impossible to say with the information you gave. My guess is that
your function does not require the variables z1,z2,z3 as input or does
not handle these correctly internally. If your function does not require
the variable names you should not return some symbols that you created
within your code, there is no good reason to do so. If your function
accepts variable names and you still get contextname`z1 in your output,
you are doing something wrong within your code. In either case, if you
give an example of how your function is called and what it does you
might get more useful help...

hth,

albert

From: Yves Klett on
Hi,

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:
> Dear List:
> I made a package with a function that has a minimization. The minimization
> returns, as should be, the value of the optimization, and then the values
> of the variables, in the form {z1->10,z2->20,z3->50}.
>
> In the notebook, this works perfectly well. In the package, however, the function throws back the variables in the form {contextname`z1->10,contextname`z2->20}.
> This is obnoxious, and makes the result much harder to utilize. How can I avoid this? I tried deleting
> Begin["`Private`"], but then I got a completely crazy result.
> What should I do?
> Fg
>

From: David Park on
This is a not uncommon situation where the package routine does not behave
as the notebook routine.

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
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/


From: Francisco Gutierrez [mailto:fgutiers2002(a)yahoo.com]

Dear List:
I made a package with a function that has a minimization. The minimization
returns, as should be, the value of the optimization, and then the values
of the variables, in the form {z1->10,z2->20,z3->50}.

In the notebook, this works perfectly well. In the package, however, the
function throws back the variables in the form
{contextname`z1->10,contextname`z2->20}.
This is obnoxious, and makes the result much harder to utilize. How can I
avoid this? I tried deleting
Begin["`Private`"], but then I got a completely crazy result.
What should I do?
Fg



From: Albert Retey on
Hi,

> 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


From: becko BECKO on

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(a)yahoo.com
> Subject: Re:
elementary questio about packages
> To: mathgroup(a)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
>