source:trunk/yat/statistics/EuclideanDistance.h@1706

Last change on this file since 1706 was 1487, checked in by Jari Häkkinen, 13 years ago

• Property svn:eol-style set to native
• Property svn:keywords set to Id
File size: 2.8 KB
Line
1#ifndef theplu_yat_statistics_euclidean_distance_h
2#define theplu_yat_statistics_euclidean_distance_h
3
4// $Id: EuclideanDistance.h 1487 2008-09-10 08:41:36Z jari$
5
6/*
7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
8  Copyright (C) 2008 Peter Johansson, Markus Ringnér
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "AveragerPair.h"
27#include "AveragerPairWeighted.h"
28#include "yat/utility/iterator_traits.h"
29
30#include <cmath>
31
32namespace theplu {
33namespace yat {
34namespace statistics {
35
36  ///
37  /// @brief Calculates the Euclidean distance between two points
38  /// given by elements of ranges.
39  ///
40  /// This class is modelling the concept \ref concept_distance.
41  ///
42  ///
43  struct EuclideanDistance
44  {
45    /**
46       \brief Calculates the Euclidean distance between elements of
47       two ranges.
48
49       If elements of both ranges are unweighted the distance is
50       calculated as \f$\sqrt{\sum (x_i-y_i)^2 } \f$, where \f$x_i 51 \f$ and \f$y_i \f$ are elements of the first and second range,
52       respectively.
53
54       If elements of one or both of ranges have weights the distance
55       is calculated as \f$\sqrt{N \sum 56 w_{x,i}w_{y,i}(x_i-y_i)^2/\sum w_{x,i}w_{y,i}} \f$, where \f$N 57 \f$ is the number of elements in the two ranges and \f$w_x \f$
58       and \f$w_y \f$ are weights for the elements of the first and
59       the second range, respectively. If the elements of one of the
60       two ranges are unweighted, the weights for these elements are
61       set to unity.
62    */
63    template <typename Iter1, typename Iter2>
64    double operator()
65    (Iter1 beg1,Iter1 end1, Iter2 beg2) const
66    {
67      return this->distance(beg1, end1, beg2,
68                      typename utility::weighted_if_any2<Iter1,Iter2>::type());
69    }
70
71  private:
72    template <typename Iter1, typename Iter2>
73    double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
74                     utility::unweighted_iterator_tag) const
75    {
76      AveragerPair ap;
78      return sqrt(ap.sum_squared_deviation());
79    }
80
81    template <typename Iter1, typename Iter2>
82    double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
83                     utility::weighted_iterator_tag) const
84    {
85      AveragerPairWeighted ap;