Skip to content

Commit

Permalink
Fix soft clamp when limits are at infinity.
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoOinonen committed Oct 14, 2024
1 parent d17c566 commit 10dff57
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions ppafm/ocl/cl/FF.cl
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,10 @@ __kernel void clamp_soft(
if (ind >= n) return;

float val = array_in[ind];
val = (val - max_value) / (1 + exp((val - max_value) / width)) + max_value;
val = (val - min_value) / (1 + exp((val - min_value) / -width)) + min_value;
if (max_value < INFINITY)
val = (val - max_value) / (1 + exp((val - max_value) / width)) + max_value;
if (min_value > -INFINITY)
val = (val - min_value) / (1 + exp((val - min_value) / -width)) + min_value;

array_out[ind] = val;
}
Expand Down
10 changes: 7 additions & 3 deletions tests/test_datagrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,18 @@ def test_clamp():
array = np.array([-1.0, 0.0, 1.0, 2.0])[None, None]
data_grid = FFcl.DataGrid(array, lvec=np.concatenate([np.zeros((1, 3)), np.eye(3)], axis=0))

new_grid = data_grid.clamp(minimum=0.0, maximum=1.0, in_place=False)
new_grid = data_grid.clamp(minimum=0.0, maximum=1.0, clamp_type="hard", in_place=False)

assert np.allclose(new_grid.array, [0.0, 0.0, 1.0, 1.0])
assert np.allclose(data_grid.array, array)

data_grid.clamp(maximum=1.0, in_place=True)
new_grid = data_grid.clamp(maximum=1.0, clamp_type="hard", in_place=False)

assert np.allclose(data_grid.array, [-1.0, 0.0, 1.0, 1.0])
assert np.allclose(new_grid.array, [-1.0, 0.0, 1.0, 1.0])

data_grid.clamp(maximum=1.0, clamp_type="soft", in_place=True)

assert (data_grid.array < 2.0).all()


def test_add_mult():
Expand Down

0 comments on commit 10dff57

Please sign in to comment.