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

Using 'Module' inside 'Compile'

86 views
Skip to first unread message

Scott D. Kasen

unread,
Sep 2, 2010, 2:31:48 AM9/2/10
to
While I've worked with Mathematica for quite some time, I'm first
learning the Compile function. Keeping it brief...

Compile the function...

f1 = Compile[{},
Module[{x, temp},
x = 4.;
If[3 > 2, temp = x; Print["temp in 'if' statement: ", temp]];
Print["temp outside 'if' statement: ", temp];
];
];

Calling the function...

f1[]

Produces the following output:

temp in 'if' statement: 4.
temp outside 'if' statement: temp$3107

First, why does the function create a new local variable temp$3107
outside of the 'If' statement? Second, how can I bring the first value
for "temp" outside of the if statement?

Thanks in advance,
Scott Kasen


Patrick Scheibe

unread,
Sep 3, 2010, 6:09:41 AM9/3/10
to
Hi,

I assume when tmp is not used bevor, then it is *declared* and
*initialized* inside the if block like in C

f1() {
double x = 4.0;
if(3>2) {
double tmp = x;
printf("...",tmp);
}
printf("...",tmp);
}

so why don't you try

f1 = Compile[{}, Module[{x, temp = 0.0}, x = 4.;


If[3 > 2, temp = x; Print["temp in 'if' statement: ", temp]];
Print["temp outside 'if' statement: ", temp];];];

?

Cheers
Patrick

0 new messages