Skip to content

Instantly share code, notes, and snippets.

@lambday
Created May 9, 2014 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambday/9dbda55fa7af5d1a34cf to your computer and use it in GitHub Desktop.
Save lambday/9dbda55fa7af5d1a34cf to your computer and use it in GitHub Desktop.
#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