Unverified Commit 64bb8c5c authored by Stephan Rave's avatar Stephan Rave Committed by GitHub
Browse files

Merge pull request #1408 from pymor/fix_1386_2

Introduce ListVectorSpace.vector_type and make make_array smarter
parents 98b56543 206cda03
Pipeline #102067 passed with stages
in 52 minutes and 26 seconds
......@@ -126,7 +126,8 @@ if config.HAVE_FENICS:
class FenicsVectorSpace(ComplexifiedListVectorSpace):
complexified_vector_type = ComplexifiedFenicsVector
real_vector_type = FenicsVector
vector_type = ComplexifiedFenicsVector
def __init__(self, V, id='STATE'):
self.__auto_init(locals())
......
......@@ -68,7 +68,8 @@ if config.HAVE_NGSOLVE:
class NGSolveVectorSpace(ComplexifiedListVectorSpace):
complexified_vector_type = ComplexifiedNGSolveVector
real_vector_type = NGSolveVector
vector_type = ComplexifiedNGSolveVector
def __init__(self, V, id='STATE'):
self.__auto_init(locals())
......
......@@ -93,7 +93,7 @@ class LinearComplexifiedListVectorArrayOperatorBase(ListVectorArrayOperatorBase)
imag_part = self._real_apply_one_vector(u.imag_part, mu=mu, prepare_data=prepare_data)
else:
imag_part = None
return self.range.complexified_vector_type(real_part, imag_part)
return self.range.vector_type(real_part, imag_part)
def _apply_inverse_one_vector(self, v, mu=None, initial_guess=None, least_squares=False, prepare_data=None):
real_part = self._real_apply_inverse_one_vector(v.real_part, mu=mu,
......@@ -109,7 +109,7 @@ class LinearComplexifiedListVectorArrayOperatorBase(ListVectorArrayOperatorBase)
prepare_data=prepare_data)
else:
imag_part = None
return self.source.complexified_vector_type(real_part, imag_part)
return self.source.vector_type(real_part, imag_part)
def _apply_adjoint_one_vector(self, v, mu=None, prepare_data=None):
real_part = self._real_apply_adjoint_one_vector(v.real_part, mu=mu, prepare_data=prepare_data)
......@@ -117,7 +117,7 @@ class LinearComplexifiedListVectorArrayOperatorBase(ListVectorArrayOperatorBase)
imag_part = self._real_apply_adjoint_one_vector(v.imag_part, mu=mu, prepare_data=prepare_data)
else:
imag_part = None
return self.source.complexified_vector_type(real_part, imag_part)
return self.source.vector_type(real_part, imag_part)
def _apply_inverse_adjoint_one_vector(self, u, mu=None, initial_guess=None, least_squares=False, prepare_data=None):
real_part = self._real_apply_inverse_adjoint_one_vector(u.real_part, mu=mu,
......@@ -134,7 +134,7 @@ class LinearComplexifiedListVectorArrayOperatorBase(ListVectorArrayOperatorBase)
prepare_data=prepare_data)
else:
imag_part = None
return self.range.complexified_vector_type(real_part, imag_part)
return self.range.vector_type(real_part, imag_part)
class NumpyListVectorArrayMatrixOperator(ListVectorArrayOperatorBase, NumpyMatrixOperator):
......
......@@ -548,6 +548,7 @@ class ListVectorSpace(VectorSpace):
"""|VectorSpace| of |ListVectorArrays|."""
dim = None
vector_type = Vector
@abstractmethod
def zero_vector(self):
......@@ -606,7 +607,7 @@ class ListVectorSpace(VectorSpace):
@make_array.instancemethod
def make_array(self, obj):
""":noindex:"""
return ListVectorArray([v if isinstance(v, Vector) else self.make_vector(v) for v in obj], self)
return ListVectorArray([v if isinstance(v, self.vector_type) else self.make_vector(v) for v in obj], self)
@classinstancemethod
def from_numpy(cls, data, id=None, ensure_copy=False):
......@@ -620,34 +621,38 @@ class ListVectorSpace(VectorSpace):
class ComplexifiedListVectorSpace(ListVectorSpace):
complexified_vector_type = ComplexifiedVector
real_vector_type = Vector
vector_type = ComplexifiedVector
@abstractmethod
def real_zero_vector(self):
pass
def zero_vector(self):
return self.complexified_vector_type(self.real_zero_vector(), None)
return self.vector_type(self.real_zero_vector(), None)
def real_full_vector(self, value):
return self.real_vector_from_numpy(np.full(self.dim, value))
def full_vector(self, value):
return self.complexified_vector_type(self.real_full_vector(value), None)
return self.vector_type(self.real_full_vector(value), None)
def real_random_vector(self, distribution, random_state, **kwargs):
values = _create_random_values(self.dim, distribution, random_state, **kwargs)
return self.real_vector_from_numpy(values)
def random_vector(self, distribution, random_state, **kwargs):
return self.complexified_vector_type(self.real_random_vector(distribution, random_state, **kwargs), None)
return self.vector_type(self.real_random_vector(distribution, random_state, **kwargs), None)
@abstractmethod
def real_make_vector(self, obj):
pass
def make_vector(self, obj):
return self.complexified_vector_type(self.real_make_vector(obj), None)
if isinstance(obj, self.real_vector_type):
return self.vector_type(obj, None)
else:
return self.vector_type(self.real_make_vector(obj), None)
def real_vector_from_numpy(self, data, ensure_copy=False):
raise NotImplementedError
......@@ -659,11 +664,13 @@ class ComplexifiedListVectorSpace(ListVectorSpace):
else:
real_part = self.real_vector_from_numpy(data, ensure_copy=ensure_copy)
imag_part = None
return self.complexified_vector_type(real_part, imag_part)
return self.vector_type(real_part, imag_part)
class NumpyListVectorSpace(ListVectorSpace):
vector_type = NumpyVector
def __init__(self, dim, id=None):
self.dim = dim
self.id = id
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment