For example, let's say that I mimic a bunch of particles doing something with time, and I have a multidimensional array with a name particleswith these indices:
- Coordinates x / y / z particles (length
a, which 3for three-dimensional space) - Single particle index (length
b) - Time step index (length
c)
Is it better to build an array such that particles.shape == (a, b, c)or particles.shape == (c, b, a)?
I'm more interested in the convention than efficiency: Numpy arrays can be configured either in the C-style (the last index changes most quickly) or in the Fortran style (first index), so it can effectively support any tuning. I also understand what I can use transposeto place indexes in any order I need, but I would like to minimize this.
I started researching this myself and found support for both ways:
Pro- (c, b, a):
- By default, Numpy uses C-style arrays, where the last index is the fastest.
- Most vector algebra functions (
inner, crossetc.) act on the last index. ( dotacts on the last of the one and the second to the last of the other.) matplotlib (LineCollection, PolyCollection) .
Pro- (, , ):
meshgrid mgrid , . , np.mgrid[0:5,0:5,0:5].shape == (3,5,5,5). , , .matplotlib scatter plot , , ax.plot3d(particles[0], particles[1], particles[2]) , particles[..., 0]
, (, - C Fortran), , Numpy, , .