Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- //#include <omp.h>
- //#include <bitset>
- #include <cstdint>
- //#include <chrono>
- //#include <random>
- #include <functional>
- #include <vector>
- #include <cmath>
- using namespace std;
- //#include <boost/functional/hash.hpp>
- uint32_t hash32( uint32_t a)
- {
- a = (a+0x7ed55d16) + (a<<12);
- a = (a^0xc761c23c) ^ (a>>19);
- a = (a+0x165667b1) + (a<<5);
- a = (a+0xd3a2646c) ^ (a<<9);
- a = (a+0xfd7046c5) + (a<<3);
- a = (a^0xb55a4f09) ^ (a>>16);
- return a;
- }
- double HH(int m, int k)
- {// entropy of k-bits sequences generated from m bits random variable
- int N = 1<<m;
- int K = 1<<k;
- vector<int> counts(K );
- for (int i=0;i<N;i++)
- {
- uint32_t x = hash32(i);
- // cout<<x<<" "<<i<<endl;
- counts[(x)%K]++;
- }
- double H=0;
- for (int i=0;i<counts.size();i++)
- {
- double P = counts[i]/(double)N;
- if (P > 0)
- H+= -P*log2(P);
- }
- return H;
- }
- int main(void)
- {
- cout.precision(2);
- std::cout.setf( std::ios::fixed, std:: ios::floatfield );
- cout<<" ";
- for (int m=1;m<=20;m++)
- {
- std::cout.fill(' ');
- std::cout.width(5);
- cout<<m<<" ";
- }
- cout<<endl;
- for (int k=1;k<25;k++)
- {
- std::cout.fill(' ');
- std::cout.width(2);
- cout<<k<<":";
- for (int m=1;m<=20;m++)
- {
- std::cout.fill(' ');
- std::cout.width(5);
- cout<<(HH(m,k))<<" ";
- }
- cout<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement