Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add matrix multiplier methods #122

Merged
merged 1 commit into from
Jan 27, 2025
Merged

Add matrix multiplier methods #122

merged 1 commit into from
Jan 27, 2025

Conversation

mwtoews
Copy link
Contributor

@mwtoews mwtoews commented Jan 26, 2025

Add support for @ matrix multiplier methods. The Affine @ Affine operation (matrix-matrix) is the same as with *. Only the Affine @ tuple (matrix-vector) combination is slightly different, as the operator accepts an iterable of 2 or 3 values:

>>> from affine import Affine
>>> A = Affine(2, 0, 3, 0, 3, 2)
>>> print(A)
| 2.00, 0.00, 3.00|
| 0.00, 3.00, 2.00|
| 0.00, 0.00, 1.00|
>>> A @ (2, 3)
(7.0, 11.0)
>>> A @ (2, 3, 1)
(7.0, 11.0, 1.0)

and when it's 3 values, the third must value must be 1.0, because this is a 2D affine transformation module. Allowing 2 values enables a bit of backwards compatibility with the * operator, which only accepts 2 values.

I've added a note to enable PendingDeprecationWarning for a future release. I've held back in refactoring the examples in README.rst as often this is a user's reference for older releases.

Closes #107

@mwtoews mwtoews force-pushed the matmul branch 2 times, most recently from 75f07b1 to 9df1193 Compare January 26, 2025 09:28
@mwtoews
Copy link
Contributor Author

mwtoews commented Jan 26, 2025

I've just noticed (and corrected) this inconstancy with imul:

>>> from affine import Affine, identity
>>> t = Affine.translation(3, 5)
>>> t *= identity
>>> t
Affine(1.0, 0.0, 3.0,
       0.0, 1.0, 5.0)
>>> t *= (1, 2)
<stdin>:1: DeprecationWarning: in-place multiplication with tuple is deprecated
>>> t
(4.0, 7.0)

it's unexpected that an in-place operation would change the type to tuple, so I'm adding a warning.

The implementation of imatmul will allow another Affine, but raise TypeError with a tuple.

>>> t = Affine.translation(3, 5)
>>> t @= Affine.translation(2, 4)
>>> t
Affine(1.0, 0.0, 5.0,
       0.0, 1.0, 9.0)
>>> t @= (1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mtoews/src/affine/src/affine.py", line 604, in __imatmul__
    raise TypeError("Operation not supported")
TypeError: Operation not supported

Copy link
Member

@sgillies sgillies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great @mwtoews !

@sgillies sgillies merged commit c6eed35 into rasterio:main Jan 27, 2025
7 checks passed
@mwtoews mwtoews deleted the matmul branch January 27, 2025 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose __matmul__ for matrix multiplication
2 participants