user1767754
user1767754

Reputation: 25094

Representing particle motion in numpy

I've never used numpy to represent more than images or volumetric data I would run operations on them. Anyhow is numpy good for storing a table or is this more suited to accomplish on pandas?

I basically need to store particles to represent particle motion and I'm not sure how I would define a shape so i can run for example linalg.svd on the Matrix and update the last field Determinant

Position (Vector3 or 3 floats)
Velocity (Vector3 or 3 floats)
Matrix (Matrix or 9 floats)
Determinant (1 float)

I imagined that I would create a shape (16, 1000) to hold 1000 particles but how would I reshape the matrix of 9 floats so I can pass it to linalg.svd

Any advice appreciated.

Upvotes: 0

Views: 458

Answers (1)

user2699
user2699

Reputation: 3147

Numpy handles this case quite well, using structured arrays. For your particular case, you can create an array as follows (Assuming you want Matrix to be 3x3 for use with the SVD). edit: It should be np.zeros to create the array, then you can copy data over row by row or field by field.

d = dtype([('pos', float, 3), ('v', float, 3), ('Matrix', float, (3, 3)), ('det', float)])
arr = np.zeros(1000, dtype=d).view(np.recarray)

You don't have to take a view as a recarray, but it allows acessing the structured array fields as attributes (i.e., arr.Matrix will return a 1000x3x3 array). It's handy so you can do things like

arr.det = [det(a) for a in arr.Matrix] 

You can also access fields using bracket notation and the name of the field, which works with recarrays or structured arrays.

arr['det'] = [det(a) for a in arr['Matrix']] 

Upvotes: 1

Related Questions