CluE  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cfentry.h
Go to the documentation of this file.
1 #ifndef CFENTRY_H
2 #define CFENTRY_H
3 
4 #include <type_traits>
5 
6 #include "../base/weightmodifier.h"
7 
8 namespace CluE
9 {
10 
22 template<typename T> struct CFEntry
23 {
27  size_t number;
28 
32  T LS;
33 
37  double SS;
38 
39  const bool isWeighted;
40 
41  CFEntry(size_t number, T ls, double ss);
42 
43  CFEntry& operator+=(CFEntry const & x);
44  CFEntry& operator-=(CFEntry const & x);
45  CFEntry operator+(CFEntry const & x) const;
46  CFEntry operator-(CFEntry const & x) const;
47 
51  void insert(T const & x);
52 
56  void remove(T const & x);
57 
61  T cog();
62 
66  double kMeansCost(T const & center);
67 };
68 
69 template<typename T> CFEntry<T>::CFEntry(size_t number, T ls, double ss) :
70  number(number),
71  LS(ls),
72  SS(ss),
73  isWeighted(std::is_base_of<WeightedObject, T>::value)
74 {
75 }
76 
77 template<typename T> CFEntry<T>& CFEntry<T>::operator+=(CFEntry<T> const & x)
78 {
79  number += x.number;
80  LS += x.LS;
81  SS += x.SS;
82  return *this;
83 }
84 
85 template<typename T> CFEntry<T>& CFEntry<T>::operator -=(CFEntry<T> const & x)
86 {
87  number -= x.number;
88  LS -= x.LS;
89  SS -= x.SS;
90  return *this;
91 }
92 
93 template<typename T> CFEntry<T> CFEntry<T>::operator+(CFEntry<T> const & x) const
94 {
95  return CFEntry(*this) += x;
96 }
97 
98 template<typename T> CFEntry<T> CFEntry<T>::operator-(CFEntry<T> const & x) const
99 {
100  return CFEntry(*this) -= x;
101 }
102 
103 template<typename T> void CFEntry<T>::insert(T const & x)
104 {
105  double weight = 1.0;
106  if(isWeighted)
107  {
108  WeightedObject const * wm = static_cast<WeightedObject const *>(&x);
109  weight = wm->getWeight();
110  }
111  number += weight;
112  LS += weight * x;
113  SS += weight * (x*x);
114 }
115 
116 template<typename T> void CFEntry<T>::remove(T const & x)
117 {
118  double weight = 1.0;
119  if(isWeighted)
120  {
121  WeightedObject const * wm = static_cast<WeightedObject const *>(&x);
122  weight = wm->getWeight();
123  }
124  number -= weight;
125  LS -= weight * x;
126  SS -= weight * (x*x);
127 }
128 
129 template<typename T> T CFEntry<T>::cog()
130 {
131  return (1.0 / number) * LS;
132 }
133 
134 template<typename T> double CFEntry<T>::kMeansCost(T const & center)
135 {
136  return SS - 2 * (LS * center) + number * (center * center);
137 }
138 
139 }
140 
141 #endif
CFEntry operator+(CFEntry const &x) const
Definition: cfentry.h:93
double SS
Squared sum.
Definition: cfentry.h:37
CFEntry & operator+=(CFEntry const &x)
Definition: cfentry.h:77
Abstract base class for weighted objects.
double kMeansCost(T const &center)
1-means clustering cost
Definition: cfentry.h:134
void insert(T const &x)
Inserts a point.
Definition: cfentry.h:103
const bool isWeighted
Definition: cfentry.h:39
T LS
Linear sum.
Definition: cfentry.h:32
size_t number
Number of points contained in the feature.
Definition: cfentry.h:27
CFEntry operator-(CFEntry const &x) const
Definition: cfentry.h:98
CFEntry & operator-=(CFEntry const &x)
Definition: cfentry.h:85
void remove(T const &x)
Removes a point.
Definition: cfentry.h:116
T cog()
Returns the center of gravity.
Definition: cfentry.h:129
CFEntry(size_t number, T ls, double ss)
Definition: cfentry.h:69
virtual double getWeight() const =0
Clustering feature tree entry.
Definition: cfentry.h:22