BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
index_permutation.hpp
1 // Copyright (C) 2011-2012 by the BEM++ Authors
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 
22 #ifndef bempp_index_permutation_hpp
23 #define bempp_index_permutation_hpp
24 
25 #include "../common/common.hpp"
26 
27 #include "../common/armadillo_fwd.hpp"
28 #include "../common/shared_ptr.hpp"
29 
30 class Epetra_CrsMatrix;
31 
32 namespace Bempp
33 {
34 
40 {
41 public:
42  IndexPermutation(const std::vector<unsigned int>& permutedIndices) :
43  m_permutedIndices(permutedIndices) {
44  }
45 
46  bool operator==(const IndexPermutation& other) const {
47  if (m_permutedIndices.size() != other.m_permutedIndices.size())
48  return false;
49  for (size_t i = 0; i < m_permutedIndices.size(); ++i)
50  if (m_permutedIndices[i] != other.m_permutedIndices[i])
51  return false;
52  return true;
53  }
54 
55  bool operator!=(const IndexPermutation& other) const {
56  return !operator==(other);
57  }
58 
59  const std::vector<unsigned int>& permutedIndices() const {
60  return m_permutedIndices;
61  }
62 
63  std::vector<unsigned int> unpermutedIndices() const {
64  std::vector<unsigned int> result(m_permutedIndices.size());
65  for (unsigned int o = 0; o < m_permutedIndices.size(); ++o)
66  result[m_permutedIndices[o]] = o;
67  return result;
68  }
69 
71  template <typename ValueType>
72  void permuteVector(const arma::Col<ValueType>& original,
73  arma::Col<ValueType>& permuted) const
74  {
75  const int dim = original.n_elem;
76  permuted.set_size(dim);
77  for (int i = 0; i < dim; ++i)
78  permuted(m_permutedIndices[i]) = original(i);
79  }
80 
82  template <typename ValueType>
83  void unpermuteVector(const arma::Col<ValueType>& permuted,
84  arma::Col<ValueType>& original) const
85  {
86  const int dim = permuted.n_elem;
87  original.set_size(dim);
88  for (int i = 0; i < dim; ++i)
89  original(i) = permuted(m_permutedIndices[i]);
90  }
91 
93  unsigned int permuted(unsigned int index) const {
94  return m_permutedIndices[index];
95  }
96 
98  size_t size() const {
99  return m_permutedIndices.size();
100  }
101 
102  shared_ptr<const Epetra_CrsMatrix> permutationMatrix() const;
103 
104 private:
105  const std::vector<unsigned int> m_permutedIndices;
106 };
107 
108 } // namespace Bempp
109 
110 #endif
size_t size() const
Return length of the vector of indices.
Definition: index_permutation.hpp:98
Permutation of indices.
Definition: index_permutation.hpp:39
unsigned int permuted(unsigned int index) const
Permute index.
Definition: index_permutation.hpp:93
void unpermuteVector(const arma::Col< ValueType > &permuted, arma::Col< ValueType > &original) const
Convert a vector from permuted to original ordering.
Definition: index_permutation.hpp:83
void permuteVector(const arma::Col< ValueType > &original, arma::Col< ValueType > &permuted) const
Convert a vector from original to permuted ordering.
Definition: index_permutation.hpp:72