Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: appengine/findit/libs/math/vectors.py

Issue 2547133002: Added n-ary vector summation (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import math
6 import numpy as np
7
8 def vsum(xs, shape=None):
9 """Accurate summation of a list of vectors.
10
11 This function is like ``math.fsum`` except operating over collections
12 of vectors rather than collections of scalars.
13
14 Args:
15 xs (list of np.ndarray of float): The vectors to add up.
16 shape (tuple of int): the shape of the vectors. Optional.
17
18 Returns:
19 Returns an ``np.ndarray`` if all is well, otherwise returns
20 ``None``. The situations where all is not well (and so we return
21 ``None``) are: (1) ``xs`` is empty and ``shape`` is not provided;
22 (2) a ``shape`` is provided but does not match the actual shape of
23 the vectors; (3) not all the vectors in the list have the same shape.
24 """
25 if shape is None:
26 if not xs:
27 return None
28
29 shape = xs[0].shape
30
31 # TODO(wrengr): reimplement this in C, inling CPython's implementation
Martin Barbella 2016/12/02 19:52:37 FWIW, I don't think that this is a worthwhile goal
Sharu Jiang 2016/12/02 22:17:51 +1
32 # of Shewchuk's algorithm, so that we can compute the final vector in
33 # in a single pass over the list of vectors, rather than doing this hack.
34 # <http://svn.python.org/view/python/trunk/Modules/mathmodule.c?view=markup>
35 total = np.zeros(shape)
36 it = np.nditer(total, flags=['multi_index'], op_flags=['writeonly'])
37 while not it.finished:
38 it[0] = math.fsum(x[it.multi_index] for x in xs)
39 it.iternext()
40
41 return total
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698