Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.3K Views
|
6 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Why assignment and delayed assignment to Sin[x]==0 are the same?

Posted 3 years ago

Hello, Can you tell me why they are same:

test=Sin[x]==0 
test:=Sin[x]==0

No matter in what line of code, when I input:

OwnValues[test]

They all showed:

{HoldPattern[test] :> Sin[x] == 0}

Aren't there differences between Set and SetDelayed in this situation?

POSTED BY: Zhenyu Zeng
6 Replies
Posted 3 years ago

Okay. I am still confused but your answer helps me a lot.

POSTED BY: Zhenyu Zeng
Posted 3 years ago

Sin[x]==0 is an expression. It's not a string. It's not wrapped in Hold. So, each time Mathematica sees encounters test it will "evaluate" it (i.e. apply its rewrite ruies, in this case using OwnValues). And once that replacement is made, it will try to evaluate again. If x has a value (i.e. it has OwnValues) then that replacement will be done. And so on.

Maybe it will help if you use Trace.

Clear[x, test]

test = Sin[x] == 0
(*evaluates immediately and outputs Sin[x] == 0*)

Trace[test]
(*{test,Sin[x]==0}*)

x = Pi

Trace[test]
(*{test,Sin[x]==0,{{x,\[Pi]},Sin[\[Pi]],0},0==0,True}*)

Furthermore, note that OwnValues[test] has not changed

OwnValues[test]
(*{HoldPattern[test]:>Sin[x]==0}*)

That's just because RuleDelayed has the attribute HoldRest. Of course, you can explicitly change it:

test = Sin[x] == 0
(*True*)

OwnValues[test]
(*{HoldPattern[test]:>True}*)
POSTED BY: Eric Rimbey
Posted 3 years ago

Hello, Thanks for your reply. Set function is evaluated as it shows the first time. So,

test=Sin[x]==0 

will assign Sin[x]==0 to test.

After this, I input x=pi, then I enter test the result are False.

As Set function is evaluated as it shows the first time. So, test should always be Sin[x]==0. Why after I inputing x=pi, test becomes False?

POSTED BY: Zhenyu Zeng
Posted 3 years ago

If x is unassigned, then the expression Sin[x]==0 cannot be evaluated further. That's just a consequence of the rules for Sin and Equal. So, it turns out that the immediate evaluation of Sin[x]==0 effects no change to that expression. SetDelayed holds the right hand side unevaluated, but in this case, you get the same result whether you evaluate it or not. What Gianluca showed you illustrates the difference between Set and SetDelayed by making it possible to further evaluate Sin[x]==0 (by assigning a value to x). In otherwords, the fact that OwnValues[test] is the same in both cases is a coincidence due to the particular expression you chose to assign to test.

POSTED BY: Eric Rimbey
Posted 3 years ago

But, may you tell me why they are the same in my two lines of code? Thanks.

POSTED BY: Zhenyu Zeng

Try this to feel the difference:

x = Pi/2;
testImmediate = Sin[x] == 0;
testDelayed := Sin[x] == 0;
OwnValues[testImmediate]
OwnValues[testDelayed]
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