Changeset 1509 for trunk/yat


Ignore:
Timestamp:
Sep 17, 2008, 10:56:30 PM (13 years ago)
Author:
Peter
Message:

fixes #437 and added support for having known issues in test::Suite

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/normalizer/Spearman.h

    r1508 r1509  
    2121*/
    2222
     23#include "yat/utility/DataIterator.h"
    2324#include "yat/utility/iterator_traits.h"
    2425#include "yat/utility/sort_index.h"
     26#include "yat/utility/WeightIterator.h"
    2527
     28#include <algorithm>
     29#include <functional>
    2630#include <vector>
    2731
     
    5256       \return result + (last-first)
    5357     */
    54     template<typename InputIterator, typename RandomAccessIterator>
    55     RandomAccessIterator operator()(InputIterator first, InputIterator last,
     58    template<typename ForwardIterator, typename RandomAccessIterator>
     59    RandomAccessIterator operator()(ForwardIterator first, ForwardIterator last,
    5660                                    RandomAccessIterator result) const
    5761    {
    58       typename utility::weighted_iterator_traits<InputIterator>::type tag;
     62      typename utility::weighted_iterator_traits<ForwardIterator>::type tag;
    5963      return normalize(first, last, result, tag);
    6064    }
     
    6367  private:
    6468    // unweighted version
    65     template<typename InputIterator, typename RandomAccessIterator>
    66     RandomAccessIterator normalize(InputIterator first, InputIterator last,
     69    template<typename ForwardIterator, typename RandomAccessIterator>
     70    RandomAccessIterator normalize(ForwardIterator first, ForwardIterator last,
    6771                                   RandomAccessIterator result,
    6872                                   utility::unweighted_iterator_tag) const
     
    7882
    7983    // weighted version
    80     template<typename InputIterator, typename RandomAccessIterator>
    81     RandomAccessIterator normalize(InputIterator first, InputIterator last,
     84    template<typename ForwardIterator, typename RandomAccessIterator>
     85    RandomAccessIterator normalize(ForwardIterator first, ForwardIterator last,
    8286                                   RandomAccessIterator result,
    8387                                   utility::weighted_iterator_tag) const
    8488    {
    85       return result + std::distance(first, last);
     89      std::copy(utility::weight_iterator(first),
     90                utility::weight_iterator(last),
     91                utility::weight_iterator(result));
     92      // set values with w=0 to 0 to avoid problems with NaNs
     93      utility::iterator_traits<ForwardIterator> forward_trait;
     94      for (ForwardIterator i=first; i!=last; ++i)
     95        if (forward_trait.weight(i)==0)
     96          forward_trait.data(i)=0.0;
     97
     98      std::vector<size_t> index(std::distance(first, last));
     99      utility::sort_index(utility::data_iterator(first),
     100                          utility::data_iterator(last), index);
     101      utility::iterator_traits<RandomAccessIterator> trait;
     102      trait.data(result+index[0])=0;
     103      for (size_t i=1; i<index.size(); ++i)
     104        trait.data(result+index[i]) =
     105          trait.data(result+index[i-1]) + trait.weight(result+index[i-1]);
     106      size_t n = std::distance(first, last);
     107      double w_sum = trait.data(result+index.back()) +
     108        trait.weight(result+index.back());
     109      std::transform(utility::data_iterator(result),
     110                     utility::data_iterator(result+n),
     111                     utility::data_iterator(result),
     112                     std::bind2nd(std::divides<double>(), w_sum));
     113      return result + n;
    86114    }
    87115
Note: See TracChangeset for help on using the changeset viewer.