Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

elementary questio about packages

0 views
Skip to first unread message

Francisco Gutierrez

unread,
Mar 11, 2010, 6:37:40 AM3/11/10
to
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

Patrick Scheibe

unread,
Mar 11, 2010, 7:48:08 AM3/11/10
to
Hi,

like your function-symbols, the symbols z1, z2, ... need
to be exported by giving them a usage.

Cheers
Patrick

becko BECKO

unread,
Mar 12, 2010, 7:06:40 AM3/12/10
to
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

Leonid Shifrin

unread,
Mar 12, 2010, 7:08:57 AM3/12/10
to
Hi Francisco,

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:

Francisco Gutierrez

unread,
Mar 12, 2010, 7:09:18 AM3/12/10
to
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

--- 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
>

Albert Retey

unread,
Mar 12, 2010, 7:14:08 AM3/12/10
to
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

Yves Klett

unread,
Mar 12, 2010, 7:14:29 AM3/12/10
to
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:

David Park

unread,
Mar 12, 2010, 7:15:44 AM3/12/10
to
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
djm...@comcast.net
http://home.comcast.net/~djmpark/

Albert Retey

unread,
Mar 13, 2010, 7:59:50 AM3/13/10
to
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


becko BECKO

unread,
Mar 13, 2010, 8:09:20 AM3/13/10
to

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:
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]]]
>
>

0 new messages