Using a particle simulator in Cython
Now that we have a basic understanding of how Cython works, we can rewrite the ParticleSimulator.evolve
method. Thanks to Cython, we can convert our loops in C, thus removing the overhead introduced by the Python interpreter.
In Chapter 3, Fast Array Operations with NumPy, Pandas, and Xarray, we wrote a fairly efficient version of the evolve
method using NumPy. We can rename the old version evolve_numpy
to differentiate it from the new version. The code is illustrated in the following snippet:
def evolve_numpy(self, dt): timestep = 0.00001 nsteps = int(dt/timestep) r_i = np.array([[p.x, p.y] for p in \ self.particles]) ang_speed_i...