Message Boards Message Boards

0
|
138 Views
|
4 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Why does Sqrt[2] have dimension {2}?

Hi!
I noticed that the Sqrt[2] (or for any other integer number higher then 1) has some strange dimensions and 2 additional numbers with it. If I use Sqrt[2.] or Sqrt[N[2]] then it's again just a number but given in a decimal format. What is going on?

Thank you!

4 Replies
Posted 7 hours ago

Michael, you gave a very good and complete explanation. Perhaps what Daniil is looking for is TensorDimensions?

In[1]:= Dimensions[Sqrt[2]]
TensorDimensions[Sqrt[2]]

Out[1]= {2}

Out[2]= {}

I have to say that I have not found this to be an issue when the object appears in lists (like when I need to get the dimensions of an array). Typically what one needs is the dimensions of the list containing the object and Mathematica does not need to "go inside" the object itself. The example below may clarify what I mean:

In[3]:= Dimensions[{{Sqrt[2], E}, {\[Pi], 3}}]
TensorDimensions[{{Sqrt[2], E}, {\[Pi], 3}}]

Out[3]= {2, 2}

Out[4]= {2, 2}

Even in a situation like the next one below, Dimensions still gives the same answer as TensorDimensions, despite the fact that each one of the objects has length 2. In this case it is the change of heads from one level to the next what does the trick, as Michael explained (here at each level all the objects have the same head, and all have Length 2, but Dimensions does not print {2, 2, 2}).

In[5]:= Dimensions[{{Sqrt[2], Sqrt[3]}, {Sqrt[5], Sqrt[7]}}]
TensorDimensions[{{Sqrt[2], Sqrt[3]}, {Sqrt[5], Sqrt[7]}}]

Out[5]= {2, 2}

Out[6]= {2, 2}

There may be more situations where Dimensions and TensorDimensions give different results.

You may also want to look at the outputs of the Depth function on examples like the ones above and at the results of Mapping the Length function on those arrays at different levels. Hope this helps, Otto Linsuain

POSTED BY: Otto Linsuain

Sqrt[2] evaluates to Power[2, 1/2], which has dimensions {2}. Evaluate FullForm[Sqrt[2]] to see the actual expression.

POSTED BY: Michael Rogers

True, thank you. But the question is still there... Why does Power[2, 1/2] give 2 additional numbers to the main result?

Thank you

I suppose you're wondering about expression structure, for which one might look into some of the Tech Note links in the guide Expression Structure.

An expression in WL has the structure h[a, b,...] where h is called the Head and the number of "arguments" a, b,... is a nonnegative integer. For instance f[], f[x], f[x, y], f[x, y, z], etc.

  1. Parts of expressions. The parts of say h[a, b] are numbered: h is part 0, a is part 1, b is part 2. If k is a nonnegative integer, expr[[k]] returns part k if it exists. So h[a, b][[0]] returns h, h[a, b][[1]] returns a, and h[a, b][[2]] returns b. In the case of Sqrt[2] = Power[2, 1/2], part 0 is Power, part 1 is 2, and part 2 is 1/2.

  2. What Dimensions calculates. Dimensions[h[a, b]] returns {2} because the expression is a list of two arguments with head h. Dimensions[h[h[a, b, c], h[d, e, f]]] returns {2, 3} because the expression is a list of 2 expressions, each having a list of 3 arguments, and all with the same head h as at the top level. Dimensions[h[h[a, b, c], g[d, e, f]]] returns {2} because it is a list of two expressions, but each expression does not have the same head h. Dimensions[h[h[a, b, c], h[d, e]]] returns {2} because it is a list of two expressions, but each expression does not have the same length. Both head and length have to be the same for Dimensions to return a number for the next level.

  3. Numbers and numeric expressions. You were perhaps expecting Sqrt[2] to be a number. It is not, not in WL according to what is defined to be a number: NumberQ[Sqrt[2]] returns False. It is, instead, a numeric expression: NumericQ[Sqrt[2]] returns True. Power[2, 1/2] is an "exact" symbolic expression representing the real number $\sqrt{2}$. When you square it, you get the Integer 2 and not the Real 2. (Integer and Real are two types of numbers in WL, which you can look up.) It can be approximated to arbitrary precision. And generally it is used in computations as if it were the exact $\sqrt{2}$. But Power[2, 1/2] is an expression with parts and not a type of WL number.

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

Group Abstract Group Abstract