Skip to content

Commit

Permalink
Raise ValueError when axis1==axis2 for jnp.trace
Browse files Browse the repository at this point in the history
  • Loading branch information
rajasekharporeddy committed Sep 26, 2024
1 parent f6fdfb4 commit 6072f97
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jax/_src/numpy/lax_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6702,6 +6702,10 @@ def trace(a: ArrayLike, offset: int | ArrayLike = 0, axis1: int = 0, axis2: int
util.check_arraylike("trace", a)
if out is not None:
raise NotImplementedError("The 'out' argument to jnp.trace is not supported.")

if _canonicalize_axis(axis1, ndim(a)) == _canonicalize_axis(axis2, ndim(a)):
raise ValueError(f"axis1 and axis2 can not be same. axis1={axis1} and axis2={axis2}")

dtypes.check_user_dtype_supported(dtype, "trace")

a_shape = shape(a)
Expand Down
5 changes: 5 additions & 0 deletions tests/lax_numpy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,11 @@ def np_fun(arg):
self._CheckAgainstNumpy(np_fun, jnp_fun, args_maker)
self._CompileAndCheck(jnp_fun, args_maker)

def testTraceSameAxesError(self):
a = jnp.arange(1, 13).reshape(2, 3, 2)
with self.assertRaisesRegex(ValueError, r"axis1 and axis2 can not be same"):
jnp.trace(a, axis1=1, axis2=-2)

@jtu.sample_product(
ashape=[(15,), (16,), (17,)],
vshape=[(), (5,), (5, 5)],
Expand Down

0 comments on commit 6072f97

Please sign in to comment.