Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
distance_L1.hpp
Go to the documentation of this file.
1#ifndef FSTDISTANCEL1_H
2#define FSTDISTANCEL1_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
89template<typename DATATYPE, typename DIMTYPE, class SUBSET>
90class Distance_L1 : public Clonable_Impl<Distance_L1<DATATYPE,DIMTYPE,SUBSET>,Distance<DATATYPE,DIMTYPE,SUBSET> > {
91public:
94 Distance_L1() {notify("Distance_L1 (empty) constructor.");} // base Distance() zeroes _n and _d
96 Distance_L1(const DIMTYPE n) {parent::reset(n); notify("Distance_L1 constructor.");}
98 Distance_L1(const Distance_L1& dl1) : parent_impl(dl1) {notify("Distance_L1 copy-constructor.");}
99 virtual ~Distance_L1() {notify("Distance_L1 destructor.");}
100
101 virtual DATATYPE distance(const DATATYPE *pattern1, const DATATYPE *pattern2) override;
102
103
104
105 virtual std::ostream& print(std::ostream& os) const override {os << "Distance_L1()"; return os;}
106};
107
108template<typename DATATYPE, typename DIMTYPE, class SUBSET>
109DATATYPE Distance_L1<DATATYPE,DIMTYPE,SUBSET>::distance(const DATATYPE *pattern1, const DATATYPE *pattern2)
110{
111 assert(parent::_index);
112 assert(parent::_d>0);
113 assert(pattern1!=nullptr);
114 assert(pattern2!=nullptr);
115 DATATYPE result=0;
116 DIMTYPE index_i;
117 for(DIMTYPE i=0;i<parent::_d;i++) {
118 index_i=parent::_index[i];
119 result+=std::abs(pattern1[index_i]-pattern2[index_i]); // qualified: bare abs(double) risks the int overload on some toolchains
120 }
121 return result;
122}
123
124} // namespace
125#endif // FSTDISTANCEL1_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Implements L_1 (city-block / Manhattan) distance.
Definition distance_L1.hpp:90
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_L1.hpp:109
Clonable_Impl< Distance_L1, parent > parent_impl
implements clone(); sharing/stateless_clone throw in Distance
Definition distance_L1.hpp:93
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition distance_L1.hpp:105
Distance_L1(const DIMTYPE n)
Constructs and immediately reset()s for n raw features (see Distance::reset()).
Definition distance_L1.hpp:96
Distance_L1(const Distance_L1 &dl1)
Copy-constructor; delegates to parent_impl (deep-copies the current _index[] mapping).
Definition distance_L1.hpp:98
Distance< DATATYPE, DIMTYPE, SUBSET > parent
base class: the abstract distance interface
Definition distance_L1.hpp:92
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.