Tutorials‎ > ‎

Arrays

pDynamo makes extensive use of various types of numerical array and has its own C library that implements them. It does not employ the Python numpy library, principally because the latter does not provide a clear separation between Python and C code. However, there is no reason why users should not use numpy in their pDynamo scripts and, indeed, it is probably advantageous to do so if complicated array operations are to be performed.

Nevertheless, for many purposes, pDynamo's inbuilt arrays should be sufficient. Currently these consist of:
  • One, two and N-dimensional arrays of various types. As examples, the arrays of pDynamo's Real type are named Real1DArray, Real2DArray and RealNDArray, respectively.
  • Some specialized forms of Real matrix, including AntisymmetricMatrix and SymmetricMatrix.
  • Real arrays devoted to coordinate manipulation, notably Coordinates3, Matrix33 and Vector3.
Array types conform to the following notation:
  • extent is the number of items along a particular dimension of the array.
  • item is an element in the array.
  • rank is the number of dimensions in the array.
  • shape is the ensemble of extents for all the array's dimensions.
  • size is the total number of items in the array.
Individual items in an array can be accessed in standard Python fashion using square brackets. Indexing starts at 0, with negative indices denoting indexing from the end of an array dimension. An integer is required for each dimension in the array, otherwise an error is raised.

In addition, the 1DArray, 2DArray and NDArray objects permit slicing, again using standard Python terminology, with an integer or a slice specified for each array dimension. It is important to note that slicing does not generate a new array, but instead returns a minimal-rank view of the underlying parent object.

A Coordinates3 object is composite, behaving in some respects like a Real2DArray object of shape [n,3], and in others like a one-dimensional array of Vector3 objects. To reflect this dual behavior, Coordinates3 objects may be sliced like Real2DArray objects, but they may also be indexed with only a single integer or slice. If an integer is used a Vector3 view of the appropriate row in the Coordinates3 matrix will be returned, whereas a slice gives a Coordinates3 view of the underlying data.

Array views behave identically in most ways to arrays of the corresponding type, although there are some subtleties to consider when cloning and pickling. Thus, shallow cloning and pickling preserves the view, in the sense that the relation between the parent and view is maintained, whereas deep cloning generates a non-view array clone of the viewed data.