1
$\begingroup$
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!

$\endgroup$
2
  • 1
    $\begingroup$ Times @@ tmp // FullForm. Take a closer look at documentation of Length too. $\endgroup$ Commented Nov 25, 2014 at 19:11
  • $\begingroup$ Perhaps you really want a test for the result being a List or not? Then you might look at ListQ (undocumented tho). $\endgroup$ Commented Nov 25, 2014 at 21:04

2 Answers 2

6
$\begingroup$

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])

$\endgroup$
4
$\begingroup$

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.

$\endgroup$
9
  • $\begingroup$ Map will operate over non-lists. For example, Map[f, Times[a, b, c]] gives Times[f[a],f[b],f[c]]. $\endgroup$ Commented Nov 25, 2014 at 19:32
  • $\begingroup$ 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. $\endgroup$ Commented Nov 25, 2014 at 19:47
  • 3
    $\begingroup$ @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.) $\endgroup$ Commented Nov 25, 2014 at 20:10
  • 1
    $\begingroup$ @KellenMyers oh, ok, I'm blind and stupid :P My apologies. p.s. I mean only f. $\endgroup$ Commented Nov 25, 2014 at 21:25
  • 4
    $\begingroup$ @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. $\endgroup$ Commented Nov 27, 2014 at 8:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.