-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add numerically safe implementation of
unit_vector
Fixes #4.
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from absl.testing import absltest | ||
import jax | ||
import numpy as np | ||
|
||
from hj_reachability import sets | ||
|
||
|
||
class SetsTest(absltest.TestCase): | ||
|
||
def setUp(self): | ||
np.random.seed(0) | ||
|
||
def test_box(self): | ||
box = sets.Box(np.ones(3), 2 * np.ones(3)) | ||
np.testing.assert_allclose(box.extreme_point(np.array([1, -1, 1])), np.array([2, 1, 2])) | ||
self.assertTrue(np.all(np.isfinite(box.extreme_point(np.zeros(3))))) | ||
self.assertEqual(box.bounding_box, box) | ||
np.testing.assert_allclose(box.max_magnitudes, 2 * np.ones(3)) | ||
self.assertEqual(box.ndim, 3) | ||
|
||
def test_ball(self): | ||
ball = sets.Ball(np.ones(3), np.sqrt(3)) | ||
np.testing.assert_allclose(ball.extreme_point(np.array([1, -1, 1])), np.array([2, 0, 2]), atol=1e-6) | ||
self.assertTrue(np.all(np.isfinite(ball.extreme_point(np.zeros(3))))) | ||
jax.tree_map(np.testing.assert_allclose, ball.bounding_box, | ||
sets.Box((1 - np.sqrt(3)) * np.ones(3), (1 + np.sqrt(3)) * np.ones(3))) | ||
np.testing.assert_allclose(ball.max_magnitudes, (1 + np.sqrt(3)) * np.ones(3)) | ||
self.assertEqual(ball.ndim, 3) | ||
|
||
|
||
if __name__ == "__main__": | ||
absltest.main() |
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,40 @@ | ||
from absl.testing import absltest | ||
import jax | ||
import jax.numpy as jnp | ||
import numpy as np | ||
|
||
from hj_reachability import utils | ||
|
||
|
||
class UtilsTest(absltest.TestCase): | ||
|
||
def setUp(self): | ||
np.random.seed(0) | ||
|
||
def test_multivmap(self): | ||
a = np.random.random((3, 4, 5, 6)) | ||
np.testing.assert_allclose(utils.multivmap(jnp.max, np.array([0, 1]))(a), np.max(a, (2, 3))) | ||
np.testing.assert_allclose(utils.multivmap(jnp.max, np.array([0, 1, 2]))(a), np.max(a, -1)) | ||
np.testing.assert_allclose(utils.multivmap(jnp.max, np.array([0, 1, 3]), np.array([0, 1, 2]))(a), np.max(a, 2)) | ||
np.testing.assert_allclose( | ||
utils.multivmap(jnp.max, np.array([1, 0, 2]), np.array([0, 1, 2]))(a), | ||
np.max(a, 3).swapaxes(0, 1)) | ||
np.testing.assert_allclose( | ||
utils.multivmap(jnp.max, np.array([3, 2]), np.array([0, 1]))(a), | ||
np.max(a, (0, 1)).swapaxes(0, 1)) | ||
|
||
def test_unit_vector(self): | ||
unsafe_unit_vector = lambda x: x / jnp.linalg.norm(x, axis=-1, keepdims=True) | ||
for d in range(1, 4): | ||
np.testing.assert_array_equal(utils.unit_vector(np.zeros(d)), np.zeros(d)) | ||
self.assertTrue(np.all(np.isfinite(jax.jacobian(utils.unit_vector)(np.zeros(d))))) | ||
self.assertTrue(np.all(np.isnan(jax.jacobian(unsafe_unit_vector)(np.zeros(d))))) | ||
a = np.random.random((100, d)) | ||
np.testing.assert_allclose(jax.vmap(utils.unit_vector)(a), unsafe_unit_vector(a), atol=1e-6) | ||
np.testing.assert_allclose(jax.vmap(jax.jacobian(utils.unit_vector))(a), | ||
jax.vmap(jax.jacobian(unsafe_unit_vector))(a), | ||
atol=1e-6) | ||
|
||
|
||
if __name__ == "__main__": | ||
absltest.main() |