pymor.algorithms.to_matrix

Classes

ToMatrixRules

RuleTable for the to_matrix algorithm.

Functions

to_matrix

Convert a linear Operator to a matrix.

class pymor.algorithms.to_matrix.ToMatrixRules(format, mu)[source]

Bases: RuleTable

RuleTable for the to_matrix algorithm.

action_AdjointOperator = @match_class(AdjointOperator)     def action_AdjointOperator(self, op):         format = self.format         res = self.apply(op.operator).T.conj()         if op.range_product is not None:             range_product = self.apply(op.range_product)             if format is None and not sps.issparse(res) and sps.issparse(range_product):                 res = range_product.T.dot(res.T).T             else:                 res = res.dot(range_product)         if op.source_product is not None:             source_product = self.apply(op.source_product)             if not sps.issparse(source_product):                 res = spla.solve(source_product, res)             else:                 source_product = source_product.tocsc()                 if sps.issparse(res):                     res = res.tocsc()                 res = spsla.spsolve(source_product, res)         if format is not None and format != 'dense':             res = getattr(sps, format + '_matrix')(res)         return res [source]
action_BlockOperator = @match_class(BlockOperatorBase)     def action_BlockOperator(self, op):         format = self.format         op_blocks = op.blocks         mat_blocks = [[] for i in range(op.num_range_blocks)]         is_dense = True         for i in range(op.num_range_blocks):             for j in range(op.num_source_blocks):                 mat_ij = self.apply(op_blocks[i, j])                 if sps.issparse(mat_ij):                     is_dense = False                 mat_blocks[i].append(mat_ij)         if format is None and is_dense or format == 'dense':             return np.block(mat_blocks)         else:             return sps.bmat(mat_blocks, format=format) [source]
action_ComponentProjectionOperator = @match_class(ComponentProjectionOperator)     def action_ComponentProjectionOperator(self, op):         format = self.format         if format == 'dense':             res = np.zeros((op.range.dim, op.source.dim))             for i, j in enumerate(op.components):                 res[i, j] = 1         else:             data = np.ones((op.range.dim,))             i = np.arange(op.range.dim)             j = op.components             res = sps.coo_matrix((data, (i, j)), shape=(op.range.dim, op.source.dim))             res = res.asformat(format if format else 'csc')         return res [source]
action_ConcatenationOperator = @match_class(ConcatenationOperator)     def action_ConcatenationOperator(self, op):         mats = [self.apply(o) for o in op.operators]         while len(mats) > 1:             if self.format is None and not sps.issparse(mats[-2]) and sps.issparse(mats[-1]):                 mats = mats[:-2] + [mats[-1].T.dot(mats[-2].T).T]             else:                 mats = mats[:-2] + [mats[-2].dot(mats[-1])]         return mats[0] [source]
action_IdentityOperator = @match_class(IdentityOperator)     def action_IdentityOperator(self, op):         format = self.format         if format == 'dense':             return np.eye(op.source.dim)         else:             return sps.eye(op.source.dim, format=format if format else 'csc') [source]
action_LincombOperator = @match_class(LincombOperator)     def action_LincombOperator(self, op):         op_coefficients = op.evaluate_coefficients(self.mu)         res = op_coefficients[0] * self.apply(op.operators[0])         for i in range(1, len(op.operators)):             res = res + op_coefficients[i] * self.apply(op.operators[i])         return res [source]
action_LowRankOperator = @match_class(LowRankOperator)     def action_LowRankOperator(self, op):         format = self.format         if not op.inverted:             res = op.left.to_numpy() @ op.core @ op.right.to_numpy().T         else:             res = op.left.to_numpy() @ spla.solve(op.core, op.right.to_numpy().T)         if format is not None and format != 'dense':             res = getattr(sps, format + '_matrix')(res)         return res [source]
action_LowRankUpdatedOperator = @match_class(LowRankUpdatedOperator)     def action_LowRankUpdatedOperator(self, op):         return op.coeff * self.apply(op.operator) + op.lr_coeff * self.apply(op.lr_operator) [source]
action_NumpyCirculantOperator = @match_class(NumpyCirculantOperator)     def action_NumpyCirculantOperator(self, op):         format = self.format         if format in (None, 'dense'):             p, m = op.c.shape[1:]             op_mat = np.zeros((op.range.dim, op.source.dim), dtype=op._arr.dtype)             for (i, j) in np.ndindex((p, m)):                 op_mat[i::p, j::m] = spla.circulant(op.c[:, i, j])             return op_mat         else:             raise NotImplementedError [source]
action_NumpyHankelOperator = @match_class(NumpyHankelOperator)     def action_NumpyHankelOperator(self, op):         format = self.format         if format in (None, 'dense'):             p, m = op.c.shape[1:]             op_mat = np.zeros((op.range.dim, op.source.dim), dtype=op._circulant._arr.dtype)             for (i, j) in np.ndindex((p, m)):                 op_mat[i::p, j::m] = spla.hankel(op.c[:, i, j], r=op.r[:, i, j])             return op_mat         else:             raise NotImplementedError [source]
action_NumpyMatrixOperator = @match_class(NumpyMatrixOperator)     def action_NumpyMatrixOperator(self, op):         format = self.format         if format is None:             return op.matrix         elif format == 'dense':             if not op.sparse:                 return op.matrix             else:                 return op.matrix.toarray()         else:             if not op.sparse:                 return getattr(sps, format + '_matrix')(op.matrix)             else:                 return op.matrix.asformat(format) [source]
action_NumpyToeplitzOperator = @match_class(NumpyToeplitzOperator)     def action_NumpyToeplitzOperator(self, op):         format = self.format         if format in (None, 'dense'):             p, m = op.c.shape[1:]             op_mat = np.zeros((op.range.dim, op.source.dim), dtype=op._circulant._arr.dtype)             for (i, j) in np.ndindex((p, m)):                 op_mat[i::p, j::m] = spla.toeplitz(op.c[:, i, j], r=op.r[:, i, j])             return op_mat         else:             raise NotImplementedError [source]
action_VectorArrayOperator = @match_class(VectorArrayOperator)     def action_VectorArrayOperator(self, op):         format = self.format         res = op.array.conj().to_numpy().T if op.adjoint else op.array.to_numpy()         if format is not None and format != 'dense':             res = getattr(sps, format + '_matrix')(res)         return res [source]
action_ZeroOperator = @match_class(ZeroOperator)     def action_ZeroOperator(self, op):         format = self.format         if format is None:             return sps.csc_matrix((op.range.dim, op.source.dim))         elif format == 'dense':             return np.zeros((op.range.dim, op.source.dim))         else:             return getattr(sps, format + '_matrix')((op.range.dim, op.source.dim)) [source]
action_as_array = @match_always     def action_as_array(self, op):         if op.source.dim < as_array_max_length():             if not isinstance(op.source, NumpyVectorSpace):                 op = op @ NumpyConversionOperator(op.source, 'from_numpy')             try:                 return op.as_range_array(mu=self.mu).to_numpy()             except NotImplementedError:                 pass         if op.range.dim < as_array_max_length():             if not isinstance(op.range, NumpyVectorSpace):                 op = NumpyConversionOperator(op.range, 'to_numpy') @ op             try:                 return op.as_source_array(mu=self.mu).to_numpy().conj().T             except NotImplementedError:                 pass         raise RuleNotMatchingError [source]
rules: list[rule] = [    @match_class(NumpyHankelOperator)     def action_NumpyHankelOperator(self, op):         format = self.format         if format in (None, 'dense'):             p, m = op.c.shape[1:]             op_mat = np.zeros((op.range.dim, op.source.dim), dtype=op._circulant._arr.dtype)             for (i, j) in np.ndindex((p, m)):                 op_mat[i::p, j::m] = spla.hankel(op.c[:, i, j], r=op.r[:, i, j])             return op_mat         else:             raise NotImplementedError ,     @match_class(NumpyToeplitzOperator)     def action_NumpyToeplitzOperator(self, op):         format = self.format         if format in (None, 'dense'):             p, m = op.c.shape[1:]             op_mat = np.zeros((op.range.dim, op.source.dim), dtype=op._circulant._arr.dtype)             for (i, j) in np.ndindex((p, m)):                 op_mat[i::p, j::m] = spla.toeplitz(op.c[:, i, j], r=op.r[:, i, j])             return op_mat         else:             raise NotImplementedError ,     @match_class(NumpyCirculantOperator)     def action_NumpyCirculantOperator(self, op):         format = self.format         if format in (None, 'dense'):             p, m = op.c.shape[1:]             op_mat = np.zeros((op.range.dim, op.source.dim), dtype=op._arr.dtype)             for (i, j) in np.ndindex((p, m)):                 op_mat[i::p, j::m] = spla.circulant(op.c[:, i, j])             return op_mat         else:             raise NotImplementedError ,     @match_class(NumpyMatrixOperator)     def action_NumpyMatrixOperator(self, op):         format = self.format         if format is None:             return op.matrix         elif format == 'dense':             if not op.sparse:                 return op.matrix             else:                 return op.matrix.toarray()         else:             if not op.sparse:                 return getattr(sps, format + '_matrix')(op.matrix)             else:                 return op.matrix.asformat(format) ,     @match_class(BlockOperatorBase)     def action_BlockOperator(self, op):         format = self.format         op_blocks = op.blocks         mat_blocks = [[] for i in range(op.num_range_blocks)]         is_dense = True         for i in range(op.num_range_blocks):             for j in range(op.num_source_blocks):                 mat_ij = self.apply(op_blocks[i, j])                 if sps.issparse(mat_ij):                     is_dense = False                 mat_blocks[i].append(mat_ij)         if format is None and is_dense or format == 'dense':             return np.block(mat_blocks)         else:             return sps.bmat(mat_blocks, format=format) ,     @match_class(AdjointOperator)     def action_AdjointOperator(self, op):         format = self.format         res = self.apply(op.operator).T.conj()         if op.range_product is not None:             range_product = self.apply(op.range_product)             if format is None and not sps.issparse(res) and sps.issparse(range_product):                 res = range_product.T.dot(res.T).T             else:                 res = res.dot(range_product)         if op.source_product is not None:             source_product = self.apply(op.source_product)             if not sps.issparse(source_product):                 res = spla.solve(source_product, res)             else:                 source_product = source_product.tocsc()                 if sps.issparse(res):                     res = res.tocsc()                 res = spsla.spsolve(source_product, res)         if format is not None and format != 'dense':             res = getattr(sps, format + '_matrix')(res)         return res ,     @match_class(ComponentProjectionOperator)     def action_ComponentProjectionOperator(self, op):         format = self.format         if format == 'dense':             res = np.zeros((op.range.dim, op.source.dim))             for i, j in enumerate(op.components):                 res[i, j] = 1         else:             data = np.ones((op.range.dim,))             i = np.arange(op.range.dim)             j = op.components             res = sps.coo_matrix((data, (i, j)), shape=(op.range.dim, op.source.dim))             res = res.asformat(format if format else 'csc')         return res ,     @match_class(ConcatenationOperator)     def action_ConcatenationOperator(self, op):         mats = [self.apply(o) for o in op.operators]         while len(mats) > 1:             if self.format is None and not sps.issparse(mats[-2]) and sps.issparse(mats[-1]):                 mats = mats[:-2] + [mats[-1].T.dot(mats[-2].T).T]             else:                 mats = mats[:-2] + [mats[-2].dot(mats[-1])]         return mats[0] ,     @match_class(IdentityOperator)     def action_IdentityOperator(self, op):         format = self.format         if format == 'dense':             return np.eye(op.source.dim)         else:             return sps.eye(op.source.dim, format=format if format else 'csc') ,     @match_class(LincombOperator)     def action_LincombOperator(self, op):         op_coefficients = op.evaluate_coefficients(self.mu)         res = op_coefficients[0] * self.apply(op.operators[0])         for i in range(1, len(op.operators)):             res = res + op_coefficients[i] * self.apply(op.operators[i])         return res ,     @match_class(LowRankOperator)     def action_LowRankOperator(self, op):         format = self.format         if not op.inverted:             res = op.left.to_numpy() @ op.core @ op.right.to_numpy().T         else:             res = op.left.to_numpy() @ spla.solve(op.core, op.right.to_numpy().T)         if format is not None and format != 'dense':             res = getattr(sps, format + '_matrix')(res)         return res ,     @match_class(LowRankUpdatedOperator)     def action_LowRankUpdatedOperator(self, op):         return op.coeff * self.apply(op.operator) + op.lr_coeff * self.apply(op.lr_operator) ,     @match_class(VectorArrayOperator)     def action_VectorArrayOperator(self, op):         format = self.format         res = op.array.conj().to_numpy().T if op.adjoint else op.array.to_numpy()         if format is not None and format != 'dense':             res = getattr(sps, format + '_matrix')(res)         return res ,     @match_class(ZeroOperator)     def action_ZeroOperator(self, op):         format = self.format         if format is None:             return sps.csc_matrix((op.range.dim, op.source.dim))         elif format == 'dense':             return np.zeros((op.range.dim, op.source.dim))         else:             return getattr(sps, format + '_matrix')((op.range.dim, op.source.dim)) ,     @match_always     def action_as_array(self, op):         if op.source.dim < as_array_max_length():             if not isinstance(op.source, NumpyVectorSpace):                 op = op @ NumpyConversionOperator(op.source, 'from_numpy')             try:                 return op.as_range_array(mu=self.mu).to_numpy()             except NotImplementedError:                 pass         if op.range.dim < as_array_max_length():             if not isinstance(op.range, NumpyVectorSpace):                 op = NumpyConversionOperator(op.range, 'to_numpy') @ op             try:                 return op.as_source_array(mu=self.mu).to_numpy().conj().T             except NotImplementedError:                 pass         raise RuleNotMatchingError ][source]

list of all defined rules.

pymor.algorithms.to_matrix.to_matrix(op, format=None, mu=None)[source]

Convert a linear Operator to a matrix.

Parameters:
  • op – The Operator to convert.

  • format – Format of the resulting matrix: NumPy array if ‘dense’, otherwise the appropriate SciPy spmatrix. If None, a choice between dense and sparse format is automatically made.

  • mu – The parameter values for which to convert op.

Returns:

res – The matrix equivalent to op.