1 #ifndef EIGENVECTORALGEBRA_H_
2 #define EIGENVECTORALGEBRA_H_
6 #include <boost/none.hpp>
7 #include <boost/any.hpp>
12 class EigenVectorAlgebra {
17 virtual ~EigenVectorAlgebra();
24 static bool IsEigenVector(std::type_info
const& obj_type);
31 static bool IsZero(boost::any
const& obj);
38 static unsigned int Size(boost::any
const& vec);
45 static double Norm(boost::any
const& vec);
53 static double InnerProduct(boost::any
const& vec1, boost::any
const& vec2);
61 static boost::any OuterProduct(boost::any
const& vec1, boost::any
const& vec2);
70 static boost::any AccessElement(boost::any
const& obj,
unsigned int const i);
79 static boost::any Identity(std::type_info
const& type,
unsigned int const rows,
unsigned int const cols);
87 static boost::any Add(boost::any
const& in0, boost::any
const& in1);
95 static boost::any Subtract(boost::any
const& in0, boost::any
const& in1);
103 static boost::any ScalarMultiply(boost::any
const& in0, boost::any
const& in1);
111 static boost::any Apply(boost::any
const& A, boost::any
const& x);
119 static boost::any ApplyInverse(boost::any
const& A, boost::any
const& x);
126 static boost::any Zero(std::type_info
const& type,
unsigned int const size);
133 static boost::any SquareRoot(boost::any
const& obj);
140 static double LogDeterminate(boost::any
const& obj);
149 template<
typename EigenType>
150 static inline bool IsZero(boost::any
const& obj) {
151 const EigenType&
v = boost::any_cast<EigenType const&>(obj);
153 return (
v.array()==EigenType::Zero(
v.size()).array()).all();
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());
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);
182 return (mattype)(x1*x2.transpose());
192 template<
typename vectype>
193 static inline boost::any AccessElement(boost::any
const& vec,
unsigned int const i) {
195 const vectype& vecref = boost::any_cast<const vectype&>(vec);
198 assert(i<vecref.size());
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());
216 return (type0)(x0+x1);
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());
231 return (type0)(x0-x1);
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);
245 return (type1)(x0 * x1);
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());
260 return (vectype)(mat.asDiagonal()*vec);
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());
275 return (vectype)((1/mat.array()).matrix().asDiagonal()*vec);
283 template<
typename type>
284 inline static double LogDeterminate(boost::any
const& obj) {
285 const type& mat = boost::any_cast<type const&>(obj);
287 return mat.array().log().sum();