Skip to content

Instantly share code, notes, and snippets.

@lisitsyn
Created May 9, 2014 18:29
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 lisitsyn/7ad1882f6be8e0ff4108 to your computer and use it in GitHub Desktop.
Save lisitsyn/7ad1882f6be8e0ff4108 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 <class Vector>
struct type_traits
{
typedef typename Vector::Scalar value_type;
};
template <class Vector, typename T, typename... Other>
struct dot_impl;
template <class T, typename... Other>
struct dot_impl<SGVector<T>, T, Other...>
{
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 <class T, typename... Other>
struct dot_impl<Matrix<T,Other...>,T, Other...>
{
static T compute(Matrix<T,Other...> a, Matrix<T,Other...> b)
{
return a.dot(b);
}
};
namespace linalg
{
template <template class Vector, typename... Other>
typename type_traits<Vector>::value_type dot(Vector a, Vector b)
{
return dot_impl<Vector,type_traits<Vector>::value_type,Other...>::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