| 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 | /** Constructor of class OrthogonalSummation. | 
|---|
| 21 | * | 
|---|
| 22 | */ | 
|---|
| 23 | template <class T> | 
|---|
| 24 | OrthogonalSummation<T>::OrthogonalSummation( | 
|---|
| 25 | InputSets_t &indices, | 
|---|
| 26 | InputValues_t& values, | 
|---|
| 27 | SubsetMap::ptr _subsetmap) : | 
|---|
| 28 | subsetmap(_subsetmap) | 
|---|
| 29 | { | 
|---|
| 30 | ASSERT( indices.size() == values.size(), | 
|---|
| 31 | "OrthogonalSummation<T>::OrthogonalSummation() - indices and values mismatch in size: " | 
|---|
| 32 | +toString(indices.size())+" != "+toString(values.size())+"."); | 
|---|
| 33 | /// place each index | 
|---|
| 34 | /// create own map if none is given | 
|---|
| 35 | if (!subsetmap) { | 
|---|
| 36 | typename InputSets_t::iterator iter = indices.begin(); | 
|---|
| 37 | IndexSetContainer container(*iter); | 
|---|
| 38 | for (; iter != indices.end(); ++iter) | 
|---|
| 39 | container.insert(*iter); | 
|---|
| 40 | subsetmap.reset(new SubsetMap(container)); | 
|---|
| 41 | } else { | 
|---|
| 42 | LOG(1, "INFO: Using given SubsetMap."); | 
|---|
| 43 | } | 
|---|
| 44 | /// instantiate all SubSetValue's by requesting the IndexSet from the Subsetmap | 
|---|
| 45 | typename InputSets_t::iterator indexiter = indices.begin(); | 
|---|
| 46 | typename InputValues_t::iterator valueiter = values.begin(); | 
|---|
| 47 | for (;valueiter != values.end(); ++indexiter, ++valueiter) { | 
|---|
| 48 | LOG(1, "INFO: Adding set " << **indexiter << " with value " << *valueiter << "."); | 
|---|
| 49 | setvalues.addValue( *indexiter, *valueiter ); | 
|---|
| 50 | } | 
|---|
| 51 | /// bind static lookup functions for SetValue<T> | 
|---|
| 52 | SetValue<T>::lookupSubset = | 
|---|
| 53 | boost::bind(&SubsetMap::getSubsets, boost::ref(*subsetmap), _1); | 
|---|
| 54 | SetValue<T>::lookupValue = | 
|---|
| 55 | boost::bind(&SetValueMap<T>::getValue, boost::ref(setvalues), _1); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | template <class T> | 
|---|
| 59 | T OrthogonalSummation<T>::operator()() const | 
|---|
| 60 | { | 
|---|
| 61 | return Sum(); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | template <class T> | 
|---|
| 65 | T OrthogonalSummation<T>::Sum() const | 
|---|
| 66 | { | 
|---|
| 67 | typename SetValueMap<T>::const_iterator iter = setvalues.begin(); | 
|---|
| 68 | T sum = (iter->second)->getContribution(); | 
|---|
| 69 | LOG(1, "DEBUG: Contribution from subset "+toString(*(iter->second->getIndexSet())) | 
|---|
| 70 | +" is "+toString(sum)+"."); | 
|---|
| 71 | for(++iter;iter != setvalues.end(); ++iter) { | 
|---|
| 72 | const T tempvalue = (iter->second)->getContribution(); | 
|---|
| 73 | sum += tempvalue; | 
|---|
| 74 | LOG(1, "DEBUG: Contribution from subset "+toString(*(iter->second->getIndexSet())) | 
|---|
| 75 | +" is "+toString(tempvalue)+"."); | 
|---|
| 76 | } | 
|---|
| 77 | return sum; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 | #endif /* ORTHOGONALSUMMATION_IMPL_HPP_ */ | 
|---|