pythonnumpyastropyexp

Getting error "Can only apply 'exp' function to dimensionless quantities", Not sure how to fix this


I have been working on converting some MatLab code over to python for one of my professors (not an assignment just working on putting together some stuff) and I am stuck on this one part.

When I run the code I am getting UnitTypeError: "Can only apply 'exp' function to dimensionless quantities", all of the methods I have tried to fix this won't work. I imagine the error is caused by the linspace command but am not sure. Any help with this would be great.

here is the line

IM0 = ((2*h*c**2)/(l**5))/(np.exp(h*c/( h*c/(k*T1*l)))-1)

with the constants being from astropy

h = const.h;
c = const.c;
k = const.k_B;
l = np.linspace(0, 1.5e-6, 1500);
T1 = 3750

Solution

  • The astropy constants are instances of classes. Try extracting the "value" for each before using them as an argument to np.exp():

    import astropy.constants as const
    import numpy as np
    
    h = const.h.value
    c = const.c.value
    k = const.k_B.value
    l = np.linspace(0, 1.5e-6, 1500);
    T1 = 3750
    IM0 = ((2*h*c**2)/(l**5))/(np.exp(h*c/( h*c/(k*T1*l)))-1)
    

    However please note there are numerical problems with IM0. The denominator is zero over all l.