Esempio n. 1
0
 def action_ZeroOperator(self, op):
     dim_range, dim_source = self.dim_range, self.dim_source
     range_space = op.range if dim_range is None else NumpyVectorSpace(
         dim_range)
     source_space = op.source if dim_source is None else NumpyVectorSpace(
         dim_source)
     return ZeroOperator(range_space, source_space, name=op.name)
Esempio n. 2
0
    def apply(self, U, mu=None):
        assert self.parameters.assert_compatible(mu)
        if len(self.interpolation_dofs) == 0:
            return self.range.zeros(len(U))

        if hasattr(self, 'restricted_operator'):
            U_dofs = NumpyVectorSpace.make_array(U.dofs(self.source_dofs))
            AU = self.restricted_operator.apply(U_dofs, mu=mu)
        else:
            AU = NumpyVectorSpace.make_array(
                self.operator.apply(U, mu=mu).dofs(self.interpolation_dofs))
        try:
            if self.triangular:
                interpolation_coefficients = solve_triangular(
                    self.interpolation_matrix,
                    AU.to_numpy().T,
                    lower=True,
                    unit_diagonal=True).T
            else:
                interpolation_coefficients = solve(self.interpolation_matrix,
                                                   AU.to_numpy().T).T
        except ValueError:  # this exception occurs when AU contains NaNs ...
            interpolation_coefficients = np.empty(
                (len(AU), len(self.collateral_basis))) + np.nan
        return self.collateral_basis.lincomb(interpolation_coefficients)
Esempio n. 3
0
    def action_EmpiricalInterpolatedOperator(self,
                                             op,
                                             range_basis,
                                             source_basis,
                                             product=None):
        if len(op.interpolation_dofs) == 0:
            return self.apply(ZeroOperator(op.source, op.range, op.name),
                              range_basis, source_basis, product)
        elif not hasattr(op, 'restricted_operator') or source_basis is None:
            raise RuleNotMatchingError(
                'Has no restricted operator or source_basis is None')
        else:
            if range_basis is not None:
                if product is None:
                    projected_collateral_basis = NumpyVectorSpace.make_array(
                        op.collateral_basis.dot(range_basis), op.range.id)
                else:
                    projected_collateral_basis = NumpyVectorSpace.make_array(
                        product.apply2(op.collateral_basis, range_basis),
                        op.range.id)
            else:
                projected_collateral_basis = op.collateral_basis

            return ProjectedEmpiciralInterpolatedOperator(
                op.restricted_operator, op.interpolation_matrix,
                NumpyVectorSpace.make_array(
                    source_basis.components(op.source_dofs)),
                projected_collateral_basis, op.triangular, op.source.id, None,
                op.name)
Esempio n. 4
0
    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.to_numpy())
            for u in U)
        bounding_box = kwargs.pop('bounding_box', self.domain)
        visualize_patch(self,
                        U,
                        codim=codim,
                        bounding_box=bounding_box,
                        **kwargs)
Esempio n. 5
0
class NumpyGenericOperator(Operator):
    """Wraps an arbitrary Python function between |NumPy arrays| as an |Operator|.

    Parameters
    ----------
    mapping
        The function to wrap. If `parameters` is `None`, the function is of
        the form `mapping(U)` and is expected to be vectorized. In particular::

            mapping(U).shape == U.shape[:-1] + (dim_range,).

        If `parameters` is not `None`, the function has to have the signature
        `mapping(U, mu)`.
    adjoint_mapping
        The adjoint function to wrap. If `parameters` is `None`, the function is of
        the form `adjoint_mapping(U)` and is expected to be vectorized. In particular::

            adjoint_mapping(U).shape == U.shape[:-1] + (dim_source,).

        If `parameters` is not `None`, the function has to have the signature
        `adjoint_mapping(U, mu)`.
    dim_source
        Dimension of the operator's source.
    dim_range
        Dimension of the operator's range.
    linear
        Set to `True` if the provided `mapping` and `adjoint_mapping` are linear.
    parameters
        The |Parameters| the depends on.
    solver_options
        The |solver_options| for the operator.
    name
        Name of the operator.
    """

    def __init__(self, mapping, adjoint_mapping=None, dim_source=1, dim_range=1, linear=False, parameters={},
                 source_id=None, range_id=None, solver_options=None, name=None):
        self.__auto_init(locals())
        self.source = NumpyVectorSpace(dim_source, source_id)
        self.range = NumpyVectorSpace(dim_range, range_id)
        self.parameters_own = parameters

    def apply(self, U, mu=None):
        assert U in self.source
        assert self.parameters.assert_compatible(mu)
        if self.parametric:
            return self.range.make_array(self.mapping(U.to_numpy(), mu=mu))
        else:
            return self.range.make_array(self.mapping(U.to_numpy()))

    def apply_adjoint(self, V, mu=None):
        if self.adjoint_mapping is None:
            raise ValueError('NumpyGenericOperator: adjoint mapping was not defined.')
        assert V in self.range
        assert self.parameters.assert_compatible(mu)
        V = V.to_numpy()
        if self.parametric:
            return self.source.make_array(self.adjoint_mapping(V, mu=mu))
        else:
            return self.source.make_array(self.adjoint_mapping(V))
Esempio n. 6
0
    def action_ProjectedEmpiciralInterpolatedOperator(self,
                                                      op,
                                                      dim_range=None,
                                                      dim_source=None):
        if not isinstance(op.projected_collateral_basis.space,
                          NumpyVectorSpace):
            raise NotImplementedError

        restricted_operator = op.restricted_operator

        old_pcb = op.projected_collateral_basis
        projected_collateral_basis = NumpyVectorSpace.make_array(
            old_pcb.data[:, :dim_range], old_pcb.space.id)

        old_sbd = op.source_basis_dofs
        source_basis_dofs = NumpyVectorSpace.make_array(
            old_sbd.data[:dim_source])

        return ProjectedEmpiciralInterpolatedOperator(
            restricted_operator,
            op.interpolation_matrix,
            source_basis_dofs,
            projected_collateral_basis,
            op.triangular,
            op.source.id,
            solver_options=op.solver_options,
            name=op.name)
Esempio n. 7
0
 def __init__(self,
              operator,
              range_basis,
              source_basis,
              product=None,
              solver_options=None,
              name=None):
     assert isinstance(operator, OperatorInterface)
     assert source_basis is None or source_basis in operator.source
     assert range_basis is None or range_basis in operator.range
     assert product is None \
         or (isinstance(product, OperatorInterface)
             and range_basis is not None
             and operator.range == product.source
             and product.range == product.source)
     self.build_parameter_type(inherits=(operator, ))
     self.source = NumpyVectorSpace(
         len(source_basis)) if source_basis is not None else operator.source
     self.range = NumpyVectorSpace(
         len(range_basis)) if range_basis is not None else operator.range
     self.solver_options = solver_options
     self.name = name
     self.operator = operator
     self.source_basis = source_basis.copy(
     ) if source_basis is not None else None
     self.range_basis = range_basis.copy(
     ) if range_basis is not None else None
     self.linear = operator.linear
     self.product = product
Esempio n. 8
0
def test_to_matrix_ZeroOperator():
    n = 3
    m = 4
    Z = np.zeros((n, m))

    Zop = ZeroOperator(NumpyVectorSpace(n), NumpyVectorSpace(m))
    assert_type_and_allclose(Z, Zop, 'sparse')
Esempio n. 9
0
    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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    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_pcb.space.id)

        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,
            self.source.id,
            solver_options=self.solver_options,
            name=self.name)
Esempio n. 12
0
    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)
Esempio n. 13
0
 def action_ConstantOperator(self, op):
     dim_range, dim_source = self.dim_range, self.dim_srouce
     source = op.source if dim_source is None else NumpyVectorSpace(
         dim_source, op.source.id)
     value = op._value if dim_range is None else NumpyVectorSpace(
         op._value.data[:, :dim_range], op.range.id)
     return ConstantOperator(value, source, name=op.name)
Esempio n. 14
0
 def action_ConstantOperator(self, op):
     dim_range, dim_source = self.dim_range, self.dim_source
     source = op.source if dim_source is None else NumpyVectorSpace(
         dim_source)
     value = op.value if dim_range is None else NumpyVectorSpace(
         op.value.to_numpy()[:, :dim_range])
     return ConstantOperator(value, source, name=op.name)
Esempio n. 15
0
 def __init__(self,
              matrix,
              source_id=None,
              range_id=None,
              solver_options=None,
              name=None):
     super().__init__(matrix,
                      source_id=source_id,
                      range_id=range_id,
                      solver_options=solver_options,
                      name=name)
     functional = self.range_id is None
     vector = self.source_id is None
     if functional and vector:
         raise NotImplementedError
     if vector:
         self.source = NumpyVectorSpace(1, source_id)
     else:
         self.source = NumpyListVectorSpace(matrix.shape[1], source_id)
     if functional:
         self.range = NumpyVectorSpace(1, range_id)
     else:
         self.range = NumpyListVectorSpace(matrix.shape[0], range_id)
     self.functional = functional
     self.vector = vector
Esempio n. 16
0
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)
Esempio n. 17
0
 def __init__(self, claw):
     self.claw = claw
     self.num_eqn = claw.solution.state.q.shape[0]
     self.mx = claw.solution.state.q.shape[1]
     self.my = claw.solution.state.q.shape[2]
     self.mz = claw.solution.state.q.shape[3]
     self.solution_space = NumpyVectorSpace(claw.solution.state.q.size)
     self.claw.start_frame = self.claw.solution.start_frame
Esempio n. 18
0
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)
Esempio n. 19
0
def numpy_vector_array_factory(length, dim, seed):
    np.random.seed(seed)
    if np.random.randint(2):
        return NumpyVectorSpace.from_numpy(np.random.random((length, dim)))
    else:
        return NumpyVectorSpace.from_numpy(
            np.random.random((length, dim)) +
            np.random.random((length, dim)) * 1j)
Esempio n. 20
0
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]
Esempio n. 21
0
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))
Esempio n. 22
0
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))
Esempio n. 23
0
 def action_ConstantOperator(self, op):
     range_basis, source_basis = self.range_basis, self.source_basis
     if range_basis is not None:
         projected_value = NumpyVectorSpace.make_array(range_basis.dot(op.value).T)
     else:
         projected_value = op.value
     if source_basis is None:
         return ConstantOperator(projected_value, op.source, name=op.name)
     else:
         return ConstantOperator(projected_value, NumpyVectorSpace(len(source_basis)), name=op.name)
Esempio n. 24
0
 def action_ZeroOperator(self, op):
     range_basis, source_basis = self.range_basis, self.source_basis
     if source_basis is not None and range_basis is not None:
         from pymor.operators.numpy import NumpyMatrixOperator
         return NumpyMatrixOperator(np.zeros((len(range_basis), len(source_basis))),
                                    name=op.name)
     else:
         new_source = NumpyVectorSpace(len(source_basis)) if source_basis is not None else op.source
         new_range = NumpyVectorSpace(len(range_basis)) if range_basis is not None else op.range
         return ZeroOperator(new_range, new_source, name=op.name)
Esempio n. 25
0
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)
Esempio n. 26
0
 def __init__(self, array, transposed=False, name=None):
     self._array = array.copy()
     if transposed:
         self.source = array.space
         self.range = NumpyVectorSpace(len(array))
     else:
         self.source = NumpyVectorSpace(len(array))
         self.range = array.space
     self.transposed = transposed
     self.name = name
Esempio n. 27
0
 def __init__(self, matrix, solver_options=None, name=None):
     assert matrix.ndim <= 2
     if matrix.ndim == 1:
         matrix = np.reshape(matrix, (1, -1))
     self.source = NumpyVectorSpace(matrix.shape[1])
     self.range = NumpyVectorSpace(matrix.shape[0])
     self.solver_options = solver_options
     self.name = name
     self._matrix = matrix
     self.sparse = issparse(matrix)
Esempio n. 28
0
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)
Esempio n. 29
0
def test_axpy():
    x = NumpyVectorSpace.from_data(np.array([1.]))
    y = NumpyVectorSpace.from_data(np.array([1.]))
    y.axpy(1 + 1j, x)
    assert y.data[0, 0] == 2 + 1j

    x = NumpyVectorSpace.from_data(np.array([1 + 1j]))
    y = NumpyVectorSpace.from_data(np.array([1.]))
    y.axpy(-1, x)
    assert y.data[0, 0] == -1j
Esempio n. 30
0
        def __init__(self, neural_network, output_functional=None, products=None,
                     error_estimator=None, visualizer=None, name=None):

            super().__init__(products=products, error_estimator=error_estimator, visualizer=visualizer, name=name)

            self.__auto_init(locals())
            self.solution_space = NumpyVectorSpace(neural_network.output_dimension)
            self.linear = output_functional is None or output_functional.linear
            if output_functional is not None:
                self.output_space = output_functional.range
Esempio n. 31
0
    def __init__(self, array, adjoint=False, space_id=None, name=None):
        array = array.copy()

        self.__auto_init(locals())
        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
Esempio n. 32
0
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
Esempio n. 33
0
 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
Esempio n. 34
0
 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
Esempio n. 35
0
 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
Esempio n. 36
0
def test_block_identity_lincomb():
    space = NumpyVectorSpace(10)
    space2 = BlockVectorSpace([space, space])
    identity = BlockDiagonalOperator([IdentityOperator(space), IdentityOperator(space)])
    identity2 = IdentityOperator(space2)
    ones = space.ones()
    ones2 = space2.make_array([ones, ones])
    idid = identity + identity2
    assert almost_equal(ones2 * 2, idid.apply(ones2))
    assert almost_equal(ones2 * 2, idid.apply_adjoint(ones2))
    assert almost_equal(ones2 * 0.5, idid.apply_inverse(ones2))
    assert almost_equal(ones2 * 0.5, idid.apply_inverse_adjoint(ones2))
Esempio n. 37
0
 def __init__(self, matrix, source_id=None, range_id=None, solver_options=None, name=None):
     assert matrix.ndim <= 2
     if matrix.ndim == 1:
         matrix = np.reshape(matrix, (1, -1))
     self.source = NumpyVectorSpace(matrix.shape[1], source_id)
     self.range = NumpyVectorSpace(matrix.shape[0], range_id)
     self.solver_options = solver_options
     self.name = name
     self._matrix = matrix
     self.source_id = source_id
     self.range_id = range_id
     self.sparse = issparse(matrix)
Esempio n. 38
0
def test_identity_lincomb():
    space = NumpyVectorSpace(10)
    identity = IdentityOperator(space)
    ones = space.ones()
    idid = (identity + identity)
    idid_ = ExpressionParameterFunctional('2', {}) * 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))
    assert almost_equal(ones * 0.5, idid_.apply_inverse(ones))
    assert almost_equal(ones * 0.5, idid_.apply_inverse_adjoint(ones))
Esempio n. 39
0
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())
Esempio n. 40
0
 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
Esempio n. 41
0
    def __init__(self, matrix, source_id=None, range_id=None, solver_options=None, name=None):
        assert matrix.ndim <= 2
        if matrix.ndim == 1:
            matrix = np.reshape(matrix, (1, -1))
        try:
            matrix.setflags(write=False)  # make numpy arrays read-only
        except AttributeError:
            pass

        self.__auto_init(locals())
        self.source = NumpyVectorSpace(matrix.shape[1], source_id)
        self.range = NumpyVectorSpace(matrix.shape[0], range_id)
        self.sparse = issparse(matrix)
Esempio n. 42
0
 def __init__(self, mapping, transpose_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._transpose_mapping = transpose_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
Esempio n. 43
0
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_data(C)

    # assemble_lincomb
    assert not np.iscomplexobj(
        Aop.assemble_lincomb((Iop, Bop), (1, 1))._matrix)
    assert not np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1, 1))._matrix)
    assert np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1 + 0j, 1 + 0j))._matrix)
    assert np.iscomplexobj(Aop.assemble_lincomb((Aop, Bop), (1j, 1))._matrix)
    assert np.iscomplexobj(Aop.assemble_lincomb((Bop, Aop), (1, 1j))._matrix)

    # apply_inverse
    assert not np.iscomplexobj(Aop.apply_inverse(Cva).data)
    assert np.iscomplexobj((Aop * 1j).apply_inverse(Cva).data)
    assert np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1, 1j)).apply_inverse(Cva).data)
    assert np.iscomplexobj(Aop.apply_inverse(Cva * 1j).data)

    # 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_data(D)

            assert not np.iscomplexobj(va.data)
            assert np.iscomplexobj(Dva.data)
            va.append(Dva[o_ind])
            assert np.iscomplexobj(va.data)

    # scal
    assert not np.iscomplexobj(Cva.data)
    assert np.iscomplexobj((Cva * 1j).data)
    assert np.iscomplexobj((Cva * (1 + 0j)).data)

    # axpy
    assert not np.iscomplexobj(Cva.data)
    Cva[0].axpy(1, Dva)
    assert np.iscomplexobj(Cva.data)

    Cva = NumpyVectorSpace.from_data(C)
    assert not np.iscomplexobj(Cva.data)
    Cva[0].axpy(1j, Dva)
    assert np.iscomplexobj(Cva.data)
Esempio n. 44
0
File: qt.py Progetto: renemilk/pyMor
 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)
Esempio n. 45
0
 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
Esempio n. 46
0
File: ei.py Progetto: pymor/pymor
    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)
Esempio n. 47
0
class RestrictedDuneSpaceOperator(OperatorBase):

    linear = False

    def __init__(self, impl, range_dofs):
        self._impl = impl
        self._range_dofs = range_dofs
        self.source = NumpyVectorSpace(impl.dimSource)
        self.range = NumpyVectorSpace(len(range_dofs))
        self.name = 'DuneBurgersSpaceOperator_restricted'

        self._source_vec = dune_module.Vector(impl.dimSource, 0.)
        self._range_vec = dune_module.Vector(impl.dimRange, 0.)
        self._source_array = np.frombuffer(self._source_vec.buffer())
        self._range_array = np.frombuffer(self._range_vec.buffer())
        self.build_parameter_type({'exponent': tuple()}, local_global=True)

    def apply(self, U, ind=None, mu=None):
        assert U in self.source
        mu = self.parse_parameter(mu)
        exponent = float(mu['exponent'])

        U = U.data if ind is None else \
            U.data[ind] if hasattr(ind, '__len__') else \
            U.data[ind:ind + 1]
        R = self.range.zeros(len(U))
        R_array = R.data

        for i, u in enumerate(U):
            self._source_array[:] = u
            self._range_array[:] = 0
            self._impl.apply(self._source_vec, self._range_vec, exponent, 1.)
            R_array[i] = self._range_array[:][self._range_dofs]

        return R
Esempio n. 48
0
    def action_ProjectedEmpiciralInterpolatedOperator(self, op):
        if not isinstance(op.projected_collateral_basis.space, NumpyVectorSpace):
            raise NotImplementedError

        restricted_operator = op.restricted_operator

        old_pcb = op.projected_collateral_basis
        projected_collateral_basis = NumpyVectorSpace.make_array(old_pcb.to_numpy()[:, :self.dim_range],
                                                                 old_pcb.space.id)

        old_sbd = op.source_basis_dofs
        source_basis_dofs = NumpyVectorSpace.make_array(old_sbd.to_numpy()[:self.dim_source])

        return ProjectedEmpiciralInterpolatedOperator(restricted_operator, op.interpolation_matrix,
                                                      source_basis_dofs, projected_collateral_basis, op.triangular,
                                                      op.source.id, solver_options=op.solver_options, name=op.name)
Esempio n. 49
0
 def __init__(self, matrix, source_id=None, range_id=None, solver_options=None, name=None):
     assert matrix.ndim <= 2
     if matrix.ndim == 1:
         matrix = np.reshape(matrix, (1, -1))
     try:
         matrix.setflags(write=False)  # make numpy arrays read-only
     except AttributeError:
         pass
     self.source = NumpyVectorSpace(matrix.shape[1], source_id)
     self.range = NumpyVectorSpace(matrix.shape[0], range_id)
     self.solver_options = solver_options
     self.name = name
     self.matrix = matrix
     self.source_id = source_id
     self.range_id = range_id
     self.sparse = issparse(matrix)
Esempio n. 50
0
class ComponentProjection(OperatorBase):
    """|Operator| representing the projection of a |VectorArray| on some of its components.

    Parameters
    ----------
    components
        List or 1D |NumPy array| of the indices of the vector
        :meth:`~pymor.vectorarrays.interfaces.VectorArrayInterface.components` that ar
        to be extracted by the operator.
    source
        Source |VectorSpace| of the operator.
    name
        Name of the operator.
    """

    linear = True

    def __init__(self, components, source, name=None):
        assert all(0 <= c < source.dim for c in components)
        self.components = np.array(components, dtype=np.int32)
        self.range = NumpyVectorSpace(len(components))
        self.source = source
        self.name = name

    def apply(self, U, mu=None):
        assert U in self.source
        return self.range.make_array(U.components(self.components))

    def restricted(self, dofs):
        assert all(0 <= c < self.range.dim for c in dofs)
        source_dofs = self.components[dofs]
        return IdentityOperator(NumpyVectorSpace(len(source_dofs))), source_dofs
Esempio n. 51
0
 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 range_basis is not None:
         if product:
             projected_value = NumpyVectorSpace.make_array(product.apply2(range_basis, self._value).T, self.range.id)
         else:
             projected_value = NumpyVectorSpace.make_array(range_basis.dot(self._value).T, self.range.id)
     else:
         projected_value = self._value
     if source_basis is None:
         return ConstantOperator(projected_value, self.source, name=self.name + '_projected')
     else:
         return ConstantOperator(projected_value, NumpyVectorSpace(len(source_basis), self.source.id),
                                 name=self.name + '_projected')
Esempio n. 52
0
def test_to_matrix():
    np.random.seed(0)
    A = np.random.randn(2, 2)
    B = np.random.randn(3, 3)
    C = np.random.randn(3, 3)

    X = np.bmat([[np.eye(2) + A, np.zeros((2, 3))], [np.zeros((3, 2)), B.dot(C.T)]])

    C = sps.csc_matrix(C)

    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cop = NumpyMatrixOperator(C)

    Xop = BlockDiagonalOperator([LincombOperator([IdentityOperator(NumpyVectorSpace(2)), Aop],
                                                 [1, 1]), Concatenation(Bop, AdjointOperator(Cop))])

    assert np.allclose(X, to_matrix(Xop))
    assert np.allclose(X, to_matrix(Xop, format='csr').toarray())

    np.random.seed(0)
    V = np.random.randn(10, 2)
    Vva = NumpyVectorSpace.make_array(V.T)
    Vop = VectorArrayOperator(Vva)
    assert np.allclose(V, to_matrix(Vop))
    Vop = VectorArrayOperator(Vva, transposed=True)
    assert np.allclose(V, to_matrix(Vop).T)
Esempio n. 53
0
    def action_EmpiricalInterpolatedOperator(self, op):
        range_basis, source_basis, product = self.range_basis, self.source_basis, self.product
        if len(op.interpolation_dofs) == 0:
            return self.apply(ZeroOperator(op.range, op.source, op.name))
        elif not hasattr(op, 'restricted_operator') or source_basis is None:
            raise RuleNotMatchingError('Has no restricted operator or source_basis is None')
        else:
            if range_basis is not None:
                projected_collateral_basis = NumpyVectorSpace.make_array(op.collateral_basis.inner(range_basis,
                                                                                                   product),
                                                                         op.range.id)
            else:
                projected_collateral_basis = op.collateral_basis

            return ProjectedEmpiciralInterpolatedOperator(op.restricted_operator, op.interpolation_matrix,
                                                          NumpyVectorSpace.make_array(source_basis.dofs(op.source_dofs)),
                                                          projected_collateral_basis, op.triangular,
                                                          op.source.id, None, op.name)
Esempio n. 54
0
 def reconstruct(self, U):
     """Reconstruct high-dimensional vector from reduced vector `U`."""
     assert isinstance(U.space, NumpyVectorSpace)
     UU = np.zeros((len(U), self.dim))
     UU[:, :self.dim_subbasis] = U.data
     UU = NumpyVectorSpace.make_array(UU, U.space.id)
     if self.old_recontructor:
         return self.old_recontructor.reconstruct(UU)
     else:
         return UU
Esempio n. 55
0
File: basic.py Progetto: pymor/pymor
 def __init__(self, operator, range_basis, source_basis, product=None, solver_options=None):
     assert isinstance(operator, OperatorInterface)
     assert source_basis is None or source_basis in operator.source
     assert range_basis is None or range_basis in operator.range
     assert (product is None
             or (isinstance(product, OperatorInterface)
                 and range_basis is not None
                 and operator.range == product.source
                 and product.range == product.source))
     self.build_parameter_type(operator)
     self.source = NumpyVectorSpace(len(source_basis)) if source_basis is not None else operator.source
     self.range = NumpyVectorSpace(len(range_basis)) if range_basis is not None else operator.range
     self.solver_options = solver_options
     self.name = operator.name
     self.operator = operator
     self.source_basis = source_basis.copy() if source_basis is not None else None
     self.range_basis = range_basis.copy() if range_basis is not None else None
     self.linear = operator.linear
     self.product = product
Esempio n. 56
0
File: ei.py Progetto: pymor/pymor
    def apply(self, U, mu=None):
        mu = self.parse_parameter(mu)
        if len(self.interpolation_dofs) == 0:
            return self.range.zeros(len(U))

        if hasattr(self, 'restricted_operator'):
            U_dofs = NumpyVectorSpace.make_array(U.dofs(self.source_dofs))
            AU = self.restricted_operator.apply(U_dofs, mu=mu)
        else:
            AU = NumpyVectorSpace.make_array(self.operator.apply(U, mu=mu).dofs(self.interpolation_dofs))
        try:
            if self.triangular:
                interpolation_coefficients = solve_triangular(self.interpolation_matrix, AU.to_numpy().T,
                                                              lower=True, unit_diagonal=True).T
            else:
                interpolation_coefficients = solve(self.interpolation_matrix, AU.to_numpy().T).T
        except ValueError:  # this exception occurs when AU contains NaNs ...
            interpolation_coefficients = np.empty((len(AU), len(self.collateral_basis))) + np.nan
        return self.collateral_basis.lincomb(interpolation_coefficients)
Esempio n. 57
0
def test_to_matrix_VectorArrayOperator():
    np.random.seed(0)
    V = np.random.randn(10, 2)

    Vva = NumpyVectorSpace.make_array(V.T)
    Vop = VectorArrayOperator(Vva)
    assert_type_and_allclose(V, Vop, 'dense')

    Vop = VectorArrayOperator(Vva, adjoint=True)
    assert_type_and_allclose(V.T, Vop, 'dense')
Esempio n. 58
0
def test_vtkio(rect_or_tria_grid):
    grid = rect_or_tria_grid
    steps = 4
    for dim in range(1, 2):
        for codim, data in enumerate((NumpyVectorSpace.from_numpy(np.zeros((steps, grid.size(c)))) for c in range(grid.dim+1))):
            with SafeTemporaryFileName('wb') as out_name:
                if codim == 1:
                    with pytest.raises(NotImplementedError):
                        write_vtk(grid, data, out_name, codim=codim)
                else:
                    write_vtk(grid, data, out_name, codim=codim)
Esempio n. 59
0
def test_blk_diag_apply_inverse():
    np.random.seed(0)

    A = np.random.randn(2, 2)
    B = np.random.randn(3, 3)
    C = spla.block_diag(A, B)
    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cop = BlockDiagonalOperator((Aop, Bop))

    v1 = np.random.randn(2)
    v2 = np.random.randn(3)
    v = np.hstack((v1, v2))
    v1va = NumpyVectorSpace.from_data(v1)
    v2va = NumpyVectorSpace.from_data(v2)
    vva = BlockVectorSpace.make_array((v1va, v2va))

    wva = Cop.apply_inverse(vva)
    w = np.hstack((wva.block(0).data, wva.block(1).data))
    assert np.allclose(spla.solve(C, v), w)
Esempio n. 60
0
def test_scal():
    v = np.array([[1, 2, 3],
                  [4, 5, 6]], dtype=float)
    v = NumpyVectorSpace.from_numpy(v)
    v.scal(1j)

    k = 0
    for i in range(2):
        for j in range(3):
            k += 1
            assert v.to_numpy()[i, j] == k * 1j