]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/misc.py
Add a test for the empty product.
[sage.d.git] / mjo / misc.py
1 """
2 Stuff that doesn't fit anywhere else.
3 """
4
5 from sage.all import *
6 from functools import reduce
7
8
9 def legend_latex(obj):
10 """
11 Return the LaTeX representation of ``obj``, but wrap it in dollar
12 ($) signs so that we can pass it directly to plot() et al. as a
13 legend label.
14 """
15 return '$%s$' % latex(obj)
16
17
18 def product(factors):
19 """
20 Returns the product of the elements in the list ``factors``. If
21 the list is empty, we return 1.
22
23 EXAMPLES:
24
25 Normal integer multiplication::
26
27 sage: product([1,2,3])
28 6
29
30 And with symbolic variables::
31
32 sage: x,y,z = SR.var('x,y,z')
33 sage: product([x,y,z])
34 x*y*z
35
36 TESTS:
37
38 The empty product is the multiplicative identity (one)::
39
40 sage: product([])
41 1
42
43 """
44 return reduce(operator.mul, factors, 1)