File: metric.hpp

package info (click to toggle)
python-pyclustering 0.10.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 38,888; python: 24,311; sh: 384; makefile: 105
file content (751 lines) | stat: -rwxr-xr-x 20,328 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*!

@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

*/


#pragma once


#include <pyclustering/definitions.hpp>

#include <algorithm>
#include <cmath>
#include <exception>
#include <functional>
#include <string>
#include <vector>


namespace pyclustering {

namespace utils {

namespace metric {



/*!

@brief   Encapsulates distance metric calculation function between two objects.

*/
template <typename TypeContainer>
using distance_functor = std::function<double(const TypeContainer &, const TypeContainer &)>;


/*!

@brief   Calculates square of Euclidean distance between points.

@param[in] point1: point #1 that is represented by coordinates.
@param[in] point2: point #2 that is represented by coordinates.

@return  Returns square of Euclidean distance between points.

*/
template <typename TypeContainer>
double euclidean_distance_square(const TypeContainer & point1, const TypeContainer & point2) {
    double distance = 0.0;
    typename TypeContainer::const_iterator iter_point1 = point1.begin();

    for (const auto & dim_point2 : point2) {
        double difference = (*iter_point1 - dim_point2);
        distance += difference * difference;

        ++iter_point1;
    }

    return distance;
}


/*!

@brief   Calculates Euclidean distance between points.

@param[in] point1: point #1 that is represented by coordinates.
@param[in] point2: point #2 that is represented by coordinates.

@return  Returns Euclidean distance between points.

*/
template <typename TypeContainer>
double euclidean_distance(const TypeContainer & point1, const TypeContainer & point2) {
    return std::sqrt(euclidean_distance_square(point1, point2));
}


/*!

@brief   Calculates Manhattan distance between points.

@param[in] point1: point #1 that is represented by coordinates.
@param[in] point2: point #2 that is represented by coordinates.

@return  Returns Manhattan distance between points.

*/
template <typename TypeContainer>
double manhattan_distance(const TypeContainer & point1, const TypeContainer & point2) {
    double distance = 0.0;
    typename TypeContainer::const_iterator iter_point1 = point1.begin();

    for (const auto & dim_point2 : point2) {
        distance += std::abs(*iter_point1 - dim_point2);
        ++iter_point1;
    }

    return distance;
}


/*!

@brief   Calculates Chebyshev distance between points.

@param[in] point1: point #1 that is represented by coordinates.
@param[in] point2: point #2 that is represented by coordinates.

@return  Returns Chebyshev distance between points.

*/
template <typename TypeContainer>
double chebyshev_distance(const TypeContainer & point1, const TypeContainer & point2) {
    double distance = 0.0;
    typename TypeContainer::const_iterator iter_point1 = point1.begin();

    for (const auto & dim_point2 : point2) {
        distance = std::max(distance, std::abs(*iter_point1 - dim_point2));
        ++iter_point1;
    }

    return distance;
}


/*!

@brief   Calculates Minkowski distance between points.

@param[in] p_point1: point #1 that is represented by coordinates.
@param[in] p_point2: point #2 that is represented by coordinates.
@param[in] p_degree: degree of Minkownski equation.

@return  Returns Minkowski distance between points.

*/
template <typename TypeContainer>
double minkowski_distance(const TypeContainer & p_point1, const TypeContainer & p_point2, const double p_degree) {
    double distance = 0.0;
    typename TypeContainer::const_iterator iter_point1 = p_point1.begin();

    for (const auto & dim_point2 : p_point2) {
        double difference = (*iter_point1 - dim_point2);
        distance += std::pow(difference, p_degree);

        ++iter_point1;
    }

    return std::pow(distance, 1.0 / p_degree);
}


/*!

@brief   Calculates Canberra distance between points.

@param[in] point1: point #1 that is represented by coordinates.
@param[in] point2: point #2 that is represented by coordinates.

@return  Returns Canberra distance between points.

*/
template <typename TypeContainer>
double canberra_distance(const TypeContainer & point1, const TypeContainer & point2) {
    double distance = 0.0;
    typename TypeContainer::const_iterator iter_point1 = point1.begin();

    for (const auto & dim_point2 : point2) {
        const auto dim_point1 = *iter_point1;

        const double divider = std::abs(dim_point1) + std::abs(dim_point2);
        if (divider == 0) {
            continue;
        }

        distance += std::abs(dim_point1 - dim_point2) / divider;
        ++iter_point1;
    }

    return distance;
}


/*!

@brief   Calculates Chi square distance between points.

@param[in] point1: point #1 that is represented by coordinates.
@param[in] point2: point #2 that is represented by coordinates.

@return  Returns Chi square distance between points.

*/
template <typename TypeContainer>
double chi_square_distance(const TypeContainer & point1, const TypeContainer & point2) {
    double distance = 0.0;
    typename TypeContainer::const_iterator iter_point1 = point1.begin();

    for (const auto & dim_point2 : point2) {
        const auto dim_point1 = *iter_point1;

        const double divider = std::abs(dim_point1) + std::abs(dim_point2);
        if (divider == 0) {
            continue;
        }

        distance += std::pow(dim_point1 - dim_point2, 2) / divider;
        ++iter_point1;
    }

    return distance;
}


/*!

@brief   Calculates Gower distance between points.

@param[in] p_point1: point #1 that is represented by coordinates.
@param[in] p_point2: point #2 that is represented by coordinates.
@param[in] p_max_range: max range in each data dimension.

@return  Returns Gower distance between points.

*/
template <typename TypeContainer>
double gower_distance(const TypeContainer & p_point1, const TypeContainer & p_point2, const TypeContainer & p_max_range) {
    double distance = 0.0;
    typename TypeContainer::const_iterator iter_point1 = p_point1.begin();
    typename TypeContainer::const_iterator iter_range  = p_max_range.begin();

    for (const auto & dim_point2 : p_point2) {
        if (*iter_range != 0.0) {
            distance += std::abs(*iter_point1 - dim_point2) / *iter_range;
        }

        ++iter_point1;
        ++iter_range;
    }

    return distance / p_point1.size();
}


/*!

@class   distance_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Basic distance metric provides interface for calculation distance between objects in line with
          specific metric.

*/
template <typename TypeContainer>
class distance_metric {
protected:
    distance_functor<TypeContainer> m_functor = nullptr;    /**< Function that defines metric calculation. */

public:
    /*!
    
    @brief  Default constructor of distance metric.
    
    */
    distance_metric() = default;

    /*!
    
    @brief  Parameterized constructor of distance metric.
    
    @param[in] p_functor: function that defines how to calculate distance metric.

    */
    explicit distance_metric(const distance_functor<TypeContainer> & p_functor) : m_functor(p_functor) { }

    /*!
    
    @brief  Default copy constructor of distance metric.

    @param[in] p_other: other distance metric that should be copied.

    */
    distance_metric(const distance_metric & p_other) = default;

    /*!

    @brief  Default move constructor of distance metric.

    @param[in] p_other: other distance metric that should be copied.

    */
    distance_metric(distance_metric && p_other) = default;

    /*!

    @brief  Default destructor of distance metric.

    */
    virtual ~distance_metric() = default;

public:
    /*!

    @brief   Performs calculation of distance metric between two points.

    @param[in] p_point1: the first iterable point.
    @param[in] p_point2: the second iterable point.

    @return  Calculated distance between two points.

    */
    double operator()(const TypeContainer & p_point1, const TypeContainer & p_point2) const {
        return m_functor(p_point1, p_point2);
    }

public:
    /*!
    
    @brief  Check if the distance metric is initialized.
    
    @return `true` if distance metric has been initialized by a non-nullptr function that defines how to
             calculate distance metric.

    */
    operator bool() const {
        return m_functor != nullptr;
    }

    /*!
    
    @brief  Assignment operator to copy distance metric.
    
    @param[in] p_other: other distance metric that should be copied.

    @return Reference to the distance metric.

    */
    distance_metric<TypeContainer>& operator=(const distance_metric<TypeContainer>& p_other) {
        if (this != &p_other) {
            m_functor = p_other.m_functor;
        }

        return *this;
    }
};


/*!

@class   euclidean_distance_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Euclidean distance metric calculator between two points.

\f[dist(a, b) = \sqrt{ \sum_{i=0}^{N}(a_{i} - b_{i})^{2} };\f]

*/
template <typename TypeContainer>
class euclidean_distance_metric : public distance_metric<TypeContainer> {
public:
    /*!
    
    @brief  Constructor of Euclidean distance metric.
    
    */
    euclidean_distance_metric() :
        distance_metric<TypeContainer>(std::bind(euclidean_distance<TypeContainer>, std::placeholders::_1, std::placeholders::_2))
    { }
};


/*!

@class   euclidean_distance_square_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Square Euclidean distance metric calculator between two points.

\f[dist(a, b) = \sum_{i=0}^{N}(a_{i} - b_{i})^{2};\f]

*/
template <typename TypeContainer>
class euclidean_distance_square_metric : public distance_metric<TypeContainer> {
public:
    /*!

    @brief  Constructor of square Euclidean distance metric.

    */
    euclidean_distance_square_metric() :
        distance_metric<TypeContainer>(std::bind(euclidean_distance_square<TypeContainer>, std::placeholders::_1, std::placeholders::_2))
    { }
};


/*!

@class   manhattan_distance_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Manhattan distance metric calculator between two points.

\f[dist(a, b) = \sum_{i=0}^{N}\left | a_{i} - b_{i} \right |;\f]

*/
template <typename TypeContainer>
class manhattan_distance_metric : public distance_metric<TypeContainer> {
public:
    /*!

    @brief  Constructor of Manhattan distance metric.

    */
    manhattan_distance_metric() :
        distance_metric<TypeContainer>(std::bind(manhattan_distance<TypeContainer>, std::placeholders::_1, std::placeholders::_2))
    { }
};


/*!

@class   chebyshev_distance_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Chebyshev distance metric calculator between two points.

@details Chebyshev distance is a metric defined on a vector space where the distance between two vectors is the
          greatest of their differences along any coordinate dimension.

\f[dist(a, b) = \max_{}i\left (\left | a_{i} - b_{i} \right |\right );\f]

*/
template <typename TypeContainer>
class chebyshev_distance_metric : public distance_metric<TypeContainer> {
public:
    /*!
    
    @brief  Constructor of Chebyshev distance metric.
    
    */
    chebyshev_distance_metric() :
        distance_metric<TypeContainer>(std::bind(chebyshev_distance<TypeContainer>, std::placeholders::_1, std::placeholders::_2))
    { }
};


/*!

@class   minkowski_distance_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Minkowski distance metric calculator between two points.

\f[dist(a, b) = \sqrt[p]{ \sum_{i=0}^{N}\left(a_{i} - b_{i}\right)^{p} };\f]

*/
template <typename TypeContainer>
class minkowski_distance_metric : public distance_metric<TypeContainer> {
public:
    /*!

    @brief  Constructor of Minkowski distance metric.

    @param[in] p_degree: degree of Minkowski equation.

    */
    explicit minkowski_distance_metric(const double p_degree) :
        distance_metric<TypeContainer>(std::bind(minkowski_distance<TypeContainer>, std::placeholders::_1, std::placeholders::_2, p_degree))
    { }
};


/*!

@class   canberra_distance_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Canberra distance metric calculator between two points.

\f[dist(a, b) = \sum_{i=0}^{N}\frac{\left | a_{i} - b_{i} \right |}{\left | a_{i} \right | + \left | b_{i} \right |};\f]

*/
template <typename TypeContainer>
class canberra_distance_metric : public distance_metric<TypeContainer> {
public:
    /*!

    @brief  Constructor of Canberra distance metric.

    */
    canberra_distance_metric() :
        distance_metric<TypeContainer>(std::bind(canberra_distance<TypeContainer>, std::placeholders::_1, std::placeholders::_2))
    { }
};


/*!

@class   chi_square_distance_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Chi square distance metric calculator between two points.

\f[dist(a, b) = \sum_{i=0}^{N}\frac{\left ( a_{i} - b_{i} \right )^{2}}{\left | a_{i} \right | + \left | b_{i} \right |};\f]

*/
template <typename TypeContainer>
class chi_square_distance_metric : public distance_metric<TypeContainer> {
public:
    /*!

    @brief  Constructor of Chi square distance metric.

    */
    chi_square_distance_metric() :
        distance_metric<TypeContainer>(std::bind(chi_square_distance<TypeContainer>, std::placeholders::_1, std::placeholders::_2))
    { }
};


/*!

@class   gower_distance_metric metric.hpp pyclustering/utils/metric.hpp

@brief   Gower distance metric calculator between two points.
@details Implementation is based on the paper @cite article::utils::metric::gower. Gower distance is calculate
using following formula:
\f[
dist\left ( a, b \right )=\frac{1}{p}\sum_{i=0}^{p}\frac{\left | a_{i} - b_{i} \right |}{R_{i}},
\f]

where \f$R_{i}\f$ is a max range for ith dimension. \f$R\f$ is defined in line following formula:

\f[
R=max\left ( X \right )-min\left ( X \right )
\f]

*/
template <typename TypeContainer>
class gower_distance_metric : public distance_metric<TypeContainer> {
public:
    /*!

    @brief   Constructor of Gower distance metric.

    @param[in] p_max_range: max range in each data dimension.

    */
    explicit gower_distance_metric(const TypeContainer & p_max_range) :
        distance_metric<TypeContainer>(std::bind(gower_distance<TypeContainer>, std::placeholders::_1, std::placeholders::_2, p_max_range))
    { }
};


/*!

@class   distance_metric_factory metric.hpp pyclustering/utils/metric.hpp

@brief   Distance metric factory provides services for creation available metric in the 'pyclustering::utils::metric' and also user-defined.

*/
template <typename TypeContainer>
class distance_metric_factory {
public:
    /*!

    @brief   Creates Euclidean distance metric.

    @return  Euclidean distance metric.

    */
    static distance_metric<TypeContainer> euclidean() {
        return euclidean_distance_metric<TypeContainer>();
    }

    /*!

    @brief   Creates square Euclidean distance metric.
  
    @return  Square Euclidean distance metric
  
    */
    static distance_metric<TypeContainer> euclidean_square() {
        return euclidean_distance_square_metric<TypeContainer>();
    }

    /*!
   
    @brief   Creates Manhattan distance metric.
   
    @return  Manhattan distance metric.
   
    */
    static distance_metric<TypeContainer> manhattan() {
        return manhattan_distance_metric<TypeContainer>();
    }

    /*!
   
    @brief   Creates Chebyshev distance metric.
   
    @return  Chebyshev distance metric.
   
    */
    static distance_metric<TypeContainer> chebyshev() {
        return chebyshev_distance_metric<TypeContainer>();
    }

    /*!
   
    @brief   Creates Minkowski distance metric.
   
    @param[in] p_degree: degree of Minkowski equation.
   
    @return  Minkowski distance metric.
   
    */
    static distance_metric<TypeContainer> minkowski(const double p_degree) {
        return minkowski_distance_metric<TypeContainer>(p_degree);
    }

    /*!

    @brief   Creates Canberra distance metric.

    @return  Canberra distance metric.

    */
    static distance_metric<TypeContainer> canberra() {
        return canberra_distance_metric<TypeContainer>();
    }

    /*!

    @brief   Creates Chi square distance metric.

    @return  Chi square distance metric.

    */
    static distance_metric<TypeContainer> chi_square() {
        return chi_square_distance_metric<TypeContainer>();
    }

    /*!

    @brief   Creates Gower distance metric.

    @param[in] p_max_range: max range in each data dimension.

    @return  Gower distance metric.

    */
    static distance_metric<TypeContainer> gower(const TypeContainer & p_max_range) {
        return gower_distance_metric<TypeContainer>(p_max_range);
    }

   /*!

    @brief   Creates user-defined distance metric.

    @param[in] p_functor: user-defined metric for calculation distance between two points.

    @return  User-defined distance metric.

    */
    static distance_metric<TypeContainer> user_defined(const distance_functor<TypeContainer> & p_functor) {
        return distance_metric<TypeContainer>(p_functor);
    }
};


/*!

@brief   Returns average distance for establish links between specified number of neighbors.

@param[in] points: input data.
@param[in] num_neigh: number of neighbors.

@return  Returns average distance for establish links between `num_neigh` in data set `points`.

*/
double average_neighbor_distance(const std::vector<std::vector<double> > * points, const std::size_t num_neigh);


/*!

@brief   Finds farthest distance between points in specified container (data).

@param[in] p_container: input data.
@param[in] p_metric: metric that is used for distance calculation between points.

@return  Returns farthest distance between points.

*/
template <typename TypeContainer>
double farthest_distance(const TypeContainer & p_container, const distance_metric<point> & p_metric)
{
    double distance = 0;
    for (std::size_t i = 0; i < p_container.size(); i++) {
        for (std::size_t j = i + 1; j < p_container.size(); j++) {
            double candidate_distance = p_metric(p_container[i], p_container[j]);
            if (candidate_distance > distance) {
                distance = candidate_distance;
            }
        }
    }

    return distance;
}


/*!

@brief   Calculates distance matrix using points container using Euclidean distance.

@param[in]  p_points: input data that is represented by points.
@param[in]  p_metric: metric for distance calculation between points.
@param[out] p_distance_matrix: output distance matrix of points.

*/
template <typename TypeContainer>
void distance_matrix(const TypeContainer & p_points, const distance_metric<point> & p_metric, TypeContainer & p_distance_matrix) {
    using TypeElement = typename TypeContainer::value_type;
    
    p_distance_matrix = TypeContainer(p_points.size(), TypeElement(p_points.size(), 0.0));

    for (std::size_t i = 0; i < p_points.size(); i++) {
        for (std::size_t j = i + 1; j < p_points.size(); j++) {
            const double distance = p_metric(p_points.at(i), p_points.at(j));
            p_distance_matrix[i][j] = distance;
            p_distance_matrix[j][i] = distance;
        }
    }
}


/*!

@brief   Calculates distance matrix using points container using Euclidean distance.

@param[in]  p_points: input data that is represented by points.
@param[out] p_distance_matrix: output distance matrix of points.

*/
template <typename TypeContainer>
void distance_matrix(const TypeContainer & p_points, TypeContainer & p_distance_matrix) {
    distance_matrix(p_points, distance_metric_factory<point>::euclidean(), p_distance_matrix);
}


}

}

}