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

Unified Diff: appengine/findit/libs/math/test/vectors_test.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 side-by-side diff with in-line comments
Download patch
Index: appengine/findit/libs/math/test/vectors_test.py
diff --git a/appengine/findit/libs/math/test/vectors_test.py b/appengine/findit/libs/math/test/vectors_test.py
new file mode 100644
index 0000000000000000000000000000000000000000..e6ccd3e758025bbff7b564acc09743fbaba10c1b
--- /dev/null
+++ b/appengine/findit/libs/math/test/vectors_test.py
@@ -0,0 +1,45 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import unittest
+import math
+import numpy as np
+
+from libs.math.vectors import vsum
+
+BIG = 1e100
+LITTLE = 1
+
+class VectorsTest(unittest.TestCase):
Martin Barbella 2016/12/02 19:52:37 2 blank lines before this one. Does gpylint compla
wrengr 2016/12/02 20:54:46 It really wants the numpy import to be grouped wit
Martin Barbella 2016/12/02 22:12:11 Yeah, those are fine to ignore. stgao, katesonia:
Sharu Jiang 2016/12/02 22:26:12 No, somehow the presubmit won't check the 2 empty
+
+ def setUp(self):
+ self._xs = [BIG, LITTLE, -BIG]
+
+ def testFsumKeepsPrecision(self):
+ """This test is to make sure that ``testVsum`` should actually work.
+
+ N.B., because ``math.fsum`` uses Shewchuk's algorithm whereas ``vsum``
+ uses Kahan's algorithm, the former is more precise than the latter. So
+ we need to exercise some caution when designing our tests.
+ """
+ # N.B., with precision loss, we'll get 0 rather than LITTLE.
+ self.assertEqual(LITTLE, math.fsum(self._xs))
+
+ def testVsumKeepsPrecision(self):
Martin Barbella 2016/12/02 19:52:37 Please add docstrings for all tests. They're prett
wrengr 2016/12/02 20:54:46 None of the other tests in the repo have them, but
Martin Barbella 2016/12/02 22:12:11 Thanks. They're really helpful. We probably should
+ vs = [np.array([x, -x, 2*x]) for x in self._xs]
+ total = vsum(vs)
+ self.assertIsNotNone(total)
+
+ self.assertListEqual([LITTLE, -LITTLE, 2*LITTLE], total.tolist())
+
+ def testVsumEmptyWithoutShape(self):
+ self.assertIsNone(vsum([]))
+
+ def testVSumEmptyWithShape(self):
+ expected_shape = (3,)
+ total = vsum([], shape=expected_shape)
+ self.assertIsNotNone(total)
+ self.assertTupleEqual(expected_shape, total.shape)
+ self.assertListEqual(np.zeros(expected_shape).tolist(), total.tolist())
+

Powered by Google App Engine
This is Rietveld 408576698