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

Matrix-Free Linear Operator #7

Merged
merged 13 commits into from
Jul 19, 2024
53 changes: 52 additions & 1 deletion psydac/linalg/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import numpy as np
from scipy.sparse import coo_matrix
from types import LambdaType

from psydac.utilities.utils import is_real

Expand All @@ -25,7 +26,8 @@
'ComposedLinearOperator',
'PowerLinearOperator',
'InverseLinearOperator',
'LinearSolver'
'LinearSolver',
'MatrixFreeLinearOperator'
)

#===============================================================================
Expand Down Expand Up @@ -1111,3 +1113,52 @@ def solve(self, rhs, out=None):
@property
def T(self):
return self.transpose()

#===============================================================================
class MatrixFreeLinearOperator(LinearOperator):
"""
General operator acting between two vector spaces V and W. It only requires a callable dot method.

campospinto marked this conversation as resolved.
Show resolved Hide resolved
"""

def __init__(self, domain, codomain, dot):

assert isinstance(domain, VectorSpace)
assert isinstance(codomain, VectorSpace)
assert isinstance(dot, LambdaType)

self._domain = domain
self._codomain = codomain
self._dot = dot

@property
def domain(self):
return self._domain

@property
def codomain(self):
return self._codomain

@property
def dtype(self):
return None

def dot(self, v, out=None):
assert isinstance(v, Vector)
assert v.space == self.domain

if out is not None:
assert isinstance(out, Vector)
assert out.space == self.codomain

return self._dot(v, out=out)

def toarray(self):
raise NotImplementedError('toarray() is not defined for MatrixFreeLinearOperator.')

def tosparse(self):
raise NotImplementedError('tosparse() is not defined for MatrixFreeLinearOperator.')

def transpose(self):
raise NotImplementedError('transpose() is not defined for MatrixFreeLinearOperator.')

Loading