Group Abstract Group Abstract

Message Boards Message Boards

0
|
11.4K Views
|
7 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Fix function that doesn't evaluate when passed to a package?

Posted 8 years ago

Hi, I'm baffled by the following...if I define a function in a notebook as follows:

func = Function[x^2][x];

I can call it in the notebook:

Table[func, {x, 1, 6}]
{1, 4, 9, 16, 25, 36}

I can also call it in a function defined in the notebook:

runNb[func_] :=
    Module[{},
        Table[func, {x, 1, 6}]
    ]
runNb[func]
{1, 4, 9, 16, 25, 36}

but, when I try to run this as a package, the function doesn't evaluate:

BeginPackage["run`"]
    run::usage = "run[func]"
    Begin["`Private`"]
        run[func_] :=
            Module[{},
                Table[func, {x, 1, 6}]
            ]
    End[]
EndPackage[]

and, calling from the notebook:

<< run.m
run[func]
{x^2, x^2, x^2, x^2, x^2, x^2}

Makes no sense to me! Can anyone please explain?

Thanks

POSTED BY: Stephan Foley
7 Replies
Posted 8 years ago

OK, I got everything working now, thanks to Gianluca Gorni. As pointed out, my mistake was how I was defining functions. Even though it was incorrect, it was working in the notebook. New code that is working both in the notebook and with a package follows.

Package is now:

BeginPackage["mypackage`"]
    run::usage = "run[func]"
    Begin["`Private`"]
        run[func_] :=
            Module[{},
                Table[func[x], {x, 1, 6}]
            ]
    End[]
EndPackage[]

and code in notebook is:

<< mypackage.m
func = #^2 &;
run[func]

Thanks for taking the time to help me out on this!

POSTED BY: Stephan Foley
POSTED BY: Gianluca Gorni
Posted 8 years ago

@Gianluca thanks for the help, I like what you did with your second piece of code.

@John totally agree about not using Global`x. I find Mathematica's handling of variables pretty frustrating sometimes.

Well, I would say I'm still not happy here. It makes no sense to me that you cannot pass a function defined in a notebook to another function defined in a package. It's just my expectation that the function will evaluate. I don't think using Global`x is the way to go. Although I like Gianluca's second approach, I think it is a workaround and doesn't solve the initial problem.

POSTED BY: Stephan Foley
Anonymous User
Anonymous User
Posted 8 years ago
POSTED BY: Anonymous User

Module does not replace the local variable x in Table:

Module[{x = 3}, Table[x, {x, 0, 2}]]

You can do that with With:

BeginPackage["run`"];
run::usage = "run[func]";
Begin["`Private`"];
run[func_] := With[{x = Global`x}, Table[{x, func}, {x, 1, 6}]];
End[];
EndPackage[];

The way I would define a function and do a table of the values is this, however:

f[x_] := x^2;
BeginPackage["run`"];
run::usage = "run[func]";
Begin["`Private`"];
run[func_] := Table[func[x], {x, 1, 6}];
End[];
EndPackage[];
run[f]
POSTED BY: Gianluca Gorni
Posted 8 years ago

Hi Gianlunca and thanks for the reply.

Original problem was I wanted to take a list of functions and pass them into a package which had a plotting function. This did not work, so I started stripping things down and found the core of the problem was that the functions were not evaluating. I had written the functions like so:

funcs = {x^2, x^3, x^4}

I thought it might be because I wasn't explicitly defining an anonymous function, so that's why I used the Function call above.

I did try to use global x in the package like so:

BeginPackage["run`"]
    run::usage = "run[func]"
    Begin["`Private`"]
        run[func_] :=
            Module[{x = Global`x},
                Table[func, {x, 1, 6}]
            ]
    End[]
EndPackage[]

But this didn't help. I think the x is local to the Table block and so should just be replaced in the anonymous function. Either using global x or not shouldn't matter.

POSTED BY: Stephan Foley
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard