yat/statistics/EuclideanDistance.h

Code
Comments
Other
Rev Date Author Line
1031 04 Feb 08 markus 1 #ifndef theplu_yat_statistics_euclidean_distance_h
1031 04 Feb 08 markus 2 #define theplu_yat_statistics_euclidean_distance_h
889 25 Sep 07 markus 3
889 25 Sep 07 markus 4 // $Id$
889 25 Sep 07 markus 5
999 23 Dec 07 jari 6 /*
2119 12 Dec 09 peter 7   Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
3550 03 Jan 17 peter 8   Copyright (C) 2010, 2016 Peter Johansson
999 23 Dec 07 jari 9
1437 25 Aug 08 peter 10   This file is part of the yat library, http://dev.thep.lu.se/yat
999 23 Dec 07 jari 11
999 23 Dec 07 jari 12   The yat library is free software; you can redistribute it and/or
999 23 Dec 07 jari 13   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 14   published by the Free Software Foundation; either version 3 of the
999 23 Dec 07 jari 15   License, or (at your option) any later version.
999 23 Dec 07 jari 16
999 23 Dec 07 jari 17   The yat library is distributed in the hope that it will be useful,
999 23 Dec 07 jari 18   but WITHOUT ANY WARRANTY; without even the implied warranty of
999 23 Dec 07 jari 19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
999 23 Dec 07 jari 20   General Public License for more details.
999 23 Dec 07 jari 21
999 23 Dec 07 jari 22   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 23   along with yat. If not, see <http://www.gnu.org/licenses/>.
999 23 Dec 07 jari 24 */
999 23 Dec 07 jari 25
889 25 Sep 07 markus 26 #include "AveragerPair.h"
890 25 Sep 07 markus 27 #include "AveragerPairWeighted.h"
3534 21 Dec 16 peter 28 #include "Distance.h"
889 25 Sep 07 markus 29
889 25 Sep 07 markus 30 #include <cmath>
889 25 Sep 07 markus 31
889 25 Sep 07 markus 32 namespace theplu {
889 25 Sep 07 markus 33 namespace yat {
889 25 Sep 07 markus 34 namespace statistics {
3531 11 Oct 16 peter 35
3534 21 Dec 16 peter 36   /**
3534 21 Dec 16 peter 37      \brief Calculates the Euclidean distance between elements of
3534 21 Dec 16 peter 38      two ranges.
3531 11 Oct 16 peter 39
3534 21 Dec 16 peter 40      If elements of both ranges are unweighted the distance is
3534 21 Dec 16 peter 41      calculated as \f$ \sqrt{\sum (x_i-y_i)^2 } \f$, where \f$ x_i
3534 21 Dec 16 peter 42      \f$ and \f$ y_i \f$ are elements of the first and second range,
3534 21 Dec 16 peter 43      respectively.
1093 14 Feb 08 peter 44
3534 21 Dec 16 peter 45      If elements of one or both of ranges have weights the distance
3534 21 Dec 16 peter 46      is calculated as \f$ \sqrt{N \sum
3534 21 Dec 16 peter 47      w_{x,i}w_{y,i}(x_i-y_i)^2/\sum w_{x,i}w_{y,i}} \f$, where \f$ N
3534 21 Dec 16 peter 48      \f$ is the number of elements in the two ranges and \f$ w_x \f$
3534 21 Dec 16 peter 49      and \f$ w_y \f$ are weights for the elements of the first and
3534 21 Dec 16 peter 50      the second range, respectively. If the elements of one of the
3534 21 Dec 16 peter 51      two ranges are unweighted, the weights for these elements are
3534 21 Dec 16 peter 52      set to unity.
3534 21 Dec 16 peter 53   */
3534 21 Dec 16 peter 54   class EuclideanDistance : public Distance<EuclideanDistance>
3534 21 Dec 16 peter 55   {
3534 21 Dec 16 peter 56   private:
3534 21 Dec 16 peter 57     friend class Distance<EuclideanDistance>;
889 25 Sep 07 markus 58
1050 07 Feb 08 peter 59     template <typename Iter1, typename Iter2>
3531 11 Oct 16 peter 60     double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
1092 14 Feb 08 peter 61                      utility::unweighted_iterator_tag) const
1050 07 Feb 08 peter 62     {
1050 07 Feb 08 peter 63       AveragerPair ap;
1050 07 Feb 08 peter 64       add(ap,beg1,end1,beg2);
1050 07 Feb 08 peter 65       return sqrt(ap.sum_squared_deviation());
1050 07 Feb 08 peter 66     }
889 25 Sep 07 markus 67
1050 07 Feb 08 peter 68     template <typename Iter1, typename Iter2>
3531 11 Oct 16 peter 69     double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
1092 14 Feb 08 peter 70                      utility::weighted_iterator_tag) const
1050 07 Feb 08 peter 71     {
1050 07 Feb 08 peter 72       AveragerPairWeighted ap;
1050 07 Feb 08 peter 73       add(ap,beg1,end1,beg2);
1050 07 Feb 08 peter 74       return sqrt(std::distance(beg1,end1)*ap.msd());
1050 07 Feb 08 peter 75     }
3531 11 Oct 16 peter 76
1050 07 Feb 08 peter 77   };
890 25 Sep 07 markus 78
889 25 Sep 07 markus 79 }}} // of namespace statistics, yat, and theplu
3531 11 Oct 16 peter 80
889 25 Sep 07 markus 81 #endif