/* * OrthogonalSummation_impl.hpp * * Created on: Jun 25, 2012 * Author: heber */ #ifndef ORTHOGONALSUMMATION_IMPL_HPP_ #define ORTHOGONALSUMMATION_IMPL_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/IteratorAdaptors.hpp" #include "CodePatterns/Log.hpp" /** Constructor of class OrthogonalSummation. * */ template OrthogonalSummation::OrthogonalSummation( InputSets_t &indices, InputValues_t& values, SubsetMap::ptr _subsetmap) : subsetmap(_subsetmap) { ASSERT( indices.size() == values.size(), "OrthogonalSummation::OrthogonalSummation() - indices and values mismatch in size: " +toString(indices.size())+" != "+toString(values.size())+"."); /// place each index /// create own map if none is given if (!subsetmap) { typename InputSets_t::iterator iter = indices.begin(); IndexSetContainer container(*iter); for (; iter != indices.end(); ++iter) container.insert(*iter); subsetmap.reset(new SubsetMap(container)); } else { LOG(1, "INFO: Using given SubsetMap."); } /// instantiate all SubSetValue's by requesting the IndexSet from the Subsetmap typename InputSets_t::iterator indexiter = indices.begin(); typename InputValues_t::iterator valueiter = values.begin(); for (;valueiter != values.end(); ++indexiter, ++valueiter) { LOG(1, "INFO: Adding set " << **indexiter << " with value " << *valueiter << "."); setvalues.addValue( *indexiter, *valueiter ); } /// bind static lookup functions for SetValue SetValue::lookupSubset = boost::bind(&SubsetMap::getSubsets, boost::ref(*subsetmap), _1); SetValue::lookupValue = boost::bind(&SetValueMap::getValue, boost::ref(setvalues), _1); } template T OrthogonalSummation::operator()() const { return Sum(); } template T OrthogonalSummation::Sum() const { typename SetValueMap::const_iterator iter = setvalues.begin(); T sum = (iter->second)->getContribution(); LOG(1, "DEBUG: Contribution from subset "+toString(*(iter->second->getIndexSet())) +" is "+toString(sum)+"."); for(++iter;iter != setvalues.end(); ++iter) { const T tempvalue = (iter->second)->getContribution(); sum += tempvalue; LOG(1, "DEBUG: Contribution from subset "+toString(*(iter->second->getIndexSet())) +" is "+toString(tempvalue)+"."); } return sum; } #endif /* ORTHOGONALSUMMATION_IMPL_HPP_ */