1 /*-----------------------------------------------------------------------
2  * features_int8.c: Helper functions for the OCAS solver working with
3  *                 INT8 features.
4  *-------------------------------------------------------------------- */
5 
6 #include <stdint.h>
7 #include <string.h>
8 
9 #include "ocas_helper.h"
10 #include "features_int8.h"
11 
12 
13 /*----------------------------------------------------------------------------------
14   full_add_new_cut( new_col_H, new_cut, cut_length, nSel ) does the following:
15 
16     new_a = sum(data_X(:,find(new_cut ~=0 )),2);
17     new_col_H = [full_A(:,1:nSel)'*new_a ; new_a'*new_a];
18     full_A(:,nSel+1) = new_a;
19 
20   ---------------------------------------------------------------------------------*/
full_int8_add_new_cut(double * new_col_H,uint32_t * new_cut,uint32_t cut_length,uint32_t nSel,void * user_data)21 int full_int8_add_new_cut( double *new_col_H,
22                            uint32_t *new_cut,
23                            uint32_t cut_length,
24                            uint32_t nSel,
25                            void* user_data)
26 {
27 /*  double *new_a, */
28   double sq_norm_a;
29   uint32_t i, j;
30   int8_t *ptr;
31 
32   ptr = (int8_t*)mxGetPr(data_X);
33 
34   memset(new_a, 0, sizeof(double)*nDim);
35 
36 
37   for(i=0; i < cut_length; i++) {
38     for(j=0; j < nDim; j++ ) {
39       new_a[j] += (double)ptr[LIBOCAS_INDEX(j,new_cut[i],nDim)];
40     }
41 
42     A0[nSel] += X0*data_y[new_cut[i]];
43   }
44 
45   /* compute new_a'*new_a and insert new_a to the last column of full_A */
46   sq_norm_a = A0[nSel]*A0[nSel];
47   for(j=0; j < nDim; j++ ) {
48     sq_norm_a += new_a[j]*new_a[j];
49     full_A[LIBOCAS_INDEX(j,nSel,nDim)] = new_a[j];
50   }
51 
52   new_col_H[nSel] = sq_norm_a;
53   for(i=0; i < nSel; i++) {
54     double tmp = A0[nSel]*A0[i];
55 
56     for(j=0; j < nDim; j++ ) {
57       tmp += new_a[j]*full_A[LIBOCAS_INDEX(j,i,nDim)];
58     }
59     new_col_H[i] = tmp;
60   }
61 
62   return 0;
63 }
64 
65 /*----------------------------------------------------------------------
66   full_int8_compute_output( output ) does the follwing:
67 
68   output = data_X'*W;
69   ----------------------------------------------------------------------*/
full_int8_compute_output(double * output,void * user_data)70 int full_int8_compute_output( double *output, void* user_data )
71 {
72   uint32_t i, j;
73   double tmp;
74   int8_t* ptr;
75 
76   ptr = (int8_t*)mxGetPr( data_X );
77 
78   for(i=0; i < nData; i++) {
79     tmp = data_y[i]*X0*W0;
80 
81     for(j=0; j < nDim; j++ ) {
82       tmp += W[j]*(double)ptr[LIBOCAS_INDEX(j,i,nDim)];
83     }
84     output[i] = tmp;
85   }
86 
87   return 0;
88 }
89 
90