| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import math | 5 import math |
| 6 import numpy as np | 6 import numpy as np |
| 7 | 7 |
| 8 | 8 |
| 9 def vsum(vs, shape=None): | 9 def vsum(vs, shape=None): |
| 10 """Accurate summation of a list of vectors. | 10 """Accurate summation of a list of vectors. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 the vectors; (3) not all the vectors in the list have the same shape. | 24 the vectors; (3) not all the vectors in the list have the same shape. |
| 25 """ | 25 """ |
| 26 if shape is None: | 26 if shape is None: |
| 27 if not vs: | 27 if not vs: |
| 28 return None | 28 return None |
| 29 | 29 |
| 30 shape = vs[0].shape | 30 shape = vs[0].shape |
| 31 | 31 |
| 32 # It'd be better to vectorize the implementation of Shewchuk's | 32 # It'd be better to vectorize the implementation of Shewchuk's |
| 33 # algorithm directly, so we can avoid needing to traverse ``vs`` | 33 # algorithm directly, so we can avoid needing to traverse ``vs`` |
| 34 # repeatedly. However, this is deemed to have too high a maintinence | 34 # repeatedly. However, this is deemed to have too high a maintenance |
| 35 # cost for the performance benefit. | 35 # cost for the performance benefit. |
| 36 total = np.zeros(shape) | 36 total = np.zeros(shape) |
| 37 it = np.nditer(total, flags=['multi_index'], op_flags=['writeonly']) | 37 it = np.nditer(total, flags=['multi_index'], op_flags=['writeonly']) |
| 38 while not it.finished: | 38 while not it.finished: |
| 39 it[0] = math.fsum(v[it.multi_index] for v in vs) | 39 it[0] = math.fsum(v[it.multi_index] for v in vs) |
| 40 it.iternext() | 40 it.iternext() |
| 41 | 41 |
| 42 return total | 42 return total |
| OLD | NEW |