|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
# Copyright 2014-2018 by Christopher C. Little. |
|
4
|
|
|
# This file is part of Abydos. |
|
5
|
|
|
# |
|
6
|
|
|
# Abydos is free software: you can redistribute it and/or modify |
|
7
|
|
|
# it under the terms of the GNU General Public License as published by |
|
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
# (at your option) any later version. |
|
10
|
|
|
# |
|
11
|
|
|
# Abydos is distributed in the hope that it will be useful, |
|
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
# GNU General Public License for more details. |
|
15
|
|
|
# |
|
16
|
|
|
# You should have received a copy of the GNU General Public License |
|
17
|
|
|
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
|
|
19
|
|
|
"""abydos.util. |
|
20
|
|
|
|
|
21
|
|
|
The util module defines various utility functions for other modules within |
|
22
|
|
|
Abydos, including: |
|
23
|
|
|
|
|
24
|
|
|
- prod -- computes the product of a collection of numbers (akin to sum) |
|
25
|
|
|
- jitter -- adds small random noise to each member of a list of numbers |
|
26
|
|
|
(ported from R's jitter function) |
|
27
|
|
|
""" |
|
28
|
|
|
|
|
29
|
|
|
from operator import mul |
|
30
|
|
|
|
|
31
|
|
|
from six.moves import reduce |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def prod(nums): |
|
35
|
|
|
"""Return the product of nums. |
|
36
|
|
|
|
|
37
|
|
|
The product is Π(nums). |
|
38
|
|
|
|
|
39
|
|
|
Cf. https://en.wikipedia.org/wiki/Product_(mathematics) |
|
40
|
|
|
|
|
41
|
|
|
:param nums: a collection (list, tuple, set, etc.) of numbers |
|
42
|
|
|
:returns: the product of a nums |
|
43
|
|
|
|
|
44
|
|
|
>>> prod([1,1,1,1]) |
|
45
|
|
|
1 |
|
46
|
|
|
>>> prod((2,4,8)) |
|
47
|
|
|
64 |
|
48
|
|
|
>>> prod({1,2,3,4}) |
|
49
|
|
|
24 |
|
50
|
|
|
>>> prod(2**i for i in range(5)) |
|
51
|
|
|
1024 |
|
52
|
|
|
""" |
|
53
|
|
|
return reduce(mul, nums, 1) |
|
54
|
|
|
|