Skip to content

Commit

Permalink
feat(Numpy-frontend): Added eigvals function to numpy frontend (ivy-l…
Browse files Browse the repository at this point in the history
…lc#22920)

Co-authored-by: hirwa-nshuti <[email protected]>
  • Loading branch information
2 people authored and druvdub committed Oct 14, 2023
1 parent 32c638d commit 262eaa5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ivy/functional/frontends/numpy/linalg/matrix_eigenvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def eigh(a, /, UPLO="L"):
return ivy.eigh(a, UPLO=UPLO)


@to_ivy_arrays_and_back
def eigvals(a):
return ivy.eig(a)[0]


@to_ivy_arrays_and_back
@from_zero_dim_arrays_to_scalar
def eigvalsh(a, /, UPLO="L"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,55 @@ def test_numpy_eigh(
)


@handle_frontend_test(
fn_tree="numpy.linalg.eigvals",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
min_value=0,
max_value=10,
shape=helpers.ints(min_value=2, max_value=4).map(lambda x: tuple([x, x])),
).filter(
lambda x: "float16" not in x[0]
and "bfloat16" not in x[0]
and np.linalg.cond(x[1][0]) < 1 / sys.float_info.epsilon
and np.linalg.det(np.asarray(x[1][0])) != 0
),
test_with_out=st.just(False),
)
def test_numpy_eigvals(
dtype_and_x,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
):
dtype, x = dtype_and_x
ret, frontend_ret = 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,
a=x,
)
with BackendHandler.update_backend(backend_fw) as ivy_backend:
ret = np.sort(
np.array([ivy_backend.to_numpy(x).astype(np.float128) for x in ret])
)
frontend_ret = np.sort(np.array([x.astype(np.float128) for x in frontend_ret]))
assert_all_close(
ret_np=ret,
ret_from_gt_np=frontend_ret,
backend=backend_fw,
ground_truth_backend=frontend,
atol=1e-2,
rtol=1e-2,
)


# eigvalsh
@handle_frontend_test(
fn_tree="numpy.linalg.eigvalsh",
Expand Down

0 comments on commit 262eaa5

Please sign in to comment.