Group Abstract Group Abstract

Message Boards Message Boards

0
|
4K Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

The difference between "0." and "0" in patterns?

Posted 3 years ago

Hello,

In my knowledge, the difference between 0. and 0 is that 0. keeps default precision and 0 is an integer in Mathematica. However, in the codes below, I can't understand how Mathematica deal with 0. and 0?

{1, 2, 3, 0., 0, 5, 6, 0., 0., 8} /. {a___, 0, b___} -> {a, , b}

Why didn't Mathematica replace the first 0. in {1, 2, 3, 0., 0, 5, 6, 0., 0., 8}?

POSTED BY: Zhenyu Zeng
8 Replies
Posted 3 years ago

What is happening here is pattern matching. Pattern matching happens on the form of an expression. But you need to be careful. What you see in a notebook is a visualization of the expression, a presentation form of the expression. This is an interface thing so that it looks reasonable to the human user. Pattern matching happens on the "real" form of the expression. You can see this "real" form by using FullForm (well, even this is probably not always the real form, but that's beyond my knowledge).

FullForm[0.5]
(*gives 0.5`*)

FullForm[1/2]
(*gives Rational[1, 2]*)

Since we're pattern matching, we would start by thinking about MatchQ.

MatchQ[0., 0] (*False*)

So, at the very least you can confirm for yourself that the expression 0. does not match the pattern 0. (<- that's the period of the sentence, not a numeric dot) And yes, since we're pattern matching, 0 is a pattern here. It's a pattern that can only match exactly one thing, since there are no possible variations (as there would be with _Integer for example). But saying "can only match exactly one thing" is effectively saying SameQ. Now, for what it's worth, I don't actually know if SameQ is used during pattern matching (it could be using some lower level function). So, for your question "how do you know /. looks for match using SameQ?", I admit I don't really know. But it's the semantics of pattern matching that you should be trying to understand. The reason why MatchQ[0., 0] is False is the same reason why MatchQ[0.5, 0] is False, and it's the same reason why MatchQ["0", 0] is False. The fact that 0. and 0 are somehow numerically close is just totally irrelevant.

POSTED BY: Eric Rimbey

MatchQ is indeed the better answer I think, but likely will use SameQ (or something similar) internally…

POSTED BY: Sander Huisman
Posted 3 years ago

Yes, Sander, I suspect you're correct. FWIW, I wasn't trying to correct your answer, I was just trying to nudge the questioner toward a different way to understand.

POSTED BY: Eric Rimbey
Posted 3 years ago

Hi Eric,

Thanks for your reply. From your answer, I learned much more!

POSTED BY: Zhenyu Zeng

It doesn't replace because it looks for match using SameQ, see the difference:

SameQ[0, 0.]
Equal[0, 0.]

To solve this, you can make a pattern that allows for both:

{a___, 0 | 0., b___} -> {a, x , b}
POSTED BY: Sander Huisman
Posted 3 years ago

May you tell me why you know it looks for a match using SameQ? I never see SameQ before.

POSTED BY: Zhenyu Zeng

Equal basically looks if the LHS and RHS are identical in a number-sense. While SameQ looks if they are really identical (type, number, etc).

Short notation:

0 === 0. 
0 == 0.

Another thing is, is that SameQ will ALWAYS return True or False:

 x === y
 x == y
POSTED BY: Sander Huisman
Posted 3 years ago

May you tell me how do you know /. looks for match using SameQ?

POSTED BY: Zhenyu Zeng
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard