1

I'm using Numpy arrays to represent different multi-dimensional physical quantities. For example, I might have a (6,) array (let's call it linear) where the first 3 elements represent 3D position of an object, and the last 3 elements represent 3D velocity of that object. I might also have defined a (7,) array (let's call it angular) where the first 4 elements represent a quaternion describing an objects rotation and the last 3 elements represent the angular velocity.

To access parts of these arrays I might do things like:

position = linear[:3]
angular_velocity = angular[-3:]

When it gets overwhelming to remember the slices (which it does when the arrays incorporate more information) I can do:

class Linear:
    p_slice = slice(0, 3)
    v_slice = slice(3, 6)

class Angular:
    q_slice = slice(0, 4)
    omega_slice = slice(4, 7)

position = linear[Linear.p_slice]
angular_velocity = angular[Angular.omega_slice]

This is a bit neater, but I still have to make sure I always know which type of variable I'm working with otherwise I might use the wrong type of slice like linear[Angular.q_slice] (whoops, I thought linear was an instance of my angular orientation/velocity arrays). So I'd rather make sure I'm disallowed from using the wrong type of slice with something like:

class Linear(np.ndarray):
    def p(self):
        return self[:3]

# make an instance of Linear

position = linear.p

I'm considering subclassing ndarray as I suggested above, but I'm wondering if there are better ways (and I'd like to avoid having to deal with the gotchas of subclassing ndarray). I definitely want to treat my objects as ndarrays though, so that means I don't want to wrap them in some other object type and have to access the ndarray as an instance property.

6
  • Does this help stackoverflow.com/questions/67509913/…? Commented Sep 6, 2022 at 17:22
  • @MichaelSzczesny it kind of helps. The first answer suggests subclassing which I'm aware of but I'd like to know if there's a better way to get the job done. The second answer is not relevant. Commented Sep 6, 2022 at 17:31
  • numpy lets you create custom dtypes for this sort of thing. Have you considered this: numpy.org/doc/stable/reference/arrays.dtypes.html Commented Sep 6, 2022 at 19:13
  • @SimonTartakovksy would that not mean I'd need to put a 3-tuple in one array position for the 3D position for instance? That would mean I'm not really able to use my array as a regular ndarray for mathematical operations like dot product or matrix multiplication. Commented Sep 7, 2022 at 12:39
  • @MichaelSzczesny Yep, would not be able to do that kind of thing. That being said what unified mathematical operation makes sense on an array containing both position and velocity? There really is not much performance benefit to merging the sets of floats into one array. Why not make two separate arrays, perhaps an object which contains them so you can define some useful operations on them? Commented Sep 7, 2022 at 22:15

0

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.