source: trunk/yat/statistics/EuclideanDistance.h

Last change on this file was 3550, checked in by Peter, 6 years ago

Update copyright years. Happy New Year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1#ifndef theplu_yat_statistics_euclidean_distance_h
2#define theplu_yat_statistics_euclidean_distance_h
3
4// $Id: EuclideanDistance.h 3550 2017-01-03 05:41:02Z peter $
5
6/*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
8  Copyright (C) 2010, 2016 Peter Johansson
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
15  License, or (at your option) any later version.
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 "Distance.h"
29
30#include <cmath>
31
32namespace theplu {
33namespace yat {
34namespace statistics {
35
36  /**
37     \brief Calculates the Euclidean distance between elements of
38     two ranges.
39
40     If elements of both ranges are unweighted the distance is
41     calculated as \f$ \sqrt{\sum (x_i-y_i)^2 } \f$, where \f$ x_i
42     \f$ and \f$ y_i \f$ are elements of the first and second range,
43     respectively.
44
45     If elements of one or both of ranges have weights the distance
46     is calculated as \f$ \sqrt{N \sum
47     w_{x,i}w_{y,i}(x_i-y_i)^2/\sum w_{x,i}w_{y,i}} \f$, where \f$ N
48     \f$ is the number of elements in the two ranges and \f$ w_x \f$
49     and \f$ w_y \f$ are weights for the elements of the first and
50     the second range, respectively. If the elements of one of the
51     two ranges are unweighted, the weights for these elements are
52     set to unity.
53  */
54  class EuclideanDistance : public Distance<EuclideanDistance>
55  {
56  private:
57    friend class Distance<EuclideanDistance>;
58
59    template <typename Iter1, typename Iter2>
60    double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
61                     utility::unweighted_iterator_tag) const
62    {
63      AveragerPair ap;
64      add(ap,beg1,end1,beg2);
65      return sqrt(ap.sum_squared_deviation());
66    }
67
68    template <typename Iter1, typename Iter2>
69    double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
70                     utility::weighted_iterator_tag) const
71    {
72      AveragerPairWeighted ap;
73      add(ap,beg1,end1,beg2);
74      return sqrt(std::distance(beg1,end1)*ap.msd());
75    }
76
77  };
78
79}}} // of namespace statistics, yat, and theplu
80
81#endif
Note: See TracBrowser for help on using the repository browser.