Alexandria  2.19
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Distance.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * @file Distance.h
21  * @author nikoapos
22  */
23 
24 #ifndef SOM_DISTANCE_H
25 #define SOM_DISTANCE_H
26 
27 #include <array>
28 #include <cmath> // for sqrt
29 
31 
32 namespace Euclid {
33 namespace SOM {
34 namespace Distance {
35 
36 template <typename std::size_t ND>
37 class Interface {
38 
39 public:
40  virtual ~Interface() = default;
41 
42  virtual double distance(const std::array<double, ND>& left, const std::array<double, ND>& right) const = 0;
43 
44  virtual double distance(const std::array<double, ND>&, const std::array<double, ND>&, const std::array<double, ND>&) const {
45  throw Elements::Exception() << "Distance with uncertainties is not supported "
46  << "for this type of distance";
47  }
48 };
49 
50 template <typename std::size_t ND>
51 class L2 : public Interface<ND> {
52 
53 public:
54  virtual ~L2() = default;
55 
56  double distance(const std::array<double, ND>& left, const std::array<double, ND>& right) const override {
57  double result = 0;
58  for (std::size_t i = 0; i < ND; ++i) {
59  result += (left[i] - right[i]) * (left[i] - right[i]);
60  }
61  return std::sqrt(result);
62  }
63 
64  double distance(const std::array<double, ND>& left, const std::array<double, ND>& right,
65  const std::array<double, ND>& uncertainties) const override {
66  double result = 0;
67  for (std::size_t i = 0; i < ND; ++i) {
68  double up = (left[i] - right[i]) * (left[i] - right[i]);
69  double down = uncertainties[i] * uncertainties[i];
70  result += up / down;
71  }
72  return std::sqrt(result);
73  }
74 };
75 
76 } // namespace Distance
77 } // namespace SOM
78 } // namespace Euclid
79 
80 #endif /* SOM_DISTANCE_H */
virtual ~L2()=default
double distance(const std::array< double, ND > &left, const std::array< double, ND > &right) const override
Definition: Distance.h:56
virtual double distance(const std::array< double, ND > &, const std::array< double, ND > &, const std::array< double, ND > &) const
Definition: Distance.h:44
STL class.
T sqrt(T...args)
virtual double distance(const std::array< double, ND > &left, const std::array< double, ND > &right) const =0
double distance(const std::array< double, ND > &left, const std::array< double, ND > &right, const std::array< double, ND > &uncertainties) const override
Definition: Distance.h:64