int DblMatrix::qtClustering(double thresh, std::vector<DblMatrix>& clusters) { /// initialize the members IntMatrix members; members.assign(size(), IntVector()); ///// each entry is a neighbor of itself for(unsigned int i=0; i<size(); i++) members[i].push_back(i); /// get the other members for(unsigned int j=0; j<size()-1; j++) { for(unsigned int k=j+1; k<size(); k++) { double diff = (*this)[j].EuclDist((*this)[k]); if(diff <= thresh) { members[j].push_back(k); members[k].push_back(j); } } } /// QT clustering std::sort(members.begin(), members.end(), IntVectorGreater); IntMatrix::iterator it1; int n=0; for(it1=members.begin(); it1!=members.end(); it1++, n++) { for(unsigned int j=1; j<it1->size(); j++) { IntMatrix::iterator it2=it1; it2++; for(; it2!=members.end();) { if((*it1)[j]==it2->front()) it2=members.erase(it2); else it2++; } } } /// build groups clusters.clear(); for(unsigned int i=0; i<members.size(); i++) { DblMatrix cluster; for(unsigned int j=0; j<members[i].size(); j++) { cluster.push_back((*this)[members[i][j]]); } clusters.push_back(cluster); } return 0; }
// sub-determinant double DblMatrix::GetHelpDeterminant(int k) { int h=GetHeight(); assert(h+1==GetWidth()); DblMatrix HelpDet; // replace the for(int i=0; i<h; i++) { HelpDet.push_back(DblVector()); for(int j=0; j<h; j++) { if(j==k) HelpDet[i].push_back((*this)[i][h]); else HelpDet[i].push_back((*this)[i][j]); } } return HelpDet.GetDeterminantNxN(); }
// solve linear NxN equation system (cramer) int DblMatrix::SolveLinearCramer(DblVector& Solution) { // get main determinant DblMatrix DetMat; double Det=0.0; int w = GetWidth(); int h = GetHeight(); for(int i=0; i<h; i++) { DetMat.push_back(DblVector()); for(int j=0; j<w-1; j++) DetMat[i].push_back((*this)[i][j]); } Det = DetMat.GetDeterminantNxN(); if(fabs(Det)<THE_EPS_DEF) return 0; // get solutions Solution.clear(); for(int k=0; k<h; k++) Solution.push_back(this->GetHelpDeterminant(k)/Det); return 1; }