pymor.vectorarrays.numpy
¶
Module Contents¶
Classes¶



 class pymor.vectorarrays.numpy.NumpyVectorArray(array, space)[source]¶
Bases:
pymor.vectorarrays.interface.VectorArray
VectorArray
implementation viaNumPy arrays
.This is the default
VectorArray
type used by allOperators
in pyMOR’s discretization toolkit. Moreover, all reducedOperators
are based onNumpyVectorArray
.This class is just a thin wrapper around the underlying
NumPy array
. Thus, while operations likeaxpy
orinner
will be quite efficient, removing or appending vectors will be costly.Warning
This class is not intended to be instantiated directly. Use the associated
VectorSpace
instead. to_numpy(self, ensure_copy=False)[source]¶
Return (len(self), self.dim) NumPy Array with the data stored in the array.
Parameters
 ensure_copy
If
False
, modifying the returnedNumPy array
might alter the originalVectorArray
. IfTrue
always a copy of the array data is made.
 __getitem__(self, ind)[source]¶
Return a
VectorArray
view onto a subset of the vectors in the array.
 copy(self, deep=False, *, _ind=None)[source]¶
Returns a copy of the array.
All
VectorArray
implementations in pyMOR have copyonwrite semantics: if not specified otherwise by settingdeep
toTrue
, the returned copy will hold a handle to the same array data as the original array, and a deep copy of the data will only be performed when one of the arrays is modified.Note that for
NumpyVectorArray
, a deep copy is always performed when only some vectors in the array are copied.Parameters
 deep
Ensure that an actual copy of the array data is made (see above).
Returns
A copy of the
VectorArray
.
 append(self, other, remove_from_other=False)[source]¶
Append vectors to the array.
Parameters
 other
A
VectorArray
containing the vectors to be appended. remove_from_other
If
True
, the appended vectors are removed fromother
. For listlike implementations this can be used to prevent unnecessary copies of the involved vectors.
 scal(self, alpha, *, _ind=None)[source]¶
BLAS SCAL operation (inplace scalar multiplication).
This method calculates
self = alpha*self
If
alpha
is a scalar, each vector is multiplied by this scalar. Otherwise,alpha
has to be a onedimensionalNumPy array
of the same length asself
containing the factors for each vector.Parameters
 alpha
The scalar coefficient or onedimensional
NumPy array
of coefficients with which the vectors inself
are multiplied.
 axpy(self, alpha, x, *, _ind=None)[source]¶
BLAS AXPY operation.
This method forms the sum
self = alpha*x + self
If the length of
x
is 1, the samex
vector is used for all vectors inself
. Otherwise, the lengths ofself
andx
have to agree. Ifalpha
is a scalar, eachx
vector is multiplied with the same factoralpha
. Otherwise,alpha
has to be a onedimensionalNumPy array
of the same length asself
containing the coefficients for eachx
vector.Parameters
 alpha
The scalar coefficient or onedimensional
NumPy array
of coefficients with which the vectors inx
are multiplied. x
A
VectorArray
containing the xsummands.
 inner(self, other, product=None, *, _ind=None)[source]¶
Returns the inner products between
VectorArray
elements.If
product
isNone
, the Euclidean inner product between thedofs
ofself
andother
are returned, i.e.U.inner(V)
is equivalent to:
U.dofs(np.arange(U.dim)) @ V.dofs(np.arange(V.dim)).T
(Note, that
dofs
is only intended to be called for a small number of DOF indices.)If a
product
Operator
is specified, thisOperator
is used to compute the inner products usingapply2
, i.e.U.inner(V, product)
is equivalent to:product.apply2(U, V)
which in turn is, by default, implemented as:
U.inner(product.apply(V))
In the case of complex numbers, this is antilinear in the first argument, i.e. in ‘self’. Complex conjugation is done in the first argument because most numerical software in the community handles it this way: Numpy, DUNE, FEniCS, Eigen, Matlab and BLAS do complex conjugation in the first argument, only PetSc and deal.ii do complex conjugation in the second argument.
Parameters
 other
A
VectorArray
containing the second factors. product
If not
None
anOperator
representing the inner product bilinear form.
 pairwise_inner(self, other, product=None, *, _ind=None)[source]¶
Returns the pairwise inner products between
VectorArray
elements.If
product
isNone
, the Euclidean inner product between thedofs
ofself
andother
are returned, i.e.U.pairwise_inner(V)
is equivalent to:
np.sum(U.dofs(np.arange(U.dim)) * V.dofs(np.arange(V.dim)), axis=1)
(Note, that
dofs
is only intended to be called for a small number of DOF indices.)If a
product
Operator
is specified, thisOperator
is used to compute the inner products usingpairwise_apply2
, i.e.U.inner(V, product)
is equivalent to:product.pairwise_apply2(U, V)
which in turn is, by default, implemented as:
U.pairwise_inner(product.apply(V))
In the case of complex numbers, this is antilinear in the first argument, i.e. in ‘self’. Complex conjugation is done in the first argument because most numerical software in the community handles it this way: Numpy, DUNE, FEniCS, Eigen, Matlab and BLAS do complex conjugation in the first argument, only PetSc and deal.ii do complex conjugation in the second argument.
Parameters
 other
A
VectorArray
containing the second factors. product
If not
None
anOperator
representing the inner product bilinear form.
 lincomb(self, coefficients, *, _ind=None)[source]¶
Returns linear combinations of the vectors contained in the array.
Parameters
 coefficients
A
NumPy array
of dimension 1 or 2 containing the linear coefficients.coefficients.shape[1]
has to agree withlen(self)
.
Returns
A
VectorArray
result
such thatresult[i] = ∑ self[j] * coefficients[i,j]
in case
coefficients
is of dimension 2, otherwiselen(result) == 1
andresult[0] = ∑ self[j] * coefficients[j].
 sup_norm(self, *, _ind=None)[source]¶
The linfinitynorms of the vectors contained in the array.
Returns
A
NumPy array
result
such thatresult[i]
contains the norm ofself[i]
.
 dofs(self, dof_indices, *, _ind=None)[source]¶
Extract DOFs of the vectors contained in the array.
Parameters
 dof_indices
List or 1D
NumPy array
of indices of the DOFs that are to be returned.
Returns
A
NumPy array
result
such thatresult[i, j]
is thedof_indices[j]
th DOF of thei
th vector of the array.
 amax(self, *, _ind=None)[source]¶
The maximum absolute value of the DOFs contained in the array.
Returns
 max_ind
NumPy array
containing for each vector a DOF index at which the maximum is attained. max_val
NumPy array
containing for each vector the maximum absolute value of its DOFs.
 class pymor.vectorarrays.numpy.NumpyVectorSpace(dim, id=None)[source]¶
Bases:
pymor.vectorarrays.interface.VectorSpace
VectorSpace
ofNumpyVectorArrays
.Parameters
 dim
The dimension of the vectors contained in the space.
 id
See
id
.
 zeros(self, count=1, reserve=0)[source]¶
Create a
VectorArray
of null vectorsParameters
 count
The number of vectors.
 reserve
Hint for the backend to which length the array will grow.
Returns
A
VectorArray
containingcount
vectors with each component zero.
 full(self, value, count=1, reserve=0)[source]¶
Create a
VectorArray
of vectors with all DOFs set to the same value.Parameters
 value
The value each DOF should be set to.
 count
The number of vectors.
 reserve
Hint for the backend to which length the array will grow.
Returns
A
VectorArray
containingcount
vectors with each DOF set tovalue
.
 random(self, count=1, distribution='uniform', random_state=None, seed=None, reserve=0, **kwargs)[source]¶
Create a
VectorArray
of vectors with random entries.Supported random distributions:
'uniform': Uniform distribution in halfopen interval [`low`, `high`). 'normal': Normal (Gaussian) distribution with mean `loc` and standard deviation `scale`.
Note that not all random distributions are necessarily implemented by all
VectorSpace
implementations.Parameters
 count
The number of vectors.
 distribution
Random distribution to use (
'uniform'
,'normal'
). low
Lower bound for
'uniform'
distribution (defaults to0
). high
Upper bound for
'uniform'
distribution (defaults to1
). loc
Mean for
'normal'
distribution (defaults to0
). scale
Standard deviation for
'normal'
distribution (defaults to1
). random_state
RandomState
to use for sampling. IfNone
, a new random state is generated usingseed
as random seed, or thedefault
random state is used. seed
If not
None
, a new random state with this seed is used. reserve
Hint for the backend to which length the array will grow.
 make_array(cls, obj, id=None)[source]¶
Create a
VectorArray
from raw data.This method is used in the implementation of
Operators
andModels
to create newVectorArrays
from raw data of the underlying solver backends. The ownership of the data is transferred to the newly created array.The exact signature of this method depends on the wrapped solver backend.
 from_numpy(cls, data, id=None, ensure_copy=False)[source]¶
Create a
VectorArray
from aNumPy array
Note that this method will not be supported by all vector space implementations.
Parameters
 data
NumPy
array of shape(len, dim)
wherelen
is the number of vectors anddim
their dimension. ensure_copy
If
False
, modifying the returnedVectorArray
might alter the originalNumPy array
. IfTrue
always a copy of the array data is made.
Returns
A
VectorArray
withdata
as data.
 class pymor.vectorarrays.numpy.NumpyVectorArrayView(array, ind)[source]¶
Bases:
NumpyVectorArray
VectorArray
implementation viaNumPy arrays
.This is the default
VectorArray
type used by allOperators
in pyMOR’s discretization toolkit. Moreover, all reducedOperators
are based onNumpyVectorArray
.This class is just a thin wrapper around the underlying
NumPy array
. Thus, while operations likeaxpy
orinner
will be quite efficient, removing or appending vectors will be costly.Warning
This class is not intended to be instantiated directly. Use the associated
VectorSpace
instead. to_numpy(self, ensure_copy=False)[source]¶
Return (len(self), self.dim) NumPy Array with the data stored in the array.
Parameters
 ensure_copy
If
False
, modifying the returnedNumPy array
might alter the originalVectorArray
. IfTrue
always a copy of the array data is made.
 __getitem__(self, ind)[source]¶
Return a
VectorArray
view onto a subset of the vectors in the array.
 append(self, other, remove_from_other=False)[source]¶
Append vectors to the array.
Parameters
 other
A
VectorArray
containing the vectors to be appended. remove_from_other
If
True
, the appended vectors are removed fromother
. For listlike implementations this can be used to prevent unnecessary copies of the involved vectors.
 copy(self, deep=False)[source]¶
Returns a copy of the array.
All
VectorArray
implementations in pyMOR have copyonwrite semantics: if not specified otherwise by settingdeep
toTrue
, the returned copy will hold a handle to the same array data as the original array, and a deep copy of the data will only be performed when one of the arrays is modified.Note that for
NumpyVectorArray
, a deep copy is always performed when only some vectors in the array are copied.Parameters
 deep
Ensure that an actual copy of the array data is made (see above).
Returns
A copy of the
VectorArray
.
 scal(self, alpha)[source]¶
BLAS SCAL operation (inplace scalar multiplication).
This method calculates
self = alpha*self
If
alpha
is a scalar, each vector is multiplied by this scalar. Otherwise,alpha
has to be a onedimensionalNumPy array
of the same length asself
containing the factors for each vector.Parameters
 alpha
The scalar coefficient or onedimensional
NumPy array
of coefficients with which the vectors inself
are multiplied.
 axpy(self, alpha, x)[source]¶
BLAS AXPY operation.
This method forms the sum
self = alpha*x + self
If the length of
x
is 1, the samex
vector is used for all vectors inself
. Otherwise, the lengths ofself
andx
have to agree. Ifalpha
is a scalar, eachx
vector is multiplied with the same factoralpha
. Otherwise,alpha
has to be a onedimensionalNumPy array
of the same length asself
containing the coefficients for eachx
vector.Parameters
 alpha
The scalar coefficient or onedimensional
NumPy array
of coefficients with which the vectors inx
are multiplied. x
A
VectorArray
containing the xsummands.
 inner(self, other, product=None)[source]¶
Returns the inner products between
VectorArray
elements.If
product
isNone
, the Euclidean inner product between thedofs
ofself
andother
are returned, i.e.U.inner(V)
is equivalent to:
U.dofs(np.arange(U.dim)) @ V.dofs(np.arange(V.dim)).T
(Note, that
dofs
is only intended to be called for a small number of DOF indices.)If a
product
Operator
is specified, thisOperator
is used to compute the inner products usingapply2
, i.e.U.inner(V, product)
is equivalent to:product.apply2(U, V)
which in turn is, by default, implemented as:
U.inner(product.apply(V))
In the case of complex numbers, this is antilinear in the first argument, i.e. in ‘self’. Complex conjugation is done in the first argument because most numerical software in the community handles it this way: Numpy, DUNE, FEniCS, Eigen, Matlab and BLAS do complex conjugation in the first argument, only PetSc and deal.ii do complex conjugation in the second argument.
Parameters
 other
A
VectorArray
containing the second factors. product
If not
None
anOperator
representing the inner product bilinear form.
 pairwise_inner(self, other, product=None)[source]¶
Returns the pairwise inner products between
VectorArray
elements.If
product
isNone
, the Euclidean inner product between thedofs
ofself
andother
are returned, i.e.U.pairwise_inner(V)
is equivalent to:
np.sum(U.dofs(np.arange(U.dim)) * V.dofs(np.arange(V.dim)), axis=1)
(Note, that
dofs
is only intended to be called for a small number of DOF indices.)If a
product
Operator
is specified, thisOperator
is used to compute the inner products usingpairwise_apply2
, i.e.U.inner(V, product)
is equivalent to:product.pairwise_apply2(U, V)
which in turn is, by default, implemented as:
U.pairwise_inner(product.apply(V))
In the case of complex numbers, this is antilinear in the first argument, i.e. in ‘self’. Complex conjugation is done in the first argument because most numerical software in the community handles it this way: Numpy, DUNE, FEniCS, Eigen, Matlab and BLAS do complex conjugation in the first argument, only PetSc and deal.ii do complex conjugation in the second argument.
Parameters
 other
A
VectorArray
containing the second factors. product
If not
None
anOperator
representing the inner product bilinear form.
 lincomb(self, coefficients)[source]¶
Returns linear combinations of the vectors contained in the array.
Parameters
 coefficients
A
NumPy array
of dimension 1 or 2 containing the linear coefficients.coefficients.shape[1]
has to agree withlen(self)
.
Returns
A
VectorArray
result
such thatresult[i] = ∑ self[j] * coefficients[i,j]
in case
coefficients
is of dimension 2, otherwiselen(result) == 1
andresult[0] = ∑ self[j] * coefficients[j].
 sup_norm(self)[source]¶
The linfinitynorms of the vectors contained in the array.
Returns
A
NumPy array
result
such thatresult[i]
contains the norm ofself[i]
.
 dofs(self, dof_indices)[source]¶
Extract DOFs of the vectors contained in the array.
Parameters
 dof_indices
List or 1D
NumPy array
of indices of the DOFs that are to be returned.
Returns
A
NumPy array
result
such thatresult[i, j]
is thedof_indices[j]
th DOF of thei
th vector of the array.
 amax(self)[source]¶
The maximum absolute value of the DOFs contained in the array.
Returns
 max_ind
NumPy array
containing for each vector a DOF index at which the maximum is attained. max_val
NumPy array
containing for each vector the maximum absolute value of its DOFs.