Tapkee
eigen_callbacks.hpp
Go to the documentation of this file.
00001 /* This software is distributed under BSD 3-clause license (see LICENSE file).
00002  *
00003  * Copyright (c) 2012-2013 Sergey Lisitsyn, Fernando Iglesias
00004  */
00005 
00006 #ifndef TAPKEE_EIGEN_CALLBACKS_H_
00007 #define TAPKEE_EIGEN_CALLBACKS_H_
00008 
00009 namespace tapkee
00010 {
00011     // Features callback that provides operation that 
00012     // puts contents of the specified feature 
00013     // vector to given DenseVector instance.
00014     struct eigen_features_callback
00015     {
00016         eigen_features_callback(const tapkee::DenseMatrix& matrix) : feature_matrix(matrix) {};
00017         inline tapkee::IndexType dimension() const
00018         {
00019             return feature_matrix.rows();
00020         }
00021         inline void vector(tapkee::IndexType i, tapkee::DenseVector& v) const
00022         {
00023             v = feature_matrix.col(i);
00024         }
00025         const tapkee::DenseMatrix& feature_matrix;
00026     };
00027 
00028     // Kernel function callback that computes
00029     // similarity function values on vectors 
00030     // given by their indices. This impl. computes 
00031     // linear kernel i.e. dot product between two vectors.
00032     struct eigen_kernel_callback
00033     {
00034         eigen_kernel_callback(const tapkee::DenseMatrix& matrix) : feature_matrix(matrix) {};
00035         inline tapkee::ScalarType kernel(tapkee::IndexType a, tapkee::IndexType b) const
00036         {
00037             return feature_matrix.col(a).dot(feature_matrix.col(b));
00038         }
00039         inline tapkee::ScalarType operator()(tapkee::IndexType a, tapkee::IndexType b) const
00040         {
00041             return kernel(a,b);
00042         }
00043         const tapkee::DenseMatrix& feature_matrix;
00044     };
00045 
00046     // Distance function callback that provides
00047     // dissimilarity function values on vectors
00048     // given by their indices. This impl. computes
00049     // euclidean distance between two vectors.
00050     struct eigen_distance_callback
00051     {
00052         eigen_distance_callback(const tapkee::DenseMatrix& matrix) : feature_matrix(matrix) {};
00053         inline tapkee::ScalarType distance(tapkee::IndexType a, tapkee::IndexType b) const
00054         {
00055             return (feature_matrix.col(a)-feature_matrix.col(b)).norm();
00056         }
00057         inline tapkee::ScalarType operator()(tapkee::IndexType a, tapkee::IndexType b) const
00058         {
00059             return distance(a,b);
00060         }
00061         const tapkee::DenseMatrix& feature_matrix;
00062 };
00063 
00064 }
00065 
00066 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines