/* call as model = mexsvmlearn(data,labels,options) */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int rows, cols,i,j,offset; double *kern,k; DOC **docsA, **docsB; SVECTOR *a, *b; MODEL *model; global_init( ); /* load model parameters from the "model" parameter */ model = restore_model((mxArray *)prhs[2]); rows = mxGetM(prhs[0]); cols = mxGetN(prhs[0]); /* load the testing arrays into docs */ mexToDOC((mxArray *)prhs[0], NULL, &docsA, NULL, NULL, NULL); mexToDOC((mxArray *)prhs[1], NULL, &docsB, NULL, NULL, NULL); /* setup output environment */ plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); kern = mxGetPr(plhs[0]); a = docsA[0]->fvec; b = docsB[0]->fvec; kern[0] = single_kernel(&(model->kernel_parm), a, b); global_destroy(); }
double kernel(KERNEL_PARM *kernel_parm, DOC *a, DOC *b) /* calculate the kernel function */ { double sum=0; SVECTOR *fa,*fb; if(kernel_parm->kernel_type == GRAM) { /* use value from explicitly */ if((a->kernelid>=0) && (b->kernelid>=0)) /* stored gram matrix */ return(kernel_parm->gram_matrix->element[MAX(a->kernelid,b->kernelid)] [MIN(a->kernelid,b->kernelid)]); else return(0); /* in case it is called for unknown vector */ } /* in case the constraints are sums of feature vector as represented as a list of SVECTOR's with their coefficient factor in the sum, take the kernel between all pairs */ for(fa=a->fvec;fa;fa=fa->next) { for(fb=b->fvec;fb;fb=fb->next) { if(fa->kernel_id == fb->kernel_id) sum+=fa->factor*fb->factor*single_kernel(kernel_parm,fa,fb); } } return(sum); }
double kernel(KERNEL_PARM *kernel_parm, DOC *a, DOC *b) /* calculate the kernel function */ { double sum=0; SVECTOR *fa,*fb; /* in case the constraints are sums of feature vector as represented as a list of SVECTOR's with their coefficient factor in the sum, take the kernel between all pairs */ for(fa=a->fvec;fa;fa=fa->next) { for(fb=b->fvec;fb;fb=fb->next) { if(fa->kernel_id == fb->kernel_id) sum+=fa->factor*fb->factor*single_kernel(kernel_parm,fa,fb); } } return(sum); }
/* call as model = mexsvmlearn(data,labels,options) */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int rows, cols,i,j,offset; double *kern,k; DOC **docs; SVECTOR *a, *b; MODEL *model; global_init(); /* load model parameters from the "model" parameter */ model = restore_model((mxArray *)prhs[1]); rows = mxGetM(prhs[0]); cols = mxGetN(prhs[0]); /* load the testing arrays into docs */ mexToDOC((mxArray *)prhs[0], NULL, &docs, NULL, NULL, NULL); /* setup output environment */ plhs[0] = mxCreateDoubleMatrix(rows,rows,mxREAL); kern = mxGetPr(plhs[0]); for (i = 0; i < rows; i++) { a = docs[i]->fvec; for (j = 0; j < rows; j++) { b = docs[j]->fvec; k = single_kernel(&(model->kernel_parm), a, b); offset = computeOffset(rows, rows, i, j); kern[offset] = k; } } global_destroy(); }