BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
armadillo_helpers.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 #ifndef bempp_armadillo_helpers_hpp
21 #define bempp_armadillo_helpers_hpp
22 
23 #include "../common/common.hpp"
24 
25 
26 #include "../common/armadillo_fwd.hpp"
27 #include <dune/common/fvector.hh>
28 
29 // Internal implementations for general Armadillo objects
30 template<typename M, typename T, int size>
31 bool _armadillo_fieldvector_compare(const M& x, const Dune::FieldVector<T, size>& y)
32 {
33  if (x.n_rows != size)
34  return false;
35  for (int i = 0; i < size; ++i)
36  if (x(i) != y[i])
37  return false;
38  return true;
39 }
40 
41 template<typename M, typename T, int rows, int cols>
42 bool _armadillo_fieldmatrix_compare(const M& x, const Dune::FieldMatrix<T, rows, cols>& y)
43 {
44  if (x.n_rows != rows || x.n_cols != cols)
45  return false;
46  for (int j = 0; j < cols; ++j)
47  for (int i = 0; i < rows; ++i)
48  if (x(i,j) != y[i][j])
49  return false;
50  return true;
51 }
52 
53 template <typename T, int size>
54 bool operator==(const arma::Col<T>& x, const Dune::FieldVector<T, size>& y)
55 {
56  return _armadillo_fieldvector_compare(x, y);
57 }
58 
59 template <typename T, int size>
60 bool operator==(const Dune::FieldVector<T, size>& x, const arma::Col<T>& y)
61 {
62  return _armadillo_fieldvector_compare(y, x);
63 }
64 
65 template <typename T, int size>
66 bool operator==(const arma::subview_col<T>& x, const Dune::FieldVector<T, size>& y)
67 {
68  return _armadillo_fieldvector_compare(x, y);
69 }
70 
71 template <typename T, int size>
72 bool operator==(const Dune::FieldVector<T, size>& x, const arma::subview_col<T>& y)
73 {
74  return _armadillo_fieldvector_compare(y, x);
75 }
76 
77 template <typename T, int rows, int cols>
78 bool operator==(const arma::Mat<T>& x, const Dune::FieldMatrix<T, rows, cols>& y)
79 {
80  return _armadillo_fieldmatrix_compare(x, y);
81 }
82 
83 template <typename T, int rows, int cols>
84 bool operator==(const Dune::FieldMatrix<T, rows, cols>& x, const arma::Mat<T>& y)
85 {
86  return _armadillo_fieldmatrix_compare(y, x);
87 }
88 
89 
90 // For BOOST_CHECK_EQUAL to work, we need to copy these comparison operators
91 // to the boost::test_tools:tt_detail namespace...
92 
93 namespace boost
94 {
95 namespace test_tools
96 {
97 namespace tt_detail
98 {
99 
100 template <typename T, int size>
101 bool operator==(const ::arma::Col<T>& x, const ::Dune::FieldVector<T, size>& y)
102 {
103  return ::operator==(x, y);
104 }
105 
106 template <typename T, int size>
107 bool operator==(const ::Dune::FieldVector<T, size>& x, const ::arma::Col<T>& y)
108 {
109  return ::operator==(x, y);
110 }
111 
112 template <typename T, int size>
113 bool operator==(const arma::subview_col<T>& x, const Dune::FieldVector<T, size>& y)
114 {
115  return ::operator==(x, y);
116 }
117 
118 template <typename T, int size>
119 bool operator==(const Dune::FieldVector<T, size>& x, const arma::subview_col<T>& y)
120 {
121  return ::operator==(x, y);
122 }
123 
124 template <typename T, int rows, int cols>
125 bool operator==(const arma::Mat<T>& x, const Dune::FieldMatrix<T, rows, cols>& y)
126 {
127  return ::operator==(x, y);
128 }
129 
130 template <typename T, int rows, int cols>
131 bool operator==(const Dune::FieldMatrix<T, rows, cols>& x, const arma::Mat<T>& y)
132 {
133  return ::operator==(x, y);
134 }
135 
136 } // namespace tt_detail
137 } // namespace test_tools
138 } // namespace boost
139 
140 #endif