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 import unittest | |
| 8 | |
| 9 from libs.math.vectors import vsum | |
| 10 | |
| 11 | |
| 12 BIG = 1e100 | |
| 13 LITTLE = 1 | |
| 14 | |
| 15 | |
| 16 class VectorsTest(unittest.TestCase): | |
| 17 | |
| 18 def setUp(self): | |
| 19 self._xs = [BIG, LITTLE, -BIG] | |
| 20 | |
| 21 def testFsumKeepsPrecision(self): | |
|
inferno
2016/12/05 00:12:37
Should a test be there for testing precision with
wrengr
2016/12/05 19:06:43
I don't know what you mean.
As math.fsum ships wi
| |
| 22 """Make sure ``testVsumKeepsPrecision`` should actually work. | |
| 23 | |
| 24 That is, this is a meta-test to make sure that the particular values | |
| 25 of ``BIG`` and ``LITTLE`` we use will indeed behave as expected. | |
| 26 """ | |
| 27 # N.B., with precision loss, we'll get 0 rather than LITTLE. | |
| 28 self.assertEqual(LITTLE, math.fsum(self._xs)) | |
| 29 | |
| 30 def testVsumKeepsPrecision(self): | |
| 31 """Ensure that ``vsum`` retains precision over ``sum``. | |
| 32 | |
| 33 Because ``BIG`` is big and ``LITTLE`` is little, performing summation | |
| 34 naively will cause the ``LITTLE`` to be lost in rounding errors. This | |
| 35 is the same test case as ``testFsumKeepsPrecision``. We make the | |
| 36 arrays have more than one element to make sure ``vsum`` actually | |
| 37 does work on vectors, as intended. We use variations on ``x`` in the | |
| 38 different components just so we don't do the same thing over and over; | |
| 39 there's nothing special about negation or doubling. | |
| 40 """ | |
| 41 vs = [np.array([x, -x, 2*x]) for x in self._xs] | |
| 42 total = vsum(vs) | |
| 43 self.assertIsNotNone(total) | |
| 44 | |
| 45 self.assertListEqual([LITTLE, -LITTLE, 2*LITTLE], total.tolist()) | |
| 46 | |
| 47 def testVsumEmptyWithoutShape(self): | |
| 48 """Ensure ``vsum`` returns ``None`` when expected. | |
| 49 | |
| 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 | |
| 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. | |
| 54 """ | |
| 55 self.assertIsNone(vsum([])) | |
| 56 | |
| 57 def testVSumEmptyWithShape(self): | |
| 58 """Ensure ``vsum`` returns the zero vector when expected. | |
| 59 | |
| 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 | |
| 62 zero vector of the correct shape. This test ensures that actually | |
| 63 does happen. | |
| 64 """ | |
| 65 expected_shape = (3,) | |
| 66 total = vsum([], shape=expected_shape) | |
| 67 self.assertIsNotNone(total) | |
| 68 self.assertTupleEqual(expected_shape, total.shape) | |
| 69 self.assertListEqual(np.zeros(expected_shape).tolist(), total.tolist()) | |
| 70 | |
| OLD | NEW |