-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lambdify ComplexSqrt to TensorFlow (#365)
* chore: define custom _TensorflowPrinter * chore: extract _replace_module function * chore: forward JAX ComplexSqrt printer to numpycode * chore: move _backend to function sub-module * feat: expand available functions in TF printer * fix: improve find_function exception message * fix: lambdify ComplexSqrt to TensorFlow * test: lambdify ComplexSqrt with different backends * test: try more functions in test_find_function
- Loading branch information
Showing
8 changed files
with
106 additions
and
44 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
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,30 @@ | ||
# pylint: disable=import-outside-toplevel | ||
import numpy as np | ||
import pytest | ||
|
||
from tensorwaves.function._backend import find_function | ||
from tensorwaves.function.sympy import create_parametrized_function | ||
|
||
|
||
@pytest.mark.parametrize("backend", ["jax", "math", "numba", "numpy", "tf"]) | ||
def test_complex_sqrt(backend: str): | ||
import sympy as sp | ||
from ampform.dynamics.math import ComplexSqrt | ||
from numpy.lib.scimath import sqrt as complex_sqrt | ||
|
||
x = sp.Symbol("x") | ||
expr = ComplexSqrt(x) | ||
function = create_parametrized_function( | ||
expr.doit(), parameters={}, backend=backend | ||
) | ||
if backend == "math": | ||
values = -4 | ||
else: | ||
linspace = find_function("linspace", backend) | ||
kwargs = {} | ||
if backend == "tf": | ||
kwargs["dtype"] = find_function("complex64", backend) | ||
values = linspace(-4, +4, 9, **kwargs) | ||
data = {"x": values} | ||
output_array = function(data) # type: ignore[arg-type] | ||
np.testing.assert_almost_equal(output_array, complex_sqrt(data["x"])) |
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,25 @@ | ||
from tensorwaves.function._backend import find_function | ||
|
||
|
||
def test_find_function(): | ||
# pylint: disable=import-error, import-outside-toplevel | ||
# pyright: reportMissingImports=false | ||
import jax.numpy as jnp | ||
import numpy as np | ||
import tensorflow as tf | ||
import tensorflow.experimental.numpy as tnp | ||
|
||
assert find_function("array", backend="numpy") is np.array | ||
assert find_function("linspace", backend="numpy") is np.linspace | ||
assert find_function("log", backend="numpy") is np.log | ||
assert find_function("mean", backend="numpy") is np.mean | ||
assert find_function("mean", backend="numba") is np.mean | ||
|
||
assert find_function("array", backend="jax") is jnp.array | ||
assert find_function("linspace", backend="jax") is jnp.linspace | ||
assert find_function("mean", backend="jax") is jnp.mean | ||
|
||
assert find_function("array", backend="tf") is tnp.array | ||
assert find_function("linspace", backend="tf") is tnp.linspace | ||
assert find_function("mean", backend="tf") is tnp.mean | ||
assert find_function("Tensor", backend="tf") is tf.Tensor |
This file was deleted.
Oops, something went wrong.