开发者

A problem when wrongly using Dot command in Mma

开发者 https://www.devze.com 2023-03-06 11:59 出处:网络
In[1]:= SameQ[Dot[1, 2], 1.2] TrueQ[Dot[1, 2] == 1.2] a = 1; b = 2; SameQ[Dot[a, b], a.b] TrueQ[Dot[a, b] == a.b]
In[1]:= SameQ[Dot[1, 2], 1.2]
TrueQ[Dot[1, 2] == 1.2]

a = 1; b = 2;
SameQ[Dot[a, b], a.b]
TrueQ[Dot[a, b] == a.b]

Out[1]= False

Out[2]= False

Out[4]= True

Out[5]= True 

I know this uses Dot command wrong. Anybody can give me a clear reson for the above different results?

than开发者_C百科ks!


a.b is interpreted as Dot[a,b] and then variables a and b are substituted, meaning Dot[1,2] and thus equality holds. This is not the same as 1.2 where the dot stands for the decimal separator and not for the inline operator of Dot.


A problem when wrongly using Dot command in Mma

When you write 1.2, Mma understands a number (aka 6/5), but if you write {1, 1}.{2, 2} or a.b Mma understands a scalar product, as usual in any book using vectors.

HTH!


It can be informative to view an expression under Hold and FullForm:

a = 1; b = 2;
SameQ[Dot[a, b], a.b]] //Hold //FullForm
    Hold[SameQ[Dot[a, b], Dot[a, b]]]

With this combination of commands, Mathematica parses but does not evaluate the expression (Hold), and then shows the long pseudo-internal form of the expression (FullForm).

In this case, you can see that the second term a.b is parsed as Dot[a, b] before any evaluation happens.

When . appears with numerals as in 1.2 it is interpreted specially as a decimal point. This is similar to other numeric entry formats such as: 1*^6 which is recognized directly as 1000000:

1*^6 //Hold //FullForm

Compare trying to enter:

a = 1;

a*^6
0

精彩评论

暂无评论...
验证码 换一张
取 消