Message Boards Message Boards

0
|
2078 Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Assignments don't apply in scoping constructs

Posted 1 year ago

Hello guys

I created the procedure below assigning values to variables a, b and c, so that they are protected. When I apply WU, the values assigned to variables a,b and c are not applied. Within the WU procedure, I used the With and Block functions, but to no avail.

WU[ex_] := Module[{a, b, c, ex1, sk},
  a = x + y;
  b = x - y;
  c = x*y;
  ex1 = ex;
  sk[t_] := Sin[t];
  Return[sk[ex1]]]

{WU[a*b/c], WU[a - b - c]}
{Sin[(a b)/c], Sin[a - b - c]}

I'm grateful if anyone can help.

Regards,

Sinval

POSTED BY: Sinval Santos
6 Replies
Posted 1 year ago

Yes Hans, it worked as expected. I am also grateful for your contribution.

POSTED BY: Sinval Santos
Posted 1 year ago

Sinval, check this;

WU[ex_]:=Block[{a,b,c,ex1,sk},
  a=x+y;
  b=x-y;
  c=x*y;
  ex1=ex;
  sk[t_]:=Sin[t];
  sk[ex1]
]

In[2]:= {WU[a*b/c], WU[a - b - c]}
Out[2]= {Sin[((x - y) (x + y))/(x y)], Sin[2 y - x y]}
POSTED BY: Hans Milton
Posted 1 year ago

Thank you for your attention, Eric. The expected result is in the post above.

POSTED BY: Sinval Santos
Posted 1 year ago

Hi Daniel Thank you for your attention. I made the procedure work using the Block function.

WU[ex_] := Module[{sk},
  sk[t_] := Sin[t];
  Block[{a = x + y, b = x - y, c = x*y}, sk[ex]]]

The result I wanted is shown below.

In[19]:= {WU[a*b/c], WU[a - b - c]}

Out[19]= {Sin[((x - y) (x + y))/(x y)], Sin[2 y - x y]}

Any additional information will be welcome.

POSTED BY: Sinval Santos
Posted 1 year ago

It would help if you provided expected output. I suspect that you're trying to do some sort of change of variables, maybe something like this;

{Sin[(a b)/c], Sin[a - b - c]} /. {a -> x + y, b -> x - y, c -> x*y}

{Sin[((x - y)*(x + y))/(x*y)], Sin[2*y - x*y]}

If that's the case, then to understand why it didn't work you need to understand how Mathematica does scoping in constructs like Module. So, modify your WU function like this:

WU[ex_] :=
 Module[
  {a, b, c, ex1, sk},
  Print[SymbolName[a]];
  a = x + y;
  b = x - y;
  c = x*y;
  ex1 = ex;
  sk[t_] := Sin[t];
  Return[sk[ex1]]]

Now evaluate WU[a*b/c]. Notice that what appears as a inside the Module actually has a more complicated name. Before going into a lengthy explanation, I think it would be easier if you explained exactly what you're trying to do, what you intend for WU to do, what your objective is, what your expected outputs are, etc. We can probably help you get to where you want to be without diving too deeply into scoping rules.

POSTED BY: Eric Rimbey

What makes you think they were not assigned? Since those assigned objects are not actually used, there is no evidence one way or the other.

POSTED BY: Daniel Lichtblau
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