]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/misc.py
Add the 'product' function to misc.py.
[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 def legend_latex(obj):
9 """
10 Return the LaTeX representation of `obj`, but wrap it in dollar
11 ($) signs so that we can pass it directly to plot() et al. as a
12 legend label.
13 """
14 return '$%s$' % latex(obj)
15
16 def product(factors):
17 """
18 Returns the product of the elements in the list `factors`. If the
19 list is empty, we return 1.
20
21 TESTS:
22
23 Normal integer multiplication::
24
25 sage: product([1,2,3])
26 6
27
28 And with symbolic variables::
29
30 sage: x,y,z = var('x,y,z')
31 sage: product([x,y,z])
32 x*y*z
33
34 """
35 return reduce(operator.mul, factors, 1)