Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
distance_euclid.hpp
Go to the documentation of this file.
1#ifndef FSTDISTANCEEUCLID_H
2#define FSTDISTANCEEUCLID_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <memory>
50#include <cassert>
51#include <iostream>
52#include <cmath>
53#include "error.hpp"
54#include "global.hpp"
55#include "distance.hpp"
56
57/*============== Template parameter type naming conventions ==============
58--------- Numeric types: -------------------------------------------------
59DATATYPE - data sample values - usually real numbers (but may be integers
60 in text processing etc.)
61REALTYPE - must be real numbers - for representing intermediate results of
62 calculations like mean, covariance etc.
63IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
64 extent depends on numbers of samples in data
65DIMTYPE - index values for enumeration of features (dimensions), or classes (not
66 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
67 but be aware of expressions like _classes*_features*_features !
68 in linearized representations of feature matrices for all classes
69BINTYPE - feature selection marker type - represents ca. <10 different feature
70 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
71RETURNTYPE - criterion value: real value, but may be extended in future to support
72 multiple values
73--------- Class types: ---------------------------------------------------
74SUBSET - class of class type Subset
75CLASSIFIER - class implementing interface defined in abstract class Classifier
76EVALUATOR - class implementing interface defined in abstract class Sequential_Step
77DISTANCE - class implementing interface defined in abstract class Distance
78DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
79INTERVALCONTAINER - class of class type Data_Intervaller
80CONTAINER - STL container of class type Data_Interval
81========================================================================== */
82
83namespace FST {
84
91template<typename DATATYPE, typename DIMTYPE, class SUBSET>
92class Distance_Euclid : public Clonable_Impl<Distance_Euclid<DATATYPE,DIMTYPE,SUBSET>,Distance<DATATYPE,DIMTYPE,SUBSET> > {
93public:
96 Distance_Euclid() {notify("Distance_Euclid (empty) constructor.");} // base Distance() zeroes _n and _d
98 Distance_Euclid(const DIMTYPE n) {parent::reset(n); notify("Distance_Euclid constructor.");}
100 Distance_Euclid(const Distance_Euclid& de) : parent_impl(de) {notify("Distance_Euclid copy-constructor.");}
101 virtual ~Distance_Euclid() {notify("Distance_Euclid destructor.");}
102
103 virtual DATATYPE distance(const DATATYPE *pattern1, const DATATYPE *pattern2) override;
104
105
106
107 virtual std::ostream& print(std::ostream& os) const override {os << "Distance_Euclid()"; return os;}
108};
109
110template<typename DATATYPE, typename DIMTYPE, class SUBSET>
111DATATYPE Distance_Euclid<DATATYPE,DIMTYPE,SUBSET>::distance(const DATATYPE *pattern1, const DATATYPE *pattern2)
112{
113 assert(parent::_index);
114 assert(parent::_d>0);
115 assert(pattern1!=nullptr);
116 assert(pattern2!=nullptr);
117 DATATYPE result=0;
118 DATATYPE tmp;
119 DIMTYPE index_i;
120 for(DIMTYPE i=0;i<parent::_d;i++) {
121 index_i=parent::_index[i];
122 tmp=(pattern1[index_i]-pattern2[index_i]);
123 result+=tmp*tmp;
124 }
125 return sqrt(result);
126}
127
128} // namespace
129#endif // FSTDISTANCEEUCLID_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Implements Euclidean (L_2) distance.
Definition distance_euclid.hpp:92
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition distance_euclid.hpp:107
Clonable_Impl< Distance_Euclid, parent > parent_impl
implements clone(); sharing/stateless_clone throw in Distance
Definition distance_euclid.hpp:95
Distance< DATATYPE, DIMTYPE, SUBSET > parent
base class: the abstract distance interface
Definition distance_euclid.hpp:94
virtual DATATYPE distance(const DATATYPE *pattern1, const DATATYPE *pattern2) override
Distance between two raw patterns over the currently active features only. Reads exactly get_d() entr...
Definition distance_euclid.hpp:111
Distance_Euclid(const Distance_Euclid &de)
Copy-constructor; delegates to parent_impl (deep-copies the current _index[] mapping).
Definition distance_euclid.hpp:100
Distance_Euclid(const DIMTYPE n)
Constructs and immediately reset()s for n raw features (see Distance::reset()).
Definition distance_euclid.hpp:98
Abstract class, defines interface for distance evaluators to be used in Classifier_kNN.
Definition distance.hpp:89
DIMTYPE _d
currently active feature count (see get_d())
Definition distance.hpp:128
void reset(const DIMTYPE n)
Allocates for n raw features and makes _index[] the identity map 0..n-1, with all n features active (...
Definition distance.hpp:142
std::unique_ptr< DIMTYPE[]> _index
maps active-feature position [0,_d) to a raw feature index in [0,_n)
Definition distance.hpp:129
Defines interface for distance evaluators (to be used primarily in Classifier_kNN).
FST4 specific error passing class.
Global definitions.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.