pythonarraysif-statementcoefficientsenergy

Is there a way to create an array which values are conditioned by the values of another array?


I have an array of values called E representing Energy values

E = np.arange(0.1, 101, 0.1)

And I want to create a group of arrays called a0, a1, a2, a3, being those coefficients that vary depending on the value of the energy, so I want to do something similar to this:

for item in E:
  if item <= 1.28:
      a3, a2, a1, a0 = 0, -8.6616, 13.879, -12.104 
  elif 1.28<item<10:
      a3, a2, a1, a0 = -0.186, 0.428, 2.831, -8.76
  elif item >=10:
      a3, a2, a1, a0 = 0, -0.0365, 1.206, -4.76

This code does not return any mistake, but I do not know how to create lists or arrays with the same length as the E (energy array), with every array containing the values of the coefficients fo specific energy values, so I would really appreciate your help!

Best regards!


Solution

  • import numpy as np
    
    constants = [[ 0, -8.6616, 13.879, -12.104 ],
                 [ 0.186, 0.428, 2.831, -8.76 ],
                 [ 0, -0.0365, 1.206, -4.76 ]]
    
    constants = np.array(constants)
    
    E = np.arange(0.1, 101, 0.1)
    
    bins = np.digitize(E, [1.28, 10])
    
    a0 = np.choose(bins, constants[:, 3])
    a1 = np.choose(bins, constants[:, 2])
    a2 = np.choose(bins, constants[:, 1])
    a3 = np.choose(bins, constants[:, 0])