Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| 9 def vsum(vs, shape=None): | |
| 10 """Accurate summation of a list of vectors. | |
|
inferno
2016/12/05 00:12:37
Where is this implementation based off ? Can you a
wrengr
2016/12/05 19:06:43
This implementation is a gross hack to compute the
| |
| 11 | |
| 12 This function is like ``math.fsum`` except operating over collections | |
| 13 of vectors rather than collections of scalars. | |
| 14 | |
| 15 Args: | |
| 16 vs (list of np.ndarray of float): The vectors to add up. | |
| 17 shape (tuple of int): the shape of the vectors. Optional. | |
| 18 | |
| 19 Returns: | |
| 20 Returns an ``np.ndarray`` if all is well, otherwise returns | |
| 21 ``None``. The situations where all is not well (and so we return | |
| 22 ``None``) are: (1) ``vs`` is empty and ``shape`` is not provided; | |
| 23 (2) a ``shape`` is provided but does not match the actual shape of | |
| 24 the vectors; (3) not all the vectors in the list have the same shape. | |
| 25 """ | |
| 26 if shape is None: | |
| 27 if not vs: | |
| 28 return None | |
| 29 | |
| 30 shape = vs[0].shape | |
| 31 | |
| 32 # It'd be better to vectorize the implementation of Shewchuk's | |
| 33 # algorithm directly, so we can avoid needing to traverse ``vs`` | |
| 34 # repeatedly. However, this is deemed to have too high a maintinence | |
| 35 # cost for the performance benefit. | |
|
inferno
2016/12/05 00:12:37
typo: of a maintenance
wrengr
2016/12/05 19:06:43
Done.
| |
| 36 total = np.zeros(shape) | |
| 37 it = np.nditer(total, flags=['multi_index'], op_flags=['writeonly']) | |
| 38 while not it.finished: | |
| 39 it[0] = math.fsum(v[it.multi_index] for v in vs) | |
| 40 it.iternext() | |
| 41 | |
| 42 return total | |
| OLD | NEW |