Repository: ivy-llc/ivy

Issue:
setdiff1d



Discussion:

Resolving patch:
diff --git a/ivy/functional/backends/paddle/experimental/manipulation.py b/ivy/functional/backends/paddle/experimental/manipulation.py
--- a/ivy/functional/backends/paddle/experimental/manipulation.py
+++ b/ivy/functional/backends/paddle/experimental/manipulation.py
@@ -351,7 +351,7 @@ def flatten(
 ) -> paddle.Tensor:
     ivy.utils.assertions.check_elem_in_list(order, ["C", "F"])
     if x.ndim == 0:
-        return x
+        return x.reshape((-1,))
 
     def _flatten(x, start_dim, end_dim):
         if x.dtype in [
diff --git a/ivy/functional/frontends/jax/numpy/creation.py b/ivy/functional/frontends/jax/numpy/creation.py
--- a/ivy/functional/frontends/jax/numpy/creation.py
+++ b/ivy/functional/frontends/jax/numpy/creation.py
@@ -1,7 +1,7 @@
 import ivy
 from ivy.func_wrapper import with_unsupported_dtypes
 from ivy.functional.frontends.jax.array import Array
-
+import ivy.functional.frontends.jax.numpy as jnp_frontend
 from ivy.functional.frontends.jax.func_wrapper import (
     to_ivy_arrays_and_back,
     outputs_to_frontend_arrays,
@@ -242,6 +242,33 @@ def ones_like(a, dtype=None, shape=None):
     return ivy.ones_like(a, dtype=dtype)
 
 
+@to_ivy_arrays_and_back
+def setdiff1d(ar1, ar2, assume_unique=False, *, size=None, fill_value=None):
+    fill_value = ivy.array(0 if fill_value is None else fill_value, dtype=ar1.dtype)
+    if ar1.size == 0:
+        return ivy.full(size or 0, fill_value, dtype=ar1.dtype)
+    if not assume_unique:
+        val = (
+            ivy.to_scalar(ivy.all(ar1))
+            if ivy.is_bool_dtype(ar1.dtype)
+            else ivy.to_scalar(ivy.min(ar1))
+        )
+        ar1 = jnp_frontend.unique(ar1, size=size and ar1.size, fill_value=val).ivy_array
+    mask = in1d(ar1, ar2, invert=True).ivy_array
+    if size is None:
+        return ar1[mask]
+    else:
+        if not (assume_unique):
+            # Set mask to zero at locations corresponding to unique() padding.
+            n_unique = ar1.size + 1 - (ar1 == ar1[0]).sum(dtype=ivy.int64)
+            mask = ivy.where(ivy.arange(ar1.size) < n_unique, mask, False)
+        return ivy.where(
+            ivy.arange(size) < mask.sum(dtype=ivy.int64),
+            ar1[jnp_frontend.where(mask, size=size)[0].ivy_array],
+            fill_value,
+        )
+
+
 @to_ivy_arrays_and_back
 def single(x):
     return ivy.astype(x, ivy.float32)
diff --git a/ivy/functional/frontends/jax/numpy/searching_sorting.py b/ivy/functional/frontends/jax/numpy/searching_sorting.py
--- a/ivy/functional/frontends/jax/numpy/searching_sorting.py
+++ b/ivy/functional/frontends/jax/numpy/searching_sorting.py
@@ -163,7 +163,7 @@ def unique(
     bools = [return_index, return_inverse, return_counts]
     # indexing each element whose condition is True except for the values
     uniques = [uniques[0]] + [uni for idx, uni in enumerate(uniques[1:]) if bools[idx]]
-    return uniques
+    return uniques[0] if len(uniques) == 1 else uniques
 
 
 @to_ivy_arrays_and_back


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_creation.py b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_creation.py
--- a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_creation.py
+++ b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_creation.py
@@ -865,6 +865,90 @@ def test_jax_numpy_in1d(
     )
 
 
+@handle_frontend_test(
+    fn_tree="jax.numpy.in1d",
+    dtype_and_a=helpers.dtype_and_values(min_num_dims=1, max_num_dims=1),
+    dtype_and_b=helpers.dtype_and_values(min_num_dims=1, max_num_dims=1),
+    assume_unique=st.booleans(),
+    invert=st.booleans(),
+)
+def test_jax_numpy_in1d(
+    *,
+    dtype_and_a,
+    dtype_and_b,
+    assume_unique,
+    invert,
+    on_device,
+    fn_tree,
+    frontend,
+    backend_fw,
+    test_flags,
+):
+    input_dtype_a, a = dtype_and_a
+    input_dtype_b, b = dtype_and_b
+
+    helpers.test_frontend_function(
+        input_dtypes=input_dtype_a + input_dtype_b,
+        frontend=frontend,
+        test_flags=test_flags,
+        backend_to_test=backend_fw,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        ar1=a[0],
+        ar2=b[0],
+        assume_unique=assume_unique,
+        invert=invert,
+    )
+
+
+@handle_frontend_test(
+    fn_tree="jax.numpy.setdiff1d",
+    dtype_and_a=helpers.dtype_and_values(
+        min_num_dims=1,
+        max_num_dims=1,
+        available_dtypes=helpers.get_dtypes("valid"),
+    ),
+    dtype_and_b=helpers.dtype_and_values(
+        min_num_dims=1,
+        max_num_dims=1,
+        available_dtypes=helpers.get_dtypes("valid"),
+    ),
+    use_size=st.booleans(),
+    size=st.integers(min_value=1, max_value=100),
+    assume_unique=st.booleans(),
+)
+def test_jax_numpy_setdiff1d(
+    *,
+    dtype_and_a,
+    dtype_and_b,
+    use_size,
+    size,
+    assume_unique,
+    on_device,
+    fn_tree,
+    frontend,
+    backend_fw,
+    test_flags,
+):
+    input_dtype_a, a = dtype_and_a
+    input_dtype_b, b = dtype_and_b
+    if not use_size:
+        size = None
+
+    helpers.test_frontend_function(
+        input_dtypes=input_dtype_a + input_dtype_b,
+        frontend=frontend,
+        test_flags=test_flags,
+        backend_to_test=backend_fw,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        ar1=a[0],
+        ar2=b[0],
+        assume_unique=assume_unique,
+        size=size,
+    )
+
+
 # ones
 @handle_frontend_test(
     fn_tree="jax.numpy.ones",