-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.cpp
More file actions
153 lines (121 loc) · 5.35 KB
/
example.cpp
File metadata and controls
153 lines (121 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//==================================================================================
// FireFly - Reconstructing rational functions and polynomial over finite fields.
// Copyright (C) 2020 Jonas Klappert and Fabian Lange
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//==================================================================================
#include "firefly/DenseSolver.hpp"
#include "firefly/Reconstructor.hpp"
namespace firefly {
// Example of how one can use the black-box functor for the automatic interface
class BlackBoxUser : public BlackBoxBase<BlackBoxUser> {
public:
// Constructor of the derived class
// A default constructor is sufficient if no internal variables are required.
// In this example a ShuntingYardParser
BlackBoxUser(const ShuntingYardParser& par_) : par(par_) {};
// The evaluation of the black box
// Return a vector of FFIntTemp objects, which are the results of the black-box evaluation
// with values inserted for the variables.
// In this example we compute functions which are parsed from a file with a
// ShuntingYardParser object and the determinant of a matrix.
template<typename FFIntTemp>
std::vector<FFIntTemp> operator()(const std::vector<FFIntTemp>& values) {
//std::vector<FFIntTemp> result;
// Get results from parsed expressions
std::vector<FFIntTemp> result = par.evaluate_pre(values);
result.emplace_back(result[0] / result[3]);
// Build the matrix mat
mat_ff<FFIntTemp> mat = {{result[0], result[1]}, {result[2], result[3]}};
// Permutation vector
std::vector<int> p {};
// Compute LU decomposition of mat
calc_lu_decomposition(mat, p, 2);
// Compute determinant of mat
result.emplace_back(calc_determinant_lu(mat, p, 2));
return result;
}
// This function is called from Reconstructor when the prime field changes.
// Update the internal variables if required.
// In this example we precompute a few things for the ShuntingYardParser in
// the new prime field.
inline void prime_changed() {
par.precompute_tokens();
}
private:
// Internal variables for the black box
// In this example a ShuntingYardParser
ShuntingYardParser par;
};
}
using namespace firefly;
int main() {
std::string root_dir = FIREFLY_ROOT_DIR;
// Parse the functions from "../s_y_4_v.m" with the variables x1, y, zZ, W
ShuntingYardParser par(root_dir + "/parser_test/s_y_4_v.m", {"x1", "y", "zZ", "W"});
// Create the user defined black box
BlackBoxUser bb(par);
// Initialize the Reconstructor
Reconstructor<BlackBoxUser> reconst(4 /*n_vars*/,
std::thread::hardware_concurrency() /*n_threads*/,
1 /*bunch size*/,
bb /*black box*//*,
Reconstructor<BlackBoxUser>::CHATTY *//* verbosity mode*/);
// Enables scan for factors
reconst.enable_factor_scan();
// Stops after factor scan if one is only interesed in the occurring factors
//reconst.stop_after_factor_scan();
// Enables a scan for a sparse shift
reconst.enable_shift_scan();
// Set flag for the safe mode
//reconst.set_safe_interpolation();
// Write the state of all reconstruction objects after each interpolation over a prime field
// The intermediate results are stored in ./ff_save
//reconst.set_tags();
// Read in all saved states from the directory ./ff_save
//reconst.resume_from_saved_state();
// Reconstruct the black box
reconst.reconstruct();
// Print all found factors
/*for (const auto & el : reconst.get_factors_string({"x","y","z","w"})) {
std::cout << el << "\n";
}*/
// Get results after the first interpolation. Has to be called with
// reconst.reconstruct(1) to work
/*for(const auto& rf : reconst.get_result_ff()) {
std::cout << rf.to_string({"x","y","z","w"}) << "\n";
}*/
// Get results
/*std::vector<RationalFunction> results = reconst.get_result();
//std::ofstream file;
//file.open("test.m");
//file << "{";
//std::string str = "";
// Print all reconstruced functions
for (uint32_t i = 0; i < results.size(); ++i) {
std::cout << "Function " << i + 1 << ":\n" << results[i].to_string( {"x", "y", "z", "w"}) << "\n";
//file << str << "\n";
//str = results[i].to_string( {"x", "y", "z", "w"}) + "\n,";
}
//str.pop_back();
//file << str << "}\n";
//file.close();
*/
// Rewrite result in Horner form
/*std::string f15_horner = results[14].generate_horner({"x", "y", "z", "w"});
std::cout << "Function 15 in Horner form:\n" << f15_horner << "\n";*/
// Resets all statics in RatReconst to start a new reconstruction
//RatReconst::reset();
return 0;
}