X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Fmisc.py;h=f5a7bcf124d50cbc7c97d2b6628ce391679cbda5;hb=64c3510634164b0d8a43c2f034bdcc0c24b281cb;hp=28113b6473d871eb0cd3a9abb6b104be5df01a19;hpb=0c3f106bf9d1eb680a3429b8895077bb2d9c5812;p=sage.d.git diff --git a/mjo/misc.py b/mjo/misc.py index 28113b6..f5a7bcf 100644 --- a/mjo/misc.py +++ b/mjo/misc.py @@ -3,6 +3,7 @@ Stuff that doesn't fit anywhere else. """ from sage.all import * +from functools import reduce def legend_latex(obj): """ @@ -11,3 +12,24 @@ def legend_latex(obj): legend label. """ return '$%s$' % latex(obj) + +def product(factors): + """ + Returns the product of the elements in the list `factors`. If the + list is empty, we return 1. + + TESTS: + + Normal integer multiplication:: + + sage: product([1,2,3]) + 6 + + And with symbolic variables:: + + sage: x,y,z = var('x,y,z') + sage: product([x,y,z]) + x*y*z + + """ + return reduce(operator.mul, factors, 1)