import ivy
from ivy.functional.frontends.numpy.func_wrapper import (
    to_ivy_arrays_and_back,
    handle_numpy_dtype,
)


@to_ivy_arrays_and_back
def tril(m, k=0):
    return ivy.tril(m, k=k)


@to_ivy_arrays_and_back
def triu(m, k=0):
    return ivy.triu(m, k=k)


@handle_numpy_dtype
@to_ivy_arrays_and_back
def tri(N, M=None, k=0, dtype="float64", *, like=None):
    if M is None:
        M = N
    ones = ivy.ones((N, M), dtype=dtype)
    return ivy.tril(ones, k=k)


@to_ivy_arrays_and_back
def diag(v, k=0):
    return ivy.diag(v, k=k)


@to_ivy_arrays_and_back
def vander(x, N=None, increasing=False):
    if ivy.is_float_dtype(x):
        x = x.astype(ivy.float64)
    elif ivy.is_bool_dtype or ivy.is_int_dtype(x):
        x = x.astype(ivy.int64)
    return ivy.vander(x, N=N, increasing=increasing)

ivy/functional/frontends/numpy/creation_routines/building_matrices.py
import ivy
from ivy.functional.frontends.numpy.func_wrapper import (
    to_ivy_arrays_and_back,
    handle_numpy_dtype,
)


@to_ivy_arrays_and_back
def tril(m, k=0):
    return ivy.tril(m, k=k)


@to_ivy_arrays_and_back
def triu(m, k=0):
    return ivy.triu(m, k=k)


@handle_numpy_dtype
@to_ivy_arrays_and_back
def tri(N, M=None, k=0, dtype="float64", *, like=None):
    if M is None:
        M = N
    ones = ivy.ones((N, M), dtype=dtype)
    return ivy.tril(ones, k=k)


@to_ivy_arrays_and_back
def diag(v, k=0):
    return ivy.diag(v, k=k)


@to_ivy_arrays_and_back
def vander(x, N=None, increasing=False):
    if ivy.is_float_dtype(x):
        x = x.astype(ivy.float64)
    elif ivy.is_bool_dtype or ivy.is_int_dtype(x):
        x = x.astype(ivy.int64)
    return ivy.vander(x, N=N, increasing=increasing)


# diagflat
@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

ivy/functional/frontends/numpy/creation_routines/building_matrices.py
diff --git a/ivy/functional/frontends/numpy/creation_routines/building_matrices.py b/ivy/functional/frontends/numpy/creation_routines/building_matrices.py
index 53fb4ff7cc8e0..4940faa544c32 100644
--- a/ivy/functional/frontends/numpy/creation_routines/building_matrices.py
+++ b/ivy/functional/frontends/numpy/creation_routines/building_matrices.py
@@ -36,3 +36,12 @@ def vander(x, N=None, increasing=False):
     elif ivy.is_bool_dtype or ivy.is_int_dtype(x):
         x = x.astype(ivy.int64)
     return ivy.vander(x, N=N, increasing=increasing)
+
+
+# diagflat
+@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
diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_creation_routines/test_building_matrices.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_creation_routines/test_building_matrices.py
index 4013053c27ddd..f7b67ceeaf5b9 100644
--- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_creation_routines/test_building_matrices.py
+++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_creation_routines/test_building_matrices.py
@@ -7,7 +7,6 @@
 from ivy_tests.test_ivy.helpers import handle_frontend_test
 from ivy_tests.test_ivy.test_functional.test_core.test_linalg import _diag_helper
 
-
 # tril
 @handle_frontend_test(
     fn_tree="numpy.tril",
@@ -161,3 +160,27 @@ def test_numpy_vander(
         N=N,
         increasing=increasing,
     )
+
+
+# diagflat
+@handle_frontend_test(
+    fn_tree="numpy.diagflat",
+    dtype_and_x_k=_diag_helper(),
+)
+def test_numpy_diagflat(
+    dtype_and_x_k,
+    frontend,
+    test_flags,
+    fn_tree,
+    on_device,
+):
+    dtype, x, k = dtype_and_x_k
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        v=x[0],
+        k=k,
+    )

diagflat

