Repository: ivy-llc/ivy

Issue:
diagflat



Discussion:

Resolving patch:
diff --git a/ivy/functional/backends/numpy/experimental/linear_algebra.py b/ivy/functional/backends/numpy/experimental/linear_algebra.py
--- a/ivy/functional/backends/numpy/experimental/linear_algebra.py
+++ b/ivy/functional/backends/numpy/experimental/linear_algebra.py
@@ -68,15 +68,7 @@ def diagflat(
     diagonal_to_add = np.diag(x - np.full_like(x, padding_value), k=offset)
 
     diagonal_to_add = diagonal_to_add[tuple(slice(0, n) for n in output_array.shape)]
-    output_array += np.pad(
-        diagonal_to_add.astype(output_array.dtype),
-        [
-            (0, max([output_array.shape[0] - diagonal_to_add.shape[0], 0])),
-            (0, max([output_array.shape[1] - diagonal_to_add.shape[1], 0])),
-        ],
-        mode="constant",
-    )
-    ret = output_array.astype(out_dtype)
+    ret = diagonal_to_add.astype(out_dtype)
 
     if ivy.exists(out):
         ivy.inplace_update(out, ret)
diff --git a/ivy/functional/backends/paddle/experimental/linear_algebra.py b/ivy/functional/backends/paddle/experimental/linear_algebra.py
--- a/ivy/functional/backends/paddle/experimental/linear_algebra.py
+++ b/ivy/functional/backends/paddle/experimental/linear_algebra.py
@@ -13,7 +13,8 @@
 
 
 @with_unsupported_device_and_dtypes(
-    {"2.5.1 and below": {"cpu": ("int8", "int16", "uint8", "float16")}}, backend_version
+    {"2.5.1 and below": {"cpu": ("int8", "int16", "uint8", "float16", "bfloat16")}},
+    backend_version,
 )
 def diagflat(
     x: paddle.Tensor,
diff --git a/ivy/functional/frontends/jax/numpy/manipulations.py b/ivy/functional/frontends/jax/numpy/manipulations.py
--- a/ivy/functional/frontends/jax/numpy/manipulations.py
+++ b/ivy/functional/frontends/jax/numpy/manipulations.py
@@ -116,6 +116,14 @@ def concatenate(arrays, axis=0, dtype=None):
     return ret
 
 
+@to_ivy_arrays_and_back
+def diagflat(v, k=0):
+    ret = ivy.diagflat(v, offset=k)
+    while len(ivy.shape(ret)) < 2:
+        ret = ret.expand_dims(axis=0)
+    return ret
+
+
 @to_ivy_arrays_and_back
 def dsplit(ary, indices_or_sections):
     if isinstance(indices_or_sections, (list, tuple, ivy.Array)):


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_manipulations.py b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_manipulations.py
--- a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_manipulations.py
+++ b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_manipulations.py
@@ -705,6 +705,41 @@ def test_jax_concat(
     )
 
 
+@handle_frontend_test(
+    fn_tree="jax.numpy.diagflat",
+    dtype_x=helpers.dtype_and_values(
+        available_dtypes=helpers.get_dtypes("float"),
+        shape=helpers.get_shape(
+            min_num_dims=1, max_num_dims=2, min_dim_size=1, max_dim_size=10
+        ),
+        small_abs_safety_factor=2.5,
+        large_abs_safety_factor=2.5,
+        safety_factor_scale="log",
+    ),
+    k=st.integers(min_value=-5, max_value=5),
+)
+def test_jax_diagflat(
+    dtype_x,
+    k,
+    frontend,
+    test_flags,
+    fn_tree,
+    backend_fw,
+    on_device,
+):
+    dtype, x = dtype_x
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        v=x[0],
+        k=k,
+    )
+
+
 # dsplit
 @handle_frontend_test(
     fn_tree="jax.numpy.dsplit",