|
| 1 | +import pytest |
| 2 | + |
| 3 | +from .._flags import set_array_api_strict_flags |
| 4 | + |
| 5 | +import array_api_strict as xp |
| 6 | + |
| 7 | +# TODO: Maybe all of these exceptions should be IndexError? |
| 8 | + |
| 9 | +# Technically this is linear_algebra, not linalg, but it's simpler to keep |
| 10 | +# both of these tests together |
| 11 | +def test_vecdot_2023_12(): |
| 12 | + # Test the axis < 0 restriction for 2023.12, and also the 2022.12 axis >= |
| 13 | + # 0 behavior (which is primarily kept for backwards compatibility). |
| 14 | + |
| 15 | + a = xp.ones((2, 3, 4, 5)) |
| 16 | + b = xp.ones(( 3, 4, 1)) |
| 17 | + |
| 18 | + # 2022.12 behavior, which is to apply axis >= 0 after broadcasting |
| 19 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=0)) |
| 20 | + assert xp.linalg.vecdot(a, b, axis=1).shape == (2, 4, 5) |
| 21 | + assert xp.linalg.vecdot(a, b, axis=2).shape == (2, 3, 5) |
| 22 | + # This is disallowed because the arrays must have the same values before |
| 23 | + # broadcasting |
| 24 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=-1)) |
| 25 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=-4)) |
| 26 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=3)) |
| 27 | + |
| 28 | + # Out-of-bounds axes even after broadcasting |
| 29 | + pytest.raises(IndexError, lambda: xp.linalg.vecdot(a, b, axis=4)) |
| 30 | + pytest.raises(IndexError, lambda: xp.linalg.vecdot(a, b, axis=-5)) |
| 31 | + |
| 32 | + # negative axis behavior is unambiguous when it's within the bounds of |
| 33 | + # both arrays before broadcasting |
| 34 | + assert xp.linalg.vecdot(a, b, axis=-2).shape == (2, 3, 5) |
| 35 | + assert xp.linalg.vecdot(a, b, axis=-3).shape == (2, 4, 5) |
| 36 | + |
| 37 | + # 2023.12 behavior, which is to only allow axis < 0 and axis >= |
| 38 | + # min(x1.ndim, x2.ndim), which is unambiguous |
| 39 | + with pytest.warns(UserWarning): |
| 40 | + set_array_api_strict_flags(api_version='2023.12') |
| 41 | + |
| 42 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=0)) |
| 43 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=1)) |
| 44 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=2)) |
| 45 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=3)) |
| 46 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=-1)) |
| 47 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=-4)) |
| 48 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=4)) |
| 49 | + pytest.raises(ValueError, lambda: xp.linalg.vecdot(a, b, axis=-5)) |
| 50 | + |
| 51 | + assert xp.linalg.vecdot(a, b, axis=-2).shape == (2, 3, 5) |
| 52 | + assert xp.linalg.vecdot(a, b, axis=-3).shape == (2, 4, 5) |
| 53 | + |
| 54 | +@pytest.mark.parametrize('api_version', ['2021.12', '2022.12', '2023.12']) |
| 55 | +def test_cross(api_version): |
| 56 | + # This test tests everything that should be the same across all supported |
| 57 | + # API versions. |
| 58 | + |
| 59 | + if api_version != '2022.12': |
| 60 | + with pytest.warns(UserWarning): |
| 61 | + set_array_api_strict_flags(api_version=api_version) |
| 62 | + else: |
| 63 | + set_array_api_strict_flags(api_version=api_version) |
| 64 | + |
| 65 | + a = xp.ones((2, 4, 5, 3)) |
| 66 | + b = xp.ones(( 4, 1, 3)) |
| 67 | + assert xp.linalg.cross(a, b, axis=-1).shape == (2, 4, 5, 3) |
| 68 | + |
| 69 | + a = xp.ones((2, 4, 3, 5)) |
| 70 | + b = xp.ones(( 4, 3, 1)) |
| 71 | + assert xp.linalg.cross(a, b, axis=-2).shape == (2, 4, 3, 5) |
| 72 | + |
| 73 | + # This is disallowed because the axes must equal 3 before broadcasting |
| 74 | + a = xp.ones((3, 2, 3, 5)) |
| 75 | + b = xp.ones(( 2, 1, 1)) |
| 76 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=-1)) |
| 77 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=-2)) |
| 78 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=-3)) |
| 79 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=-4)) |
| 80 | + |
| 81 | + # Out-of-bounds axes even after broadcasting |
| 82 | + pytest.raises(IndexError, lambda: xp.linalg.cross(a, b, axis=4)) |
| 83 | + pytest.raises(IndexError, lambda: xp.linalg.cross(a, b, axis=-5)) |
| 84 | + |
| 85 | +@pytest.mark.parametrize('api_version', ['2021.12', '2022.12']) |
| 86 | +def test_cross_2022_12(api_version): |
| 87 | + # Test the 2022.12 axis >= 0 behavior, which is primarily kept for |
| 88 | + # backwards compatibility. Note that unlike vecdot, array_api_strict |
| 89 | + # cross() never implemented the "after broadcasting" axis behavior, but |
| 90 | + # just reused NumPy cross(), which applies axes before broadcasting. |
| 91 | + if api_version != '2022.12': |
| 92 | + with pytest.warns(UserWarning): |
| 93 | + set_array_api_strict_flags(api_version=api_version) |
| 94 | + else: |
| 95 | + set_array_api_strict_flags(api_version=api_version) |
| 96 | + |
| 97 | + a = xp.ones((3, 2, 4, 5)) |
| 98 | + b = xp.ones((3, 2, 4, 1)) |
| 99 | + assert xp.linalg.cross(a, b, axis=0).shape == (3, 2, 4, 5) |
| 100 | + |
| 101 | + # ambiguous case |
| 102 | + a = xp.ones(( 3, 4, 5)) |
| 103 | + b = xp.ones((3, 2, 4, 1)) |
| 104 | + assert xp.linalg.cross(a, b, axis=0).shape == (3, 2, 4, 5) |
| 105 | + |
| 106 | +def test_cross_2023_12(): |
| 107 | + # 2023.12 behavior, which is to only allow axis < 0 and axis >= |
| 108 | + # min(x1.ndim, x2.ndim), which is unambiguous |
| 109 | + with pytest.warns(UserWarning): |
| 110 | + set_array_api_strict_flags(api_version='2023.12') |
| 111 | + |
| 112 | + a = xp.ones((3, 2, 4, 5)) |
| 113 | + b = xp.ones((3, 2, 4, 1)) |
| 114 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=0)) |
| 115 | + |
| 116 | + a = xp.ones(( 3, 4, 5)) |
| 117 | + b = xp.ones((3, 2, 4, 1)) |
| 118 | + pytest.raises(ValueError, lambda: xp. linalg.cross(a, b, axis=0)) |
| 119 | + |
| 120 | + a = xp.ones((2, 4, 5, 3)) |
| 121 | + b = xp.ones(( 4, 1, 3)) |
| 122 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=0)) |
| 123 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=1)) |
| 124 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=2)) |
| 125 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=3)) |
| 126 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=-2)) |
| 127 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=-3)) |
| 128 | + pytest.raises(ValueError, lambda: xp.linalg.cross(a, b, axis=-4)) |
| 129 | + |
| 130 | + pytest.raises(IndexError, lambda: xp.linalg.cross(a, b, axis=4)) |
| 131 | + pytest.raises(IndexError, lambda: xp.linalg.cross(a, b, axis=-5)) |
| 132 | + |
| 133 | + assert xp.linalg.cross(a, b, axis=-1).shape == (2, 4, 5, 3) |
0 commit comments