summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_function_base.py
diff options
context:
space:
mode:
authorMaximilian Trescher <maximilian@trescherpost.de>2015-07-29 13:25:55 +0200
committerMaximilian Trescher <maximilian@trescherpost.de>2015-09-03 14:10:30 +0200
commit2b7fff08567fd1e06df66b8ad2b71959dee63070 (patch)
treeb7ca9ae313bc89a626b83076ae020938f90d0e48 /numpy/lib/tests/test_function_base.py
parentc4924a73b348545238fab661543f61cc6631e087 (diff)
downloadnumpy-2b7fff08567fd1e06df66b8ad2b71959dee63070.tar.gz
possibility to calculate gradient over specific axes instead of all axes
when given axis=None, behave the same as axis not being provided
Diffstat (limited to 'numpy/lib/tests/test_function_base.py')
-rw-r--r--numpy/lib/tests/test_function_base.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index af9315d83..6a9d76a27 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -601,6 +601,31 @@ class TestGradient(TestCase):
num_error = np.abs((np.gradient(y, dx, edge_order=2) / analytical) - 1)
assert_(np.all(num_error < 0.03) == True)
+ def test_specific_axes(self):
+ # Testing that gradient can work on a given axis only
+ v = [[1, 1], [3, 4]]
+ x = np.array(v)
+ dx = [np.array([[2., 3.], [2., 3.]]),
+ np.array([[0., 0.], [1., 1.]])]
+ assert_array_equal(gradient(x, axis=0), dx[0])
+ assert_array_equal(gradient(x, axis=1), dx[1])
+ assert_array_equal(gradient(x, axis=-1), dx[1])
+ assert_array_equal(gradient(x, axis=(1,0)), [dx[1], dx[0]])
+
+ # test axis=None which means all axes
+ assert_almost_equal(gradient(x, axis=None), [dx[0], dx[1]])
+ # and is the same as no axis keyword given
+ assert_almost_equal(gradient(x, axis=None), gradient(x))
+
+ # test vararg order
+ assert_array_equal(gradient(x, 2, 3, axis=(1,0)), [dx[1]/2.0, dx[0]/3.0])
+ # test maximal number of varargs
+ assert_raises(SyntaxError, gradient, x, 1, 2, axis=1)
+
+ assert_raises(ValueError, gradient, x, axis=3)
+ assert_raises(ValueError, gradient, x, axis=-3)
+ assert_raises(TypeError, gradient, x, axis=[1,])
+
class TestAngle(TestCase):