samedi 27 juin 2020

How exactly does the .any() Python method work?

I'm trying to write a script that simulates a system of chemical reactions over time. One of the inputs to the function is the following array:

popul_num = np.array([200, 100, 0, 0])

Which contains the number of discrete molecules of each species in the system. Part of the main function has an if statement that's meant to check that number of molecules is positive. if it is processed to the next iteration, else break out of the whole simulation

if popul_num.any() < 0: # Any method isn't working! --> Does .any() work on arrays or just lists? 
        print("Break out of loop negative molecule numbers")
        tao_all = tao_all[0:-1]
        popul_num_all = popul_num_all[0:-1]       
    else:
        break

I've used the .any() to try find if any element of the popul_num array is negative. But it doesn't work, it doesn't throw an error, the system just never enters the if statement and I can't figure out why?

I've just ran the program and the final number of molecules the system returned was: [135 -19 65 54] the program should have broken out before the second element got to -19.

Any suggestions?

Cheers

Aucun commentaire:

Enregistrer un commentaire