BruceSherwood / vpython-jupyter

This repository has been moved to https://github.com/vpython/vpython-jupyter
64 stars 33 forks source link

Fix vector operators #138

Closed bauen1 closed 5 years ago

bauen1 commented 5 years ago

The current Implementation of vectors raises an exception in __mul__ when you try to operate on a non-vector type. This can be quite annoying, because even if for example Quaternion implements __rmul__ it will never be called when the vector is on the left side of the equation and instead error out. Furthermore: https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types specifies that (some) operators should return NotImplemented.

# Does not work, should work
v = vector(0, 0, 0) * quaternion(0, 0, 0, 0)
# Does work, should work
v = quaternion(0,0,0,0) * vector(0,0,0,0)

Both the python and cython implementations have been tested (existing physics project that makes heavy use of vectors didn't break)

BruceSherwood commented 5 years ago

Many thanks for this excellent improvement. You're right; this is the way the vector class should work.