1 | #ifndef theplu_yat_statistics_euclidean_distance_h |
---|
2 | #define theplu_yat_statistics_euclidean_distance_h |
---|
3 | |
---|
4 | // $Id: EuclideanDistance.h 1437 2008-08-25 17:55:00Z peter $ |
---|
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 2 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 this program; if not, write to the Free Software |
---|
24 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
25 | 02111-1307, USA. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "AveragerPair.h" |
---|
29 | #include "AveragerPairWeighted.h" |
---|
30 | #include "yat/utility/iterator_traits.h" |
---|
31 | |
---|
32 | #include <cmath> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace statistics { |
---|
37 | |
---|
38 | /// |
---|
39 | /// @brief Calculates the Euclidean distance between two points |
---|
40 | /// given by elements of ranges. |
---|
41 | /// |
---|
42 | /// This class is modelling the concept \ref concept_distance. |
---|
43 | /// |
---|
44 | /// |
---|
45 | struct EuclideanDistance |
---|
46 | { |
---|
47 | /** |
---|
48 | \brief Calculates the Euclidean distance between elements of |
---|
49 | two ranges. |
---|
50 | |
---|
51 | If elements of both ranges are unweighted the distance is |
---|
52 | calculated as \f$ \sqrt{\sum (x_i-y_i)^2 } \f$, where \f$ x_i |
---|
53 | \f$ and \f$ y_i \f$ are elements of the first and second range, |
---|
54 | respectively. |
---|
55 | |
---|
56 | If elements of one or both of ranges have weights the distance |
---|
57 | is calculated as \f$ \sqrt{N \sum |
---|
58 | w_{x,i}w_{y,i}(x_i-y_i)^2/\sum w_{x,i}w_{y,i}} \f$, where \f$ N |
---|
59 | \f$ is the number of elements in the two ranges and \f$ w_x \f$ |
---|
60 | and \f$ w_y \f$ are weights for the elements of the first and |
---|
61 | the second range, respectively. If the elements of one of the |
---|
62 | two ranges are unweighted, the weights for these elements are |
---|
63 | set to unity. |
---|
64 | */ |
---|
65 | template <typename Iter1, typename Iter2> |
---|
66 | double operator() |
---|
67 | (Iter1 beg1,Iter1 end1, Iter2 beg2) const |
---|
68 | { |
---|
69 | return this->distance(beg1, end1, beg2, |
---|
70 | typename utility::weighted_if_any2<Iter1,Iter2>::type()); |
---|
71 | } |
---|
72 | |
---|
73 | private: |
---|
74 | template <typename Iter1, typename Iter2> |
---|
75 | double distance (Iter1 beg1,Iter1 end1, Iter2 beg2, |
---|
76 | utility::unweighted_iterator_tag) const |
---|
77 | { |
---|
78 | AveragerPair ap; |
---|
79 | add(ap,beg1,end1,beg2); |
---|
80 | return sqrt(ap.sum_squared_deviation()); |
---|
81 | } |
---|
82 | |
---|
83 | template <typename Iter1, typename Iter2> |
---|
84 | double distance (Iter1 beg1,Iter1 end1, Iter2 beg2, |
---|
85 | utility::weighted_iterator_tag) const |
---|
86 | { |
---|
87 | AveragerPairWeighted ap; |
---|
88 | add(ap,beg1,end1,beg2); |
---|
89 | return sqrt(std::distance(beg1,end1)*ap.msd()); |
---|
90 | } |
---|
91 | |
---|
92 | }; |
---|
93 | |
---|
94 | }}} // of namespace statistics, yat, and theplu |
---|
95 | |
---|
96 | #endif |
---|