/* * Compute global cost matrix - companion * to the dtw R package * (c) Toni Giorgino 2007-2012 * Distributed under GPL-2 with NO WARRANTY. * * $Id: computeCM.c 268 2012-08-12 15:01:18Z tonig $ * */ #include #include #include #ifdef TEST_UNIT // Define R-like functions - a bad idea #define R_alloc(n,size) malloc((n)*(size)) #define my_R_free(ptr) free((ptr)) #define error(...) { fprintf (stderr, __VA_ARGS__); exit(-1); } #else #include #define my_R_free(ptr) #endif #include "computeCM.h" #ifndef NAN #error "This code requires native IEEE NAN support. Possible solutions: 1) verify you are using gcc with -std=gnu99; 2) use the fallback interpreted DTW version (should happen automatically); 3) ask the author" #endif /* undo R indexing */ #define EP(ii,jj) ((jj)*nsteps+(ii)) #define EM(ii,jj) ((jj)*n+(ii)) #define CLEARCLIST { \ for(int z=0; z=nsteps) { error("Error on pattern row %d, pattern number %d out of bounds\n", i,pn[i]); } } /* assuming pattern ids are in ascending order */ int npats=pn[nsteps-1]; /* prepare a cost list per pattern */ double *clist=(double*) R_alloc(npats,sizeof(double)); /* we do not initialize the seed - the caller is supposed to do so cm[0]=lm[0]; */ /* lets go */ for(int j=0; j=0 && jj>=0) { /* address ok? C convention */ double cc=sc[s]; if(cc==-1.0) { clist[p]=cm[EM(ii,jj)]; } else { /* we rely on NAN to propagate */ clist[p] += cc*lm[EM(ii,jj)]; } } } int minc=argmin(clist,npats); if(minc>-1) { cm[EM(i,j)]=clist[minc]; sm[EM(i,j)]=minc+1; /* convert to 1-based */ } } } /* Memory alloc'd by R_alloc is automatically freed */ my_R_free(clist); my_R_free(sc); my_R_free(di); my_R_free(dj); my_R_free(pn); } /* return the arg min, ignoring NANs, -1 if all NANs */ int argmin(double *list, int n) { int ii=-1; double vv=INFINITY; for(int i=0; imyg2 */ #define TS 10 #define TSS (TS*TS) int main(int argc,char **argv) { int ts[]={TS,TS}; int *twm; double *tlm; int tnstepsp[]={6}; double tdir[]={1, 1, 2, 2, 3, 3, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,-1, 1,-1, 1,-1, 1}; double *tcm; int *tsm; int i,j; twm=malloc(TSS*sizeof(int)); for( i=0;i