/***************************************************************************** * Name: distance * Input: point p point q func * Description: distance from p to q. * Output: computes the distance from p to q using a distance * function passed as parameter. * Complexity: O(1) *****************************************************************************/ TYPE distance( struct Point_T *p, struct Point_T *q) { TYPE dist=0.0; // Compute distance. dist = Euclidean( p, q); return(dist); }
int main (int argc, const char * argv[]) { int a, b, gcd; a = atoi(argv[1]); b = atoi(argv[2]); printf("Input: %d, %d\n", a, b); printf("Computing by Euclidean algorithm...\n"); gcd = Euclidean(a, b); printf("GCD: %d\n", gcd); // printf("Computing by naive enumeration algorithm...\n"); // gcd = Naive(a, b); // printf("GCD: %d\n", gcd); return 0; }
vector< Claster* > Forel::clustering(float R) { int M = rand() % this->claster->objects.size(); vector< Claster* > after_clustering; //vector of clustering that will be got after clustering Claster* new_claster = NULL, *ptr = NULL; vector<float> center_prew; vector<float> center_new; vector<vector<float> >::iterator del_vec; int abs_val = 0; bool is_same; while (!this->claster->objects.empty()) { center_prew = this->claster->objects[M]; do { if(new_claster != NULL) { delete(new_claster); new_claster = NULL; } new_claster = new Claster(); //new sphere new_claster->propnum = this->claster->propnum; for(int i=0;i<this->claster->objects.size(); ++i) //forming a sphere(new claster) { if ( Euclidean(center_prew, this->claster->objects[i], center_prew.size() ) <= R) //if distance from center { //of a shphere to curent object <= R than add object to new sphere new_claster->add(this->claster->objects[i]); } } center_new = new_claster->count_center(); //counting new center of a shpere abs_val = 0; for (int i=0;i<this->claster->propnum;++i) { abs_val += fabs(center_new[i] - center_prew[i]); //the difference between new and previous centers } center_prew = center_new; // }while(abs_val > 0.005); }while(abs_val!=0); ptr = new Claster(); *ptr = *new_claster; after_clustering.push_back(ptr); //saving new claster //deleting ready claster from input claster for (int k=0; k<new_claster->objects.size(); ++k) //by number of objects { del_vec = this->claster->objects.begin(); for (int i=0; i<this->claster->objects.size(); ++i) //by number of objects { is_same = true; for (int j=0;j<this->claster->objects[i].size();++j) //by number of properties { if (this->claster->objects[i][j] != new_claster->objects[k][j]) { is_same = false; } } if (is_same == true) { this->claster->objects.erase(del_vec); break; } ++del_vec; } } //end deleting if (!this->claster->objects.empty()) M = rand() % this->claster->objects.size(); //new center of new claster(sphere) } return after_clustering; }
// Initialize global alignment of all images. int initGlobalAlign(const vector<FeatureSet> &fs, int minMatches, MotionModel m, float f, int width, int height, int nRANSAC, double RANSACthresh, AlignMatrix &am, vector<CTransform3x3> &ms) { int n = fs.size(); CTransform3x3 transMat; vector<FeatureMatch> matches; // create the n-by-n alignment matrix am.resize(n); for (int i=0; i<n; i++) am[i].resize(n); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (i != j) { printf("matching image %d with image %d, ", i, j); // BEGIN TODO // write code to fill in the information for am[i][j] // // you'll need to call your feature matching routine, // then your pair alignment routine matches.clear(); int count = 0; FeatureSet f1 = fs[i]; int m = f1.size(); FeatureSet f2 = fs[j]; int n = f2.size(); double d; double dBest[2]; int idBest; FeatureMatch feamatch; for (int i = 0; i < m; i++) { dBest[0] = 1e64; dBest[1] = 1e64 + 1; idBest = 0; for (int j = 0; j < n; j++) { d = Euclidean(f1[i], f2[j]); if (d < dBest[0]) { dBest[1] = dBest[0]; dBest[0] = d; idBest = f2[j].id; } else if (d < dBest[1]) { dBest[1] = d; } } if (sqrt(dBest[0] / dBest[1]) < 1.0) { feamatch.id = idBest; matches.push_back(feamatch); count++; } else { feamatch.id = -1; matches.push_back(feamatch); } } if (count < minMatches) { am[i][j].matches = matches; am[i][j].inliers.clear(); am[i][j].r = transMat; } else { am[i][j].matches = matches; alignImagePair(fs[i], fs[j], matches, eRotate3D, f, width, height, nRANSAC, RANSACthresh, transMat, am[i][j].inliers); am[i][j].r = transMat; } // END TODO printf("%d inliers\n", am[i][j].inliers.size()); if ((int) am[i][j].inliers.size() < minMatches) am[i][j].inliers.clear(); } } } vector<AlignmentImage> nodes(n); for (int i=1; i<n; i++) { nodes[i].added = false; nodes[i].nBest = 0; nodes[i].imageID = i; nodes[i].parentID = -1; } // create the image heap ImageHeap heap(nodes); // add the first image and update the match quality of // its neighbors nodes[0].added = true; for (int j=1; j<n; j++) { int nMatches = am[0][j].inliers.size(); if (nodes[j].nBest < nMatches) { heap.increaseKey(nodes[j].heapIndex, nMatches); nodes[j].parentID = 0; } } AlignmentImage *nextImage; // add the rest of the images for (int i=0; i<n-1; i++) { nextImage = heap.extractMax(); if (nextImage->nBest == 0) { // image set seems to be disconnected return -1; } nextImage->added = true; int id = nextImage->imageID; int pid = nextImage->parentID; // compute the global alignment of the extracted image nextImage->r = am[pid][id].r * nodes[pid].r; // update the match quality for its neighbor images for (int j=0; j<n; j++) { if ((id != j) && (!nodes[j].added)) { int nMatches = am[id][j].inliers.size(); if (nodes[j].nBest < nMatches) { heap.increaseKey(nodes[j].heapIndex, nMatches); nodes[j].parentID = id; } } } } ms.clear(); // put the global transformations into the output array for (int i=0; i<n; i++) { ms.push_back(nodes[i].r); } return 0; }
bool UnitTests::WeightedDataMatrixMothur() { std::vector< std::vector<double> > dissMatrix; // weighted Bray-Curtis DiversityCalculator BC("../unit-tests/DataMatrixMothur.env", "", "Bray-Curtis", 1000, true, false, false, false, false); BC.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.8)) return false; if(!Compare(dissMatrix[2][0], 0.6)) return false; if(!Compare(dissMatrix[2][1], 0.8)) return false; // weighted Canberra DiversityCalculator Canberra("../unit-tests/DataMatrixMothur.env", "", "Canberra", 1000, true, false, false, false, false); Canberra.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 6.35152)) return false; if(!Compare(dissMatrix[2][0], 8.11111)) return false; if(!Compare(dissMatrix[2][1], 5.92063)) return false; // weighted Gower DiversityCalculator Gower("../unit-tests/DataMatrixMothur.env", "", "Gower", 1000, true, false, false, false, false); Gower.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 6.58333)) return false; if(!Compare(dissMatrix[2][0], 7.88889)) return false; if(!Compare(dissMatrix[2][1], 5.52778)) return false; // weighted Hellinger DiversityCalculator Hellinger("../unit-tests/DataMatrixMothur.env", "", "Hellinger", 1000, true, false, false, false, false); Hellinger.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 1.13904)) return false; if(!Compare(dissMatrix[2][0], 1.05146)) return false; if(!Compare(dissMatrix[2][1], 1.17079)) return false; // weighted Manhattan DiversityCalculator Manhattan("../unit-tests/DataMatrixMothur.env", "", "Manhattan", 1000, true, false, false, false, false); Manhattan.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 1.6)) return false; if(!Compare(dissMatrix[2][0], 1.2)) return false; if(!Compare(dissMatrix[2][1], 1.6)) return false; // weighted Morisita-Horn DiversityCalculator MH("../unit-tests/DataMatrixMothur.env", "", "Morisita-Horn", 1000, true, false, false, false, false); MH.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.873239)) return false; if(!Compare(dissMatrix[2][0], 0.333333)) return false; if(!Compare(dissMatrix[2][1], 0.859155)) return false; // weighted Soergel DiversityCalculator Soergel("../unit-tests/DataMatrixMothur.env", "", "Soergel", 1000, true, false, false, false, false); Soergel.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.88888901)) return false; if(!Compare(dissMatrix[2][0], 0.75)) return false; if(!Compare(dissMatrix[2][1], 0.88888901)) return false; // weighted species profile /* DiversityCalculator SP("../unit-tests/DataMatrixMothur.env", "", "Species profile", 1000, true, false, false, false, false); SP.Dissimilarity("../unit-tests/temp.diss"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.78740102)) return false; if(!Compare(dissMatrix[2][0], 0.44721401)) return false; if(!Compare(dissMatrix[2][1], 0.78102499)) return false; */ // weighted Chi-squared // Note: EBD uses a slightly different form of the Chi-squared measure as suggested in Numerical Ecology by Legendre adn Legendre // Nonetheless, it is easy to verify this using mothur. Simply divide by sqrt(N), N is the total number of sequences. DiversityCalculator ChiSquared("../unit-tests/DataMatrixMothur.env", "", "Chi-squared", 1000, true, false, false, false, false); ChiSquared.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 1.0973200)) return false; if(!Compare(dissMatrix[2][0], 0.96513098)) return false; if(!Compare(dissMatrix[2][1], 1.1147900)) return false; // weighted Euclidean DiversityCalculator Euclidean("../unit-tests/DataMatrixMothur.env", "", "Euclidean", 1000, true, false, false, false, false); Euclidean.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.78740102)) return false; if(!Compare(dissMatrix[2][0], 0.44721401)) return false; if(!Compare(dissMatrix[2][1], 0.78102499)) return false; // weighted Kulczynski DiversityCalculator Kulczynski("../unit-tests/DataMatrixMothur.env", "", "Kulczynski", 1000, true, false, false, false, false); Kulczynski.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.8)) return false; if(!Compare(dissMatrix[2][0], 0.6)) return false; if(!Compare(dissMatrix[2][1], 0.8)) return false; // weighted Pearson DiversityCalculator uPearson("../unit-tests/DataMatrixMothur.env", "", "Pearson", 1000, true, false, false, false, false); uPearson.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 1.22089)) return false; if(!Compare(dissMatrix[2][0], 0.5)) return false; if(!Compare(dissMatrix[2][1], 1.2008)) return false; // weighted Yue-Clayton DiversityCalculator YC("../unit-tests/DataMatrixMothur.env", "", "YueClayton", 1000, true, false, false, false, false); YC.Dissimilarity("../unit-tests/temp", "UPGMA"); ReadDissMatrix("../unit-tests/temp.diss", dissMatrix); if(!Compare(dissMatrix[1][0], 0.93233103)) return false; if(!Compare(dissMatrix[2][0], 0.5)) return false; if(!Compare(dissMatrix[2][1], 0.92424202)) return false; return true; }
int InRangeEuclidean(value *valueX, value *valueY, double R) { return (Euclidean(valueX, valueY) <= R); }
int EqualEuclidean(value *valueX, value *valueY) { return (Euclidean(valueX, valueY) == 0); }