Created
May 9, 2014 04:24
-
-
Save lambday/9dbda55fa7af5d1a34cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <shogun/base/init.h> | |
#include <shogun/lib/SGVector.h> | |
#include <Eigen/Eigen> | |
using namespace shogun; | |
using namespace std; | |
using namespace Eigen; | |
template <template <class,int...> class Vector,class T,int... Info> | |
struct type_traits; | |
template <> template <class T> | |
struct type_traits<SGVector, T> | |
{ | |
typedef typename SGVector<T>::value_type value_type; | |
}; | |
template <> template <class T, int... Info> | |
struct type_traits<Matrix, T, Info...> | |
{ | |
typedef typename Matrix<T,Info...>::Scalar value_type; | |
}; | |
template <template <class, int...> class Vector, class T, int... Info> | |
struct dot_impl | |
{ | |
static typename type_traits<Vector,T,Info...>::value_type compute(Vector<T,Info...> a, Vector<T,Info...> b); | |
}; | |
template <> template <class T> | |
struct dot_impl<SGVector, T> | |
{ | |
static T compute(SGVector<T> a, SGVector<T> b) | |
{ | |
typedef Matrix<T, Dynamic, 1> VectorXt; | |
Map<VectorXt> av(a.vector, a.vlen); | |
Map<VectorXt> bv(b.vector, b.vlen); | |
return av.dot(bv); | |
} | |
}; | |
template <> template <class T, int... Info> | |
struct dot_impl<Matrix,T,Info...> | |
{ | |
static T compute(Matrix<T,Info...> a, Matrix<T,Info...> b) | |
{ | |
return a.dot(b); | |
} | |
}; | |
namespace linalg | |
{ | |
template <template <class,int...> class Vector, class T, int... Info> | |
typename type_traits<Vector,T,Info...>::value_type dot(Vector<T,Info...> a, Vector<T,Info...> b) | |
{ | |
return dot_impl<Vector,T,Info...>::compute(a, b); | |
} | |
} | |
int main() | |
{ | |
init_shogun_with_defaults(); | |
SGVector<float64_t> a(10); | |
a.set_const(10); | |
cout << linalg::dot(a, a) << endl; | |
VectorXf b(10); | |
b=VectorXf::Random(10); | |
cout << linalg::dot(b, b) << endl; | |
Vector3d c=Vector3d::Random(); | |
cout << linalg::dot(c, c) << endl; | |
SGVector<int64_t> d(10); | |
d.set_const(10); | |
cout << linalg::dot(d, d) << endl; | |
exit_shogun(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment