svmdataset.c
Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2012 Daniele Perrone.
00009 Copyright (C) 2013 Andrea Vedaldi.
00010 All rights reserved.
00011 
00012 This file is part of the VLFeat library and is made available under
00013 the terms of the BSD license (see the COPYING file).
00014 */
00015 
00096 /* ---------------------------------------------------------------- */
00097 #ifndef VL_SVMDATASET_INSTANTIATING
00098 /* ---------------------------------------------------------------- */
00099 
00100 #include "svmdataset.h"
00101 #include <string.h>
00102 #include <math.h>
00103 
00104 struct VlSvmDataset_ {
00105   vl_type dataType ;                
00106   void * data ;                     
00107   vl_size numData ;                 
00108   vl_size dimension ;               
00109   VlHomogeneousKernelMap * hom ;    
00110   void * homBuffer ;                
00111   vl_size homDimension ;            
00112 } ;
00113 
00114 /* templetized parts of the implementation */
00115 #define FLT VL_TYPE_FLOAT
00116 #define VL_SVMDATASET_INSTANTIATING
00117 #include "svmdataset.c"
00118 
00119 #define FLT VL_TYPE_DOUBLE
00120 #define VL_SVMDATASET_INSTANTIATING
00121 #include "svmdataset.c"
00122 
00137 VlSvmDataset*
00138 vl_svmdataset_new (vl_type dataType, void *data, vl_size dimension, vl_size numData)
00139 {
00140   VlSvmDataset * self ;
00141   assert(dataType == VL_TYPE_DOUBLE || dataType == VL_TYPE_FLOAT) ;
00142   assert(data) ;
00143 
00144   self = vl_calloc(1, sizeof(VlSvmDataset)) ;
00145   if (self == NULL) return NULL ;
00146 
00147   self->dataType = dataType ;
00148   self->data = data ;
00149   self->dimension = dimension ;
00150   self->numData = numData ;
00151   self->hom = NULL ;
00152   self->homBuffer = NULL ;
00153   return self ;
00154 }
00155 
00164 void vl_svmdataset_delete (VlSvmDataset *self)
00165 {
00166   if (self->homBuffer) {
00167     vl_free(self->homBuffer) ;
00168     self->homBuffer = 0 ;
00169   }
00170   vl_free (self) ;
00171 }
00172 
00178 void*
00179 vl_svmdataset_get_data (VlSvmDataset const *self)
00180 {
00181   return self->data ;
00182 }
00183 
00189 vl_size
00190 vl_svmdataset_get_num_data (VlSvmDataset const *self)
00191 {
00192   return self->numData ;
00193 }
00194 
00200 vl_size
00201 vl_svmdataset_get_dimension (VlSvmDataset const *self)
00202 {
00203   if (self->hom) {
00204     return self->dimension * vl_homogeneouskernelmap_get_dimension(self->hom) ;
00205   }
00206   return self->dimension ;
00207 }
00208 
00214 VlHomogeneousKernelMap *
00215 vl_svmdataset_get_homogeneous_kernel_map (VlSvmDataset const *self)
00216 {
00217   assert(self) ;
00218   return self->hom ;
00219 }
00220 
00237 void
00238 vl_svmdataset_set_homogeneous_kernel_map (VlSvmDataset * self,
00239                                           VlHomogeneousKernelMap * hom)
00240 {
00241   assert(self) ;
00242   self->hom = hom ;
00243   self->homDimension = 0 ;
00244   if (self->homBuffer) {
00245     vl_free (self->homBuffer) ;
00246     self->homBuffer = 0 ;
00247   }
00248   if (self->hom) {
00249     self->homDimension = vl_homogeneouskernelmap_get_dimension(self->hom) ;
00250     self->homBuffer = vl_calloc(self->homDimension, vl_get_type_size(self->dataType)) ;
00251   }
00252 }
00253 
00259 VlSvmAccumulateFunction
00260 vl_svmdataset_get_accumulate_function(VlSvmDataset const *self)
00261 {
00262   if (self->hom == NULL) {
00263     switch (self->dataType) {
00264       case VL_TYPE_FLOAT:
00265         return (VlSvmAccumulateFunction) vl_svmdataset_accumulate_f ;
00266         break ;
00267       case VL_TYPE_DOUBLE:
00268         return (VlSvmAccumulateFunction) vl_svmdataset_accumulate_d ;
00269         break ;
00270     }
00271   } else {
00272     switch (self->dataType) {
00273       case VL_TYPE_FLOAT:
00274         return (VlSvmAccumulateFunction) vl_svmdataset_accumulate_hom_f ;
00275         break ;
00276       case VL_TYPE_DOUBLE:
00277         return (VlSvmAccumulateFunction) vl_svmdataset_accumulate_hom_d ;
00278         break ;
00279     }
00280   }
00281   assert(0) ;
00282   return NULL ;
00283 }
00284 
00290 VlSvmInnerProductFunction
00291 vl_svmdataset_get_inner_product_function (VlSvmDataset const *self)
00292 {
00293   if (self->hom == NULL) {
00294     switch (self->dataType) {
00295       case VL_TYPE_FLOAT:
00296         return (VlSvmInnerProductFunction) _vl_svmdataset_inner_product_f ;
00297         break ;
00298       case VL_TYPE_DOUBLE:
00299         return (VlSvmInnerProductFunction) _vl_svmdataset_inner_product_d ;
00300         break ;
00301       default:
00302         assert(0) ;
00303     }
00304   } else {
00305     switch (self->dataType) {
00306       case VL_TYPE_FLOAT:
00307         return (VlSvmInnerProductFunction) _vl_svmdataset_inner_product_hom_f ;
00308         break ;
00309       case VL_TYPE_DOUBLE:
00310         return (VlSvmInnerProductFunction) _vl_svmdataset_inner_product_hom_d ;
00311         break ;
00312       default:
00313         assert(0) ;
00314     }
00315   }
00316 
00317   return NULL;
00318 }
00319 
00320 /* VL_SVMDATASET_INSTANTIATING */
00321 #endif
00322 
00323 /* ---------------------------------------------------------------- */
00324 #ifdef VL_SVMDATASET_INSTANTIATING
00325 /* ---------------------------------------------------------------- */
00326 
00327 #include "float.th"
00328 
00329 double
00330 VL_XCAT(_vl_svmdataset_inner_product_,SFX) (VlSvmDataset const *self,
00331                                             vl_uindex element,
00332                                             double const *model)
00333 {
00334   double product = 0 ;
00335   T* data = ((T*)self->data) + self->dimension * element ;
00336   T* end = data + self->dimension ;
00337   while (data != end) {
00338     product += (*data++) * (*model++) ;
00339   }
00340   return product ;
00341 }
00342 
00343 void
00344 VL_XCAT(vl_svmdataset_accumulate_,SFX)(VlSvmDataset const *self,
00345                                        vl_uindex element,
00346                                        double *model,
00347                                        const double multiplier)
00348 {
00349   T* data = ((T*)self->data) + self->dimension * element ;
00350   T* end = data + self->dimension ;
00351   while (data != end) {
00352     *model += (*data++) * multiplier ;
00353     model++ ;
00354   }
00355 }
00356 
00357 double
00358 VL_XCAT(_vl_svmdataset_inner_product_hom_,SFX) (VlSvmDataset const *self,
00359                                                 vl_uindex element,
00360                                                 double const *model)
00361 {
00362   double product = 0 ;
00363   T* data = ((T*)self->data) + self->dimension * element ;
00364   T* end = data + self->dimension ;
00365   T* bufEnd = ((T*)self->homBuffer)+ self->homDimension ;
00366   while (data != end) {
00367     /* TODO: zeros in data could be optimized by skipping over them */
00368     T* buf = self->homBuffer ;
00369     VL_XCAT(vl_homogeneouskernelmap_evaluate_,SFX)(self->hom,
00370                                                    self->homBuffer,
00371                                                    1,
00372                                                    (*data++)) ;
00373     while (buf != bufEnd) {
00374       product += (*buf++) * (*model++) ;
00375     }
00376   }
00377   return product ;
00378 }
00379 
00380 void
00381 VL_XCAT(vl_svmdataset_accumulate_hom_,SFX)(VlSvmDataset const *self,
00382                                            vl_uindex element,
00383                                            double *model,
00384                                            const double multiplier)
00385 {
00386   T* data = ((T*)self->data) + self->dimension * element ;
00387   T* end = data + self->dimension ;
00388   T* bufEnd = ((T*)self->homBuffer)+ self->homDimension ;
00389   while (data != end) {
00390     /* TODO: zeros in data could be optimized by skipping over them */
00391     T* buf = self->homBuffer ;
00392     VL_XCAT(vl_homogeneouskernelmap_evaluate_,SFX)(self->hom,
00393                                                    self->homBuffer,
00394                                                    1,
00395                                                    (*data++)) ;
00396     while (buf != bufEnd) {
00397       *model += (*buf++) * multiplier ;
00398       model++ ;
00399     }
00400   }
00401 }
00402 
00403 #undef FLT
00404 #undef VL_SVMDATASET_INSTANTIATING
00405 
00406 /* VL_SVMDATASET_INSTANTIATING */
00407 #endif


libvlfeat
Author(s): Andrea Vedaldi
autogenerated on Thu Jun 6 2019 20:25:51