diff --git a/main.cpp b/main.cpp index d304e81..5088524 100644 --- a/main.cpp +++ b/main.cpp @@ -2,8 +2,10 @@ #include #include #include +#include #include #include +#include #include "al_function.hpp" #include "al_bool_matrix.hpp" @@ -17,6 +19,8 @@ const size_t FUNCTIONS_COUNT = 1ll << FUNCTION_LEN; typedef Function MyFunction; typedef BoolSquareMatrix MyMatrix; +map function_formulas; + void test_function() { Function f_8(16); assert(("sizeof(sizeof(Function)", sizeof(Function)==2)); @@ -128,8 +132,9 @@ vector get_function_class(MyFunction f, const vector< MyMatrix >& tr ); bool is_inserted = transformed_res.insert(linear_transformed).second; if ( is_inserted ) { - // out << linear_transformed.string() << " = " << f.string() << " with (" - // << transformation.string(",") << ")" << endl; + if ( function_formulas.find(linear_transformed.value()) == function_formulas.end() ) { + function_formulas[linear_transformed.value()] = "LT"; + } } } } @@ -141,20 +146,38 @@ vector get_function_class(MyFunction f, const vector< MyMatrix >& tr vector get_linear_components() { vector res; 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 + function_formulas[res.back().value()] = "1"; res.push_back(MyFunction("0011")); // f = x_1 + function_formulas[res.back().value()] = "x_1"; res.push_back(MyFunction("0101")); // f = x_2 + function_formulas[res.back().value()] = "x_2"; } 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 + function_formulas[res.back().value()] = "1"; res.push_back(MyFunction("00001111")); // f = x_1 + function_formulas[res.back().value()] = "x_1"; res.push_back(MyFunction("00110011")); // f = x_2 + function_formulas[res.back().value()] = "x_2"; res.push_back(MyFunction("01010101")); // f = x_3 + function_formulas[res.back().value()] = "x_3"; } 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 + function_formulas[res.back().value()] = "1"; res.push_back(MyFunction("0000000011111111")); // f = x_1 + function_formulas[res.back().value()] = "x_1"; res.push_back(MyFunction("0000111100001111")); // f = x_2 + function_formulas[res.back().value()] = "x_2"; res.push_back(MyFunction("0011001100110011")); // f = x_3 + function_formulas[res.back().value()] = "x_3"; res.push_back(MyFunction("0101010101010101")); // f = x_4 + function_formulas[res.back().value()] = "x_4"; } else if constexpr ( ARGS_COUNT == 5 ) { res.push_back(MyFunction("11111111111111111111111111111111")); // f = 1 res.push_back(MyFunction("00000000000000001111111111111111")); // f = x_1 @@ -176,12 +199,22 @@ vector get_linear_combinations(const vector &linear_comp for (auto el_first: res) for (auto el_second: res) { 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; } } return vector(res.begin(), res.end()); } +string preprocess_factor(string factor) { + if ( factor.find("+") != string::npos and factor.find("*") == string::npos ) + return "(" + factor + ")"; + return factor; +} + vector get_all_monomials(const vector &linear_combinations) { set res(linear_combinations.begin(), linear_combinations.end()); bool is_added = true; @@ -190,12 +223,21 @@ vector get_all_monomials(const vector &linear_combinatio for (auto el_first: res) for (auto el_second: res) { 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; } } return vector(res.begin(), res.end()); } +string preprocess_monom(string monom) { + return monom; +} + void fill_ranks(vector monomials) { vector used_map(FUNCTIONS_COUNT, 0); size_t functions_remains = FUNCTIONS_COUNT; @@ -219,6 +261,11 @@ void fill_ranks(vector monomials) { MyFunction res_el = el_first xor el_second; if ( used_map[res_el.value()] == 0 ) { --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; ranks.at(total_ranks).push_back(res_el); } @@ -231,8 +278,6 @@ void fill_ranks(vector monomials) { auto possible_tranformations = get_good_matrices(); cout << "Total " << possible_tranformations.size() << " linear tranformations" << endl; - //for (auto el: possible_tranformations) - // cout << el.string(",") <