diff options
author | Jaime <jaime.frio@gmail.com> | 2015-09-06 08:30:30 -0700 |
---|---|---|
committer | Jaime <jaime.frio@gmail.com> | 2015-09-06 08:30:30 -0700 |
commit | 37ece45df248ec514b08bd8d2164a1b68eea6bc8 (patch) | |
tree | f3babc746e207ab7f911867dcd2392d8af50306d /numpy/lib/tests/test_function_base.py | |
parent | c4924a73b348545238fab661543f61cc6631e087 (diff) | |
parent | bc1990e770966535d188785cafaa3230c0a0377e (diff) | |
download | numpy-37ece45df248ec514b08bd8d2164a1b68eea6bc8.tar.gz |
Merge pull request #6129 from faucon/gradient-axes
possibility to calculate gradient over specific axes instead of all axes
Diffstat (limited to 'numpy/lib/tests/test_function_base.py')
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 25 |
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): |