Debian Codegraph @

packages/anasazi/src-rbgen/RBGen_Filter.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Anasazi: Block Eigensolvers Package
4 //
5 // Copyright 2004 NTESS and the Anasazi contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef RBGEN_FILTER_H
11 #define RBGEN_FILTER_H
12
13 #include "Teuchos_ScalarTraits.hpp"
14 #include "Teuchos_RCP.hpp"
15
16 // finish: amend filter() to take a pointer to the IncSVDPOD object
17
18 namespace RBGen {
19
20 //! \brief Enumerated type to list the selection criteria for singular value filters.
21 enum SortType {
22 LARGEST, /*!< Select the largest singular values */
23 SMALLEST /*!< Select the smallest singular values */
24 };
25
26 //! Class for selecting desired singular values
27 /*!
28 *
29 */
30 template <class ScalarType>
31 class Filter {
32 public:
33
34 //! @name Constructor/Destructor.
35 //@{
36
37 //! Default constructor.
38 Filter(SortType which = LARGEST) {};
39
40 //! Destructor.
41 virtual ~Filter() {};
42 //@}
43
44 //! @name Filter Method
45 //@{
46
47 //! Selects targetted singular values.
48
49 /*!
50 * This method accepts a list of singular values (in decreasing order, as returned by GESVD) and
51 * returns a list of indices consisting of the values
52 * allowed through the filter.
53 *
54 * @param svals [in] Singular values under consideration by the filter.
55 * \returns A vector of ints, corresponding to the indices of the targetted singular values.
56 * The indices are in ascending order and employ zero-based indexing.
57 */
58 virtual std::vector<int> filter(const std::vector<ScalarType> &svals) = 0;
59 //@}
60 };
61
62 //! Range-based filter
63 template <class ScalarType>
64 class RangeFilter : public Filter<ScalarType> {
65 public:
66 //! @name Constructor/Destructor.
67 //@{
68
69 //! Default constructor.
70 RangeFilter(SortType which, int minRank = 1, int maxRank = 1)
71 : which_(which) {
72 TEUCHOS_TEST_FOR_EXCEPTION(minRank < 1,std::invalid_argument,"RangeFilter: minRank must be > 1");
73 TEUCHOS_TEST_FOR_EXCEPTION(maxRank < 1,std::invalid_argument,"RangeFilter: maxRank must be > 1");
74 minRank_ = minRank;
75 maxRank_ = maxRank;
76 };
77
78 //! Destructor.
79 virtual ~RangeFilter() {};
80 //@}
81
82 //! @name Filter Method
83 //@{
84
85 //! Pass at most maxRank and at most minRank values through the filter.
86 /*!
87 * \note It is assumed that svals are sorted in decreasing order.
88 */
89 std::vector<int> filter(const std::vector<ScalarType> &svals) {
90 int num = (unsigned int)svals.size();
91 if (num > maxRank_) num = maxRank_;
92 else if (minRank_ < num) num = minRank_;
93 std::vector<int> ret;
94 ret.reserve(num);
95 if (LARGEST == which_) {
96 for (int i=0; i<num; ++i) {
97 ret.push_back(i);
98 }
99 }
100 else if (SMALLEST == which_) {
101 for (unsigned int i=svals.size()-num; i<svals.size(); ++i) {
102 ret.push_back(i);
103 }
104 }
105 return ret;
106 }
107 //@}
108
109 private:
110 SortType which_;
111 int minRank_;
112 int maxRank_;
113 };
114
115 //! Threshold-based filter
116 template <class ScalarType>
117 class ThreshFilter : public Filter<ScalarType> {
118 public:
119 //! @name Constructor/Destructor.
120 //@{
121
122 //! Default constructor.
123 /*!
124 * The threshold definition depends on \c which:
125 * - ::LARGEST: value \c cand passes the filter if \f$cand \geq tval\f$
126 * - ::SMALLEST: value \c cand passes the filter if \f$cand \leq tval\f$
127 * where \c tval depends on \c absthresh:
128 * - \c false:
129 * - \f$tval \doteq thresh \cdot max(svals)\f$
130 * - \f$tval \doteq thresh \cdot min(svals)\f$
131 * - \c true: \f$tval \doteq thresh\f$
132 */
133 ThreshFilter(SortType which, bool absthresh,
134 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType thresh)
135 : which_(which), absthresh_(absthresh) {
136 TEUCHOS_TEST_FOR_EXCEPTION(thresh < Teuchos::ScalarTraits<ScalarType>::zero(),
137 std::invalid_argument,"ThreshFilter: minRank must be > 1");
138 thresh_ = thresh;
139 };
140
141 //! Destructor.
142 virtual ~ThreshFilter() {};
143 //@}
144
145 //! @name Filter Method
146 //@{
147
148 //! Return an index for the singular values falling within the threshold.
149
150 /*!
151 * \note It is assumed that svals are sorted in decreasing order.
152 */
153 std::vector<int> filter(const std::vector<ScalarType> &svals) {
154 const int last = (unsigned int)svals.size() - 1;
155
156 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType tval;
157 if (absthresh_) {
158 tval = thresh_;
159 }
160 else {
161 if (LARGEST == which_) {
162 tval = thresh_ * svals[0];
163 }
164 else if (SMALLEST == which_) {
165 tval = thresh_ * svals[last];
166 }
167 }
168
169 std::vector<int> ret;
170 if (LARGEST == which_) {
171 const int num = find(svals.begin(),svals.end(),
172 [=] (const ScalarType sval) {
173 return sval < tval;
174 }) - svals.begin();
175 ret.resize(num);
176 for (int i=0; i<num; ++i) {
177 ret.push_back(i);
178 }
179 }
180 else if (SMALLEST == which_) {
181 const int num = svals.end() - find(svals.begin(),svals.end(),
182 [=] (const ScalarType sval) {
183 return sval < tval;
184 }) + 1;
185 ret.resize(num);
186 for (int i=last-num+1; i<last; ++i) {
187 ret.push_back(i);
188 }
189 }
190 return ret;
191 }
192 //@}
193
194 private:
195 SortType which_;
196 bool absthresh_;
197 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType thresh_;
198 };
199
200 //! Composite filter
201 template <class ScalarType>
202 class CompFilter : public Filter<ScalarType> {
203 public:
204
205 enum CompType {
206 AND,
207 OR
208 };
209
210 //! @name Constructor/Destructor.
211 //@{
212
213 //! Default constructor.
214 CompFilter(CompType andor,
215 const Teuchos::RCP<Filter<ScalarType> > &f1,
216 const Teuchos::RCP<Filter<ScalarType> > &f2 )
217 : andor_(andor), f1_(f1), f2_(f2) {
218 TEUCHOS_TEST_FOR_EXCEPTION(f1_ == Teuchos::null || f2_ == Teuchos::null,
219 std::invalid_argument,"CompFilter: Component filters must be non-null.");
220 };
221
222 //! Destructor.
223 virtual ~CompFilter() {};
224 //@}
225
226 //! @name Filter Method
227 //@{
228
229 //! \brief
230 /*! \note It is assumed that svals are sorted in decreasing order.
231 */
232 std::vector<int> filter(const std::vector<ScalarType> &svals) {
233 std::vector<int> ind1 = f1_->filter(svals);
234 std::vector<int> ind2 = f2_->filter(svals);
235 std::vector<int> ret;
236 if (AND == andor_) {
237 set_intersection(ind1.begin(),ind1.end(),ind2.begin(),ind2.end(),ret.begin());
238 }
239 else if (OR == andor_) {
240 set_union(ind1.begin(),ind1.end(),ind2.begin(),ind2.end(),ret.begin());
241 }
242 return ret;
243 }
244 //@}
245
246 private:
247 CompType andor_;
248 Teuchos::RCP<Filter<ScalarType> > f1_, f2_;
249 };
250
251 }
252 #endif