Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#### Closed issues

- 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)
- Center floating-point inputs in `ot.utils.proj_simplex` to prevent large common offsets from erasing the simplex mass (Issue #879, PR #880).
- Fix `ot.semidiscrete_wasserstein2_unif_circle` pairing sorted sample locations with unsorted weights, which broke permutation and rotation invariance for nonuniform weights (Issue #921)
- Fix an out-of-bounds read in `ot.bsp.merge_bijections`: the number of plans was taken from `perms.shape[1]` (the number of samples) instead of `perms.shape[0]`, so merging fewer plans than samples read past the array and merging more silently dropped plans. A `perms` outside the documented `(n_plans, n_samples)` layout, an empty plan stack and rows that are not permutations are now rejected (PR #911, Issue #910)
- Fix overflow to NaN in `ot.stochastic.coordinate_grad_semi_dual` by shifting before exponentiating, as `ot.stochastic.c_transform_entropic` already does. The factor cancels in the normalisation, so results are unchanged where the old code did not overflow (PR #866, Issue #264)
Expand Down
7 changes: 7 additions & 0 deletions ot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def proj_simplex(v, z=1):
.. note:: This function is backend-compatible and will work on arrays
from all compatible backends.

Floating-point inputs are centered by their column maximum before projection.
A common offset does not change the simplex projection; removing it prevents
a large offset from erasing the simplex size in the cumulative sums.

Parameters
----------
v : {array-like}, shape (n, d)
Expand All @@ -123,6 +127,9 @@ def proj_simplex(v, z=1):
Array of projections on the simplex
"""
nx = get_backend(v)
# The simplex projection is invariant to a common offset per column.
if nx.is_floating_point(v):
v = v - nx.max(v, axis=0)
n = v.shape[0]
if v.ndim == 1:
d1 = 1
Expand Down
18 changes: 18 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ def test_proj_simplex(nx):
np.testing.assert_allclose(l1, l2, atol=1e-5)


@pytest.mark.parametrize(
"dtype, offset",
[
(np.float32, 0),
(np.float32, 1e8),
(np.float64, 0),
(np.float64, 1e16),
(np.int64, 0),
],
)
@pytest.mark.parametrize("matrix", [False, True])
def test_proj_simplex_large_translation(nx, dtype, offset, matrix):
values = np.full((3, 2) if matrix else (3,), offset, dtype=dtype)
result = nx.to_numpy(ot.utils.proj_simplex(nx.from_numpy(values)))
np.testing.assert_allclose(result, np.full(values.shape, 1 / 3), rtol=1e-6)
np.testing.assert_allclose(result.sum(axis=0), 1, rtol=1e-6)


def test_projection_sparse_simplex():
def double_sort_projection_sparse_simplex(X, max_nz, z=1, axis=None):
r"""This is an equivalent but less efficient version
Expand Down
Loading