Mat matrixAllocInv(Mat source) { Mat dest; dest = matrixAlloc(source.row, source.row); if(matrixInv(dest, source)) { dest.row = dest.clm = 0; } return dest; }
LIBRARY_API bool matrixPseudoInv (matrix &in, matrix &out) { matrix in_T, inTin, inTinInv; bool state; state = matrixTrans (in, in_T); state = state ? matrixMult (in_T, in, inTin) : state; state = state ? matrixInv (inTin, inTinInv) : state; state = state ? matrixMult (inTinInv, in_T, out) : state; return state; }
Matrix Block::inverse(cflag tp)const{ try{ checkUni10TypeError(tp); if(!(Rnum == Cnum)){ std::ostringstream err; err<<"Cannot perform inversion on a non-square matrix."; throw std::runtime_error(exception_msg(err.str())); } Matrix invM(*this); assert(ongpu == invM.isOngpu()); matrixInv(invM.cm_elem, Rnum, invM.diag, invM.ongpu); return invM; } catch(const std::exception& e){ propogate_exception(e, "In function Matrix::inverse(uni10::cflag ):"); return Matrix(); } }
// ###################################################################### Image<float> TaskRelevanceMapGistClassify:: computeGistDist(Image<float> currGist) { FILE* itsFile = fopen(itsClusterCenterFileName.getVal().c_str(), "rb"); ASSERT(itsFile != 0); if(fread(&itsNumOfCategory, sizeof(int), 1, itsFile) != 1) LFATAL("fread failed"); if(fread(&itsUsedDims, sizeof(int),1,itsFile) != 1) LFATAL("fread failed"); ASSERT(itsNumOfCategory > 0 && itsUsedDims == itsPCADims.getVal()); LDEBUG("there are %4d categories, pca_dims: %4d",itsNumOfCategory, itsUsedDims); Image<float> currGistCut(currGist.getDims().sz()/NUM_GIST_FEAT, 1, NO_INIT); Image<float> currGistPCA(itsPCADims.getVal(), 1, NO_INIT); //! only use the whole image's gist feature to do the classification for(int i=0; i<currGistCut.getDims().sz(); i++) currGistCut.setVal(i,0, currGist.getVal(i*NUM_GIST_FEAT, 0)); LDEBUG("currGistCut dim: %d, PCA matrix height: %4d", currGistCut.getWidth(), itsPCAMatrix.getHeight()); ASSERT(currGistCut.getWidth() == itsPCAMatrix.getHeight()); currGistPCA = matrixMult(currGistCut, itsPCAMatrix); LDEBUG("currGistPCA : %f, %f, %f", currGistPCA.getVal(0,0), currGistPCA.getVal(1,0), currGistPCA.getVal(2,0)); Image<float> gistDist(itsNumOfCategory, 1,NO_INIT ); Image<float> gistCenter(itsUsedDims, 1, NO_INIT); Image<float> gistCenterCovarMatrix(itsUsedDims,itsUsedDims, NO_INIT); Image<float> gistCenterCovarMatrixInv(itsUsedDims, itsUsedDims, NO_INIT); Image<float> gistDiff(itsUsedDims,1,NO_INIT); float meanClusterGistDist=0.0F; float det = 0.0F; float coef1 = pow(2*PI, itsUsedDims/2.0F); float coef2 = 0.0F, coef3=0.0F, coef4=0.0F; for(int i=0; i<itsNumOfCategory; i++) { if(fread(gistCenter.beginw(), sizeof(float), itsUsedDims, itsFile) != (size_t)itsUsedDims) LFATAL("fread failed"); if(fread(&meanClusterGistDist, sizeof(float), 1, itsFile) != 1) LFATAL("fread failed"); if(fread(&det, sizeof(float), 1, itsFile) != 1) LFATAL("fread failed"); size_t sz = itsUsedDims*itsUsedDims; if(fread(gistCenterCovarMatrix.beginw(), sizeof(float), sz, itsFile) != sz) LFATAL("fread failed"); gistCenterCovarMatrixInv = matrixInv(gistCenterCovarMatrix); gistDiff = gistCenter - currGistPCA; coef2 =1.0F /( coef1 * pow(det, 0.5)+0.0000001F ); Image<float> tmp = matrixMult(matrixMult(gistDiff, gistCenterCovarMatrixInv), transpose(gistDiff)); coef3 = exp(-0.5F * tmp.getVal(0,0)); coef4 = coef2 * coef3 / (meanClusterGistDist+0.5F); LDEBUG("%d's is %f, %f, %f , mean is %f,sumDiff is%f\n", i+1, tmp.getVal(0,0), coef3, coef4, meanClusterGistDist, sum(gistDiff*gistDiff)); gistDist.setVal(i, 0, coef4); } fclose(itsFile); return gistDist; }
void Weights::calculateWeights() { mCoefficientNumber = (mTwoDim ? ((size_t)mPolynomeOrder + 1) * ((size_t)mPolynomeOrder + 1) : (size_t)mPolynomeOrder + 1); size_t ix, iy, i, j; int x, y; // Determine coordinates of pixels to be sampled if (mTwoDim) { int iPolynomeOrder = (int) mPolynomeOrder; //lets avoid signed/unsigned comparison warnings int iHeight = (int) height(); int iWidth = (int) width(); for (y = -iPolynomeOrder; y < iHeight + iPolynomeOrder; ++y) { for (x = -iPolynomeOrder; x < iWidth + iPolynomeOrder; ++x) { if ((x < 0 && y < 0 && -x - y < iPolynomeOrder + 2) || (x < 0 && y >= iHeight && -x + y - iHeight < iPolynomeOrder + 1) || (x >= iWidth && y < 0 && x - y - iWidth < iPolynomeOrder + 1) || (x >= iWidth && y >= iHeight && x + y - iWidth - iHeight < iPolynomeOrder) || (x < 0 && y >= 0 && y < iHeight) || (x >= iWidth && y >= 0 && y < iHeight) || (y < 0 && x >= 0 && x < iWidth ) || (y >= iHeight && x >= 0 && x < iWidth)) { QPoint position(x,y); mPositions.append(position); } } } } else { // In the one-dimensional case, only the y coordinate and y size is used. */ for (y = (-1)*mPolynomeOrder; y < 0; ++y) { QPoint position(0,y); mPositions.append(position); } for (y = (int) height(); y < (int) height() + (int) mPolynomeOrder; ++y) { QPoint position(0,y); mPositions.append(position); } } // Allocate memory. QScopedArrayPointer<double> matrix (new double[mCoefficientNumber * mCoefficientNumber]); QScopedArrayPointer<double> vector0(new double[mPositions.count() * mCoefficientNumber]); QScopedArrayPointer<double> vector1(new double[mPositions.count() * mCoefficientNumber]); // Calculate coefficient matrix and vectors for (iy = 0; iy < mCoefficientNumber; ++iy) { for (ix = 0; ix < mCoefficientNumber; ++ix) { matrix [iy* mCoefficientNumber+ix] = 0.0; } for (j = 0; j < (size_t)mPositions.count(); ++j) { vector0 [iy * mPositions.count() + j] = polyTerm (iy, mPositions.at(j).x(), mPositions.at(j).y(), mPolynomeOrder); for (ix = 0; ix < mCoefficientNumber; ++ix) { matrix [iy* mCoefficientNumber + ix] += (vector0 [iy * mPositions.count() + j] * polyTerm (ix, mPositions.at(j).x(), mPositions.at(j).y(), mPolynomeOrder)); } } } // Invert matrix. matrixInv (matrix.data(), mCoefficientNumber); // Multiply inverse matrix with vector. for (iy = 0; iy < mCoefficientNumber; ++iy) { for (j = 0; j < (size_t)mPositions.count(); ++j) { vector1 [iy * mPositions.count() + j] = 0.0; for (ix = 0; ix < mCoefficientNumber; ++ix) { vector1 [iy * mPositions.count() + j] += matrix [iy * mCoefficientNumber + ix] * vector0 [ix * mPositions.count() + j]; } } } // Store weights // Allocate mPositions.count() matrices. mWeightMatrices = new double** [mPositions.count()]; for (i=0 ; i < (size_t)mPositions.count() ; ++i) { // Allocate mHeight rows on each position mWeightMatrices[i] = new double*[mHeight]; for (j=0 ; j < mHeight ; ++j) { // Allocate mWidth columns on each row mWeightMatrices[i][j] = new double[mWidth]; } } for (y = 0; y < (int) mHeight; ++y) { for (x = 0; x < (int) mWidth; ++x) { for (j = 0; j < (size_t)mPositions.count(); ++j) { mWeightMatrices [j][y][x] = 0.0; for (iy = 0; iy < mCoefficientNumber; ++iy) { mWeightMatrices [j][y][x] += vector1 [iy * mPositions.count() + j] * polyTerm (iy, x, y, mPolynomeOrder); } mWeightMatrices [j][y][x] *= (double) mPositions.count(); } } } }