tag:blogger.com,1999:blog-1578495381497154301.post8400647612235816508..comments2023-02-18T05:42:34.509-05:00Comments on The Way of Mathematica: Use Block to Temporarily Block Global VariablesKrishttp://www.blogger.com/profile/00932979261710013619noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-1578495381497154301.post-6395551749789331952019-10-14T10:12:58.446-04:002019-10-14T10:12:58.446-04:00Dear Fernando, I am not sure your solutions have t...Dear Fernando, I am not sure your solutions have to do with scoping. Scoping is automatic with SetDelayed and Pattern (e.g. f@x_:= x^2. For compound expressions it is good form to use Module or the more specialized Block or With. <br /><br />I am not clear on why the Activate@Inactivate piece is required, but it does seem to be. Perhaps you can explain that. See some explorations below (you'll have to Evaluate the code).<br /><br />Best,<br /><br />Kris<br /><br />Fernando - Even/Odd Function test<br /><br />polyTest[exp_] := Module[{f}, Activate@Inactive[SetDelayed][f[x_], exp];<br /> Which[f[x] === f[-x], Print["is even"], -f[x] === f[-x], Print["is odd"], <br /> True, Print["Nor even, nor odd"]]]<br /><br />Does it work without Activate@Inactivate?<br /><br />polyTest2[exp_] := Module[{f}, SetDelayed[f[x_], exp];<br /> Which[f[x] === f[-x], Print["is even"], -f[x] === f[-x], Print["is odd"], <br /> True, Print["Nor even, nor odd"]]<br /> ]<br /><br />A simple intuitive test<br /><br />polyTest /@ {x^2, x^3}<br /><br />Omitting Activate@Inactivate fails - why?<br /><br />polyTest2 /@ {x^2, x^3}<br /><br />Again, failure<br /><br />polyTest2[1/2 (E^x + E^-x)]<br /><br />Again, success<br /><br />polyTest[1/2 (E^x - E^-x)]<br /><br />polyTest2[(E^x + E^-x)] // Trace<br /><br />The documentation doesn't explain how SameQ tests for equivalence other than syntactical equivalence. Reduce fails (see below).<br /><br />-(E^x + E^-x) === (E^x + E^-x)<br /><br />polyTest[(E^x + E^-x)] // Trace<br /><br />(E^x + E^-x) == -(E^x + E^-x)<br /><br />Reduce[(E^x + E^-x) == -(E^x + E^-x), x, Reals] // Trace<br /><br />Reduce[(E^x - E^-x) == -(E^x + E^-x), x, Reals] // Trace<br /><br />Plot[{E^x, E^-x}, {x, -2, 2}]<br /><br />Plot[(E^x + E^-x), {x, -2, 2}, AxesOrigin -> {0, 0}]<br /><br />Plot[(E^x - E^-x), {x, -2, 2}, AxesOrigin -> {0, 0}]<br /><br />Plot[-(E^x + E^-x), {x, -2, 2}]<br /><br />Your simpler function seems to work.<br /><br />f2[x_] := 1/2 (E^x - E^-x);<br />Which[f2[x] === f2[-x], Print[f2[x], " is Even"], -f2[x] === f2[-x], <br /> Print[f2[x], " is Odd"], True, Print[f2[x], "Nor even, nor odd"]]<br /><br />f2[x_] := x^2; f3@x_ := x^3;<br />Which[f2[x] === f2[-x], Print[f2[x], " is Even"], -f2[x] === f2[-x], <br /> Print[f2[x], " is Odd"], True, Print[f2[x], "Nor even, nor odd"]];<br />Which[f3[x] === f3[-x], Print[f3[x], " is Even"], -f3[x] === f3[-x], <br /> Print[f3[x], " is Odd"], True, Print[f3[x], "Nor even, nor odd"]];<br /><br />Remove[f, f2, f3, polyTest, polyTest2]Krishttps://www.blogger.com/profile/00932979261710013619noreply@blogger.comtag:blogger.com,1999:blog-1578495381497154301.post-15331643798941778882019-10-12T23:26:25.179-04:002019-10-12T23:26:25.179-04:00Your explanation was pleasantly clear. It clears u...Your explanation was pleasantly clear. It clears up a lot. May I ask you for an explanation about this topic? I wanted to write a function that test if a function is even or odd. I end up writing a piece of code that seems to work but I don’t really know how. The 2nd line is the difficult one. I don’t quite get why have to be this way.<br /><br />polyTest[exp_]:=<br />Module[{f},Activate@Inactive[SetDelayed][f[x_],exp];<br />Which[<br />f[x]===f[-x],Print["is even"],<br />-f[x]===f[-x],Print["is odd"],<br />True,Print["Nor even, nor odd"]<br />]]<br /><br />Applying this function to an expression will look like:<br /><br />polyTest[1/2 (E^x+E^-x)]<br /><br />I previously wrote a more simple but (to me) clear code to do the same. But then I thought and if I write it as a function of Mathematica? What gave rise to this matter.<br /><br />f[x_]:=1/2 (E^x-E^-x);<br />Which[<br />f[x]===f[-x],Print[f[x]," is Even"],<br />-f[x]===f[-x],Print[f[x]," is Odd"],<br />True,Print[f[x],"Nor even, nor odd"]<br />]<br />ClearAll[f]<br /><br />Investigating I found that the 1st code could be called “nested scoping constructs”. My solution was an adaptation from various examples from mathematica.stackexchange.com, specifically at: https://mathematica.stackexchange.com/questions/180544/assign-a-function-within-a-function/<br /><br />If you find some time for this, I will appreciate it, but even if not, this article is a thumb up.<br /><br />Thank you.<br />Fernandohttps://www.blogger.com/profile/03739299473011007107noreply@blogger.com