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 {
32 class TsallisEntropy : public IEntropyFunction {
33 public:
38 explicit TsallisEntropy(const float &q);
39 ~TsallisEntropy();
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:
87 TsallisEntropy();
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;
99 DISALLOW_COPY_AND_ASSIGN(TsallisEntropy);
100 };
101 } // namespace forpy
102
103 CEREAL_REGISTER_TYPE(forpy::TsallisEntropy);
104 #endif // FORPY_IMPURITIES_TSALLISENTROPY_H_
forpy::TsallisEntropy::operator()
float operator()(const float *class_members_numbers, const size_t &n, const float &fsum) const
Definition: tsallisentropy.h:41
forpy::TsallisEntropy::shannon_entropy
std::unique_ptr< ShannonEntropy > shannon_entropy
Definition: tsallisentropy.h:97
forpy::TsallisEntropy::operator==
bool operator==(const IEntropyFunction &rhs) const
forpy::fpowi
float fpowi(float base, unsigned int exp)
Computes a float power by an unsigned int.
Definition: exponentials.h:56
forpy::TsallisEntropy::q
float q
Definition: tsallisentropy.h:94
CEREAL_REGISTER_TYPE
CEREAL_REGISTER_TYPE(forpy::TsallisEntropy)
forpy::TsallisEntropy
Computes the Tsallis entropy.
Definition: tsallisentropy.h:32
forpy::TsallisEntropy::induced_p
std::unique_ptr< InducedEntropy > induced_p
Definition: tsallisentropy.h:98
shannonentropy.h
forpy::TsallisEntropy::TsallisEntropy
TsallisEntropy()
DON&#39;T USE. Non-initializing constructor for serialization purposes.
forpy::TsallisEntropy::serialize
void serialize(Archive &ar, const uint &)
Definition: tsallisentropy.h:91
forpy::TsallisEntropy::get_q
float get_q() const
forpy::IEntropyFunction
Interface for an entropy calculation functor.
Definition: ientropyfunction.h:26
FASSERT
#define FASSERT(condition)
Definition: global.h:124
forpy::TsallisEntropy::~TsallisEntropy
~TsallisEntropy()
forpy::ShannonEntropy
Computes the classical Shannon-Entropy.
Definition: shannonentropy.h:34
forpy::TsallisEntropy::operator<<
friend std::ostream & operator<<(std::ostream &stream, const TsallisEntropy &self)
Definition: tsallisentropy.h:69
forpy::InducedEntropy
Computes the induced p entropy.
Definition: inducedentropy.h:42
forpy
Definition: fastdprov.h:13
ientropyfunction.h
forpy::TsallisEntropy::DISALLOW_COPY_AND_ASSIGN
DISALLOW_COPY_AND_ASSIGN(TsallisEntropy)
inducedentropy.h
forpy::TsallisEntropy::access
friend class cereal::access
Definition: tsallisentropy.h:89
forpy::uint
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:113