Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Please Consider :

list={{5,2,6},{2,8,3}}

I need to get the product of all the element within list :

As of now I do this :

(#[[1]]*#[[2]]*#[[3]]) & /@ list

Which means I have to manually specify the number of elements within each least and the sublists length has to b equal.

How could do this automatically to automatically deal with the following case ?

listA={{5,2,6,3},{2,8,3,4}}
listB={{5,2,6},{2,4}}
share|improve this question

3 Answers 3

up vote 14 down vote accepted

You could use Apply at the first level:

Times @@@ listA
share|improve this answer

Just for fun:

Replace[list, {x__} :> 1 x, 1]

Since this answer has garnered some votes here is a more direct form of the implicit Times:

1##& @@@ list
share|improve this answer
1  
Nice. Implicitly passing x to Times by prefixing it with a 1, and because it is a Sequence it becomes Times[1, x]. Very clever. –  rcollyer Feb 19 '12 at 2:09
    
@rcollyer thank you :-) –  Mr.Wizard Feb 19 '12 at 2:20

Short answer: See Heike's post.

Longer version:

There are a couple of functions in the functional programming toolbox that are so common that they've gotten their own symbols. One of them is Apply, which takes a list and uses its elements as function arguments:

Apply[f, {a, b, c}]
f[a, b, c]

The shorthand notation for this would be f @@ {a, b, c}.

Additionally, Apply has an optinal third argument, specifying the depth at which the function should be applied:

(* Apply on 0-th level *)
Apply[f, {{a,b}, {c,d}}]
f[{a,b}, {c,d}]
(* Apply on the first level *)
Apply[f, {{a,b}, {c,d}}, {1}]
{f[a,b], f[c,d]}

The latter is also a very common expression, it has the shorthand notation f @@@ {{a,b}, {c,d}}. The notation ends here, i.e. there's no @@@@; if you need that, you'll have to use Apply explicitly. The documentation features plenty of examples if you need further help.

That said, what you want is converting a list of lists of numbers to a list of the product of the numbers. This is equivalent to applying the product function on the first level of the original list; that function is called Times in Mathematica (Product stands for the mathematical expression $\prod_a^bx$). Times is the internal function that is called when you enter something like a*b*c: it becomes Times[a,b,c] internally. Therefore, using the Apply function from above,

Apply[Times, {{a,b}, {c,d}}, {1}]

(* Evaluates to ... *)
{Times[a,b], Times[c,d]}

(* ... and is equivalent to ... *)
{a b, c d}

That's precisely what you want. Adding the syntactic sugar from above, @@@, this leaves you with the final short notation

Times @@@ list

to solve the problem.

share|improve this answer
    
Thank You very much for the Detailed explanation –  500 Feb 18 '12 at 21:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.