MUQ  0.4.3
EigenVectorAlgebra.h
Go to the documentation of this file.
1 #ifndef EIGENVECTORALGEBRA_H_
2 #define EIGENVECTORALGEBRA_H_
3 
4 #include <Eigen/Core>
5 
6 #include <boost/none.hpp>
7 #include <boost/any.hpp>
8 
9 namespace muq {
10  namespace Modeling {
12  class EigenVectorAlgebra {
13  public:
14 
15  EigenVectorAlgebra();
16 
17  virtual ~EigenVectorAlgebra();
18 
20 
24  static bool IsEigenVector(std::type_info const& obj_type);
25 
27 
31  static bool IsZero(boost::any const& obj);
32 
34 
38  static unsigned int Size(boost::any const& vec);
39 
41 
45  static double Norm(boost::any const& vec);
46 
48 
53  static double InnerProduct(boost::any const& vec1, boost::any const& vec2);
54 
56 
61  static boost::any OuterProduct(boost::any const& vec1, boost::any const& vec2);
62 
64 
70  static boost::any AccessElement(boost::any const& obj, unsigned int const i);
71 
73 
79  static boost::any Identity(std::type_info const& type, unsigned int const rows, unsigned int const cols);
80 
82 
87  static boost::any Add(boost::any const& in0, boost::any const& in1);
88 
90 
95  static boost::any Subtract(boost::any const& in0, boost::any const& in1);
96 
98 
103  static boost::any ScalarMultiply(boost::any const& in0, boost::any const& in1);
104 
106 
111  static boost::any Apply(boost::any const& A, boost::any const& x);
112 
114 
119  static boost::any ApplyInverse(boost::any const& A, boost::any const& x);
120 
122 
126  static boost::any Zero(std::type_info const& type, unsigned int const size);
127 
129 
133  static boost::any SquareRoot(boost::any const& obj);
134 
136 
140  static double LogDeterminate(boost::any const& obj);
141 
142  private:
143 
145 
149  template<typename EigenType>
150  static inline bool IsZero(boost::any const& obj) {
151  const EigenType& v = boost::any_cast<EigenType const&>(obj);
152 
153  return (v.array()==EigenType::Zero(v.size()).array()).all();
154  }
155 
157 
162  template<typename EigenType1, typename EigenType2>
163  static inline double InnerProduct(boost::any const& vec1, boost::any const& vec2) {
164  const EigenType1& x1 = boost::any_cast<EigenType1 const&>(vec1);
165  const EigenType2& x2 = boost::any_cast<EigenType2 const&>(vec2);
166  assert(x1.size()==x2.size());
167 
168  return x1.dot(x2);
169  }
170 
172 
177  template<typename mattype, typename EigenType1, typename EigenType2>
178  static inline boost::any OuterProduct(boost::any const& vec1, boost::any const& vec2) {
179  const EigenType1& x1 = boost::any_cast<EigenType1 const&>(vec1);
180  const EigenType2& x2 = boost::any_cast<EigenType2 const&>(vec2);
181 
182  return (mattype)(x1*x2.transpose());
183  }
184 
186 
192  template<typename vectype>
193  static inline boost::any AccessElement(boost::any const& vec, unsigned int const i) {
194  // get a constant reference to the vector
195  const vectype& vecref = boost::any_cast<const vectype&>(vec);
196 
197  // check the size
198  assert(i<vecref.size());
199 
200  // return ith element
201  return vecref(i);
202  }
203 
205 
210  template<typename type0, typename type1>
211  static inline boost::any Add(boost::any const& in0, boost::any const& in1) {
212  const type0& x0 = boost::any_cast<type0 const&>(in0);
213  const type1& x1 = boost::any_cast<type1 const&>(in1);
214  assert(x0.size()==x1.size());
215 
216  return (type0)(x0+x1);
217  }
218 
220 
225  template<typename type0, typename type1>
226  static inline boost::any Subtract(boost::any const& in0, boost::any const& in1) {
227  const type0& x0 = boost::any_cast<type0 const&>(in0);
228  const type1& x1 = boost::any_cast<type1 const&>(in1);
229  assert(x0.size()==x1.size());
230 
231  return (type0)(x0-x1);
232  }
233 
235 
240  template<typename type0, typename type1>
241  static inline boost::any ScalarMultiply(boost::any const& in0, boost::any const& in1) {
242  const type0 x0 = boost::any_cast<type0 const>(in0);
243  const type1 x1 = boost::any_cast<type1 const>(in1);
244 
245  return (type1)(x0 * x1);
246  }
247 
249 
254  template<typename mattype, typename vectype>
255  static inline boost::any Apply(boost::any const& A, boost::any const& x) {
256  const mattype& mat = boost::any_cast<mattype const&>(A);
257  const vectype& vec = boost::any_cast<vectype const&>(x);
258  assert(mat.size()==vec.size());
259 
260  return (vectype)(mat.asDiagonal()*vec);
261  }
262 
264 
269  template<typename mattype, typename vectype>
270  static inline boost::any ApplyInverse(boost::any const& A, boost::any const& x) {
271  const mattype& mat = boost::any_cast<mattype const&>(A);
272  const vectype& vec = boost::any_cast<vectype const&>(x);
273  assert(mat.size()==vec.size());
274 
275  return (vectype)((1/mat.array()).matrix().asDiagonal()*vec);
276  }
277 
279 
283  template<typename type>
284  inline static double LogDeterminate(boost::any const& obj) {
285  const type& mat = boost::any_cast<type const&>(obj);
286 
287  return mat.array().log().sum();
288  }
289  };
290  } // namespace Modeling
291 } // namespace muq
292 
293 #endif
int int diyfp diyfp v
Definition: json.h:15163