MatrixDouble MatrixDouble::multiple(const MatrixDouble &b){ const unsigned int M = rows; const unsigned int N = cols; const unsigned int K = (unsigned int)b.getNumRows(); const unsigned int L = (unsigned int)b.getNumCols(); if( N != K ) { warningLog << "multiple(MatrixDouble b) - The number of rows in b (" << b.getNumRows() << ") does not match the number of columns in this matrix (" << N << ")" << std::endl; return MatrixDouble(); } MatrixDouble c(M,L); for(unsigned int i=0; i<M; i++){ for(unsigned int j=0; j<L; j++){ c[i][j] = 0; for(unsigned int k=0; k<K; k++){ c[i][j] += dataPtr[i][k] * b[k][j]; } } } return c; }
MatrixDouble RandomForests::getLeafNodeFeatureWeights( const bool normWeights ) const{ if( !trained ) return MatrixDouble(); MatrixDouble weights( getNumClasses(), numInputDimensions ); weights.setAllValues(0.0); for(UINT i=0; i<forestSize; i++){ if( !forest[i]->computeLeafNodeWeights( weights ) ){ warningLog << "computeLeafNodeWeights( const bool normWeights ) - Failed to compute leaf node weights for tree: " << i << endl; } } //Normalize the weights if( normWeights ){ for(UINT j=0; j<weights.getNumCols(); j++){ double sum = 0.0; for(UINT i=0; i<weights.getNumRows(); i++){ sum += weights[i][j]; } if( sum != 0.0 ){ const double norm = 1.0 / sum; for(UINT i=0; i<weights.getNumRows(); i++){ weights[i][j] *= norm; } } } } return weights; }
MatrixDouble MatrixDouble::multiple(const MatrixDouble &b) const{ const unsigned int M = rows; const unsigned int N = cols; const unsigned int K = b.getNumRows(); const unsigned int L = b.getNumCols(); if( N != K ) { errorLog << "multiple(MatrixDouble b) - The number of rows in b (" << K << ") does not match the number of columns in this matrix (" << N << ")" << std::endl; return MatrixDouble(); } MatrixDouble c(M,L); double **pb = b.getDataPointer(); double **pc = c.getDataPointer(); unsigned int i,j,k = 0; for(i=0; i<M; i++){ for(j=0; j<L; j++){ pc[i][j] = 0; for(k=0; k<K; k++){ pc[i][j] += dataPtr[i*cols+k] * pb[k][j]; } } } return c; }
MatrixDouble MatrixDouble::multiple(const double value) const{ if( dataPtr == NULL ) return MatrixDouble(); MatrixDouble d(rows,cols); for(unsigned int i=0; i<rows; i++){ for(unsigned int j=0; j<cols; j++){ d[i][j] = dataPtr[i][j] * value; } } return d; }
MatrixDouble MatrixDouble::multiple(const double value) const{ if( dataPtr == NULL ) return MatrixDouble(); MatrixDouble d(rows,cols); double *d_p = &(d[0][0]); unsigned int i = 0; for(i=0; i<rows*cols; i++){ d_p[i] = dataPtr[i] * value; } return d; }
MatrixDouble TimeSeriesClassificationDataStream::getTimeSeriesData( const TimeSeriesPositionTracker &trackerInfo ) const { if( trackerInfo.getStartIndex() >= totalNumSamples || trackerInfo.getEndIndex() > totalNumSamples ){ warningLog << "getTimeSeriesData(TimeSeriesPositionTracker trackerInfo) - Invalid tracker indexs!" << endl; return MatrixDouble(); } UINT startIndex = trackerInfo.getStartIndex(); UINT endIndex = trackerInfo.getEndIndex(); UINT M = endIndex > 0 ? trackerInfo.getLength() : totalNumSamples - startIndex; UINT N = getNumDimensions(); MatrixDouble tsData(M,N); for(UINT i=0; i<M; i++){ for(UINT j=0; j<N; j++){ tsData[i][j] = data[ i+startIndex ][j]; } } return tsData; }
MatrixDouble TimeSeriesClassificationData::getDataAsMatrixDouble() const { //Count how many samples are in the entire dataset UINT M = 0; UINT index = 0; for(UINT x=0; x<totalNumSamples; x++){ M += data[x].getLength(); } if( M == 0 ) MatrixDouble(); //Get all the data and concatenate it into 1 matrix MatrixDouble matrixData(M,numDimensions); for(UINT x=0; x<totalNumSamples; x++){ for(UINT i=0; i<data[x].getLength(); i++){ for(UINT j=0; j<numDimensions; j++){ matrixData[index][j] = data[x][i][j]; } index++; } } return matrixData; }
double DistSingleQ(double *a,double *b,int M,int N,double q,int metric_type) { int i,j; double *li,*lj,*lij,*gcur; double **g; double temp; double d; int iend,jend; if(M==0) d=N; else if(N==0) d=M; else if(q==0) d=abs(M-N); else { /* allocate memory for g */ g = MatrixDouble(M+1,N+1); /* initialize the borders */ for(i=0;i<M+1;i++) g[i][0]=i; for(j=1;j<N+1;j++) g[0][j]=j; li=&g[0][1]; lj=&g[1][0]; lij=&g[0][0]; gcur=&g[1][1]; for(i=1;i<M+1;i++) { for(j=1;j<N+1;j++) { if(metric_type==1) { iend = 0; if((i==1) | (i==M)) iend=1; jend = 0; if((j==1) | (j==N)) jend=1; if(iend & jend) temp=0; else if(iend) temp = MAX(0,a[i-1]-b[j-1]); else if(jend) temp = MAX(0,b[j-1]-a[i-1]); else temp = fabs(a[i-1]-b[j-1]); } else temp = fabs(a[i-1]-b[j-1]); *gcur = MIN3((*li)+1,(*lj)+1,(*lij)+q*temp); gcur++; li++; lj++; lij++; } gcur++; li++; lj++; lij++; } d = g[M][N]; FreeMatrixDouble(g); } return d; }
int MetricDistSingleQComp(struct options_metric *opts, int P_total, int *counts, double **times, double ***d) { int i,j,k; double *a,*b; int M,N; int p,q,max_counts; double **intervals; /* If we are doing D_interval, compute the ISIs */ if((*opts).metric==1) { /* What is the max of counts? */ max_counts=0; for(p=0;p<P_total;p++) if(counts[p]>max_counts) max_counts = counts[p]; intervals = MatrixDouble(P_total,max_counts+1); for(p=0;p<P_total;p++) { intervals[p][0] = times[p][0] - (*opts).t_start; for(q=1;q<counts[p];q++) intervals[p][q] = times[p][q] - times[p][q-1]; intervals[p][counts[p]] = (*opts).t_end - times[p][counts[p]-1]; } } /* for each pair of spike trains, compute the distance */ /* These measures are symmetric, and also D(x,x)=0, so we need to do P_total*(P_total-1)/2 comparisons */ for (i=0;i<P_total;i++) for (j=i+1;j<P_total;j++) for (k=0;k<(*opts).num_q;k++) { if((*opts).metric==1) { a = intervals[i]; M = counts[i]+1; b = intervals[j]; N = counts[j]+1; } else { a = times[i]; M = counts[i]; b = times[j]; N = counts[j]; } d[i][j][k]=DistSingleQ(a,b,M,N,(*opts).q[k],(*opts).metric); #ifdef DEBUG printf("i=%d j=%d k=%d d[%d][%d][%d]=%f\n",i,j,k,i,j,k,d[i][j][k]); #endif d[j][i][k]=d[i][j][k]; /* Exploit symmetry of distance matrix */ } if((*opts).metric==1) FreeMatrixDouble(intervals); return EXIT_SUCCESS; }
MatrixDouble MovementTrajectoryFeatures::getCentroids(){ if( initialized ){ return centroids; } return MatrixDouble(); }