Message Boards Message Boards

GROUPS:

Evaluate a global expression within functions?

Posted 7 months ago
687 Views
|
3 Replies
|
0 Total Likes
|

Some hours after my original post, I edit that post: I'm sorry, but my code is nonsense. Of course I am interested in the value of exp WITHIN my functions and not after the functions being called. I will come back with a corrected version.

I have some global symbolic expression exp=a*x and want to substitute a within a function f[a_] by the parameter a. See the code below. The first three cases #1,#2,#3 are on the global level and work as expected. The following cases are within functions and I do not understand why they don't work as I expect. I'm afraid I have some basic misunderstandings with things like Evaluate, Hold, etc. and even Module.

#4 is a simple function without Module. It does not substitute the parameter a within exp. Why? But this can be cured through Evaluate as in #5.

The cases #6, #7, #7S use functions defined as modules and do never work. Not without and not with Evaluate. Why?

Case #7S is different from all others since it uses a symbolic parameter a=myA instead of a number. This does not work either.

Maybe I should try things like Replace, Eliminate, PolynomialReduce?

If[True,
 ClearAll["Global`*"]; 
 exp = a*x; Print["#1: Global exp: ", exp, "\n\tok"];
 a = 3; Print["#2: a=3 substituted: ", exp, "\n\tok"];
 a =.; Print["#3: a reset: ", exp, "\n\tok"];
 f4[a_] := exp; 
 Print["#4: a=3 substituted within a function: " f4[3], 
  "\n\tNOK! Should give 3*x"];
 f5[a_] := Evaluate[exp]; 
 Print["#5: a=3 substituted within a function with Evaluate: ", f5[3],
   "\n\tok"];
 f6[a_] := Module[{}, exp]; 
 Print["#6: a=3 substituted within a function-Module: ", f6[3], 
  "\n\tNOK! Should give 3*x"];
 f7[a_] := Module[{}, Evaluate[exp]]; 
 Print["#7: a=3 substituted within a function-Module with Evaluate: ",
   f7[3], "\n\tNOK! Should give 3*x"];
 Print["#7S: a=myA substituted within a function-Module with \
Evaluate: ", f7[myA], "\n\tNOK! Should give myA*x"];
 ]
#1: Global exp: a x
    ok
#2: a=3 substituted: 3 x
    ok
#3: a reset: a x
    ok
#4: a=3 substituted within a function:  a x
    NOK! Should give 3*x
#5: a=3 substituted within a function with Evaluate: 3 x
    ok
#6: a=3 substituted within a function-Module: a x
    NOK! Should give 3*x
#7: a=3 substituted within a function-Module with Evaluate: a x
    NOK! Should give 3*x
#7S: a=myA substituted within a function-Module with Evaluate: a x
    NOK! Should give myA*x
3 Replies
Posted 7 months ago

From documentation:

-SetDelayed has attribute HoldAll, rather than HoldFirst.

Evaluate[expr] causes expr to be evaluated even if it appears as the argument of a function whose attributes specify that it should be held unevaluated.

Thus in F5 ,the right hand side of the SetDelayed is evaluated

f5[a_] := Evaluate[exp]

This shows what Mathematica sees.

?f5
    Global`f5
    f5[a_]:=a x

a is recognized as a parameter in the right hand side of the function f5. Parameters are "renamed" so as not to conflict with globals. Maybe this is what you want:

f10[parameterA_] := (a = parameterA; exp)
f10[3]
    3 x
Posted 7 months ago

Sorry for confusion. I noticed that my code is nonsense and edited my original post meanwhile. I'll come back with corrected questions.

Posted 7 months ago

Meanwhile I have changed my faulty testprogram from my first post (sorry about that). It now prints exp within the functions and not after the call.

Now everything works as expected by using Replace. No matter if the functions are defined with or without Module. My mistake was, that I used the same name "a" for function parameters and the global expression exp=a*x.

It's still a bit strange and I do not fully understand it. But it works.

If[True,
 ClearAll["Global`*"]; 
 exp = a*x; Print["#1: Global exp: ", exp, "\n\tok"];
 a = 3; Print["#2: a=3 substituted: ", exp, "\n\tok"];
 a =.; Print["#3: a reset: ", exp, "\n\tok"];

 f14[aparm_] := 
  Print["#14: a=", aparm, 
   " substituted within a function by Replace: ", exp /. a -> aparm, 
   "\n\tok"];
 f14[3];
 f14[myA];

 f16[aparm_] := 
  Module[{}, 
   Print["#16: a=", aparm, 
     " substituted within a function-Module by Replace: ", 
     exp /. a -> aparm, "\n\tok"];
   ];
 f16[3];
 f16[myA];
 ]
#1: Global exp: a x
    ok
#2: a=3 substituted: 3 x
    ok
#3: a reset: a x
    ok
#14: a=3 substituted within a function by Replace: 3 x
    ok
#14: a=myA substituted within a function by Replace: myA x
    ok
#16: a=3 substituted within a function-Module by Replace: 3 x
    ok
#16: a=myA substituted within a function-Module by Replace: myA x
    ok
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract