-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from normal-computing/fix_solve_processed_inputs
Fix how sample function handles Processed inputs
- Loading branch information
Showing
5 changed files
with
152 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import jax | ||
from jax import numpy as jnp | ||
|
||
import thermox | ||
|
||
|
||
def test_sample_array_input(): | ||
key = jax.random.PRNGKey(0) | ||
dim = 2 | ||
dt = 0.1 | ||
ts = jnp.arange(0, 10_000, dt) | ||
|
||
A = jnp.array([[3, 2], [2, 4.0]]) | ||
b, x0 = jnp.zeros(dim), jnp.zeros(dim) | ||
D = 2 * jnp.eye(dim) | ||
|
||
samples = thermox.sample(key, ts, x0, A, b, D) | ||
|
||
samp_cov = jnp.cov(samples.T) | ||
samp_mean = jnp.mean(samples.T, axis=1) | ||
assert jnp.allclose(A @ samp_cov, jnp.eye(2), atol=1e-1) | ||
assert jnp.allclose(samp_mean, b, atol=1e-1) |
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,60 @@ | ||
from jax import numpy as jnp | ||
|
||
from thermox.utils import ( | ||
handle_matrix_inputs, | ||
ProcessedDriftMatrix, | ||
ProcessedDiffusionMatrix, | ||
preprocess, | ||
) | ||
|
||
|
||
def test_handle_matrix_inputs_arrays(): | ||
A = jnp.array([[1, 3], [1, 4]]) | ||
D = jnp.array([[9, 4], [4, 20]]) | ||
|
||
a, d = preprocess(A, D) | ||
|
||
A_star, D_star = preprocess(A, D) | ||
|
||
assert isinstance(A_star, ProcessedDriftMatrix) | ||
assert isinstance(D_star, ProcessedDiffusionMatrix) | ||
assert jnp.all(a.val == A_star.val) | ||
|
||
|
||
def test_handle_matrix_inputs_processed(): | ||
A = jnp.array([[1, 3], [1, 4]]) | ||
D = jnp.array([[9, 4], [4, 20]]) | ||
|
||
a, d = preprocess(A, D) | ||
|
||
A_star, D_star = handle_matrix_inputs(a, d) | ||
|
||
assert isinstance(A_star, ProcessedDriftMatrix) | ||
assert isinstance(D_star, ProcessedDiffusionMatrix) | ||
assert jnp.all(a.val == A_star.val) | ||
|
||
|
||
def test_handle_matrix_inputs_array_drift_processed_diffusion(): | ||
A = jnp.array([[1, 3], [1, 4]]) | ||
D = jnp.array([[9, 4], [4, 20]]) | ||
|
||
a, d = preprocess(A, D) | ||
|
||
A_star, D_star = handle_matrix_inputs(A, d) | ||
|
||
assert isinstance(A_star, ProcessedDriftMatrix) | ||
assert isinstance(D_star, ProcessedDiffusionMatrix) | ||
assert jnp.all(a.val == A_star.val) | ||
|
||
|
||
def test_handle_matrix_inputs_array_diffusion_processed_drift(): | ||
A = jnp.array([[1, 3], [1, 4]]) | ||
D = jnp.array([[9, 4], [4, 20]]) | ||
|
||
a, d = preprocess(A, D) | ||
|
||
A_star, D_star = handle_matrix_inputs(a, D) | ||
|
||
assert isinstance(A_star, ProcessedDriftMatrix) | ||
assert isinstance(D_star, ProcessedDiffusionMatrix) | ||
assert not jnp.all(a.val == A_star.val) |
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