template<typename MatrixType> bool find_pivot(typename MatrixType::Scalar tol, MatrixType &diffs, Index col=0) { bool match = diffs.diagonal().sum() <= tol; if(match || col==diffs.cols()) { return match; } else { Index n = diffs.cols(); std::vector<std::pair<Index,Index> > transpositions; for(Index i=col; i<n; ++i) { Index best_index(0); if(diffs.col(col).segment(col,n-i).minCoeff(&best_index) > tol) break; best_index += col; diffs.row(col).swap(diffs.row(best_index)); if(find_pivot(tol,diffs,col+1)) return true; diffs.row(col).swap(diffs.row(best_index)); // move current pivot to the end diffs.row(n-(i-col)-1).swap(diffs.row(best_index)); transpositions.push_back(std::pair<Index,Index>(n-(i-col)-1,best_index)); } // restore for(Index k=transpositions.size()-1; k>=0; --k) diffs.row(transpositions[k].first).swap(diffs.row(transpositions[k].second)); } return false; }
typename hash_table<T, Hash, Equals, Allocator>::size_type hash_table<T, Hash, Equals, Allocator>:: find_index(const_reference x) const { size_type n = capacity(); size_type i = best_index(x); size_type j = i; do { const auto &slot = _vector[j]; if ((slot.status == slot_type::free) || (slot.status == slot_type::erased)) { break; } if (equals(slot.value(), x)) { return j; } j = (j + 1) % n; } while (i != j); return eof(); }
void RockPhysicsInversion4D::SolveGEVProblem(NRLib::Matrix sigma_prior, NRLib::Matrix sigma_posterior, NRLib::Matrix & vOut){ //Compute transforms v1, v2, v3 and v4 ---------------------------------------- // computing filter NRLib::SymmetricMatrix sigma_priorInv(6); for(int i=0;i<6;i++) for(int j=0;j<=i;j++) sigma_priorInv(j,i)=sigma_prior(i,j); NRLib::CholeskyInvert(sigma_priorInv); NRLib::Matrix eye = NRLib::IdentityMatrix(6); NRLib::Matrix filter = eye- sigma_priorInv*sigma_posterior; // computing components NRLib::Vector eigen_values(6); NRLib::Matrix eigen_vectors(6,6); NRLib::ComputeEigenVectors( filter ,eigen_values,eigen_vectors); // extracting four best components std::vector<int> current_index(6); current_index[0] = 0;current_index[1] = 1; current_index[2] = 2;current_index[3] = 3; current_index[4] = 4;current_index[5] = 5; std::vector<int> best_index(4); std::vector<int> keep_index(6); keep_index = current_index; NRLib::Vector current_value(6); NRLib::Vector keep_value(6); keep_value = eigen_values; for(int i=0;i<4;i++){ current_index=keep_index; current_value =keep_value; double maxVal=-999; // (the theoretical max value is less than 1 and larger than zero) for(int j=0;j<6-i;j++){ if(current_value(j) > maxVal){ maxVal=current_value(j); best_index[i]=current_index[j]; } } int counter=0; for(int j=0;j<6-i;j++){ if(current_value(j) != maxVal){ keep_value(counter)=current_value(j); keep_index[counter]=current_index[j]; counter++; } } } vOut.resize(6,4); for(int i=0;i<4;i++) for(int j=0;j<6;j++) vOut(j,i)=eigen_vectors(j,best_index[i]); }
typename hash_table<T, Hash, Equals, Allocator>::size_type hash_table<T, Hash, Equals, Allocator>:: nice_index(const_reference x) const { size_type n = capacity(); size_type i = best_index(x); size_type j = i; do { const auto &slot = _vector[j]; switch (slot.status) { case slot_type::free: case slot_type::erased: return j; case slot_type::busy: j = ((j + 1) % n); } } while (i != j); return eof(); }