Chapter 8: Problem 35
Muke u cluss fur \(2 D\) nutuluwr wulk. The purpose of this exercise is to reimplement the ualk2D. py program from Chapter \(8.7 .1\) with the aid of classes. Make a class Particle with the coordinates \((x, y)\) and the time step number of a particle as attributes. A method move moves the particle in one of the four directions and updates the \((x, y)\) coordinates. Another class, Particles, holds a list of Particle objects and a plotstep parameter (as in walk2D.py). A method move moves all the particles one step, a method plot can make a plot of all particles, while a method moves performes a loop over time steps and calls move and plot in each step. Equip the Particle and Particles classes with print functionality such that one can print out all particles in a nice way by saying print \(\mathrm{P}\) (for a Particles instance \(\mathrm{p}\) ) or print self (inside a method). Hint: In _str_-, apply the pformat function from the pprint module to the list of particles, and make sure that__repr__ just reuse __str__ in both classes. To verify the implementation, print the first three positions of four particles in the walk2D. py program and compare with the corresponding results produced by the class-based implementation (the seed of the random number generator must of course be fixed identically in the two programs). You can just perform p.move() and print p three times in a verify function to do this verification task. Organize the complete code as a module such that the classes Particle and Particles can be reused in other programs. The test block should call a run(N) method to run the walk for \(N\) steps, where \(N\) is given on the command line. Compare the efficiency of the class version against the vectorized version in ualk2Dv.py, using the techniques of Appendix G.6.1. Name of program file: walk2Dc.py.
Short Answer
Step by step solution
Key Concepts
These are the key concepts you need to understand to accurately answer the question.