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

Unrelated problem in physics.units #24407

Open
1e9abhi1e10 opened this issue Dec 20, 2022 · 1 comment
Open

Unrelated problem in physics.units #24407

1e9abhi1e10 opened this issue Dec 20, 2022 · 1 comment
Labels
physics.units Units and dimensions

Comments

@1e9abhi1e10
Copy link
Contributor

1e9abhi1e10 commented Dec 20, 2022

from sympy.physics.units import convert_to
from sympy.physics.units.systems.cgs import cgs_gauss
from sympy.physics.units.definitions.unit_definitions import  coulomb,  erg, volt, joule, meter, newton, statvolt
from sympy.physics.units.definitions.unit_definitions import statcoulomb as esu

print(convert_to(volt, joule/coulomb)) 
# it gives joule/coulomb

print(convert_to(volt/meter, newton/coulomb)) 
# it gives newton/coulomb

print(convert_to(statvolt, erg/esu)) 
# but it gives statvolt
@tjstienstra tjstienstra added the physics.units Units and dimensions label Jan 9, 2023
@shishir-11
Copy link
Contributor

shishir-11 commented Feb 8, 2024

def _get_conversion_matrix_for_expr(expr, target_units, unit_system):
    from sympy.matrices.dense import Matrix

    dimension_system = unit_system.get_dimension_system()

    expr_dim = Dimension(unit_system.get_dimensional_expr(expr))
    dim_dependencies = dimension_system.get_dimensional_dependencies(expr_dim, mark_dimensionless=True)
    target_dims = [Dimension(unit_system.get_dimensional_expr(x)) for x in target_units]
    canon_dim_units = [i for x in target_dims for i in dimension_system.get_dimensional_dependencies(x, mark_dimensionless=True)]
    canon_expr_units = set(dim_dependencies)

    if not canon_expr_units.issubset(set(canon_dim_units)):
        return None

    seen = set()
    canon_dim_units = [i for i in canon_dim_units if not (i in seen or seen.add(i))]

    camat = Matrix([[dimension_system.get_dimensional_dependencies(i, mark_dimensionless=True).get(j, 0) for i in target_dims] for j in canon_dim_units])
    exprmat = Matrix([dim_dependencies.get(k, 0) for k in canon_dim_units])

    try:
        res_exponents = camat.solve(exprmat)
    except NonInvertibleMatrixError:
        return None

    return res_exponents

if not canon_expr_units.issubset(set(canon_dim_units)):
return None

The problem occurs here . Why are we doing this ?
Removing this keeps rest of the results same but gives

>>> print(convert_to(statvolt, erg/esu))
1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
physics.units Units and dimensions
Projects
None yet
Development

No branches or pull requests

3 participants