static void float_sort_svd_eigenvectors(Data_Obj *u_dp, Data_Obj *w_dp, Data_Obj *v_dp) { //SORT_EIGENVECTORS(float) dimension_t i,n; u_long *index_data; n = OBJ_COLS(w_dp); index_dp = mkvec("svd_tmp_indices",n,1,PREC_UDI); if( index_dp == NULL ){ NWARN("Unable to create index object for sorting SVD eigenvalues"); return; } sort_indices(index_dp,w_dp); /* sorting is done from smallest to largest */ j=0; index_data = OBJ_DATA_PTR(index_dp); /* We should only have to have 1 column of storage to permute things, * but life is simplified if we copy the data... */ for(i=n-1;i>=0;i--){ k= *(index_data+i); } }
void test_mink() { double x[10] = {0, 90, 70, 40, 20, 10, 30, 80, 50, 60}; int idx[5]; mink(x, idx, 10, 5); printf("idx = [ "); int i; for (i = 0; i < 5; i++) printf("%d ", idx[i]); printf("]\n"); // get timing info int n = 100000; double y[n]; for (i = 0; i < n; i++) y[i] = normrand(0,1); int yi[n]; double t = get_time_ms(); sort_indices(y, yi, n); printf("sorted %d numbers in %f ms\n", n, get_time_ms() - t); t = get_time_ms(); mink(y, yi, n, 100); printf("got the min %d numbers in %f ms\n", 100, get_time_ms() - t); }
void test_sort_indices() { double x[10] = {0, 90, 70, 40, 20, 10, 30, 80, 50, 60}; int idx[10]; sort_indices(x, idx, 10); printf("x[idx] = [ "); int i; for (i = 0; i < 10; i++) printf("%f ", x[idx[i]]); printf("]\n"); }
void reproduce( population_type& pop , fitness_type& fitness ) { GPCXX_ASSERT( pop.size() == fitness.size() ); GPCXX_ASSERT( m_rates.size() == m_operators.size() ); GPCXX_ASSERT( m_operators.size() > 0 ); std::vector< size_t > indices; sort_indices( fitness , indices ); population_type new_pop; new_pop.reserve( pop.size() ); // elite for( size_t i=0 ; i<m_number_elite ; ++i ) { index_vector elite_in_indices; index_vector elite_out_indices; size_t index = indices[i] ; elite_in_indices.push_back( index ); elite_out_indices.push_back( new_pop.size() ); new_pop.push_back( pop[ index ] ); m_observer( -1 , elite_in_indices , elite_out_indices ); } size_t n = pop.size(); std::discrete_distribution< int > dist( m_rates.begin() , m_rates.end() ); while( new_pop.size() < n ) { int choice = dist( m_rng ); auto& op = m_operators[ choice ]; auto selection = op.selection( pop , fitness ); index_vector in; for( auto s : selection ) in.push_back( s - pop.begin() ); auto trees = op.operation( selection ); index_vector out; for( auto iter = trees.begin() ; ( iter != trees.end() ) && ( new_pop.size() < n ) ; ++iter ) { out.push_back( new_pop.size() ); m_final_transform( *iter ); new_pop.push_back( std::move( *iter ) ); } m_observer( choice , in , out ); } pop = std::move( new_pop ); }
void TopTerms(const int maxterms, const T* buf_w, const unsigned int ldim, const unsigned int height, const unsigned int width, std::vector<int>& term_indices) { if (height < width) throw std::logic_error("TopTerms: height of W buffer must be >= width"); unsigned int max_terms = maxterms; if (max_terms > height) max_terms = height; std::vector<unsigned int> sort_indices(height); // The term indices for each column will be packed into the term_indices // array. Indices for column 0 will occupy elements 0..maxterms-1. // Indices for the next column will occupy the next maxterm elements, etc. if (term_indices.size() < maxterms*width) term_indices.resize(maxterms*width); for (unsigned int c=0; c<width; ++c) { unsigned int term_offset = c*maxterms; // initialize indices for this column's sort for (unsigned int q=0; q<height; ++q) sort_indices[q] = q; // offset to column c in buf_w unsigned int col_offset = c*height; const T* data = &(buf_w[col_offset]); std::sort(&sort_indices[0], &sort_indices[0] + height, [&data](unsigned int i1, unsigned int i2) {return data[i1] > data[i2];}); for (unsigned int q=0; q<max_terms; ++q) { unsigned int index = sort_indices[q]; assert(index >= 0u); assert(index < height); term_indices[term_offset + q] = index; } } }
Matrix<T> matrix_sort_by_col_sum_symmetric( Matrix<T> const& data ) { if( data.rows() != data.cols() ) { throw std::runtime_error( "Symmetric sort only works on square matrices." ); } auto result = Matrix<T>( data.rows(), data.cols() ); auto col_sums = matrix_col_sums( data ); auto si = sort_indices( col_sums.begin(), col_sums.end() ); for( size_t i = 0; i < data.rows(); ++i ) { for( size_t j = 0; j < data.cols(); ++j ) { result( i, j ) = data( si[i], si[j] ); } } return result; }
std::vector<size_t> ForecastMachine::find_nearest_neighbors(const dmivr_type& dist) { if(nn < 1) { return sort_indices(dist, which_lib); } // else std::vector<size_t> neighbors; std::vector<size_t> nearest_neighbors; double curr_distance; if(nn > log(double(which_lib.size()))) { neighbors = sort_indices(dist, which_lib); std::vector<size_t>::iterator curr_lib; // find nearest neighbors for(curr_lib = neighbors.begin(); curr_lib != neighbors.end(); ++curr_lib) { nearest_neighbors.push_back(*curr_lib); if(nearest_neighbors.size() >= nn) break; } if(curr_lib == neighbors.end()) return nearest_neighbors; double tie_distance = dist.col(nearest_neighbors.back()).coeff(0, 0); // check for ties for(++curr_lib; curr_lib != neighbors.end(); ++curr_lib) { if(dist.col(*curr_lib).coeff(0, 0) > tie_distance) // distance is bigger break; nearest_neighbors.push_back(*curr_lib); // add to nearest neighbors } } else { size_t i; nearest_neighbors.push_back(which_lib[0]); for(auto curr_lib: which_lib) { curr_distance = dist.col(curr_lib).coeff(0, 0); if(curr_distance <= dist.col(nearest_neighbors.back()).coeff(0, 0)) { i = nearest_neighbors.size(); while((i > 0) && (curr_distance < dist.col(nearest_neighbors[i-1]).coeff(0, 0))) { i--; } nearest_neighbors.insert(nearest_neighbors.begin()+i, curr_lib); if((nearest_neighbors.size() > nn) && (dist.col(nearest_neighbors[nn-1]).coeff(0, 0) < dist.col(nearest_neighbors.back()).coeff(0, 0))) { nearest_neighbors.pop_back(); } } } } // filter for max_distance if(epsilon >= 0) { for(auto neighbor_iter = nearest_neighbors.begin(); neighbor_iter != nearest_neighbors.end(); ++neighbor_iter) { if(dist.col(*neighbor_iter).coeff(0, 0) > epsilon) { nearest_neighbors.erase(neighbor_iter, nearest_neighbors.end()); break; } } } return nearest_neighbors; }
//============================================================================= int Amesos_Dscpack::PerformNumericFactorization() { ResetTimer(0); ResetTimer(1); Epetra_RowMatrix* RowMatrixA = Problem_->GetMatrix(); if (RowMatrixA == 0) AMESOS_CHK_ERR(-1); const Epetra_Map& OriginalMap = RowMatrixA->RowMatrixRowMap() ; int numrows = RowMatrixA->NumGlobalRows(); assert( numrows == RowMatrixA->NumGlobalCols() ); // // Call Dscpack to perform Numeric Factorization // std::vector<double> MyANonZ; #if 0 if ( IsNumericFactorizationOK_ ) { DSC_ReFactorInitialize(PrivateDscpackData_->MyDSCObject); } #endif DscRowMap_ = Teuchos::rcp(new Epetra_Map(numrows, NumLocalCols, LocalStructOldNum, 0, Comm())); if (DscRowMap_.get() == 0) AMESOS_CHK_ERR(-1); Importer_ = rcp(new Epetra_Import(DscRowMap(), OriginalMap)); // // Import from the CrsMatrix // Epetra_CrsMatrix DscMat(Copy, DscRowMap(), 0); AMESOS_CHK_ERR(DscMat.Import(*RowMatrixA, Importer(), Insert)); AMESOS_CHK_ERR(DscMat.FillComplete()); DscColMap_ = Teuchos::rcp(new Epetra_Map(DscMat.RowMatrixColMap())); assert( MyDscRank >= 0 || NumLocalNonz == 0 ) ; assert( MyDscRank >= 0 || NumLocalCols == 0 ) ; assert( MyDscRank >= 0 || NumGlobalCols == 0 ) ; MyANonZ.resize( NumLocalNonz ) ; int NonZIndex = 0 ; int max_num_entries = DscMat.MaxNumEntries() ; std::vector<int> col_indices( max_num_entries ) ; std::vector<double> mat_values( max_num_entries ) ; assert( NumLocalCols == DscRowMap().NumMyElements() ) ; std::vector<int> my_global_elements( NumLocalCols ) ; AMESOS_CHK_ERR(DscRowMap().MyGlobalElements( &my_global_elements[0] ) ) ; std::vector<int> GlobalStructOldColNum( NumGlobalCols ) ; typedef std::pair<int, double> Data; std::vector<Data> sort_array(max_num_entries); std::vector<int> sort_indices(max_num_entries); for ( int i = 0; i < NumLocalCols ; i++ ) { assert( my_global_elements[i] == LocalStructOldNum[i] ) ; int num_entries_this_row; #ifdef USE_LOCAL AMESOS_CHK_ERR( DscMat.ExtractMyRowCopy( i, max_num_entries, num_entries_this_row, &mat_values[0], &col_indices[0] ) ) ; #else AMESOS_CHK_ERR( DscMat.ExtractGlobalRowCopy( DscMat.GRID(i), max_num_entries, num_entries_this_row, &mat_values[0], &col_indices[0] ) ) ; #endif int OldRowNumber = LocalStructOldNum[i] ; if (GlobalStructOwner[ OldRowNumber ] == -1) AMESOS_CHK_ERR(-1); int NewRowNumber = GlobalStructNewColNum[ my_global_elements[ i ] ] ; // // Sort the column elements // for ( int j = 0; j < num_entries_this_row; j++ ) { #ifdef USE_LOCAL sort_array[j].first = GlobalStructNewColNum[ DscMat.GCID( col_indices[j])] ; sort_indices[j] = GlobalStructNewColNum[ DscMat.GCID( col_indices[j])] ; #else sort_array[j].first = GlobalStructNewColNum[ col_indices[j] ]; #endif sort_array[j].second = mat_values[j] ; } sort(&sort_array[0], &sort_array[num_entries_this_row]); for ( int j = 0; j < num_entries_this_row; j++ ) { int NewColNumber = sort_array[j].first ; if ( NewRowNumber <= NewColNumber ) MyANonZ[ NonZIndex++ ] = sort_array[j].second ; #ifndef USE_LOCAL assert( NonZIndex <= NumLocalNonz ); // This assert can fail on non-symmetric matrices #endif } } OverheadTime_ = AddTime("Total Amesos overhead time", OverheadTime_, 1); if ( MyDscRank >= 0 ) { const int SchemeCode = 1; #ifndef USE_LOCAL assert( NonZIndex == NumLocalNonz ); #endif AMESOS_CHK_ERR( DSC_NFactor ( PrivateDscpackData_->MyDSCObject_, SchemeCode, &MyANonZ[0], DSC_LLT, DSC_LBLAS3, DSC_DBLAS2 ) ) ; } // if ( MyDscRank >= 0 ) IsNumericFactorizationOK_ = true ; NumFactTime_ = AddTime("Total numeric factorization time", NumFactTime_, 0); return(0); }