Define a function with Cython:
%cython import numpy def cython_collisions(x, y, z, radius): num_particles = len(x) collisions = numpy.zeros(num_particles, dtype=numpy.bool) r_min = numpy.float64(4*radius**2) for i in range(num_particles): if i>0: d2 = numpy.power((x[i]-x[0:i]),2) \ + numpy.power((y[i]-y[0:i]),2) \ + numpy.power((z[i] - z[0:i]),2) if d2.min() < r_min: collisions[i] = True if i+1 < num_particles: d2 = numpy.power((x[i]-x[i+1:]),2) \ + numpy.power((y[i]-y[i+1:]),2) \ + numpy.power((z[i]-z[i+1:]),2) if d2.min() < r_min: collisions[i] = True return collisions
When you run this cell, it may take a minute or two to compile for the first time. The results will look similar to this:
In another cell, enter...
Sage Beginner's Guide
By :
Sage Beginner's Guide
By:
Overview of this book
Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
What Can You Do with Sage?
Installing Sage
Getting Started with Sage
Introducing Python and Sage
Vectors, Matrices, and Linear Algebra
Plotting with Sage
Making Symbolic Mathematics Easy
Solving Problems Numerically
Learning Advanced Python Programming
Where to go from here
Index
Customer Reviews