-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to safely compute the norm
- Loading branch information
1 parent
c26f27d
commit 6503c1e
Showing
7 changed files
with
41 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import jax.numpy as jnp | ||
|
||
import jaxsim.typing as jtp | ||
|
||
|
||
def safe_norm(array: jtp.ArrayLike, axis=None) -> jtp.Array: | ||
""" | ||
Provides a calculation for an array norm so that it is safe | ||
to compute the gradient and the NaNs are handled. | ||
Args: | ||
array: The array for which to compute the norm. | ||
axis: The axis for which to compute the norm. | ||
""" | ||
|
||
is_zero = jnp.allclose(array, 0.0) | ||
|
||
array = jnp.where(is_zero, jnp.ones_like(array), array) | ||
|
||
norm = jnp.linalg.norm(array, axis=axis) | ||
|
||
return jnp.where(is_zero, 0.0, norm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters