Repository: ivy-llc/ivy

Issue:
fromfunction



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/numpy/creation_routines/from_shape_or_value.py b/ivy/functional/frontends/numpy/creation_routines/from_shape_or_value.py
--- a/ivy/functional/frontends/numpy/creation_routines/from_shape_or_value.py
+++ b/ivy/functional/frontends/numpy/creation_routines/from_shape_or_value.py
@@ -72,3 +72,10 @@ def full_like(a, fill_value, dtype=None, order="K", subok=True, shape=None):
     if shape:
         return ivy.full(shape, fill_value, dtype=dtype)
     return ivy.full_like(a, fill_value, dtype=dtype)
+
+
+@handle_numpy_dtype
+@outputs_to_frontend_arrays
+def fromfunction(function, shape, *, dtype="float64", like=None, **kwargs):
+    args = ivy.indices(shape, dtype=dtype)
+    return function(*args, **kwargs)


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_creation_routines/test_from_shape_or_value.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_creation_routines/test_from_shape_or_value.py
--- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_creation_routines/test_from_shape_or_value.py
+++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_creation_routines/test_from_shape_or_value.py
@@ -396,3 +396,86 @@ def test_numpy_full_like(
         subok=True,
         shape=shape,
     )
+
+
+@st.composite
+def _shape_and_function(
+    draw,
+    *,
+    allow_none=False,
+    min_num_dims=1,
+    max_num_dims=5,
+    min_dim_size=1,
+    max_dim_size=10,
+):
+    shape = draw(
+        helpers.get_shape(
+            allow_none=allow_none,
+            min_num_dims=min_num_dims,
+            max_num_dims=max_num_dims,
+            min_dim_size=min_dim_size,
+            max_dim_size=max_dim_size,
+        )
+    )
+
+    VARS = "abcdefghijklmnopqrstuvw"
+    args = ""
+    out = ""
+    for i in range(len(shape)):
+        args += f"{VARS[i]},"
+        out += f"{VARS[i]}+"
+    fn_str = f"lambda {args[:-1]}: {out[:-1]}"
+
+    def fn2(*args):
+        return args[0]
+
+    if len(shape) > 1:
+
+        def fn3(*args):
+            return args[0] == args[1]
+
+    else:
+
+        def fn3(*args):
+            return args[0] > 10
+
+    function = draw(st.sampled_from([eval(fn_str), fn2, fn3]))
+
+    return shape, function
+
+
+@handle_frontend_test(
+    fn_tree="numpy.fromfunction",
+    shape_and_function=_shape_and_function(
+        allow_none=False,
+        min_num_dims=1,
+        max_num_dims=5,
+        min_dim_size=1,
+        max_dim_size=5,
+    ),
+    # not using valid as bool a is problematic dtype
+    dtype=helpers.get_dtypes("numeric", full=False),
+    test_with_out=st.just(False),
+)
+def test_numpy_fromfunction(
+    shape_and_function,
+    dtype,
+    frontend,
+    test_flags,
+    fn_tree,
+    backend_fw,
+    on_device,
+):
+    shape, function = shape_and_function
+    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,
+        test_values=False,
+        function=function,
+        shape=shape,
+        dtype=dtype[0],
+    )