| 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 import unittest | 7 import unittest |
| 8 | 8 |
| 9 from libs.math.vectors import vsum | 9 from libs.math.vectors import vsum |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 def testVsumEmptyWithoutShape(self): | 47 def testVsumEmptyWithoutShape(self): |
| 48 """Ensure ``vsum`` returns ``None`` when expected. | 48 """Ensure ``vsum`` returns ``None`` when expected. |
| 49 | 49 |
| 50 The empty summation should return the zero vector. However, since | 50 The empty summation should return the zero vector. However, since |
| 51 we don't know the shape of the vectors in the list, we don't know | 51 we don't know the shape of the vectors in the list, we don't know |
| 52 what shape of zero vector to return. Thus, we return ``None`` as | 52 what shape of zero vector to return. Thus, we return ``None`` as |
| 53 the only sensible result. This test ensures that actually does happen. | 53 the only sensible result. This test ensures that actually does happen. |
| 54 """ | 54 """ |
| 55 self.assertIsNone(vsum([])) | 55 self.assertIsNone(vsum([])) |
| 56 | 56 |
| 57 def testVSumEmptyWithShape(self): | 57 def testVsumEmptyWithShape(self): |
| 58 """Ensure ``vsum`` returns the zero vector when expected. | 58 """Ensure ``vsum`` returns the zero vector when expected. |
| 59 | 59 |
| 60 The empty summation should return the zero vector. If we know the | 60 The empty summation should return the zero vector. If we know the |
| 61 shape of the vectors in the list then we can in fact return the | 61 shape of the vectors in the list then we can in fact return the |
| 62 zero vector of the correct shape. This test ensures that actually | 62 zero vector of the correct shape. This test ensures that actually |
| 63 does happen. | 63 does happen. |
| 64 """ | 64 """ |
| 65 expected_shape = (3,) | 65 expected_shape = (3,) |
| 66 total = vsum([], shape=expected_shape) | 66 total = vsum([], shape=expected_shape) |
| 67 self.assertIsNotNone(total) | 67 self.assertIsNotNone(total) |
| 68 self.assertTupleEqual(expected_shape, total.shape) | 68 self.assertTupleEqual(expected_shape, total.shape) |
| 69 self.assertListEqual(np.zeros(expected_shape).tolist(), total.tolist()) | 69 self.assertListEqual(np.zeros(expected_shape).tolist(), total.tolist()) |
| 70 | 70 |
| 71 def testVsumWithNonFloatVector(self): |
| 72 """Tests that ``vsum`` works for list of float-like objects.""" |
| 73 class MimicFloat(object): |
| 74 |
| 75 def __init__(self, value): |
| 76 self.value = float(value) |
| 77 |
| 78 def __add__(self, number): |
| 79 return math.fsum([self.value, number]) |
| 80 |
| 81 __radd__ = __add__ |
| 82 |
| 83 lists = [[2.3, 0.4], [0.2, 0.3]] |
| 84 array_lists = [np.array(l) for l in lists] |
| 85 mimic_float_lists = [[MimicFloat(number) for number in l] for l in lists] |
| 86 array_mimic_float_lists = [np.array(l) for l in mimic_float_lists] |
| 87 |
| 88 self.assertListEqual(vsum(array_lists).tolist(), |
| 89 vsum(array_mimic_float_lists).tolist()) |
| OLD | NEW |