| 1 | /*
 | 
|---|
| 2 |  * OrthogonalSummation_impl.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jun 25, 2012
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef ORTHOGONALSUMMATION_IMPL_HPP_
 | 
|---|
| 9 | #define ORTHOGONALSUMMATION_IMPL_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | // include config.h
 | 
|---|
| 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 14 | #include <config.h>
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include "CodePatterns/IteratorAdaptors.hpp"
 | 
|---|
| 18 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Fragmentation/Summation/OrthogonalSummation.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "Fragmentation/Summation/ZeroInstance.hpp"
 | 
|---|
| 23 | #include "Fragmentation/Summation/printKeyNames.hpp"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /** Constructor of class OrthogonalSummation.
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | template <class T>
 | 
|---|
| 29 | OrthogonalSummation<T>::OrthogonalSummation(
 | 
|---|
| 30 |     InputSets_t &indices,
 | 
|---|
| 31 |     InputValues_t& values,
 | 
|---|
| 32 |     SubsetMap::ptr _subsetmap) :
 | 
|---|
| 33 |   subsetmap(_subsetmap),
 | 
|---|
| 34 |   zeroinstance(ZeroInstance<T>())
 | 
|---|
| 35 | {
 | 
|---|
| 36 |   ASSERT( indices.size() == values.size(),
 | 
|---|
| 37 |       "OrthogonalSummation<T>::OrthogonalSummation() - indices and values mismatch in size: "
 | 
|---|
| 38 |       +toString(indices.size())+" != "+toString(values.size())+".");
 | 
|---|
| 39 |   /// place each index
 | 
|---|
| 40 |   /// create own map if none is given
 | 
|---|
| 41 |   if (!subsetmap) {
 | 
|---|
| 42 |     typename InputSets_t::iterator iter = indices.begin();
 | 
|---|
| 43 |     IndexSetContainer container(*iter);
 | 
|---|
| 44 |     for (; iter != indices.end(); ++iter)
 | 
|---|
| 45 |       container.insert(*iter);
 | 
|---|
| 46 |     subsetmap.reset(new SubsetMap(container));
 | 
|---|
| 47 |   } else {
 | 
|---|
| 48 |     LOG(2, "DEBUG: Using given SubsetMap.");
 | 
|---|
| 49 |   }
 | 
|---|
| 50 |   /// instantiate all SubSetValue's by requesting the IndexSet from the Subsetmap
 | 
|---|
| 51 |   typename InputSets_t::iterator indexiter = indices.begin();
 | 
|---|
| 52 |   typename InputValues_t::iterator valueiter = values.begin();
 | 
|---|
| 53 |   for (;valueiter != values.end(); ++indexiter, ++valueiter) {
 | 
|---|
| 54 |     LOG(2, "DEBUG: Adding set " << **indexiter << " with value " << *valueiter << ".");
 | 
|---|
| 55 |     setvalues.addValue( *indexiter, *valueiter );
 | 
|---|
| 56 |   }
 | 
|---|
| 57 |   /// bind static lookup functions for SetValue<T>
 | 
|---|
| 58 |   SetValue<T>::lookupSubset =
 | 
|---|
| 59 |       boost::bind(&SubsetMap::getSubsets, boost::ref(*subsetmap), _1);
 | 
|---|
| 60 |   SetValue<T>::lookupValue =
 | 
|---|
| 61 |       boost::bind(&SetValueMap<T>::getValue, boost::ref(setvalues), _1);
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | template <class T>
 | 
|---|
| 65 | T OrthogonalSummation<T>::operator()(const size_t level) const
 | 
|---|
| 66 | {
 | 
|---|
| 67 |   return Sum(level);
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | template <class T>
 | 
|---|
| 71 | T OrthogonalSummation<T>::Sum(const size_t level) const
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   T sum(zeroinstance);
 | 
|---|
| 74 |   for(typename SetValueMap<T>::const_iterator iter = setvalues.begin();
 | 
|---|
| 75 |       (iter != setvalues.end()) && (iter->first->size() <= level); ++iter) {
 | 
|---|
| 76 |     const T tempvalue = (iter->second)->getContribution();
 | 
|---|
| 77 |     sum += tempvalue;
 | 
|---|
| 78 |     LOG(3, "DEBUG: Contribution from subset "+toString(*(iter->second->getIndexSet()))
 | 
|---|
| 79 |         +" is "+toString(tempvalue)+".");
 | 
|---|
| 80 |   }
 | 
|---|
| 81 |   return sum;
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | 
 | 
|---|
| 85 | #endif /* ORTHOGONALSUMMATION_IMPL_HPP_ */
 | 
|---|