本文整理汇总了Python中pymor.vectorarrays.numpy.NumpyVectorSpace类的典型用法代码示例。如果您正苦于以下问题:Python NumpyVectorSpace类的具体用法?Python NumpyVectorSpace怎么用?Python NumpyVectorSpace使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NumpyVectorSpace类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: projected
def projected(self, range_basis, source_basis, product=None, name=None):
assert source_basis is None or source_basis in self.source
assert range_basis is None or range_basis in self.range
assert product is None or product.source == product.range == self.range
if len(self.interpolation_dofs) == 0:
return ZeroOperator(self.source, self.range, self.name).projected(range_basis, source_basis, product, name)
elif not hasattr(self, 'restricted_operator') or source_basis is None:
return super().projected(range_basis, source_basis, product, name)
else:
name = name or self.name + '_projected'
if range_basis is not None:
if product is None:
projected_collateral_basis = NumpyVectorSpace.make_array(self.collateral_basis.dot(range_basis),
self.range.id)
else:
projected_collateral_basis = NumpyVectorSpace.make_array(product.apply2(self.collateral_basis,
range_basis),
self.range.id)
else:
projected_collateral_basis = self.collateral_basis
return ProjectedEmpiciralInterpolatedOperator(self.restricted_operator, self.interpolation_matrix,
NumpyVectorSpace.make_array(source_basis.components(self.source_dofs)),
projected_collateral_basis, self.triangular,
self.source.id, None, name)
示例2: projected_to_subbasis
def projected_to_subbasis(self, dim_range=None, dim_source=None, dim_collateral=None, name=None):
assert dim_source is None or dim_source <= self.source.dim
assert dim_range is None or dim_range <= self.range.dim
assert dim_collateral is None or dim_collateral <= self.restricted_operator.range.dim
if not isinstance(self.projected_collateral_basis.space, NumpyVectorSpace):
raise NotImplementedError
name = name or '{}_projected_to_subbasis'.format(self.name)
interpolation_matrix = self.interpolation_matrix[:dim_collateral, :dim_collateral]
if dim_collateral is not None:
restricted_operator, source_dofs = self.restricted_operator.restricted(np.arange(dim_collateral))
else:
restricted_operator = self.restricted_operator
old_pcb = self.projected_collateral_basis
projected_collateral_basis = NumpyVectorSpace.make_array(old_pcb.data[:dim_collateral, :dim_range],
old_pcb.space.id)
old_sbd = self.source_basis_dofs
source_basis_dofs = NumpyVectorSpace.make_array(old_sbd.data[:dim_source]) if dim_collateral is None \
else NumpyVectorSpace.make_array(old_sbd.data[:dim_source, source_dofs])
return ProjectedEmpiciralInterpolatedOperator(restricted_operator, interpolation_matrix,
source_basis_dofs, projected_collateral_basis, self.triangular,
self.source.id, solver_options=self.solver_options, name=name)
示例3: visualize
def visualize(self, U, codim=2, **kwargs):
"""Visualize scalar data associated to the grid as a patch plot.
Parameters
----------
U
|NumPy array| of the data to visualize. If `U.dim == 2 and len(U) > 1`, the
data is visualized as a time series of plots. Alternatively, a tuple of
|Numpy arrays| can be provided, in which case a subplot is created for
each entry of the tuple. The lengths of all arrays have to agree.
codim
The codimension of the entities the data in `U` is attached to (either 0 or 2).
kwargs
See :func:`~pymor.gui.qt.visualize_patch`
"""
from pymor.gui.qt import visualize_patch
from pymor.vectorarrays.interfaces import VectorArrayInterface
from pymor.vectorarrays.numpy import NumpyVectorSpace, NumpyVectorArray
if isinstance(U, (np.ndarray, VectorArrayInterface)):
U = (U,)
assert all(isinstance(u, (np.ndarray, VectorArrayInterface)) for u in U)
U = tuple(NumpyVectorSpace.make_array(u) if isinstance(u, np.ndarray) else
u if isinstance(u, NumpyVectorArray) else
NumpyVectorSpace.make_array(u.data)
for u in U)
bounding_box = kwargs.pop('bounding_box', self.domain)
visualize_patch(self, U, codim=codim, bounding_box=bounding_box, **kwargs)
示例4: test_project_array
def test_project_array():
np.random.seed(123)
U = NumpyVectorSpace.from_numpy(np.random.random((2, 10)))
basis = NumpyVectorSpace.from_numpy(np.random.random((3, 10)))
U_p = project_array(U, basis, orthonormal=False)
onb = gram_schmidt(basis)
U_p2 = project_array(U, onb, orthonormal=True)
assert np.all(relative_error(U_p, U_p2) < 1e-10)
示例5: test_identity_lincomb
def test_identity_lincomb():
space = NumpyVectorSpace(10)
identity = IdentityOperator(space)
ones = space.ones()
idid = (identity + identity)
assert almost_equal(ones * 2, idid.apply(ones))
assert almost_equal(ones * 2, idid.apply_adjoint(ones))
assert almost_equal(ones * 0.5, idid.apply_inverse(ones))
assert almost_equal(ones * 0.5, idid.apply_inverse_adjoint(ones))
示例6: test_ext
def test_ext(extension_alg):
size = 5
ident = np.identity(size)
current = ident[0]
for i in range(1, size):
c = NumpyVectorSpace.from_data(current)
n, _ = extension_alg(c, NumpyVectorSpace.from_data(ident[i]))
assert np.allclose(n.data, ident[0:i+1])
current = ident[0:i+1]
示例7: test_axpy
def test_axpy():
x = NumpyVectorSpace.from_numpy(np.array([1.]))
y = NumpyVectorSpace.from_numpy(np.array([1.]))
y.axpy(1 + 1j, x)
assert y.to_numpy()[0, 0] == 2 + 1j
x = NumpyVectorSpace.from_numpy(np.array([1 + 1j]))
y = NumpyVectorSpace.from_numpy(np.array([1.]))
y.axpy(-1, x)
assert y.to_numpy()[0, 0] == -1j
示例8: test_project_array_with_product
def test_project_array_with_product():
np.random.seed(123)
U = NumpyVectorSpace.from_numpy(np.random.random((1, 10)))
basis = NumpyVectorSpace.from_numpy(np.random.random((3, 10)))
product = np.random.random((10, 10))
product = NumpyMatrixOperator(product.T.dot(product))
U_p = project_array(U, basis, product=product, orthonormal=False)
onb = gram_schmidt(basis, product=product)
U_p2 = project_array(U, onb, product=product, orthonormal=True)
assert np.all(relative_error(U_p, U_p2, product) < 1e-10)
示例9: __init__
def __init__(self, array, adjoint=False, space_id=None, name=None):
self._array = array.copy()
if adjoint:
self.source = array.space
self.range = NumpyVectorSpace(len(array), space_id)
else:
self.source = NumpyVectorSpace(len(array), space_id)
self.range = array.space
self.adjoint = adjoint
self.space_id = space_id
self.name = name
示例10: __init__
def __init__(self, array, transposed=False, space_id=None, name=None):
self._array = array.copy()
if transposed:
self.source = array.space
self.range = NumpyVectorSpace(len(array), space_id)
else:
self.source = NumpyVectorSpace(len(array), space_id)
self.range = array.space
self.transposed = transposed
self.space_id = space_id
self.name = name
示例11: test_complex
def test_complex():
np.random.seed(0)
I = np.eye(5)
A = np.random.randn(5, 5)
B = np.random.randn(5, 5)
C = np.random.randn(3, 5)
Iop = NumpyMatrixOperator(I)
Aop = NumpyMatrixOperator(A)
Bop = NumpyMatrixOperator(B)
Cva = NumpyVectorSpace.from_numpy(C)
# lincombs
assert not np.iscomplexobj((Iop * 1 + Bop * 1).assemble().matrix)
assert not np.iscomplexobj((Aop * 1 + Bop * 1).assemble().matrix)
assert np.iscomplexobj((Aop * (1+0j) + Bop * (1+0j)).assemble().matrix)
assert np.iscomplexobj((Aop * 1j + Bop * 1).assemble().matrix)
assert np.iscomplexobj((Bop * 1 + Aop * 1j).assemble().matrix)
# apply_inverse
assert not np.iscomplexobj(Aop.apply_inverse(Cva).to_numpy())
assert np.iscomplexobj((Aop * 1j).apply_inverse(Cva).to_numpy())
assert np.iscomplexobj((Aop * 1 + Bop * 1j).assemble().apply_inverse(Cva).to_numpy())
assert np.iscomplexobj(Aop.apply_inverse(Cva * 1j).to_numpy())
# append
for rsrv in (0, 10):
for o_ind in (slice(None), [0]):
va = NumpyVectorSpace(5).empty(reserve=rsrv)
va.append(Cva)
D = np.random.randn(1, 5) + 1j * np.random.randn(1, 5)
Dva = NumpyVectorSpace.from_numpy(D)
assert not np.iscomplexobj(va.to_numpy())
assert np.iscomplexobj(Dva.to_numpy())
va.append(Dva[o_ind])
assert np.iscomplexobj(va.to_numpy())
# scal
assert not np.iscomplexobj(Cva.to_numpy())
assert np.iscomplexobj((Cva * 1j).to_numpy())
assert np.iscomplexobj((Cva * (1 + 0j)).to_numpy())
# axpy
assert not np.iscomplexobj(Cva.to_numpy())
Cva[0].axpy(1, Dva)
assert np.iscomplexobj(Cva.to_numpy())
Cva = NumpyVectorSpace.from_numpy(C)
assert not np.iscomplexobj(Cva.to_numpy())
Cva[0].axpy(1j, Dva)
assert np.iscomplexobj(Cva.to_numpy())
示例12: __init__
def __init__(self, mapping, adjoint_mapping=None, dim_source=1, dim_range=1, linear=False, parameter_type=None,
source_id=None, range_id=None, solver_options=None, name=None):
self.source = NumpyVectorSpace(dim_source, source_id)
self.range = NumpyVectorSpace(dim_range, range_id)
self.solver_options = solver_options
self.name = name
self._mapping = mapping
self._adjoint_mapping = adjoint_mapping
self.linear = linear
if parameter_type is not None:
self.build_parameter_type(parameter_type)
self.source_id = source_id # needed for with_
self.range_id = range_id
示例13: save
def save(self):
if not config.HAVE_PYVTK:
msg = QMessageBox(QMessageBox.Critical, 'Error', 'VTK output disabled. Pleas install pyvtk.')
msg.exec_()
return
filename = QFileDialog.getSaveFileName(self, 'Save as vtk file')[0]
base_name = filename.split('.vtu')[0].split('.vtk')[0].split('.pvd')[0]
if base_name:
if len(self.U) == 1:
write_vtk(self.grid, NumpyVectorSpace.make_array(self.U[0]), base_name, codim=self.codim)
else:
for i, u in enumerate(self.U):
write_vtk(self.grid, NumpyVectorSpace.make_array(u), '{}-{}'.format(base_name, i),
codim=self.codim)
示例14: restricted
def restricted(self, dofs):
assert all(0 <= c < self.range.dim for c in dofs)
if not self.transposed:
restricted_value = NumpyVectorSpace.make_array(self._array.components(dofs))
return VectorArrayOperator(restricted_value, False), np.arange(self.source.dim, dtype=np.int32)
else:
raise NotImplementedError
示例15: with_cb_dim
def with_cb_dim(self, dim):
assert dim <= self.restricted_operator.range.dim
interpolation_matrix = self.interpolation_matrix[:dim, :dim]
restricted_operator, source_dofs = self.restricted_operator.restricted(np.arange(dim))
old_pcb = self.projected_collateral_basis
projected_collateral_basis = NumpyVectorSpace.make_array(old_pcb.to_numpy()[:dim, :])
old_sbd = self.source_basis_dofs
source_basis_dofs = NumpyVectorSpace.make_array(old_sbd.to_numpy()[:, source_dofs])
return ProjectedEmpiciralInterpolatedOperator(restricted_operator, interpolation_matrix,
source_basis_dofs, projected_collateral_basis, self.triangular,
solver_options=self.solver_options, name=self.name)