pyxem / orix

Analysing crystal orientations and symmetry in Python
https://orix.readthedocs.io
GNU General Public License v3.0
79 stars 46 forks source link

Dealing with division and element-wise operations on Vector3d #140

Open din14970 opened 3 years ago

din14970 commented 3 years ago

After the discussion in PR #138, it seems a more detailed discussion on how to deal with element-wise operations on data structures is handled. The point of contention was:

>>> 1/vector

If vector, a Vector3d object, were to behave like the array data it contains, then this would imply an element-wise operation, i.e. taking the inverse of each element. However, mathematically, division by a vector is undefined. This leads to the dilemma: should the behavior be more maths-like or more array like? The maths definition would better reflect "real life", but a user may expect array-like behavior, and be put off by doing element wise division like:

>>> vector = Vector3d(1/vector.data)

The issue could be avoided with more operators like the ./ operator in Matlab. However, this operator does not exist in Python and it is not possible to add custom operators - they are baked into the language.

Alternatively, one could overload operators which are unused. One could perhaps make use of **, // and ^, which are all valid operators. // could be used to represent Matlab's ./, but it might confuse users who expect // to mean floor division. In the same vein, ** could be used for element-wise multiplication, but again it might confuse users who expect it to mean power.

pc494 commented 3 years ago

https://numpy.org/doc/stable/user/basics.broadcasting.html might be relevant here, but I think in the short term this development will be postponed until after the main pyxem components have been worked through.