TopoToolbox / pytopotoolbox

Python interface to TopoToolbox
https://topotoolbox.github.io/pytopotoolbox/
GNU General Public License v3.0
1 stars 3 forks source link

Implementing "Magic Functions" for GridObject #17

Closed Teschl closed 4 months ago

Teschl commented 5 months ago

Since we probably will want to be able to stuff like dem3 = dem1 + dem2 we should "magic functions" to our class. For each operation, like add(self, other) in the case before, we'll need the normal version (e.g.__add__) , the reflected version dem1 = dem2 + some_np_array (__radd__) and the augmented version dem1 += dem2 (__iadd__). We'll probably also need stuff like len().

Most of these will only be wrappers of the already existing numpy logic.

For most of these, including the r and i version. Here are all the special method names, and here a more readable version.

wkearn commented 5 months ago

The MATLAB code has a bunch of similar methods defined for GRIDobj, so this makes sense to implement in the Python package. Multiplication and division of GRIDobj are defined elementwise, which I think is how NumPy arrays work by default.

NumPy also has some guidance on making array-like objects work well with the NumPy interface, which seems like it could be a good idea to figure out, so we can get access to a lot of NumPy functions for free.