diff --git a/RELEASES.md b/RELEASES.md index b0e12bb7c..8ee67f70e 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -22,6 +22,7 @@ #### Closed issues +- Fix integer row and scalar indexing of weighted low-rank lazy transport plans returning an extra leading dimension (PR #893, Issue #892). - Fix dead links in the README (three example gallery pages and the reference [39]), the user guide and the contributing guide (PR #928) - Fix ordinary JAX gradients for dense `ot.emd2` and preserve the magnitude and higher-order dependence of gradients supplied to `JaxBackend.set_gradients`, including the envelope gradients in `ot.solve` (Issue #625) - Fix `ot.semidiscrete_wasserstein2_unif_circle` pairing sorted sample locations with unsorted weights, which broke permutation and rotation invariance for nonuniform weights (Issue #921) diff --git a/ot/utils.py b/ot/utils.py index 69a4854b5..6fbd6973d 100644 --- a/ot/utils.py +++ b/ot/utils.py @@ -898,7 +898,7 @@ def func(i, j, Q, R): else: def func(i, j, Q, R, d): - return nx.dot(Q[i] * d[None, :], R[j].T) + return nx.dot(Q[i] * d, R[j].T) T = LazyTensor(shape, func, Q=Q, R=R, d=d) diff --git a/test/test_utils.py b/test/test_utils.py index 9c93d520a..92850382b 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -739,6 +739,35 @@ def test_lowrank_LazyTensor(nx): np.testing.assert_allclose(nx.to_numpy(T[:]), nx.to_numpy(T0)) +@pytest.mark.parametrize("weighted", [False, True]) +@pytest.mark.parametrize( + "index", + [ + 0, + -1, + (1, 2), + (-1, -2), + (1, slice(1, None)), + (slice(None), 1), + (slice(1, 3), slice(None)), + slice(None), + ], +) +def test_lowrank_LazyTensor_indexing(nx, weighted, index): + Q, R, d = nx.from_numpy( + np.arange(10, dtype=float).reshape(5, 2), + np.arange(6, dtype=float).reshape(3, 2), + np.array([0.25, 2.0]), + ) + if not weighted: + d = None + T = ot.utils.get_lowrank_lazytensor(Q, R, d) + dense = nx.dot(Q if d is None else Q * d, R.T) + + assert T[index].shape == dense[index].shape + np.testing.assert_allclose(nx.to_numpy(T[index]), nx.to_numpy(dense[index])) + + def test_labels_to_mask_helper(nx): y = np.array([1, 0, 2, 2, 1]) out = np.array(