Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

units: fixed convert_to() bug and added some tests #26612

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ Joseph Rance <56409230+Joseph-Rance@users.noreply.github.com>
Joseph Redfern <joseph@redfern.me>
Josh Burkart <jburkart@gmail.com>
José Senart <jose.senart@gmail.com>
João Bravo <joaocgbravo@tecnico.ulisboa.pt>
João Moura <operte@gmail.com>
Juan Barbosa <js.barbosa10@uniandes.edu.co>
Juan Felipe Osorio <jfosorio@gmail.com>
Expand Down
7 changes: 7 additions & 0 deletions sympy/physics/units/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def test_convert_to_quantities():


def test_convert_to_tuples_of_quantities():
from sympy.core.symbol import symbols

alpha, beta = symbols('alpha beta')

assert convert_to(speed_of_light, [meter, second]) == 299792458 * meter / second
assert convert_to(speed_of_light, (meter, second)) == 299792458 * meter / second
assert convert_to(speed_of_light, Tuple(meter, second)) == 299792458 * meter / second
Expand All @@ -98,6 +102,9 @@ def test_convert_to_tuples_of_quantities():
assert convert_to(sqrt(meter**2 + second**2.0), [meter, second]) == sqrt(meter**2 + second**2.0)
assert convert_to((meter**2 + second**2.0)**2, [meter, second]) == (meter**2 + second**2.0)**2

# similar to https://github.com/sympy/sympy/issues/21463
assert convert_to(1/(beta*meter + meter), 1/meter) == 1/(beta*meter + meter)
assert convert_to(1/(beta*meter + alpha*meter), 1/kilometer) == (1/(kilometer*beta/1000 + alpha*kilometer/1000))

def test_eval_simplify():
from sympy.physics.units import cm, mm, km, m, K, kilo
Expand Down
4 changes: 4 additions & 0 deletions sympy/physics/units/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sympy.core.power import Pow
from sympy.core.sorting import ordered
from sympy.core.sympify import sympify
from sympy.core.function import Function
from sympy.matrices.exceptions import NonInvertibleMatrixError
from sympy.physics.units.dimensions import Dimension, DimensionSystem
from sympy.physics.units.prefixes import Prefix
Expand Down Expand Up @@ -110,6 +111,9 @@ def handle_Adds(expr):
expr = sympify(expr)
target_units = sympify(target_units)

if isinstance(expr, Function):
expr = expr.together()

if not isinstance(expr, Quantity) and expr.has(Quantity):
expr = expr.replace(lambda x: isinstance(x, Quantity),
lambda x: x.convert_to(target_units, unit_system))
Expand Down
Loading