Tapkee
chain_interface.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
00004  */
00005 
00006 #ifndef TAPKEE_CHAIN_H_
00007 #define TAPKEE_CHAIN_H_
00008 
00009 /* Tapkee includes */
00010 #include <tapkee/embed.hpp>
00011 #include <tapkee/callbacks/dummy_callbacks.hpp>
00012 #include <tapkee/callbacks/eigen_callbacks.hpp>
00013 /* End of Tapkee includes */
00014 
00015 namespace tapkee
00016 {
00017 
00018 namespace tapkee_internal
00019 {
00020     template<class KernelCallback, class DistanceCallback, class FeaturesCallback>
00021     class CallbacksInitializedState
00022     {
00023     public:
00024         CallbacksInitializedState(const ParametersSet& params, const KernelCallback& k,
00025                                   const DistanceCallback& d, const FeaturesCallback& f) :
00026             parameters(params), kernel(k), distance(d), features(f) { }
00027 
00034         template<class RandomAccessIterator>
00035         TapkeeOutput embedRange(RandomAccessIterator begin, RandomAccessIterator end) const
00036         {
00037             return tapkee::embed(begin,end,kernel,distance,features,parameters);
00038         }
00039 
00045         template<class Container>
00046         TapkeeOutput embedUsing(const Container& container) const
00047         {
00048             return embedRange(container.begin(),container.end());
00049         }
00050     private:
00051         ParametersSet parameters;
00052         KernelCallback kernel;
00053         DistanceCallback distance;
00054         FeaturesCallback features;
00055     };
00056 
00057     template<class KernelCallback, class DistanceCallback>
00058     class KernelAndDistanceInitializedState
00059     {
00060     public:
00061         KernelAndDistanceInitializedState(const ParametersSet& params, const KernelCallback& k, const DistanceCallback& d) :
00062             parameters(params), kernel(k), distance(d) { }
00063 
00071         template<class FeaturesCallback>
00072         CallbacksInitializedState<KernelCallback,DistanceCallback,FeaturesCallback> withFeatures(const FeaturesCallback& features) const
00073         { return CallbacksInitializedState<KernelCallback,DistanceCallback,FeaturesCallback>(parameters,kernel,distance,features); }
00074         
00081         template<class RandomAccessIterator>
00082         TapkeeOutput embedRange(RandomAccessIterator begin, RandomAccessIterator end) const
00083         {
00084             return (*this).withFeatures(dummy_features_callback<typename RandomAccessIterator::value_type>())
00085                           .embedRange(begin,end);
00086         }
00087 
00093         template<class Container>
00094         TapkeeOutput embedUsing(const Container& container) const
00095         {
00096             return embedRange(container.begin(),container.end());
00097         }
00098     private:
00099         ParametersSet parameters;
00100         KernelCallback kernel;
00101         DistanceCallback distance;
00102     };
00103 
00104     template<class KernelCallback, class FeaturesCallback>
00105     class KernelAndFeaturesInitializedState
00106     {
00107     public:
00108         KernelAndFeaturesInitializedState(const ParametersSet& params, const KernelCallback& k, const FeaturesCallback& f) :
00109             parameters(params), kernel(k), features(f) { }
00110 
00118         template<class DistanceCallback>
00119         CallbacksInitializedState<KernelCallback,DistanceCallback,FeaturesCallback> withDistance(const DistanceCallback& distance) const
00120         { return CallbacksInitializedState<KernelCallback,DistanceCallback,FeaturesCallback>(parameters,kernel,distance,features); }
00121 
00128         template<class RandomAccessIterator>
00129         TapkeeOutput embedRange(RandomAccessIterator begin, RandomAccessIterator end) const
00130         {
00131             return (*this).withDistance(dummy_distance_callback<typename RandomAccessIterator::value_type>())
00132                           .embedRange(begin,end);
00133         }
00134 
00140         template<class Container>
00141         TapkeeOutput embedUsing(const Container& container) const
00142         {
00143             return embedRange(container.begin(),container.end());
00144         }
00145     private:
00146         ParametersSet parameters;
00147         KernelCallback kernel;
00148         FeaturesCallback features;
00149     };
00150 
00156     template<class DistanceCallback, class FeaturesCallback>
00157     class DistanceAndFeaturesInitializedState
00158     {
00159     public:
00160         DistanceAndFeaturesInitializedState(const ParametersSet& params, const DistanceCallback& d, const FeaturesCallback& f) :
00161             parameters(params), distance(d), features(f) { }
00162 
00170         template<class KernelCallback>
00171         CallbacksInitializedState<KernelCallback,DistanceCallback,FeaturesCallback> withKernel(const KernelCallback& kernel) const
00172         { return CallbacksInitializedState<KernelCallback,DistanceCallback,FeaturesCallback>(parameters,kernel,distance,features); }
00173 
00180         template<class RandomAccessIterator>
00181         TapkeeOutput embedRange(RandomAccessIterator begin, RandomAccessIterator end) const
00182         {
00183             return (*this).withKernel(dummy_kernel_callback<typename RandomAccessIterator::value_type>())
00184                           .embedRange(begin,end);
00185         }
00186 
00192         template<class Container>
00193         TapkeeOutput embedUsing(const Container& container) const
00194         {
00195             return embedRange(container.begin(),container.end());
00196         }
00197     private:
00198         ParametersSet parameters;
00199         DistanceCallback distance;
00200         FeaturesCallback features;
00201     };
00202 
00203     template<class KernelCallback>
00204     class KernelFirstInitializedState
00205     {
00206     public:
00207         KernelFirstInitializedState(const ParametersSet& params, const KernelCallback& callback) :
00208             parameters(params), kernel(callback) { }
00209 
00217         template<class DistanceCallback>
00218         KernelAndDistanceInitializedState<KernelCallback,DistanceCallback> withDistance(const DistanceCallback& callback) const
00219         { return KernelAndDistanceInitializedState<KernelCallback,DistanceCallback>(parameters,kernel,callback); }
00220 
00228         template<class FeaturesCallback>
00229         KernelAndFeaturesInitializedState<KernelCallback,FeaturesCallback> withFeatures(const FeaturesCallback& callback) const
00230         { return KernelAndFeaturesInitializedState<KernelCallback,FeaturesCallback>(parameters,kernel,callback); }
00231 
00238         template<class RandomAccessIterator>
00239         TapkeeOutput embedRange(RandomAccessIterator begin, RandomAccessIterator end) const
00240         {
00241             return (*this).withDistance(dummy_distance_callback<typename RandomAccessIterator::value_type>())
00242                           .withFeatures(dummy_features_callback<typename RandomAccessIterator::value_type>())
00243                           .embedRange(begin,end);
00244         }
00245 
00251         template<class Container>
00252         TapkeeOutput embedUsing(const Container& container) const
00253         {
00254             return embedRange(container.begin(),container.end());
00255         }
00256     private:
00257         ParametersSet parameters;
00258         KernelCallback kernel;
00259     };
00260 
00261     template<class DistanceCallback>
00262     class DistanceFirstInitializedState
00263     {
00264     public:
00265         DistanceFirstInitializedState(const ParametersSet& params, const DistanceCallback& callback) :
00266             parameters(params), distance(callback) { }
00267 
00275         template<class KernelCallback>
00276         KernelAndDistanceInitializedState<KernelCallback,DistanceCallback> withKernel(const KernelCallback& callback) const
00277         { return KernelAndDistanceInitializedState<KernelCallback,DistanceCallback>(parameters,callback,distance); }
00278 
00286         template<class FeaturesCallback>
00287         DistanceAndFeaturesInitializedState<DistanceCallback,FeaturesCallback> withFeatures(const FeaturesCallback& callback) const
00288         { return DistanceAndFeaturesInitializedState<DistanceCallback,FeaturesCallback>(parameters,distance,callback); }
00289 
00296         template<class RandomAccessIterator>
00297         TapkeeOutput embedRange(RandomAccessIterator begin, RandomAccessIterator end) const
00298         {
00299             return (*this).withKernel(dummy_kernel_callback<typename RandomAccessIterator::value_type>())
00300                           .withFeatures(dummy_features_callback<typename RandomAccessIterator::value_type>())
00301                           .embedRange(begin,end);
00302         }
00303 
00309         template<class Container>
00310         TapkeeOutput embedUsing(const Container& container) const
00311         {
00312             return embedRange(container.begin(),container.end());
00313         }
00314     private:
00315         ParametersSet parameters;
00316         DistanceCallback distance;
00317     };
00318 
00319     template<class FeaturesCallback>
00320     class FeaturesFirstInitializedState
00321     {
00322     public:
00323         FeaturesFirstInitializedState(const ParametersSet& params, const FeaturesCallback& callback) :
00324             parameters(params), features(callback) { }
00325 
00333         template<class KernelCallback>
00334         KernelAndFeaturesInitializedState<KernelCallback,FeaturesCallback> withKernel(const KernelCallback& callback) const
00335         { return KernelAndFeaturesInitializedState<KernelCallback,FeaturesCallback>(parameters,callback,features); }
00336 
00344         template<class DistanceCallback>
00345         DistanceAndFeaturesInitializedState<DistanceCallback,FeaturesCallback> withDistance(const DistanceCallback& callback) const
00346         { return DistanceAndFeaturesInitializedState<DistanceCallback,FeaturesCallback>(parameters,callback,features); }
00347 
00354         template<class RandomAccessIterator>
00355         TapkeeOutput embedRange(RandomAccessIterator begin, RandomAccessIterator end) const
00356         {
00357             return (*this).withKernel(dummy_kernel_callback<typename RandomAccessIterator::value_type>())
00358                           .withDistance(dummy_distance_callback<typename RandomAccessIterator::value_type>())
00359                           .embedRange(begin,end);
00360         }
00361 
00367         template<class Container>
00368         TapkeeOutput embedUsing(const Container& container) const
00369         {
00370             return embedRange(container.begin(),container.end());
00371         }
00372     private:
00373         ParametersSet parameters;
00374         FeaturesCallback features;
00375     };
00376 
00377     class ParametersInitializedState
00378     {
00379     public:
00380         ParametersInitializedState(const ParametersSet& that) : parameters(that) { }
00381         ParametersInitializedState(const ParametersInitializedState&);
00382         ParametersInitializedState& operator=(const ParametersInitializedState&);
00383 
00391         template<class KernelCallback> 
00392         KernelFirstInitializedState<KernelCallback> withKernel(const KernelCallback& callback) const
00393         { return KernelFirstInitializedState<KernelCallback>(parameters,callback); }
00394 
00402         template<class DistanceCallback>
00403         DistanceFirstInitializedState<DistanceCallback> withDistance(const DistanceCallback& callback) const
00404         { return DistanceFirstInitializedState<DistanceCallback>(parameters,callback); }
00405 
00413         template<class FeaturesCallback>
00414         FeaturesFirstInitializedState<FeaturesCallback> withFeatures(const FeaturesCallback& callback) const
00415         { return FeaturesFirstInitializedState<FeaturesCallback>(parameters,callback); }
00416 
00423         TapkeeOutput embedUsing(const DenseMatrix& matrix) const
00424         {
00425             std::vector<IndexType> indices(matrix.cols());
00426             for (IndexType i=0; i<matrix.cols(); i++) indices[i] = i;
00427             eigen_kernel_callback kcb(matrix);
00428             eigen_distance_callback dcb(matrix);
00429             eigen_features_callback fcb(matrix);
00430             return tapkee::embed(indices.begin(),indices.end(),kcb,dcb,fcb,parameters);
00431         }
00432     private:
00433         ParametersSet parameters;
00434     };
00435 } /* End of namespace tapkee_internal */
00436 
00437 struct initialize
00438 {
00442     initialize() 
00443     {
00444     }
00445 
00458     tapkee_internal::ParametersInitializedState withParameters(const ParametersSet& parameters) const
00459     {
00460         return tapkee_internal::ParametersInitializedState(parameters);
00461     }
00462 };
00463 
00464 } /* End of namespace tapkee */
00465 
00466 #endif
00467 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines