forpy  2
tsallisentropy.h
Go to the documentation of this file.
1 /* Author: Christoph Lassner. */
2 #pragma once
3 #ifndef FORPY_IMPURITIES_TSALLISENTROPY_H_
4 #define FORPY_IMPURITIES_TSALLISENTROPY_H_
5 
6 #include "../global.h"
7 #include "../util/serialization/basics.h"
8 
9 #include <numeric>
10 #include <vector>
11 
12 #include "../types.h"
13 #include "../util/checks.h"
14 #include "../util/exponentials.h"
15 #include "./ientropyfunction.h"
16 #include "./inducedentropy.h"
17 #include "./shannonentropy.h"
18 
19 namespace forpy {
33  public:
38  explicit TsallisEntropy(const float &q);
40 
41  inline float operator()(const float *class_members_numbers, const size_t &n,
42  const float &fsum) const {
43  if (q == 1.f)
44  return shannon_entropy->operator()(class_members_numbers, n, fsum);
45  // In debug mode, run checks.
46  FASSERT(static_cast<float>(std::accumulate(class_members_numbers,
47  class_members_numbers + n,
48  static_cast<float>(0))) == fsum);
49  if (fsum == 0.f) return 0.f;
50  float entropy_sum = 1.f;
51  float quot;
52  if (ceilf(q) == q || floorf(q) == q) {
53  const uint whole_q = static_cast<uint>(q);
54  // Calculate.
55  for (size_t i = 0; i < n; ++i) {
56  quot = *(class_members_numbers++) / fsum;
57  entropy_sum -= fpowi(quot, whole_q);
58  }
59  } else {
60  // Calculate.
61  for (size_t i = 0; i < n; ++i) {
62  quot = *(class_members_numbers++) / fsum;
63  entropy_sum -= powf(quot, q);
64  }
65  }
66  return entropy_sum / (q - 1.f);
67  };
68 
69  inline friend std::ostream &operator<<(std::ostream &stream,
70  const TsallisEntropy &self) {
71  stream << "forpy::TsallisEntropy[q=" << self.get_q() << "]";
72  return stream;
73  };
74  bool operator==(const IEntropyFunction &rhs) const;
75 
77  float get_q() const;
78  friend class InducedEntropy;
79  friend class ShannonEntropy;
80 
81  private:
88 
89  friend class cereal::access;
90  template <class Archive>
91  void serialize(Archive &ar, const uint &) {
92  ar(cereal::make_nvp("base", cereal::base_class<IEntropyFunction>(this)),
93  CEREAL_NVP(q), CEREAL_NVP(shannon_entropy), CEREAL_NVP(induced_p));
94  };
95 
96  float q;
97  std::unique_ptr<ShannonEntropy> shannon_entropy;
98  std::unique_ptr<InducedEntropy> induced_p;
100 };
101 } // namespace forpy
102 
104 #endif // FORPY_IMPURITIES_TSALLISENTROPY_H_
float operator()(const float *class_members_numbers, const size_t &n, const float &fsum) const
std::unique_ptr< ShannonEntropy > shannon_entropy
bool operator==(const IEntropyFunction &rhs) const
float fpowi(float base, unsigned int exp)
Computes a float power by an unsigned int.
Definition: exponentials.h:56
CEREAL_REGISTER_TYPE(forpy::TsallisEntropy)
Computes the Tsallis entropy.
std::unique_ptr< InducedEntropy > induced_p
TsallisEntropy()
DON&#39;T USE. Non-initializing constructor for serialization purposes.
void serialize(Archive &ar, const uint &)
float get_q() const
Interface for an entropy calculation functor.
#define FASSERT(condition)
Definition: global.h:124
Computes the classical Shannon-Entropy.
friend std::ostream & operator<<(std::ostream &stream, const TsallisEntropy &self)
Computes the induced p entropy.
DISALLOW_COPY_AND_ASSIGN(TsallisEntropy)
friend class cereal::access
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:113