0

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.

1 Answer 1

1

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']] 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for the example, I'll give it a try
If i do arr = np.rec.array(np.ones(1000, dtype=d)) that works, and i can access arr.det do I need the view at all?
@user1767754, that's a new way to me, but it works. I've updated the answer to use np.zeros, using array there was a typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.