Repository: cvxpy/cvxpy (language: cpp)

Issue:
diff fails on 2-D arrays with 1 row
**Describe the bug**
The `diff` command raises the ValueError `"Must have k >= 0 and X must have < k elements along "axis"` when attempting to calculate discrete differences for a 1-by-N array across the row.

**To Reproduce**
```
import cvxpy as cp

x1 = np.array([[1, 2, 3, 4, 5]])
x2 = cp.Variable((1, 5))

cp.diff(x1, axis=1)
cp.diff(x2, axis=1)
```

**Expected behavior**
`cp.diff` should be able to calculate the discrete difference for these examples. The corresponding numpy command works correctly on these examples.

**Version**
 - OS: macOS 12.4
 - CVXPY Version: 1.2.1



Discussion / hints:

Fix patch:
diff --git a/cvxpy/atoms/affine/diff.py b/cvxpy/atoms/affine/diff.py
index 2142795505..e22146c7cf 100644
--- a/cvxpy/atoms/affine/diff.py
+++ b/cvxpy/atoms/affine/diff.py
@@ -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):


Test patch:
diff --git a/cvxpy/tests/test_atoms.py b/cvxpy/tests/test_atoms.py
index d5c3833a12..1006ee1cff 100644
--- a/cvxpy/tests/test_atoms.py
+++ b/cvxpy/tests/test_atoms.py
@@ -1118,6 +1118,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)