|
| 1 | +"""Tests for np.minmax function.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from numpy.testing import assert_array_equal, assert_equal |
| 7 | + |
| 8 | + |
| 9 | +class TestMinMax: |
| 10 | + """Tests for np.minmax.""" |
| 11 | + |
| 12 | + def test_basic(self): |
| 13 | + a = np.array([3, 1, 4, 1, 5, 9, 2, 6]) |
| 14 | + mn, mx = np.minmax(a) |
| 15 | + assert_equal(mn, 1) |
| 16 | + assert_equal(mx, 9) |
| 17 | + |
| 18 | + def test_scalar(self): |
| 19 | + mn, mx = np.minmax(42) |
| 20 | + assert_equal(mn, 42) |
| 21 | + assert_equal(mx, 42) |
| 22 | + |
| 23 | + def test_2d_no_axis(self): |
| 24 | + a = np.array([[3, 1], [4, 5]]) |
| 25 | + mn, mx = np.minmax(a) |
| 26 | + assert_equal(mn, 1) |
| 27 | + assert_equal(mx, 5) |
| 28 | + |
| 29 | + def test_2d_axis0(self): |
| 30 | + a = np.array([[3, 1], [4, 5]]) |
| 31 | + mn, mx = np.minmax(a, axis=0) |
| 32 | + assert_array_equal(mn, [3, 1]) |
| 33 | + assert_array_equal(mx, [4, 5]) |
| 34 | + |
| 35 | + def test_2d_axis1(self): |
| 36 | + a = np.array([[3, 1], [4, 5]]) |
| 37 | + mn, mx = np.minmax(a, axis=1) |
| 38 | + assert_array_equal(mn, [1, 4]) |
| 39 | + assert_array_equal(mx, [3, 5]) |
| 40 | + |
| 41 | + def test_3d_axis(self): |
| 42 | + a = np.arange(24).reshape((2, 3, 4)) |
| 43 | + mn, mx = np.minmax(a, axis=2) |
| 44 | + assert_array_equal(mn, np.min(a, axis=2)) |
| 45 | + assert_array_equal(mx, np.max(a, axis=2)) |
| 46 | + |
| 47 | + def test_3d_axis_tuple(self): |
| 48 | + a = np.arange(24).reshape((2, 3, 4)) |
| 49 | + mn, mx = np.minmax(a, axis=(0, 2)) |
| 50 | + assert_array_equal(mn, np.min(a, axis=(0, 2))) |
| 51 | + assert_array_equal(mx, np.max(a, axis=(0, 2))) |
| 52 | + |
| 53 | + def test_keepdims(self): |
| 54 | + a = np.array([[3, 1], [4, 5]]) |
| 55 | + mn, mx = np.minmax(a, axis=0, keepdims=True) |
| 56 | + assert mn.shape == (1, 2) |
| 57 | + assert mx.shape == (1, 2) |
| 58 | + assert_array_equal(mn, [[3, 1]]) |
| 59 | + assert_array_equal(mx, [[4, 5]]) |
| 60 | + |
| 61 | + def test_float(self): |
| 62 | + a = np.array([1.5, -2.3, 4.7, 0.1]) |
| 63 | + mn, mx = np.minmax(a) |
| 64 | + assert_equal(mn, -2.3) |
| 65 | + assert_equal(mx, 4.7) |
| 66 | + |
| 67 | + def test_nan_propagation(self): |
| 68 | + a = np.array([1.0, np.nan, 3.0]) |
| 69 | + mn, mx = np.minmax(a) |
| 70 | + assert np.isnan(mn) |
| 71 | + assert np.isnan(mx) |
| 72 | + |
| 73 | + def test_all_same(self): |
| 74 | + a = np.array([5, 5, 5, 5]) |
| 75 | + mn, mx = np.minmax(a) |
| 76 | + assert_equal(mn, 5) |
| 77 | + assert_equal(mx, 5) |
| 78 | + |
| 79 | + def test_single_element(self): |
| 80 | + a = np.array([42]) |
| 81 | + mn, mx = np.minmax(a) |
| 82 | + assert_equal(mn, 42) |
| 83 | + assert_equal(mx, 42) |
| 84 | + |
| 85 | + def test_negative_axis(self): |
| 86 | + a = np.arange(12).reshape((3, 4)) |
| 87 | + mn, mx = np.minmax(a, axis=-1) |
| 88 | + assert_array_equal(mn, np.min(a, axis=-1)) |
| 89 | + assert_array_equal(mx, np.max(a, axis=-1)) |
| 90 | + |
| 91 | + def test_empty_with_initial(self): |
| 92 | + a = np.array([]) |
| 93 | + mn, mx = np.minmax(a, initial=(np.inf, -np.inf)) |
| 94 | + assert_equal(mn, np.inf) |
| 95 | + assert_equal(mx, -np.inf) |
| 96 | + |
| 97 | + def test_where(self): |
| 98 | + a = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) |
| 99 | + mask = np.array([True, False, True, False, True]) |
| 100 | + mn, mx = np.minmax(a, initial=(np.inf, -np.inf), where=mask) |
| 101 | + assert_equal(mn, 1.0) |
| 102 | + assert_equal(mx, 5.0) |
| 103 | + |
| 104 | + def test_integer_dtypes(self): |
| 105 | + for dtype in [ |
| 106 | + np.int8, |
| 107 | + np.int16, |
| 108 | + np.int32, |
| 109 | + np.int64, |
| 110 | + np.uint8, |
| 111 | + np.uint16, |
| 112 | + np.uint32, |
| 113 | + np.uint64, |
| 114 | + ]: |
| 115 | + a = np.array([10, 3, 7, 1, 9], dtype=dtype) |
| 116 | + mn, mx = np.minmax(a) |
| 117 | + assert_equal(mn, dtype(1)) |
| 118 | + assert_equal(mx, dtype(10)) |
| 119 | + |
| 120 | + def test_float_dtypes(self): |
| 121 | + for dtype in [np.float32, np.float64]: |
| 122 | + a = np.array([1.5, -2.3, 4.7, 0.1], dtype=dtype) |
| 123 | + mn, mx = np.minmax(a) |
| 124 | + assert_equal(mn, dtype(-2.3)) |
| 125 | + assert_equal(mx, dtype(4.7)) |
| 126 | + |
| 127 | + def test_bool(self): |
| 128 | + a = np.array([True, False, True]) |
| 129 | + mn, mx = np.minmax(a) |
| 130 | + assert_equal(mn, False) |
| 131 | + assert_equal(mx, True) |
| 132 | + |
| 133 | + def test_datetime(self): |
| 134 | + for dtype in ("m8[s]", "m8[Y]"): |
| 135 | + a = np.arange(10).astype(dtype) |
| 136 | + mn, mx = np.minmax(a) |
| 137 | + assert_equal(mn, a[0]) |
| 138 | + assert_equal(mx, a[9]) |
| 139 | + |
| 140 | + def test_datetime_nat_propagation(self): |
| 141 | + for dtype in ("m8[s]", "m8[Y]"): |
| 142 | + a = np.arange(10).astype(dtype) |
| 143 | + a[3] = "NaT" |
| 144 | + mn, mx = np.minmax(a) |
| 145 | + assert_equal(mn, a[3]) |
| 146 | + assert_equal(mx, a[3]) |
| 147 | + |
| 148 | + def test_complex(self): |
| 149 | + a = np.array([1 + 2j, 3 + 0j, 0 + 1j]) |
| 150 | + mn, mx = np.minmax(a) |
| 151 | + assert_equal(mn, np.min(a)) |
| 152 | + assert_equal(mx, np.max(a)) |
| 153 | + |
| 154 | + def test_consistency_with_min_max(self): |
| 155 | + """Verify minmax matches separate min/max calls.""" |
| 156 | + rng = np.random.default_rng(42) |
| 157 | + for _ in range(10): |
| 158 | + shape = rng.integers(1, 10, size=rng.integers(1, 4)) |
| 159 | + a = rng.standard_normal(shape) |
| 160 | + mn, mx = np.minmax(a) |
| 161 | + assert_equal(mn, np.min(a)) |
| 162 | + assert_equal(mx, np.max(a)) |
| 163 | + |
| 164 | + def test_consistency_with_min_max_axis(self): |
| 165 | + """Verify minmax matches separate min/max calls along axes.""" |
| 166 | + rng = np.random.default_rng(123) |
| 167 | + a = rng.standard_normal((5, 7, 3)) |
| 168 | + for axis in [0, 1, 2, -1, (0, 2)]: |
| 169 | + mn, mx = np.minmax(a, axis=axis) |
| 170 | + assert_array_equal(mn, np.min(a, axis=axis)) |
| 171 | + assert_array_equal(mx, np.max(a, axis=axis)) |
| 172 | + |
| 173 | + def test_list_input(self): |
| 174 | + mn, mx = np.minmax([3, 1, 4, 1, 5]) |
| 175 | + assert_equal(mn, 1) |
| 176 | + assert_equal(mx, 5) |
| 177 | + |
| 178 | + def test_empty_raises(self): |
| 179 | + with pytest.raises(ValueError): |
| 180 | + np.minmax(np.array([])) |
0 commit comments