Orbiter 2022
Combinatorial Objects
formula.cpp
Go to the documentation of this file.
1/*
2 * formula.cpp
3 *
4 * Created on: Feb 25, 2021
5 * Author: betten
6 */
7
8#include "foundations.h"
9
10using namespace std;
11
12
13namespace orbiter {
14namespace layer1_foundations {
15namespace expression_parser {
16
17
19{
20 //std::string name_of_formula;
21 //std::string name_of_formula_latex;
22 //std::string managed_variables;
23 //std::string formula_text;
24 tree = NULL;
25
27
29 degree = 0;
30
31}
32
34{
35 if (tree) {
37 }
38}
39
41{
42 cout << "formula: " << name_of_formula << endl;
43 cout << "formula: " << name_of_formula_latex << endl;
44 cout << "managed_variables: " << managed_variables << endl;
45 cout << "nb_managed_vars=" << nb_managed_vars << endl;
46 cout << "formula_text=" << formula_text << endl;
47 cout << "f_is_homogeneous=" << f_is_homogeneous << endl;
48 cout << "degree=" << degree << endl;
49}
50
51
52void formula::init(std::string &label, std::string &label_tex,
53 std::string &managed_variables, std::string &formula_text,
54 int verbose_level)
55{
56 int f_v = (verbose_level >= 1);
57
58 if (f_v) {
59 cout << "formula::init" << endl;
60 }
61
62 name_of_formula.assign(label);
63 name_of_formula_latex.assign(label_tex);
66
67 expression_parser Parser;
69 //syntax_tree *tree;
70 int i;
71
73
74 if (f_v) {
75 cout << "formula::initFormula " << name_of_formula << " is " << formula_text << endl;
76 cout << "formula::initManaged variables: " << managed_variables << endl;
77 }
78
79 const char *p = managed_variables.c_str();
80 char str[1000];
81
82 while (TRUE) {
83 if (!ST.s_scan_token_comma_separated(&p, str)) {
84 break;
85 }
86 string var;
87
88 var.assign(str);
89 if (f_v) {
90 cout << "formula::initadding managed variable " << var << endl;
91 }
92
93 tree->managed_variables.push_back(var);
95
96 }
97
98
100
101 if (f_v) {
102 cout << "formula::initManaged variables: " << endl;
103 for (i = 0; i < nb_managed_vars; i++) {
104 cout << i << " : " << tree->managed_variables[i] << endl;
105 }
106 }
107
108
109 if (f_v) {
110 cout << "formula::initStarting to parse " << name_of_formula << endl;
111 }
112 Parser.parse(tree, formula_text, 0 /*verbose_level*/);
113 if (f_v) {
114 cout << "formula::initParsing " << name_of_formula << " finished" << endl;
115 }
116
117
118 if (FALSE) {
119 cout << "formula::initSyntax tree:" << endl;
120 tree->print(cout);
121 }
122
123 std::string fname;
124 fname.assign(name_of_formula);
125 fname.append(".gv");
126
127 {
128 std::ofstream ost(fname);
130 }
131
132 if (f_is_homogeneous) {
133 cout << "formula::init before tree->is_homogeneous" << endl;
134 }
135 f_is_homogeneous = tree->is_homogeneous(degree, verbose_level - 3);
136 if (f_is_homogeneous) {
137 cout << "formula::init after tree->is_homogeneous" << endl;
138 }
139
140 if (f_is_homogeneous) {
141 cout << "formula::init the formula is homogeneous of degree " << degree << endl;
142 }
143
144 if (f_v) {
145 cout << "formula::init done" << endl;
146 }
147}
148
149
151 syntax_tree_node **&Subtrees, int &nb_monomials,
152 int verbose_level)
153{
154 int f_v = (verbose_level >= 1);
155
156 if (f_v) {
157 cout << "formula::get_subtrees" << endl;
158 }
159
160 if (!f_is_homogeneous) {
161 cout << "formula::get_subtrees !f_is_homogeneous" << endl;
162 exit(1);
163 }
164
165
166 if (f_v) {
167 cout << "homogeneous of degree " << degree << endl;
168 }
169
170 if (degree != Poly->degree) {
171 cout << "formula::get_subtrees degree != Poly->degree" << endl;
172 exit(1);
173 }
174
175 //int i;
176
177 nb_monomials = Poly->get_nb_monomials();
178
179 tree->split_by_monomials(Poly, Subtrees, verbose_level);
180
181#if 0
182 if (Descr->f_evaluate) {
183
184 cout << "before evaluate" << endl;
185
186 const char *p = Descr->evaluate_text.c_str();
187 //char str[1000];
188
189 std::map<std::string, std::string> symbol_table;
190 //vector<string> symbols;
191 //vector<string> values;
192
193 while (TRUE) {
194 if (!s_scan_token_comma_separated(&p, str)) {
195 break;
196 }
197 string assignment;
198 int len;
199
200 assignment.assign(str);
201 len = strlen(str);
202
203 std::size_t found;
204
205 found = assignment.find('=');
206 if (found == std::string::npos) {
207 cout << "did not find '=' in variable assignment" << endl;
208 exit(1);
209 }
210 std::string symb = assignment.substr (0, found);
211 std::string val = assignment.substr (found + 1, len - found - 1);
212
213
214
215 cout << "adding symbol " << symb << " = " << val << endl;
216
217 symbol_table[symb] = val;
218 //symbols.push_back(symb);
219 //values.push_back(val);
220
221 }
222
223#if 0
224 cout << "symbol table:" << endl;
225 for (i = 0; i < symbol_table.size(); i++) {
226 cout << i << " : " << symbol_table[i] << " = " << values[i] << endl;
227 }
228#endif
229 int a;
230 int *Values;
231
232 Values = NEW_int(nb_monomials);
233
234 for (i = 0; i < nb_monomials; i++) {
235 cout << "Monomial " << i << " : ";
236 if (Subtrees[i]) {
237 //Subtrees[i]->print_expression(cout);
238 a = Subtrees[i]->evaluate(symbol_table, F, verbose_level);
239 Values[i] = a;
240 cout << a << " * ";
241 Poly->print_monomial(cout, i);
242 cout << endl;
243 }
244 else {
245 cout << "no subtree" << endl;
246 Values[i] = 0;
247 }
248 }
249 cout << "evaluated polynomial:" << endl;
250 for (i = 0; i < nb_monomials; i++) {
251 cout << Values[i] << " * ";
252 Poly->print_monomial(cout, i);
253 cout << endl;
254 }
255 cout << "coefficient vector: ";
256 int_vec_print(cout, Values, nb_monomials);
257 cout << endl;
258
259 }
260#endif
261
262
263 if (f_v) {
264 cout << "formula::get_subtrees done" << endl;
265 }
266}
267
269 syntax_tree_node **Subtrees, std::string &evaluate_text, int *Values,
270 int verbose_level)
271{
272 int f_v = (verbose_level >= 1);
273
274 if (f_v) {
275 cout << "formula::evaluate" << endl;
276 }
277
279 const char *p = evaluate_text.c_str();
280 char str[1000];
281
282 std::map<std::string, std::string> symbol_table;
283 //vector<string> symbols;
284 //vector<string> values;
285
286 while (TRUE) {
287 if (!ST.s_scan_token_comma_separated(&p, str)) {
288 break;
289 }
290 string assignment;
291 int len;
292
293 assignment.assign(str);
294 len = strlen(str);
295
296 std::size_t found;
297
298 found = assignment.find('=');
299 if (found == std::string::npos) {
300 cout << "did not find '=' in variable assignment" << endl;
301 exit(1);
302 }
303 std::string symb = assignment.substr (0, found);
304 std::string val = assignment.substr (found + 1, len - found - 1);
305
306
307
308 cout << "adding symbol " << symb << " = " << val << endl;
309
310 symbol_table[symb] = val;
311 //symbols.push_back(symb);
312 //values.push_back(val);
313
314 }
315
316#if 0
317 cout << "symbol table:" << endl;
318 for (i = 0; i < symbol_table.size(); i++) {
319 cout << i << " : " << symbol_table[i] << " = " << values[i] << endl;
320 }
321#endif
322 int i, a;
323
324 //Values = NEW_int(nb_monomials);
325
326 for (i = 0; i < Poly->get_nb_monomials(); i++) {
327 cout << "Monomial " << i << " : ";
328 if (Subtrees[i]) {
329 //Subtrees[i]->print_expression(cout);
330 a = Subtrees[i]->evaluate(symbol_table, Poly->get_F(), verbose_level);
331 Values[i] = a;
332 cout << a << " * ";
333 Poly->print_monomial(cout, i);
334 cout << endl;
335 }
336 else {
337 cout << "no subtree" << endl;
338 Values[i] = 0;
339 }
340 }
341 cout << "evaluated polynomial:" << endl;
342 for (i = 0; i < Poly->get_nb_monomials(); i++) {
343 cout << Values[i] << " * ";
344 Poly->print_monomial(cout, i);
345 cout << endl;
346 }
347 cout << "coefficient vector: ";
348 Int_vec_print(cout, Values, Poly->get_nb_monomials());
349 cout << endl;
350
351
352 if (f_v) {
353 cout << "formula::evaluate done" << endl;
354 }
355}
356
357
358}}}
359
functions related to strings and character arrays
void evaluate(ring_theory::homogeneous_polynomial_domain *Poly, syntax_tree_node **Subtrees, std::string &evaluate_text, int *Values, int verbose_level)
Definition: formula.cpp:268
void init(std::string &label, std::string &label_tex, std::string &managed_variables, std::string &formula_text, int verbose_level)
Definition: formula.cpp:52
void get_subtrees(ring_theory::homogeneous_polynomial_domain *Poly, syntax_tree_node **&Subtrees, int &nb_monomials, int verbose_level)
Definition: formula.cpp:150
int evaluate(std::map< std::string, std::string > &symbol_table, field_theory::finite_field *F, int verbose_level)
void export_graphviz(std::string &name, std::ostream &ost)
void split_by_monomials(ring_theory::homogeneous_polynomial_domain *Poly, syntax_tree_node **&Subtrees, int verbose_level)
Definition: syntax_tree.cpp:85
int is_homogeneous(int &degree, int verbose_level)
Definition: syntax_tree.cpp:61
homogeneous polynomials of a given degree in a given number of variables over a finite field GF(q)
Definition: ring_theory.h:88
#define NEW_OBJECT(type)
Definition: foundations.h:638
#define FREE_OBJECT(p)
Definition: foundations.h:651
#define NEW_int(n)
Definition: foundations.h:625
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define Int_vec_print(A, B, C)
Definition: foundations.h:685
the orbiter library for the classification of combinatorial objects