Skip to content

Commit

Permalink
Fix bug with diff (#1835)
Browse files Browse the repository at this point in the history
* fix #1834

* test error

Co-authored-by: Steven Diamond <steven@gridmatic.com>
  • Loading branch information
SteveDiamond and SteveDiamond committed Aug 31, 2022
1 parent bf1e094 commit ad52347
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cvxpy/atoms/affine/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def diff(x, k: int = 1, axis: int = 0):
elif axis == 1:
x = x.T

if k < 0 or k >= x.shape[axis]:
# Always test shape[0] because if axis == 1 x is transposed.
if k < 0 or k >= x.shape[0]:
raise ValueError("Must have k >= 0 and X must have < k elements along "
"axis")
for i in range(k):
Expand Down
13 changes: 13 additions & 0 deletions cvxpy/tests/test_atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,19 @@ def test_diff(self) -> None:
self.assertEqual(cp.diff(A, axis=1).shape,
np.diff(B, axis=1).shape)

# Issue #1834

x1 = np.array([[1, 2, 3, 4, 5]])
x2 = cp.Variable((1, 5), value=x1)

expr = cp.diff(x1, axis=1)
self.assertItemsAlmostEqual(expr.value, np.diff(x1, axis=1))
expr = cp.diff(x2, axis=1)
self.assertItemsAlmostEqual(expr.value, np.diff(x1, axis=1))

with pytest.raises(ValueError, match="< k elements"):
cp.diff(x1, axis=0).value

def test_log_normcdf(self) -> None:
self.assertEqual(cp.log_normcdf(self.x).sign, s.NONPOS)
self.assertEqual(cp.log_normcdf(self.x).curvature, s.CONCAVE)
Expand Down

0 comments on commit ad52347

Please sign in to comment.