#ifndef theplu_yat_statistics_euclidean_distance_h
#define theplu_yat_statistics_euclidean_distance_h
// $Id: EuclideanDistance.h 1115 2008-02-21 19:20:59Z markus $
/*
Copyright (C) 2007 Peter Johansson, Markus Ringnér
Copyright (C) 2008 Peter Johansson
This file is part of the yat library, http://trac.thep.lu.se/yat
The yat library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The yat library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include "AveragerPair.h"
#include "AveragerPairWeighted.h"
#include "yat/utility/iterator_traits.h"
#include
namespace theplu {
namespace yat {
namespace statistics {
///
/// @brief Calculates the Euclidean distance between two points
/// given by elements of ranges.
///
/// This class is modelling the concept \ref concept_distance.
///
///
struct EuclideanDistance
{
/**
\brief Calculates the Euclidean distance between elements of
two ranges.
If elements of both ranges are unweighted the distance is
calculated as \f$ \sqrt{\sum (x_i-y_i)^2 } \f$, where \f$ x_i
\f$ and \f$ y_i \f$ are elements of the first and second range,
respectively.
If elements of one or both of ranges have weights the distance
is calculated as \f$ \sqrt{N \sum
w_{x,i}w_{y,i}(x_i-y_i)^2/\sum w_{x,i}w_{y,i}} \f$, where \f$ N
\f$ is the number of elements in the two ranges and \f$ w_x \f$
and \f$ w_y \f$ are weights for the elements of the first and
the second range, respectively. If the elements of one of the
two ranges are unweighted, the weights for these elements are
set to unity.
*/
template
double operator()
(Iter1 beg1,Iter1 end1, Iter2 beg2) const
{
return this->distance(beg1, end1, beg2,
typename utility::weighted_if_any2::type());
}
private:
template
double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
utility::unweighted_iterator_tag) const
{
AveragerPair ap;
add(ap,beg1,end1,beg2);
return sqrt(ap.sum_squared_deviation());
}
template
double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
utility::weighted_iterator_tag) const
{
AveragerPairWeighted ap;
add(ap,beg1,end1,beg2);
return sqrt(std::distance(beg1,end1)*ap.msd());
}
};
}}} // of namespace statistics, yat, and theplu
#endif