Skip to content

xt::clip() is missing a special case when a_min is greater than a_max #2860

@SoundDev

Description

@SoundDev

In the Python documentation for NumPy clip() there is a note section that gives a specific behavior when a_min is greater than a_max.

https://numpy.org/doc/stable/reference/generated/numpy.clip.html

Snippet from the numpy.clip.html page above

Notes

When a_min is greater than a_max, clip returns an array in which all values are equal to a_max, as shown in the second example.

Examples

Try it in your browser!
import numpy as np

a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
np.clip(a, 8, 1)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
np.clip(a, 3, 6, out=a)
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])
a
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])
a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4], 8)
array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])

This specific test works as specified above in Python but fails in xtensor.

This is the C++ code from our local test case that fails:

        TEST_METHOD(NativeXTensor_clip_AminGreaterThanAmax)
        {
            // clip where amin > amax should result in all values being set to amax

            xt::xarray<int> arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            auto clippedArr = xt::clip(arr, 8, 1); // amin > amax

            xt::xarray<int> expected = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
            for (auto i = 0; i < expected.size(); ++i)
            {
                Assert::AreEqual(expected[i], clippedArr[i]);
            }

            // actual incorrect clippedArr values are { 8, 8, 8, 8, 8, 8, 8, 8, 1, 1 }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions