Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
distance_Lp.hpp
Go to the documentation of this file.
1#ifndef FSTDISTANCELP_H
2#define FSTDISTANCELP_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
99template<typename DATATYPE, typename REALTYPE, typename DIMTYPE, class SUBSET, const int numerator=1, const int denominator=1>
100class Distance_Lp : public Clonable_Impl<Distance_Lp<DATATYPE,REALTYPE,DIMTYPE,SUBSET,numerator,denominator>,Distance<DATATYPE,DIMTYPE,SUBSET> > {
101public:
104 Distance_Lp() : _p(REALTYPE(numerator)/REALTYPE(denominator)) {notify("Distance_Lp (empty) constructor.");} // base Distance() zeroes _n and _d
106 Distance_Lp(const DIMTYPE n) : _p(REALTYPE(numerator)/REALTYPE(denominator)) {parent::reset(n); notify("Distance_Lp constructor.");}
108 Distance_Lp(const Distance_Lp& dlp) : parent_impl(dlp), _p(dlp._p) {notify("Distance_Lp copy-constructor.");}
109 virtual ~Distance_Lp() {notify("Distance_Lp destructor.");}
110
111 virtual DATATYPE distance(const DATATYPE *pattern1, const DATATYPE *pattern2) override;
112
113
114
115 virtual std::ostream& print(std::ostream& os) const override {os << "Distance_Lp(p=" << _p << ")"; return os;}
116protected:
117 const REALTYPE _p;
118};
119
120template<typename DATATYPE, typename REALTYPE, typename DIMTYPE, class SUBSET, const int numerator, const int denominator>
121DATATYPE Distance_Lp<DATATYPE,REALTYPE,DIMTYPE,SUBSET,numerator,denominator>::distance(const DATATYPE *pattern1, const DATATYPE *pattern2)
122{
123 assert(parent::_index);
124 assert(parent::_d>0);
125 assert(pattern1!=nullptr);
126 assert(pattern2!=nullptr);
127 DATATYPE result=0;
128 DIMTYPE index_i;
129 for(DIMTYPE i=0;i<parent::_d;i++) {
130 index_i=parent::_index[i];
131 result+=std::pow(std::abs(pattern1[index_i]-pattern2[index_i]),_p); // qualified: bare abs(double) risks the int overload on some toolchains
132 }
133 return std::pow(result,1.0/_p);
134}
135
136} // namespace
137#endif // FSTDISTANCELP_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Implements L_{p} distance, p equals numerator/denominator (template parameters).
Definition distance_Lp.hpp:100
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition distance_Lp.hpp:115
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_Lp.hpp:121
const REALTYPE _p
Definition distance_Lp.hpp:117
Clonable_Impl< Distance_Lp, parent > parent_impl
Definition distance_Lp.hpp:103
Distance_Lp(const DIMTYPE n)
Constructs and immediately reset()s for n raw features (see Distance::reset()).
Definition distance_Lp.hpp:106
Distance< DATATYPE, DIMTYPE, SUBSET > parent
Definition distance_Lp.hpp:102
Distance_Lp(const Distance_Lp &dlp)
Copy-constructor; delegates to parent_impl (deep-copies the current _index[] mapping) and copies _p.
Definition distance_Lp.hpp:108
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.