ResultType vector_comprehension(const std::vector<T>& in_vec, Function func) { ResultType result; result.reserve(in_vec.size()); for (auto& elem : in_vec) { result.push_back(func(elem)); } return result; }
void mid(const IntervalObject& v, ResultType& result) { if(v.dimension()!=result.dimension()) throw std::range_error("Unequal dimensions in function capd::vectalg::mid"); typename ResultType::iterator i = result.begin(); typename IntervalObject::const_iterator b = v.begin(), e=v.end(); while(b!=e) { *i = mid(*b); ++i; ++b; } }
void apply(const OperandType &operand, ResultType result) const { super::apply(operand, result); ResultType iterand = result; iterand.clear(); solve_bicgstab( m_matrix, m_preconditioner, iterand, operand, this->m_tolerance, this->m_maxIterations, const_cast<unsigned *>(&this->m_lastIterationCount), this->m_debugLevel); result.assign(iterand); }
void rightObject(const IntervalObject &v, ResultType& result) { typename ResultType::iterator i = result.begin(); typename IntervalObject::const_iterator b = v.begin(), e=v.end(); while(b!=e) { *i = b->rightBound(); ++i; ++b; } }
void leftObject(const IntervalObject &v, ResultType& result) { typename ResultType::iterator i = result.begin(); typename IntervalObject::const_iterator b = v.begin(), e=v.end(); while(b!=e) { *i = b->leftBound(); ++i; ++b; } // std::transform(v.begin(), v.end(), result.begin(), leftBound<ScalarType> ); }
void apply(const OperandType &operand, ResultType result) const { super::apply(operand, result); typedef typename real_operator::result_type::value_type real_t; typedef std::complex<real_t> complex_t; typename real_operator::operand_type operand_real(real(operand)), operand_imag(imag(operand)); typename real_operator::result_type result_real_1(real(result)), result_imag_1(imag(result)), result_real_2(real(result)), result_imag_2(imag(result)); m_real.apply(operand_real, result_real_1); m_imaginary.apply(operand_imag, result_real_2); result_real_2 *= -1; m_imaginary.apply(operand_real, result_imag_1); m_real.apply(operand_imag, result_imag_2); OperandType result_imag_12 = result_imag_1 + result_imag_2; result.assign(result_real_1 + result_real_2 + complex_t(0,1) * result_imag_12); }
void fastSparseProduct(const Lhs& lhs, const Rhs& rhs, ResultType& res) { // initialize result res = ResultType(lhs.rows(), rhs.cols()); // if one of the matrices does not contain non zero elements // the result will only contain an empty matrix if( lhs.nonZeros() == 0 || rhs.nonZeros() == 0 ) return; typedef typename Eigen::internal::remove_all<Lhs>::type::Scalar Scalar; typedef typename Eigen::internal::remove_all<Lhs>::type::Index Index; // make sure to call innerSize/outerSize since we fake the storage order. Index rows = lhs.innerSize(); Index cols = rhs.outerSize(); eigen_assert(lhs.outerSize() == rhs.innerSize()); std::vector<bool> mask(rows,false); Eigen::Matrix<Scalar,Eigen::Dynamic,1> values(rows); Eigen::Matrix<Index, Eigen::Dynamic,1> indices(rows); // estimate the number of non zero entries // given a rhs column containing Y non zeros, we assume that the respective Y columns // of the lhs differs in average of one non zeros, thus the number of non zeros for // the product of a rhs column with the lhs is X+Y where X is the average number of non zero // per column of the lhs. // Therefore, we have nnz(lhs*rhs) = nnz(lhs) + nnz(rhs) Index estimated_nnz_prod = lhs.nonZeros() + rhs.nonZeros(); res.setZero(); res.reserve(Index(estimated_nnz_prod)); //const Scalar epsilon = std::numeric_limits< Scalar >::epsilon(); const Scalar epsilon = 0.0; // we compute each column of the result, one after the other for (Index j=0; j<cols; ++j) { Index nnz = 0; for (typename Rhs::InnerIterator rhsIt(rhs, j); rhsIt; ++rhsIt) { const Scalar y = rhsIt.value(); for (typename Lhs::InnerIterator lhsIt(lhs, rhsIt.index()); lhsIt; ++lhsIt) { const Scalar val = lhsIt.value() * y; if( std::abs( val ) > epsilon ) { const Index i = lhsIt.index(); if(!mask[i]) { mask[i] = true; values[i] = val; indices[nnz] = i; ++nnz; } else values[i] += val; } } } if( nnz > 1 ) { // sort indices for sorted insertion to avoid later copying QuickSort< 1 >::sort( indices.data(), indices.data()+nnz ); } res.startVec(j); // ordered insertion // still using insertBackByOuterInnerUnordered since we know what we are doing for(Index k=0; k<nnz; ++k) { const Index i = indices[k]; res.insertBackByOuterInnerUnordered(j,i) = values[i]; mask[i] = false; } } res.finalize(); }
std::vector<std::vector<int>> GetCombinationsIterative(const std::vector<int>& input) { typedef std::vector<std::vector<int>> ResultType; typedef std::vector<int> InputType; typedef std::vector<std::tuple<int, InputType>> ProblemType; typedef std::map<int, ProblemType> ProblemListType; typedef std::queue<InputType> LevelResultsType; ResultType result; ProblemListType problemTree; int currentLevel = input.size(); int startLevel = currentLevel; problemTree[currentLevel] = ProblemType { std::tuple<int, InputType> {0, input} }; InputType temp = input; int nodeNr = 1; while (currentLevel > 2) { problemTree[currentLevel - 1] = ProblemType{}; const auto& nodes = problemTree[currentLevel]; for (const auto& node : nodes) { temp = std::get<1>(node); for (const auto& element : temp) { std::vector<int> diffSet = temp; diffSet.erase(std::find(diffSet.begin(), diffSet.end(), element)); problemTree[currentLevel - 1].push_back(std::make_tuple(element, diffSet)); } } currentLevel = currentLevel - 1; } LevelResultsType resultsCurrentLevel; LevelResultsType resultsPreviousLevel; while (currentLevel < startLevel) { resultsCurrentLevel = {}; const auto& nodes = problemTree[currentLevel]; int partitionSize = currentLevel == 2 ? 2 : resultsPreviousLevel.size() / nodes.size(); for (const auto& node : nodes) { temp = std::get<1>(node); if (temp.size() == 2) { resultsPreviousLevel.push({ temp[0], temp[1] }); resultsPreviousLevel.push({ temp[1], temp[0] }); } for (int i = 0; i < partitionSize ; ++i) { auto& previousLevelResult = resultsPreviousLevel.front(); previousLevelResult.insert(previousLevelResult.begin(), std::get<0>(node)); resultsCurrentLevel.push(resultsPreviousLevel.front()); resultsPreviousLevel.pop(); } } resultsPreviousLevel = resultsCurrentLevel; ++currentLevel; } while (!resultsCurrentLevel.empty()) { result.push_back(resultsCurrentLevel.front()); resultsCurrentLevel.pop(); } return result; }
/** Before using apply, operand and result must have the correct size. */ virtual void apply(const OperandType &operand, ResultType result) const { if (size2() != operand.size() || size1() != result.size()) throw std::runtime_error("invalid vector sizes in matrix_operator::apply"); }
void apply(const OperandType &operand, ResultType result) const { super::apply(operand, result); result.assign(m_factor * operand); }
int main(int argc, char * argv[]) { using namespace fbi; using namespace boost::posix_time; #ifdef __LIBFBI_USE_MULTITHREADING__ std::cout << "Multithreading enabled" << std::endl; #endif ProgramOptions options; if (!parseProgramOptions(argc, argv, options)) { return 0; } typedef SetA<Centroid,1,0 > SetType; typedef SetType::ResultType ResultType; typedef unsigned int LabelType; ptime start = microsec_clock::universal_time(); SNSplitter<SetType, LabelType> splitter(options); CentroidBoxGenerator gen(10,1); ResultType test = std::vector<std::vector<unsigned int> >(); boost::function<ResultType(std::deque<Centroid>)> boostFunctor = boost::bind( &SetType::intersect<const std::deque<Centroid>&,const CentroidBoxGenerator&, const CentroidBoxGenerator&> , _1, gen, gen); std::cout << "Looking for overlaps" << std::endl; ResultType fullAdjList = splitter.findOverlaps(boostFunctor); ptime end = microsec_clock::universal_time(); time_duration td = end - start; std::cout << "elapsed time in seconds: " << td.total_seconds() << std::endl; std::cout << "finding connected components... "; std::vector<LabelType> labels; unsigned int nComponents = findConnectedComponents(fullAdjList, labels); fullAdjList.clear(); ResultType().swap(fullAdjList); std::cout << "Rereading centroids file" << std::endl; std::vector<Centroid> centroids; std::ifstream ifs(options.inputfileName_); std::string str; while (std::getline(ifs, str)) { parseString(str, centroids, 0); } ifs.close(); std::vector<unsigned int> counter(nComponents); std::cout << "Creating Xics" << std::endl; std::vector<Xic> xics = createXicVector(centroids.begin(), centroids.end(), labels.begin(), labels.end(), counter); std::ofstream ofs(options.outputfileName_.c_str()); std::cout << "Writing xics to outputfile" << std::endl; for (std::vector<Xic>::size_type i = 0; i < xics.size(); ++i) { if (counter[i] >= options.minClusterSize_) { ofs << xics[i] << "\t" << counter[i] <<std::endl; } } }