Added functions representation string
This commit is contained in:
53
main.cpp
53
main.cpp
@@ -2,8 +2,10 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <map>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "al_function.hpp"
|
#include "al_function.hpp"
|
||||||
#include "al_bool_matrix.hpp"
|
#include "al_bool_matrix.hpp"
|
||||||
@@ -17,6 +19,8 @@ const size_t FUNCTIONS_COUNT = 1ll << FUNCTION_LEN;
|
|||||||
typedef Function<Storage, FUNCTION_LEN> MyFunction;
|
typedef Function<Storage, FUNCTION_LEN> MyFunction;
|
||||||
typedef BoolSquareMatrix<Storage, ARGS_COUNT> MyMatrix;
|
typedef BoolSquareMatrix<Storage, ARGS_COUNT> MyMatrix;
|
||||||
|
|
||||||
|
map<Storage, string> function_formulas;
|
||||||
|
|
||||||
void test_function() {
|
void test_function() {
|
||||||
Function<uint16_t, 8> f_8(16);
|
Function<uint16_t, 8> f_8(16);
|
||||||
assert(("sizeof(sizeof(Function<uint16_t, 8>)", sizeof(Function<uint16_t, 8>)==2));
|
assert(("sizeof(sizeof(Function<uint16_t, 8>)", sizeof(Function<uint16_t, 8>)==2));
|
||||||
@@ -128,8 +132,9 @@ vector<MyFunction> get_function_class(MyFunction f, const vector< MyMatrix >& tr
|
|||||||
);
|
);
|
||||||
bool is_inserted = transformed_res.insert(linear_transformed).second;
|
bool is_inserted = transformed_res.insert(linear_transformed).second;
|
||||||
if ( is_inserted ) {
|
if ( is_inserted ) {
|
||||||
// out << linear_transformed.string() << " = " << f.string() << " with ("
|
if ( function_formulas.find(linear_transformed.value()) == function_formulas.end() ) {
|
||||||
// << transformation.string(",") << ")" << endl;
|
function_formulas[linear_transformed.value()] = "LT";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,20 +146,38 @@ vector<MyFunction> get_function_class(MyFunction f, const vector< MyMatrix >& tr
|
|||||||
vector<MyFunction> get_linear_components() {
|
vector<MyFunction> get_linear_components() {
|
||||||
vector<MyFunction> res;
|
vector<MyFunction> res;
|
||||||
if constexpr ( ARGS_COUNT == 2 ) {
|
if constexpr ( ARGS_COUNT == 2 ) {
|
||||||
|
res.push_back(MyFunction("0000")); // f = 0
|
||||||
|
function_formulas[res.back().value()] = "0";
|
||||||
res.push_back(MyFunction("1111")); // f = 1
|
res.push_back(MyFunction("1111")); // f = 1
|
||||||
|
function_formulas[res.back().value()] = "1";
|
||||||
res.push_back(MyFunction("0011")); // f = x_1
|
res.push_back(MyFunction("0011")); // f = x_1
|
||||||
|
function_formulas[res.back().value()] = "x_1";
|
||||||
res.push_back(MyFunction("0101")); // f = x_2
|
res.push_back(MyFunction("0101")); // f = x_2
|
||||||
|
function_formulas[res.back().value()] = "x_2";
|
||||||
} else if constexpr ( ARGS_COUNT == 3 ) {
|
} else if constexpr ( ARGS_COUNT == 3 ) {
|
||||||
|
res.push_back(MyFunction("00000000")); // f = 0
|
||||||
|
function_formulas[res.back().value()] = "0";
|
||||||
res.push_back(MyFunction("11111111")); // f = 1
|
res.push_back(MyFunction("11111111")); // f = 1
|
||||||
|
function_formulas[res.back().value()] = "1";
|
||||||
res.push_back(MyFunction("00001111")); // f = x_1
|
res.push_back(MyFunction("00001111")); // f = x_1
|
||||||
|
function_formulas[res.back().value()] = "x_1";
|
||||||
res.push_back(MyFunction("00110011")); // f = x_2
|
res.push_back(MyFunction("00110011")); // f = x_2
|
||||||
|
function_formulas[res.back().value()] = "x_2";
|
||||||
res.push_back(MyFunction("01010101")); // f = x_3
|
res.push_back(MyFunction("01010101")); // f = x_3
|
||||||
|
function_formulas[res.back().value()] = "x_3";
|
||||||
} else if constexpr ( ARGS_COUNT == 4 ) {
|
} else if constexpr ( ARGS_COUNT == 4 ) {
|
||||||
|
res.push_back(MyFunction("0000000000000000")); // f = 0
|
||||||
|
function_formulas[res.back().value()] = "0";
|
||||||
res.push_back(MyFunction("1111111111111111")); // f = 1
|
res.push_back(MyFunction("1111111111111111")); // f = 1
|
||||||
|
function_formulas[res.back().value()] = "1";
|
||||||
res.push_back(MyFunction("0000000011111111")); // f = x_1
|
res.push_back(MyFunction("0000000011111111")); // f = x_1
|
||||||
|
function_formulas[res.back().value()] = "x_1";
|
||||||
res.push_back(MyFunction("0000111100001111")); // f = x_2
|
res.push_back(MyFunction("0000111100001111")); // f = x_2
|
||||||
|
function_formulas[res.back().value()] = "x_2";
|
||||||
res.push_back(MyFunction("0011001100110011")); // f = x_3
|
res.push_back(MyFunction("0011001100110011")); // f = x_3
|
||||||
|
function_formulas[res.back().value()] = "x_3";
|
||||||
res.push_back(MyFunction("0101010101010101")); // f = x_4
|
res.push_back(MyFunction("0101010101010101")); // f = x_4
|
||||||
|
function_formulas[res.back().value()] = "x_4";
|
||||||
} else if constexpr ( ARGS_COUNT == 5 ) {
|
} else if constexpr ( ARGS_COUNT == 5 ) {
|
||||||
res.push_back(MyFunction("11111111111111111111111111111111")); // f = 1
|
res.push_back(MyFunction("11111111111111111111111111111111")); // f = 1
|
||||||
res.push_back(MyFunction("00000000000000001111111111111111")); // f = x_1
|
res.push_back(MyFunction("00000000000000001111111111111111")); // f = x_1
|
||||||
@@ -176,12 +199,22 @@ vector<MyFunction> get_linear_combinations(const vector<MyFunction> &linear_comp
|
|||||||
for (auto el_first: res)
|
for (auto el_first: res)
|
||||||
for (auto el_second: res) {
|
for (auto el_second: res) {
|
||||||
bool is_added_now = res.insert(el_first xor el_second).second;
|
bool is_added_now = res.insert(el_first xor el_second).second;
|
||||||
|
if ( is_added_now ) {
|
||||||
|
Storage cur_value = (el_first xor el_second).value();
|
||||||
|
function_formulas[cur_value] = function_formulas[el_first.value()] + " + " + function_formulas[el_second.value()];
|
||||||
|
}
|
||||||
is_added = is_added or is_added_now;
|
is_added = is_added or is_added_now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vector<MyFunction>(res.begin(), res.end());
|
return vector<MyFunction>(res.begin(), res.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string preprocess_factor(string factor) {
|
||||||
|
if ( factor.find("+") != string::npos and factor.find("*") == string::npos )
|
||||||
|
return "(" + factor + ")";
|
||||||
|
return factor;
|
||||||
|
}
|
||||||
|
|
||||||
vector<MyFunction> get_all_monomials(const vector<MyFunction> &linear_combinations) {
|
vector<MyFunction> get_all_monomials(const vector<MyFunction> &linear_combinations) {
|
||||||
set<MyFunction> res(linear_combinations.begin(), linear_combinations.end());
|
set<MyFunction> res(linear_combinations.begin(), linear_combinations.end());
|
||||||
bool is_added = true;
|
bool is_added = true;
|
||||||
@@ -190,12 +223,21 @@ vector<MyFunction> get_all_monomials(const vector<MyFunction> &linear_combinatio
|
|||||||
for (auto el_first: res)
|
for (auto el_first: res)
|
||||||
for (auto el_second: res) {
|
for (auto el_second: res) {
|
||||||
bool is_added_now = res.insert(el_first and el_second).second;
|
bool is_added_now = res.insert(el_first and el_second).second;
|
||||||
|
if ( is_added_now ) {
|
||||||
|
Storage cur_value = (el_first and el_second).value();
|
||||||
|
function_formulas[cur_value] = preprocess_factor(function_formulas[el_first.value()])
|
||||||
|
+ " * " + preprocess_factor(function_formulas[el_second.value()]);
|
||||||
|
}
|
||||||
is_added = is_added or is_added_now;
|
is_added = is_added or is_added_now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vector<MyFunction>(res.begin(), res.end());
|
return vector<MyFunction>(res.begin(), res.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string preprocess_monom(string monom) {
|
||||||
|
return monom;
|
||||||
|
}
|
||||||
|
|
||||||
void fill_ranks(vector<MyFunction> monomials) {
|
void fill_ranks(vector<MyFunction> monomials) {
|
||||||
vector<int8_t> used_map(FUNCTIONS_COUNT, 0);
|
vector<int8_t> used_map(FUNCTIONS_COUNT, 0);
|
||||||
size_t functions_remains = FUNCTIONS_COUNT;
|
size_t functions_remains = FUNCTIONS_COUNT;
|
||||||
@@ -219,6 +261,11 @@ void fill_ranks(vector<MyFunction> monomials) {
|
|||||||
MyFunction res_el = el_first xor el_second;
|
MyFunction res_el = el_first xor el_second;
|
||||||
if ( used_map[res_el.value()] == 0 ) {
|
if ( used_map[res_el.value()] == 0 ) {
|
||||||
--functions_remains;
|
--functions_remains;
|
||||||
|
|
||||||
|
function_formulas[res_el.value()] =
|
||||||
|
preprocess_monom(function_formulas[el_first.value()]) +
|
||||||
|
" + " + preprocess_monom(function_formulas[el_second.value()]);
|
||||||
|
|
||||||
used_map.at(res_el.value()) = total_ranks;
|
used_map.at(res_el.value()) = total_ranks;
|
||||||
ranks.at(total_ranks).push_back(res_el);
|
ranks.at(total_ranks).push_back(res_el);
|
||||||
}
|
}
|
||||||
@@ -231,8 +278,6 @@ void fill_ranks(vector<MyFunction> monomials) {
|
|||||||
|
|
||||||
auto possible_tranformations = get_good_matrices();
|
auto possible_tranformations = get_good_matrices();
|
||||||
cout << "Total " << possible_tranformations.size() << " linear tranformations" << endl;
|
cout << "Total " << possible_tranformations.size() << " linear tranformations" << endl;
|
||||||
//for (auto el: possible_tranformations)
|
|
||||||
// cout << el.string(",") <<endl;
|
|
||||||
|
|
||||||
ranks.clear();
|
ranks.clear();
|
||||||
for (auto i = total_ranks - 1; i != 0; --i)
|
for (auto i = total_ranks - 1; i != 0; --i)
|
||||||
|
|||||||
Reference in New Issue
Block a user