Mathematica 中 == 和 === 的区别

发布于 2024-10-21 19:26:07 字数 589 浏览 16 评论 0原文

我的印象是 = 是一个赋值,== 是一个数字比较,而 === 是一个符号比较(以及就像在其他一些语言中一样,== 等于=== 等于 但是,看看。下面看来情况不一定如此...

In: x == x
Out: True

In: x === x
Out: True

In: 5 == 5
Out: True

In: 5 === 5
Out: True

In: x = 5
Out: 5

In: 5 == x
Out: True

In: 5 === x
Out: True

In: 5 5 == 5x
Out: True

In: 5 5 === 5x
Out: True

In: x == y
Out: x == y

In: x === y
Out: False

In: y = x
Out: 5

In: x == y
Out: True

In: x === y
Out: True

那么 Mathematica 中 == 和 === 到底有什么区别?我一直在查看文档,但我仍然不太明白它。

I was under the impression that = is an assignment, == is a numeric comparison, and === is a symbolic comparison (as well as in some other languages == being equal to and === being identical to. However, looking at the following it would appear that this is not necessarily the case...

In: x == x
Out: True

In: x === x
Out: True

In: 5 == 5
Out: True

In: 5 === 5
Out: True

In: x = 5
Out: 5

In: 5 == x
Out: True

In: 5 === x
Out: True

In: 5 5 == 5x
Out: True

In: 5 5 === 5x
Out: True

In: x == y
Out: x == y

In: x === y
Out: False

In: y = x
Out: 5

In: x == y
Out: True

In: x === y
Out: True

So what exactly is the difference between == and === in Mathematica? I have been looking at the documentation but I still don't quite understand it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终止放荡 2024-10-28 19:26:07

一个重要的区别是 === 始终返回 TrueFalse== 可以返回未计算的值(这就是它对于表示方程很有用的原因。)

In[7]:= y == x^2 + 1

Out[7]= y == 1 + x^2

In[8]:= y === x^2 + 1

Out[8]= False

有一些有趣的情况,其中 == 返回未计算的值,在编程时值得注意。例如:

In[10]:= {} == 1

Out[10]= {} == 1 

这可能会影响 If[foo=={},,] 等内容。

One important difference is that === always returns True or False. == can return unevaluated (which is why it's useful for representing equations.)

In[7]:= y == x^2 + 1

Out[7]= y == 1 + x^2

In[8]:= y === x^2 + 1

Out[8]= False

There are some interesting cases where == returns unevaluated that are worth being aware of while programming. For example:

In[10]:= {} == 1

Out[10]= {} == 1 

which can affect things like If[foo=={}, <true>, <false>].

怕倦 2024-10-28 19:26:07

===== 非常相似,如果左侧和右侧相等,则返回 True。它们不同的一个例子是当您比较不同表示格式的数字时。

In: 5.==5
Out: True

In: 5.===5
Out: False

尽管它们在数字上相同(这就是 == 返回 True 的原因),但它们并不完全相同。

仅供参考,它们在内部是不同的功能。 ==Equal,而 ===SameQ

== and === are very similar in the sense that it returns True if the lhs and rhs are equal. One example where they differ is when you compare numbers in different representation formats.

In: 5.==5
Out: True

In: 5.===5
Out: False

Although they are the same numerically, (which is why == returns True), they aren't exactly identical.

FYI, they are different functions internally. == is Equal, whereas === is SameQ.

戏剧牡丹亭 2024-10-28 19:26:07

Equal 指语义相等,而 SameQ 指句法相等。例如,Sin[x]^2+Cos[x]^21 是相同的数字,因此它们在语义上是相等的。由于如果不进行更多转换就无法确定这一点,因此 Equal 返回未计算的值。但是,实际表达式不同,因此 SameQ 给出 False

Sin[x]^2 + Cos[x]^2 == 1
Sin[x]^2 + Cos[x]^2 === 1
Simplify[Sin[x]^2 + Cos[x]^2 == 1]

请注意,对 Real 数字有特殊处理,如果 aSameQ[a,b] 可以返回 True code>b 最后一个二进制数字不同。要进行更严格的身份测试,请使用 Order[a,b]==0

a = 1. + 2^-52;
b = 1.;
a === b
Order[a, b]==0

SameQ 对于语法不同的表达式可以返回 True,因为表达式头可以自动对参数进行排序。您可以使用保留属性来防止自动排序。例如

c + d === d + c
SetAttributes[SameQ, HoldAll]
c + d === d + c

Equal refers to semantic equality whereas SameQ is syntactic equality. For instance, Sin[x]^2+Cos[x]^2 and 1 are the same number, so they are equal semantically. Since this can not be determined without more transformations, Equal returns unevaluated. However, actual expressions are different, so SameQ gives False.

Sin[x]^2 + Cos[x]^2 == 1
Sin[x]^2 + Cos[x]^2 === 1
Simplify[Sin[x]^2 + Cos[x]^2 == 1]

Note that there's special handling of Real numbers, SameQ[a,b] can return True if a and b differ in the last binary digit. To do more restrictive identity testing, use Order[a,b]==0

a = 1. + 2^-52;
b = 1.;
a === b
Order[a, b]==0

SameQ can return True for expressions that are syntactically different because expression heads may sort arguments automatically. You can prevent automatic sorting by using holding attributes. For instance

c + d === d + c
SetAttributes[SameQ, HoldAll]
c + d === d + c
分開簡單 2024-10-28 19:26:07

lhs===rhs 产生 True,如果
表达式 lhs 与 rhs 相同,
否则返回 False。

lhs==rhs 如果 lhs 和 rhs 返回 True
是相同的。

参考来自此处此处

lhs===rhs yields True if the
expression lhs is identical to rhs,
and yields False otherwise.

and

lhs==rhs returns True if lhs and rhs
are identical.

Reference from here and here.

平定天下 2024-10-28 19:26:07

我将引导您阅读 Leonid Shifrin 撰写的一本优秀著作的第 2.5 节:平等检查

I direct you to section 2.5: Equality checks of an excellent book by Leonid Shifrin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文