Re: Product
- To: mathgroup at smc.vnet.net
- Subject: [mg87517] Re: Product
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 12 Apr 2008 06:57:13 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <ftmtsj$4lf$1@smc.vnet.net>
Steven wrote:
> Silly beginner's question: is there a function to multiply all elements of a
> list, like Total does for addition?
> 'fraid I can't find it.
Hi Steven,
This not a silly question since such a built-in function does not exist.
Nevertheless, it is very easy to the product of a list by changing its
head.
What does that mean?
Every Mathematica expression is made of a head, say h, and some
elements, say e1, e2, etc., that is an expression if of the form
h[e1, e2, ...]
The difference among a list, a sum, and a product of variables,
therefore, is just their heads. For instance,
List[a, b, c, d] (* {a, b, c, d} *)
Plus[a, b, c, d] (* a + b + c + d *)
Times[a, b, c, d] (* a * b * c * d *)
are the canonical --- or *FullForm* --- representation of the list, sum,
and product of the four variables a, b, c, and d (assuming they are
unassigned). To go from one representation to another, it suffice to
change the head of the expression thanks to the command *Apply*.
The following should illustrate the process.
{a, b, c, d} (* => {a, b, c, d} *)
FullForm[{a, b, c, d}] (* => List[a, b, c, d] *)
Total[{a, b, c, d}] (* => a + b + c + d *)
FullForm[Total[{a, b, c, d}]] (* => Plus[a, b, c, d] *)
Apply[Plus, {a, b, c, d}] (* => a + b + c + d *)
Apply[Times, {a, b, c, d}] (* => a b c d *)
Range[5] (* => {1, 2, 3, 4, 5} *)
FullForm[Range[5]] (* => List[1, 2, 3, 4, 5] *)
Apply[Plus, Range[5]] (* => 15 *)
Apply[Times, Range[5]] (* => 120 *)
Regards,
-- Jean-Marc