SGMatrix<int32_t> CLocallyLinearEmbedding::get_neighborhood_matrix(SGMatrix<float64_t> distance_matrix, int32_t k) { int32_t i; int32_t N = distance_matrix.num_rows; int32_t* neighborhood_matrix = SG_MALLOC(int32_t, N*k); float64_t max_dist = SGVector<float64_t>::max(distance_matrix.matrix,N*N); CoverTree<LLE_COVERTREE_POINT>* coverTree = new CoverTree<LLE_COVERTREE_POINT>(max_dist); for (i=0; i<N; i++) coverTree->insert(LLE_COVERTREE_POINT(i,distance_matrix)); for (i=0; i<N; i++) { std::vector<LLE_COVERTREE_POINT> neighbors = coverTree->kNearestNeighbors(LLE_COVERTREE_POINT(i,distance_matrix),k+1); for (std::size_t m=1; m<unsigned(k+1); m++) neighborhood_matrix[i*k+m-1] = neighbors[m].point_index; } delete coverTree; return SGMatrix<int32_t>(neighborhood_matrix,k,N); }
SGMatrix<int32_t> CStochasticProximityEmbedding::get_neighborhood_matrix(CDistance* distance, int32_t k, int32_t N, float64_t max_dist) { int32_t i; int32_t* neighborhood_matrix = SG_MALLOC(int32_t, N*k); CoverTree<SPE_COVERTREE_POINT>* coverTree = new CoverTree<SPE_COVERTREE_POINT>(max_dist); for (i=0; i<N; i++) coverTree->insert(SPE_COVERTREE_POINT(i, distance)); for (i=0; i<N; i++) { std::vector<SPE_COVERTREE_POINT> neighbors = coverTree->kNearestNeighbors(SPE_COVERTREE_POINT(i, distance), k+1); for (std::size_t m=1; m<unsigned(k+1); m++) neighborhood_matrix[i*k+m-1] = neighbors[m].m_point_index; } delete coverTree; return SGMatrix<int32_t>(neighborhood_matrix,k,N); }