Alexandria  2.18
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 
28 #include <array>
29 
30 namespace Euclid {
31 namespace SOM {
32 namespace Distance {
33 
34 template <typename std::size_t ND>
35 class Interface {
36 
37 public:
38  virtual ~Interface() = default;
39 
40  virtual double distance(const std::array<double, ND>& left, const std::array<double, ND>& right) const = 0;
41 
42  virtual double distance(const std::array<double, ND>&, const std::array<double, ND>&, const std::array<double, ND>&) const {
43  throw Elements::Exception() << "Distance with uncertainties is not supported "
44  << "for this type of distance";
45  }
46 };
47 
48 template <typename std::size_t ND>
49 class L2 : public Interface<ND> {
50 
51 public:
52  virtual ~L2() = default;
53 
54  double distance(const std::array<double, ND>& left, const std::array<double, ND>& right) const override {
55  double result = 0;
56  for (std::size_t i = 0; i < ND; ++i) {
57  result += (left[i] - right[i]) * (left[i] - right[i]);
58  }
59  return std::sqrt(result);
60  }
61 
62  double distance(const std::array<double, ND>& left, const std::array<double, ND>& right,
63  const std::array<double, ND>& uncertainties) const override {
64  double result = 0;
65  for (std::size_t i = 0; i < ND; ++i) {
66  double up = (left[i] - right[i]) * (left[i] - right[i]);
67  double down = uncertainties[i] * uncertainties[i];
68  result += up / down;
69  }
70  return std::sqrt(result);
71  }
72 };
73 
74 } // namespace Distance
75 } // namespace SOM
76 } // namespace Euclid
77 
78 #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:54
virtual double distance(const std::array< double, ND > &, const std::array< double, ND > &, const std::array< double, ND > &) const
Definition: Distance.h:42
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:62