summaryrefslogtreecommitdiff
path: root/numpy/linalg/tests
diff options
context:
space:
mode:
authorsslivkoff <stormslivkoff@gmail.com>2020-04-29 14:08:27 -0700
committerGitHub <noreply@github.com>2020-04-30 00:08:27 +0300
commit4753652c605833e2398d7e46b754a71729a54713 (patch)
tree2d3e24c3d06a31053893cbae0b64b9401596617f /numpy/linalg/tests
parent44e26f24621ce86ed6b24bfba4a04e82319d7bf0 (diff)
downloadnumpy-4753652c605833e2398d7e46b754a71729a54713.tar.gz
ENH: update numpy.linalg.multi_dot to accept an `out` argument (#15715)
* ENH: update numpy.linalg.multi_dot to accept an `out` argument * TST ensure value returned by numpy.linalg.multi_dot matches out * DOC add note about initial call to numpy.linalg.multi_dot * DOC add release note for #15715 Co-authored-by: Matti Picus <matti.picus@gmail.com> Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net> Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
Diffstat (limited to 'numpy/linalg/tests')
-rw-r--r--numpy/linalg/tests/test_linalg.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py
index dae4ef61e..3f3bf9f70 100644
--- a/numpy/linalg/tests/test_linalg.py
+++ b/numpy/linalg/tests/test_linalg.py
@@ -1930,6 +1930,41 @@ class TestMultiDot:
# the result should be a scalar
assert_equal(multi_dot([A1d, B, C, D1d]).shape, ())
+ def test_three_arguments_and_out(self):
+ # multi_dot with three arguments uses a fast hand coded algorithm to
+ # determine the optimal order. Therefore test it separately.
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+
+ out = np.zeros((6, 2))
+ ret = multi_dot([A, B, C], out=out)
+ assert out is ret
+ assert_almost_equal(out, A.dot(B).dot(C))
+ assert_almost_equal(out, np.dot(A, np.dot(B, C)))
+
+ def test_two_arguments_and_out(self):
+ # separate code path with two arguments
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ out = np.zeros((6, 6))
+ ret = multi_dot([A, B], out=out)
+ assert out is ret
+ assert_almost_equal(out, A.dot(B))
+ assert_almost_equal(out, np.dot(A, B))
+
+ def test_dynamic_programing_optimization_and_out(self):
+ # multi_dot with four or more arguments uses the dynamic programing
+ # optimization and therefore deserve a separate test
+ A = np.random.random((6, 2))
+ B = np.random.random((2, 6))
+ C = np.random.random((6, 2))
+ D = np.random.random((2, 1))
+ out = np.zeros((6, 1))
+ ret = multi_dot([A, B, C, D], out=out)
+ assert out is ret
+ assert_almost_equal(out, A.dot(B).dot(C).dot(D))
+
def test_dynamic_programming_logic(self):
# Test for the dynamic programming part
# This test is directly taken from Cormen page 376.