Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
Distance.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2022 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 <cassert> // for assert
29#include <cmath> // for sqrt
30
33
34namespace Euclid {
35namespace SOM {
36namespace Distance {
37
38class Interface {
39
40public:
42
43 virtual ~Interface() = default;
44
45 virtual double distance(const_iterator begin1, const_iterator end1, const_iterator begin2) const = 0;
46
47 virtual double distance(const_iterator ELEMENTS_UNUSED begin1, const_iterator ELEMENTS_UNUSED end1,
48 const_iterator ELEMENTS_UNUSED begin2,
49 const_iterator ELEMENTS_UNUSED begin_uncertainties) const {
50 throw Elements::Exception() << "Distance with uncertainties is not supported "
51 << "for this type of distance";
52 }
53};
54
55class L2 final : public Interface {
56
57public:
58 virtual ~L2() = default;
59
60 double distance(const_iterator begin1, const_iterator end1, const_iterator begin2) const override {
61 double result = 0;
62 for (; begin1 != end1; ++begin1, ++begin2) {
63 double diff = (*begin1 - *begin2);
64 result += diff * diff;
65 }
66 return std::sqrt(result);
67 }
68
70 const_iterator begin_uncertainties) const override {
71
72 double result = 0;
73 for (; begin1 != end1; ++begin1, ++begin2, ++begin_uncertainties) {
74 double diff = *begin1 - *begin2;
75 double up = diff * diff;
76 double down = *begin_uncertainties * *begin_uncertainties;
77 result += up / down;
78 }
79 return std::sqrt(result);
80 }
81};
82
83} // namespace Distance
84} // namespace SOM
85} // namespace Euclid
86
87#endif /* SOM_DISTANCE_H */
virtual double distance(const_iterator begin1, const_iterator end1, const_iterator begin2) const =0
std::vector< double >::const_iterator const_iterator
Definition Distance.h:41
virtual double distance(const_iterator ELEMENTS_UNUSED begin1, const_iterator ELEMENTS_UNUSED end1, const_iterator ELEMENTS_UNUSED begin2, const_iterator ELEMENTS_UNUSED begin_uncertainties) const
Definition Distance.h:47
virtual ~L2()=default
double distance(const_iterator begin1, const_iterator end1, const_iterator begin2) const override
Definition Distance.h:60
double distance(const_iterator begin1, const_iterator end1, const_iterator begin2, const_iterator begin_uncertainties) const override
Definition Distance.h:69
T sqrt(T... args)