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

Compatibility with numpy arrays and other matrices #109

Closed
RendersJens opened this issue Oct 11, 2019 · 5 comments
Closed

Compatibility with numpy arrays and other matrices #109

RendersJens opened this issue Oct 11, 2019 · 5 comments

Comments

@RendersJens
Copy link

RendersJens commented Oct 11, 2019

Add the ability to combine linear operators with a numpy 2D arrays (or other matrices like the ones from scipy.sparse) using VStack, HStack, Block, etc. like this:

import pylops
import numpy as np

# 2x2 identity operator
A = pylops.Identity(2)

# 2x2 identity matrix
B = np.eye(2)

C = pylops.VStack([A, B])
print(C @ np.array([1, 2]))

Currently this results in
AttributeError: 'numpy.ndarray' object has no attribute 'matvec'
while a numpy 2D array does support the @ operator (as substitute for .matvec()). The same holds for other types of matrices. The implementation of VStack, HStack, Block, etc. should use the @ operator if .matvec is not implemented.

@mrava87
Copy link
Collaborator

mrava87 commented Oct 11, 2019

Hi,
You can easily do that by wrapping your numpy/scipy array into Pylops.MatrixMult. In your case you would just new to add another line, B=Pylops.MatrixMult(B).

Let me know if that works :)

@RendersJens
Copy link
Author

Thank you for the swift reply. That certainly works, and this is how I use it for now, but I opened this issue as I feel like this line should be avoided, as the matrix functionality (like indexing) is then lost.

It seems like it is perfectly doable to have numpy or sparse matrices interact with linear operators, by simply using their @ operator instead of .matvec. Is it a design choice to not support this? (In that case I will probably patch it for myself)

@mrava87
Copy link
Collaborator

mrava87 commented Oct 11, 2019

Ok, I see, that makes sense. Let me take a look where the error actually arises as it may be easy to replace matvec with @ but not so easy to replace rmatvec. Basically this would require checking if the operator is really an Pylops operator or a numpy array and behave differently.

Alternatively we could do the check once at initialization and recast numpy/scipy arrays to MatrixMult so the forward and adjoins of stacking operators would remain efficient with no further checks. This would be my preferred solution, would that make sense to use that we still use MatrixMult but we avoid the user having to do it themselves?

@RendersJens
Copy link
Author

Yes, that makes sense. Letting the functions VStack, HStack, Block, etc. do the conversions internally is a good solution.

@mrava87
Copy link
Collaborator

mrava87 commented Oct 20, 2019

Solved here ed193ea

@mrava87 mrava87 closed this as completed Oct 20, 2019
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

No branches or pull requests

2 participants