1
tmp = {x, y, z}^{1, 2, 3}
Times @@ tmp
Length[%]

This gives a length of 3. But I was expecting 1.

What is exactly this "length" of x*y^2*z^3 called? I would think this as a scalar of length 1?

Thanks!

Chen Stats Yu
  • 4,986
  • 2
  • 24
  • 50
  • 1
    Times @@ tmp // FullForm. Take a closer look at documentation of Length too. – Kuba Nov 25 '14 at 19:11
  • Perhaps you really want a test for the result being a List or not? Then you might look at ListQ (undocumented tho). – chuy Nov 25 '14 at 21:04

2 Answers2

6

According to the documentation:

Length[expr]

gives the number of elements in expr.

in your case

tmp = {x, y, z}^{1, 2, 3} 
r=Times @@ tmp
(*x y^2 z^3*)

r is an expression with thee elements. to see this you do:

FullForm[r]
(*Times[x, Power[y, 2], Power[z, 3]]*)

the expression here is Times and the elements of Times are 3 (x, Power[y, 2], and Power[z, 3])

Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
4

The Length operator will operate on lists, but if your object is not a list, it is not automatically considered to be length 1. When you apply Times@@ to your tmp, it is no longer a list.

Length will apply to many other expressions. For example:

Length[p+q+r]
(*Output: 3*)

Length[a*b+c]
(*Output: 2*)

Length[5+2]
(*Output: 0*)

In the first case, the sum has three parts, so it is a sum of length three. Note that if you did Length[List@@(p+q+r)] you'd get 3 also.

The next one is only length 2 because the "outermost" operation has length 2. It is a sum of two things. You can even do something like (a*b+c)[[1]] or even Length[(a*b+c)[[1]]].

The last one will produce 7 before attempting to evaluate Length, and because 7 is an "atomic" thing (it is not a list or other compound expression like a symbolic sum, product, etc.) it returns length 0.

Or try:

tmp2 = Times @@ tmp
Map[Length, List @@ tmp2]

Note that you are required to List@@ the expression. I don't think Map will map itself over products or sums, so you have to convert tmp2 back to a list, which would be {x,y^2,z^3}. Notice that x is not a compound expression so it has 0 length, just like 7.

Kellen Myers
  • 2,701
  • 15
  • 18
  • Map will operate over non-lists. For example, Map[f, Times[a, b, c]] gives Times[f[a],f[b],f[c]]. – evanb Nov 25 '14 at 19:32
  • Of course it will evanb, but only if you either convert it to a list (what I did) or to full form (which is basically what you did). Map[Length, tmp2] will still return 0 as expected. – Kellen Myers Nov 25 '14 at 19:47
  • 3
    @Artes, and KM, There must surely be a better use of your time and considerable talents. (I confess I now have this bizarre image of some sort of Mathematica smack-down tournament. This cannot be healthy.) – Daniel Lichtblau Nov 25 '14 at 20:10
  • I said nothing of the sort. f[1,2] is, unless f is defined elsewhere, going to be seen the same as any full form expression like Times[x,y] or the original expression in the question. Just like I said. If f is defined, f will be evaluated first, and the length of the resulting expression will depend entirely on what f gives with those inputs. – Kellen Myers Nov 25 '14 at 20:57
  • The "not." Non-lists are not assumed to be length 1. – Kellen Myers Nov 25 '14 at 21:23
  • And no, ClearAll[f];Length[f[1,2]] returns 2. Meanwhile ClearAll[f];f[p_,q_]=p+q;Length[f[1,2]] returns 0, while ClearAll[f,x];f[p_,q_]=p+q+x;Length[f[1,2]] returns 2. – Kellen Myers Nov 25 '14 at 21:25
  • 1
    @KellenMyers oh, ok, I'm blind and stupid :P My apologies. p.s. I mean only f. – Kuba Nov 25 '14 at 21:25
  • It was just a suggestion (he muttered, mostly to himself). – Daniel Lichtblau Nov 25 '14 at 22:50
  • 4
    @Artes Isn't 3 months too long to be carrying a grudge against a random person on the internet for a few fictitious points? You already did down vote in retaliation here (not that I encourage it)... your post has been acknowledged as useful by several others and the OP. Why still hang on to one down vote? For the sake of your own sanity, some things are best ignored. – rm -rf Nov 27 '14 at 08:41