Beispiel #1
0
def numpy_list_vector_array_factory(length, dim, seed):
    np.random.seed(seed)
    if np.random.randint(2):
        return NumpyListVectorSpace.from_numpy(np.random.random((length, dim)))
    else:
        return NumpyListVectorSpace.from_numpy(
            np.random.random((length, dim)) +
            np.random.random((length, dim)) * 1j)
Beispiel #2
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)
     self.source = NumpyListVectorSpace(matrix.shape[1], source_id)
     self.range = NumpyListVectorSpace(matrix.shape[0], range_id)
Beispiel #3
0
 def __init__(self, matrix, functional=False, vector=False, source_id=None, range_id=None,
              solver_options=None, name=None):
     assert not (functional and vector)
     super().__init__(matrix, source_id=source_id, range_id=range_id, solver_options=solver_options, name=name)
     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
Beispiel #4
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
Beispiel #5
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
Beispiel #6
0
 def action_NumpyMatrixOperator(self, op):
     vector = op.source.dim == 1
     functional = op.range.dim == 1
     if vector and functional:
         raise NotImplementedError
     if vector:
         space = NumpyListVectorSpace(op.range.dim, op.range.id)
         return VectorOperator(space.from_numpy(op.matrix.reshape((1, -1))), op.name)
     elif functional:
         space = NumpyListVectorSpace(op.source.dim, op.source.id)
         return VectorFunctional(space.from_numpy(op.matrix.ravel()), op.name)
     else:
         return op.with_(new_type=NumpyListVectorArrayMatrixOperator)
Beispiel #7
0
 def action_VectorArrayOperator(self, op):
     space = NumpyListVectorSpace(op.array.dim, op.array.space.id)
     return op.with_(new_type=VectorArrayOperator,
                     array=space.from_numpy(op.array.to_numpy()))
Beispiel #8
0
def _numpy_list_vector_spaces(draw, np_data_list, compatible, count, dims):
    return [(NumpyListVectorSpace(d), ar) for d, ar in zip(dims, np_data_list)]
Beispiel #9
0
class NumpyListVectorArrayMatrixOperator(NumpyMatrixOperator):
    """Variant of |NumpyMatrixOperator| using |ListVectorArray| instead of |NumpyVectorArray|."""

    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

    def apply(self, U, mu=None):
        assert U in self.source

        if self.vector:
            V = super().apply(U, mu=mu)
            return self.range.from_numpy(V.to_numpy())

        V = [self.matrix.dot(v._array) for v in U._list]

        if self.functional:
            return self.range.make_array(np.array(V)) if len(V) > 0 else self.range.empty()
        else:
            return self.range.make_array(V)

    def apply_adjoint(self, V, mu=None):
        assert V in self.range

        if self.functional:
            U = super().apply_adjoint(V, mu=mu)
            return self.source.from_numpy(U.to_numpy())

        adj_op = NumpyMatrixOperator(self.matrix).H

        U = [adj_op.apply(adj_op.source.make_array(v._array)).to_numpy().ravel() for v in V._list]

        if self.vector:
            return self.source.make_array(np.array(U)) if len(U) > 0 else self.source.empty()
        else:
            return self.source.from_numpy(U)

    def apply_inverse(self, V, mu=None, least_squares=False):
        assert V in self.range
        assert not self.functional and not self.vector

        if V.dim == 0:
            if self.source.dim == 0 and least_squares:
                return self.source.make_array([np.zeros(0) for _ in range(len(V))])
            else:
                raise InversionError

        op = NumpyMatrixOperator(self.matrix, solver_options=self.solver_options)

        return self.source.make_array([op.apply_inverse(NumpyVectorSpace.make_array(v._array),
                                                        least_squares=least_squares).to_numpy().ravel()
                                       for v in V._list])

    def as_range_array(self, mu=None):
        assert not self.sparse
        return self.range.make_array(list(self.matrix.T.copy()))

    def as_source_array(self, mu=None):
        assert not self.sparse
        return self.source.make_array(list(self.matrix.copy()))

    def _assemble_lincomb(self, operators, coefficients, identity_shift=0., solver_options=None, name=None):
        lincomb = super()._assemble_lincomb(operators, coefficients, identity_shift)
        if lincomb is None:
            return None
        else:
            return NumpyListVectorArrayMatrixOperator(lincomb.matrix, source_id=self.source.id, range_id=self.range.id,
                                                      solver_options=solver_options, name=name)
Beispiel #10
0
def numpy_list_vector_array_factory(length, dim, seed):
    np.random.seed(seed)
    return NumpyListVectorSpace.from_numpy(np.random.random((length, dim)))