Skip to content

Commit

Permalink
fix: sympy tests (sympy v1.13) (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp authored Jul 9, 2024
1 parent 0db3bc8 commit 2ccad06
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 43 deletions.
7 changes: 6 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ def lite(session: nox.Session) -> None:
def tests(session: nox.Session) -> None:
"""Run the unit and regular tests."""
session.install("-e", ".[awkward,numba,test,test-extras,sympy]")
session.run("pytest", "--ignore", "tests/test_notebooks.py", *session.posargs)
session.run(
"pytest",
"--ignore",
"tests/test_notebooks.py",
*session.posargs,
)


@nox.session(reuse_venv=True)
Expand Down
30 changes: 12 additions & 18 deletions tests/compute/sympy/lorentz/test_Et.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,18 @@ def test_xy_eta_tau():
sympy.sqrt(sympy.Abs(-(t**2) + x**2 + y**2 + z**2))
),
)
# TODO: why won't sympy equate the expressions without double
# simplifying?
assert (
sympy.simplify(
vec.Et.simplify()
- sympy.sqrt(
(
0.25
* (x**2 + y**2)
* (sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2))) + 1) ** 2
+ sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2)))
* sympy.Abs(-(t**2) + x**2 + y**2 + z**2)
)
* sympy.exp(-2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2)))
- 2
* sympy.sqrt(
0.25
* (x**2 + y**2)
* (sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2))) + 1) ** 2
+ sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2)))
* sympy.Abs(-(t**2) + x**2 + y**2 + z**2)
)
/ sympy.sqrt(z**2 / (x**2 + y**2) + 1)
/ (sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2))) + 1)
)
== 0
)
Expand Down Expand Up @@ -188,10 +184,8 @@ def test_rhophi_eta_tau():
sympy.sqrt(sympy.Abs(rho**2 - t**2 + z**2))
),
)
assert vec.Et.simplify() == sympy.sqrt(
0.25 * rho**2 * sympy.exp(2 * sympy.asinh(z / rho))
+ 0.5 * rho**2
+ 0.25 * rho**2 * sympy.exp(-2 * sympy.asinh(z / rho))
+ sympy.Abs(rho**2 - t**2 + z**2)
) / sympy.sqrt(1 + z**2 / rho**2)
assert vec.Et.simplify() == 2 * sympy.sqrt(
0.25 * rho**2 * (sympy.exp(2 * sympy.asinh(z / rho)) + 1) ** 2
+ sympy.exp(2 * sympy.asinh(z / rho)) * sympy.Abs(rho**2 - t**2 + z**2)
) / (sympy.exp(2 * sympy.asinh(z / rho)) + 1)
assert vec.Et.subs(values).evalf() == pytest.approx(math.sqrt(80))
4 changes: 2 additions & 2 deletions tests/compute/sympy/lorentz/test_Et2.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_xy_eta_tau():
sympy.sqrt(sympy.Abs(-(t**2) + x**2 + y**2 + z**2))
),
)
assert vec.Et2.simplify() == x**2 + y**2 + sympy.Abs(
assert vec.Et2.simplify() == 1.0 * x**2 + 1.0 * y**2 + sympy.Abs(
-(t**2) + x**2 + y**2 + z**2
) / (z**2 / (x**2 + y**2) + 1)
assert vec.Et2.subs(values).evalf() == pytest.approx(80)
Expand Down Expand Up @@ -167,7 +167,7 @@ def test_rhophi_eta_tau():
sympy.sqrt(sympy.Abs(rho**2 - t**2 + z**2))
),
)
assert vec.Et2.simplify() == rho**2 + sympy.Abs(rho**2 - t**2 + z**2) / (
assert vec.Et2.simplify() == 1.0 * rho**2 + sympy.Abs(rho**2 - t**2 + z**2) / (
1 + z**2 / rho**2
)
assert vec.Et2.subs(values).evalf() == pytest.approx(80)
30 changes: 11 additions & 19 deletions tests/compute/sympy/lorentz/test_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,18 @@ def test_xy_eta_tau():
sympy.sqrt(sympy.Abs(-(t**2) + x**2 + y**2 + z**2))
),
)
# TODO: why won't sympy equate the expressions without double
# simplifying?
assert (
sympy.simplify(
vec.beta.simplify()
- 1.0
- 0.5
* sympy.sqrt(x**2 + y**2)
* sympy.sqrt(z**2 / (x**2 + y**2) + 1)
* (sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2))) + 1)
/ sympy.sqrt(
(
0.25
* (x**2 + y**2)
* (sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2))) + 1) ** 2
+ sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2)))
* sympy.Abs(-(t**2) + x**2 + y**2 + z**2)
)
* sympy.exp(-2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2)))
0.25
* (x**2 + y**2)
* (sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2))) + 1) ** 2
+ sympy.exp(2 * sympy.asinh(z / sympy.sqrt(x**2 + y**2)))
* sympy.Abs(-(t**2) + x**2 + y**2 + z**2)
)
)
== 0
Expand Down Expand Up @@ -191,13 +186,10 @@ def test_rhophi_eta_tau():
sympy.sqrt(sympy.Abs(rho**2 - t**2 + z**2))
),
)
assert vec.beta.simplify() == 1.0 * rho * sympy.sqrt(
1 + z**2 / rho**2
assert vec.beta.simplify() == 0.5 * rho * (
sympy.exp(2 * sympy.asinh(z / rho)) + 1
) / sympy.sqrt(
(
0.25 * rho**2 * (sympy.exp(2 * sympy.asinh(z / rho)) + 1) ** 2
+ sympy.exp(2 * sympy.asinh(z / rho)) * sympy.Abs(rho**2 - t**2 + z**2)
)
* sympy.exp(-2 * sympy.asinh(z / rho))
0.25 * rho**2 * (sympy.exp(2 * sympy.asinh(z / rho)) + 1) ** 2
+ sympy.exp(2 * sympy.asinh(z / rho)) * sympy.Abs(rho**2 - t**2 + z**2)
)
assert vec.beta.subs(values).evalf() == pytest.approx(0.5590169943749475)
10 changes: 7 additions & 3 deletions tests/compute/sympy/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def test_planar_sympy():
u = t.unit()
assert type(u) is type(t)
assert type(u.azimuthal) is type(t.azimuthal)
assert u.rho == 1 if isinstance(u.rho, int) else u.rho.subs(values).evalf() == 1
assert (
u.rho == 1 if isinstance(u.rho, int) else u.rho.subs(values).evalf() == 1.0
)


def test_spatial_sympy():
Expand All @@ -49,7 +51,7 @@ def test_spatial_sympy():
assert type(u) is type(t)
assert type(u.azimuthal) is type(t.azimuthal)
assert type(u.longitudinal) is type(t.longitudinal)
assert u.mag.subs(values).evalf() == 1
assert u.mag.subs(values).evalf() == 1.0


def test_lorentz_sympy():
Expand Down Expand Up @@ -90,4 +92,6 @@ def test_lorentz_sympy():
assert type(u.azimuthal) is type(t.azimuthal)
assert type(u.longitudinal) is type(t.longitudinal)
assert type(u.temporal) is type(t.temporal)
assert u.tau == 1 if isinstance(u.tau, int) else u.tau.subs(values).evalf() == 1
assert (
u.tau == 1 if isinstance(u.tau, int) else u.tau.subs(values).evalf() == 1.0
)

0 comments on commit 2ccad06

Please sign in to comment.