KASKADE 7 development version
Loading...
Searching...
No Matches
matrixOps.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2024-2025 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef MATRIXOPS_HH
14#define MATRIXOPS_HH
15
16#include <algorithm>
17#include <tuple>
18#include <vector>
19
20namespace Dune
21{
22 // forward declaration for istl/bcrsmatrix.hh
23 template <class Entry, class Allocator> class BCRSMatrix;
24}
25
26namespace Kaskade
27{
29 // forward declarations such that we need not include both dense and sparse matrix headers
30 template<class K> class DynamicMatrix;
31 template <class Entry, class Index> class NumaBCRSMatrix;
32 template <class Index> class NumaCRSPatternCreator;
33
34 namespace MatrixOpsDetail
35 {
36 template <class Target>
37 class Submatrix;
38
39 template <class Entry, class Allocator>
40 class Submatrix<Dune::BCRSMatrix<Entry,Allocator>>
41 {
42 public:
43 template <class Source, class RowIndices, class ColIndices>
44 static Dune::BCRSMatrix<Entry,Allocator> apply(Source const& A,
45 RowIndices const& ri, ColIndices const& ci)
46 {
48 using Index = typename Source::size_type;
49
50 // Extract nonzero entries in the requested row and column ranges and store them
51 // in sorted triplet format.
52 std::vector<std::tuple<Index,Index>> a;
53 std::vector<Entry> av;
54 for (Index r=0; r<ri.size(); ++r)
55 {
56 auto row = A[ri[r]];
57 for (Index c=0; c<ci.size(); ++c)
58 {
59 auto coli = row.find(ci[c]);
60 if (coli != row.end())
61 {
62 a.push_back(std::make_tuple(r,c));
63 av.push_back(*coli);
64 }
65 }
66 }
67
68 // Define the sparsity pattern.
69 Target S(ri.size(),ci.size(),a.size(),Target::row_wise);
70 using CreateIter = typename Target::CreateIterator;
71 auto ai = begin(a);
72 for(CreateIter row=S.createbegin(); row!=S.createend(); ++row)
73 for ( ; ai!=end(a) && std::get<0>(*ai)==row.index(); ++ai)
74 row.insert(std::get<1>(*ai));
75
76 // Enter the matrix entries
77 auto avi = begin(av);
78 for (auto r=S.begin(); r!=S.end(); ++r)
79 for (auto c=r->begin(); c!=r->end(); ++c, ++avi)
80 *c = *avi;
81
82 return S;
83 }
84 };
85
86
87 template <class Entry, class Index>
88 class Submatrix<NumaBCRSMatrix<Entry,Index>>
89 {
90 public:
91 template <class Source, class RowIndices, class ColIndices>
92 static NumaBCRSMatrix<Entry,Index> apply(Source const& A,
93 RowIndices const& ri, ColIndices const& ci)
94 {
95 NumaCRSPatternCreator<Index> creator(ri.size(), ci.size());
96
97 // define sparsity pattern
98 // WARNING: This has O(n*m) run time if the target matrix is n x m.
99 // TODO: Devise a more sophisticated algorithm with O(nnz+n) run time.
100 for(Index r=0; r<ri.size(); ++r) // iterate over rows in ri
101 {
102 auto row = A[ri[r]];
103 for (Index c=0; c<ci.size(); ++c) // iterate over columns in ci
104 if (auto coli = row.find(ci[c]); coli != row.end())
105 creator.addElement(r,c);
106 }
107
108 NumaBCRSMatrix<Entry,Index> S(creator);
109
110 // Enter the matrix entries
111 for (auto r=S.begin(); r!=S.end(); ++r)
112 for (auto c=r->begin(); c!=r->end(); ++c)
113 *c = A[ri[r.index()]][ci[c.index()]];
114
115 return S;
116 }
117 };
118
119 template <class Entry>
120 class Submatrix<DynamicMatrix<Entry>>
121 {
122 public:
123 template <class RowIdx, class ColIdx>
124 static DynamicMatrix<Entry> apply(DynamicMatrix<Entry> const& A,
125 RowIdx const& rows, ColIdx const& cols)
126 {
127 assert(*std::min_element(begin(rows),end(rows))>=0);
128 assert(*std::min_element(begin(cols),end(cols))>=0);
129 assert(*std::max_element(begin(rows),end(rows))<A.N());
130 assert(*std::max_element(begin(cols),end(cols))<A.M());
131
132 DynamicMatrix<Entry> S(size(rows),size(cols));
133 auto ci = begin(cols);
134 for (int j=0; ci!=end(cols); ++j, ++ci)
135 {
136 auto ri = begin(rows);
137 for (int i=0; ri!=end(rows); ++i, ++ri)
138 S[i][j] = A[*ri][*ci];
139 }
140 return S;
141 }
142
143 template <class Source, class RowIndices, class ColIndices>
144 static DynamicMatrix<Entry> apply(Source const& A,
145 RowIndices const& ri, ColIndices const& ci)
146 {
147 DynamicMatrix<Entry> S(ri.size(),ci.size());
148 for (int r=0; r<ri.size(); ++r)
149 {
150 auto row = A[ri[r]];
151
152 for (int c=0; c<ci.size(); ++c)
153 if (auto coli = row.find(ci[c]); coli != row.end())
154 S[r][c] = *coli;
155 }
156
157 return S;
158 }
159 };
160 }
162
182 template <class Target, class Source, class RowIndices, class ColIndices>
183 Target submatrix(Source const& A, RowIndices const& ri, ColIndices const& ci)
184 {
185 return MatrixOpsDetail::Submatrix<Target>::apply(A,ri,ci);
186 }
187
188
189
190}
191
192#endif
Target submatrix(Source const &A, RowIndices const &ri, ColIndices const &ci)
extracts a sparse or dense submatrix of
Definition matrixOps.hh:183