[Python] DimensionalityError when calculating an exponential value "exp(x)" with pint units

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to calculate an exponential value exp with a python pint variable that includes a unit.

    import pint
    var = 0.5 * unit_reg.minute
    math.exp(-var)


    with this I get the error: DimensionalityError: Cannot convert from 'minute' to 'dimensionless'

    This also seems the case when I calculate it directly

    import pint
    var = 0.5 * unit_reg.minute
    math.e ** -var


    as well as use numpy

    import pint
    import numpy as np
    var = 0.5 * unit_reg.minute
    np.exp(-var)


    Maybe I am missing something here. But is there another way of doing the calculations rather than extracting the magnitude and units from the variable and set things together again?

    With this way there is no DimensionalityError

    import pint
    var = 0.5 * unit_reg.minute
    math.exp(-var.magnitude) * var.units


    I am guessing, that math.exp is expecting a dimensionless variable as specified in example 11 on the pint numpy description

    Continue reading...

Compartilhe esta Página