Skip to content

Instantly share code, notes, and snippets.

@zindmax
Last active January 12, 2022 10:44
Show Gist options
  • Save zindmax/bceb889938ab7e469a3c797df2297b83 to your computer and use it in GitHub Desktop.
Save zindmax/bceb889938ab7e469a3c797df2297b83 to your computer and use it in GitHub Desktop.
task 8
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double calcHi(vector<double> P_i) {
double h_i = 0;
for (double i : P_i) {
if (i == 0) {
continue;
}
h_i += i * log2(i);
}
return -h_i;
}
double calcEntrophy(vector<double> prob_vect, vector<vector<double>> P) {
double h = 0.0;
double h_i = 0.0;
for (int i = 0; i < prob_vect.size(); i++) {
h_i = calcHi(P[i]);
h += prob_vect[i] * h_i;
}
return h;
}
int main()
{
vector<vector<double>> P = {\
{0, 0.5, 0.5},
{0, 0, 1}
{1, 0, 0}
};
vector<double> prob_vect = {2.0/5, 1.0/5, 2.0/5};
double H = calcEntrophy(prob_vect, P);
cout << H << endl;
return 0;
}
02201102201021021212220010222112121100000212210021022221012010011212021010122000120021112201112000122212112201201020101220111100210210221120102110200101201211011201012210001120110112001102102221112212201120112201112121001221122012201122100000010201020111002020102110220112110221011200012201021012012020101101020021020012010201221201211121020100212110001221012022011012022012122102001100212101011020101122002010020120112210212021201020012121011010112120120120001200211120210021102221010202101210120100
00112101120021110200211220012011002010122002101202010102201010110200121220102211202100121202120112011012100120110212022102121021020101102100012210120012210121021201120122102101202200121012012022102002021201220222102010102210210201102012022210012021021012022101201021020110221020102011020021022102120101202102101202210012021200212002102012101101120221020120120222010201101102200011020102012210200120002012011221020210100121100221021200120210120112201022012012110211022212010021100211021020211022102110
02101200122210120112001201221220110221012012012012200121102110220120012201202010120210021122210211201210120112012111010022101201120121101221001201211011012001200121001210121112001120210201120120021102121202121002121200210012212012021021012100121021021110210102221210211201202120120120210210210111000120112001202210021001201012012001202110212020121001200121210012121200220210210211022120121012012102202100012001211002100120012100121001201021210012221010100212001221020120210102110210212100020201012021
01102100010222220021112000121122120020112010122001222110102022201022120212001112110212022012211211011220020120201201201211120222111010220112111202102112222102102212111010221202210011212002101210122001221100200111212101221110122211101001020022012120120221001011102122120211110210211111210110002101222022111010012202210111012020221121121210121101201201202000000212120222120202102212101202121010011201101201012212111121000222120211210210201210100002202222000110101121220022220011020222111020122020111201
22100210012102100221021010121022120012201020021121020010112020110122010122001110102022121022011200021122010110210201102001022002101221021202210102210102101212010011202202210201102112221021010202001011101220202120022122201020011201200211020012010210201211102010202102102101220201211002212101122011022011100220201100201110012011220210202100211211001110022120002210211102221100222201201110022021002221222001002000101200211211202210220112011210121111101102021200020020101101111021010212220210201112212201
1200212001201202120001200222011020010210011111111100200221012020001011102020201200210020201102120112002222220111102010111021020201012020102020101110202020101102010200000201020202201010202022201010202020112101022222111011110002022221110222011102000000002000101102020101111000202011201020000202111020110102002111022010200010121102000000220201010222010111020010120101121111020222010110200002020002210101020002010122100102110002120220000121201120122022021010202020102020001020210110110202022201000111022
#include <iostream>
#include <string>
#include <cmath>
#include <map>
#include <fstream>
using namespace std;
int main()
{
string str;
ifstream file("1.txt");
getline(file, str);
string alb = "012";
string word = "1012002100";
int k = word.size();
map<string, map<char, int>> p;
map<string, int> q;
for (int i = 0; i < str.size() - k; i++) {
string word = str.substr(i, k);
for (int j = 0; j < alb.size(); j++) {
char sym = alb[j];
if (p[word][sym] == 0) {
p[word][sym] = 1;
}
q[word] = alb.size();
}
}
double h = 0;
double p_ba = 0;
for (char sym:str) {
p[word][sym] += 1;
q[word] += 1;
// for (int i = 0; i < alb.size(); i++) {
// p_ba = 1.0 * p[word][sym] / q[word];
// h += -p_ba * log2(p_ba);
// }
word = word.substr(1, k-1);
word += sym;
}
cout << (h / str.size()) << endl;
//file_1 h = 1.46913
//file_2 h = 1.46777
//file_3 h = 1.46635
//file_4 h = 1.47035
//file_5 h = 1.47
//file_6 h = 1.46802
//file_1_1 h = 1.46919
//file_1_1 h = 1.46784
//file_1_1 h = 1.4662
//file_1_1 h = 1.4704
//file_1_1 h = 1.47006
//file_1_1 h =
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment