Orbiter 2022
Combinatorial Objects
expression_parser_domain.cpp
Go to the documentation of this file.
1/*
2 * expression_parser_domain.cpp
3 *
4 * Created on: Mar 25, 2021
5 * Author: betten
6 */
7
8
9#include "foundations.h"
10
11using namespace std;
12
13
14
15namespace orbiter {
16namespace layer1_foundations {
17namespace expression_parser {
18
19
21{
22
23}
24
26{
27
28}
29
32 std::string &name_of_formula,
33 std::string &formula_text,
34 std::string &managed_variables,
35 int f_evaluate,
36 std::string &parameters,
37 int verbose_level)
38{
39 int f_v = (verbose_level >= 1);
40
41 if (f_v) {
42 cout << "expression_parser_domain::parse_and_evaluate" << endl;
43 }
44
45 expression_parser Parser;
46 syntax_tree *tree;
48 int i;
49
50 tree = NEW_OBJECT(syntax_tree);
51
52 if (f_v) {
53 cout << "expression_parser_domain::parse_and_evaluate Formula " << name_of_formula << " is " << formula_text << endl;
54 cout << "expression_parser_domain::parse_and_evaluate Managed variables: " << managed_variables << endl;
55 }
56
57 const char *p = managed_variables.c_str();
58 char str[1000];
59
60 while (TRUE) {
61 if (!ST.s_scan_token_comma_separated(&p, str)) {
62 break;
63 }
64 string var;
65
66 var.assign(str);
67 if (f_v) {
68 cout << "expression_parser_domain::parse_and_evaluate adding managed variable " << var << endl;
69 }
70
71 tree->managed_variables.push_back(var);
73
74 }
75
76 int nb_vars;
77
78 nb_vars = tree->managed_variables.size();
79
80 if (f_v) {
81 cout << "expression_parser_domain::parse_and_evaluate Managed variables: " << endl;
82 for (i = 0; i < nb_vars; i++) {
83 cout << i << " : " << tree->managed_variables[i] << endl;
84 }
85 }
86
87
88 if (f_v) {
89 cout << "expression_parser_domain::parse_and_evaluate Starting to parse " << name_of_formula << endl;
90 }
91 Parser.parse(tree, formula_text, 0/*verbose_level*/);
92
93 if (f_v) {
94 cout << "expression_parser_domain::parse_and_evaluate Parsing " << name_of_formula << " finished" << endl;
95 }
96
97
98 if (f_v) {
99 cout << "Syntax tree:" << endl;
100 //tree->print(cout);
101 }
102
103 std::string fname;
104 fname.assign(name_of_formula);
105 fname.append(".gv");
106
107 {
108 std::ofstream ost(fname);
109 tree->Root->export_graphviz(name_of_formula, ost);
110 }
111
112 int ret, degree;
113
114 if (f_v) {
115 cout << "expression_parser_domain::parse_and_evaluate before tree->is_homogeneous" << endl;
116 }
117 ret = tree->is_homogeneous(degree, verbose_level - 3);
118 if (f_v) {
119 cout << "expression_parser_domain::parse_and_evaluate after tree->is_homogeneous" << endl;
120 }
121 if (ret) {
122 if (f_v) {
123 cout << "expression_parser_domain::parse_and_evaluate homogeneous of degree " << degree << endl;
124 }
125
127
129
130 if (f_v) {
131 cout << "expression_parser_domain::parse_and_evaluate before Poly->init" << endl;
132 }
133 Poly->init(F,
134 nb_vars /* nb_vars */, degree,
135 FALSE /* f_init_incidence_structure */,
136 t_PART,
137 verbose_level - 3);
138 if (f_v) {
139 cout << "after Poly->init" << endl;
140 }
141
142 syntax_tree_node **Subtrees;
143 int nb_monomials;
144
145 nb_monomials = Poly->get_nb_monomials();
146
147 tree->split_by_monomials(Poly, Subtrees, verbose_level);
148
149 if (f_v) {
150 for (i = 0; i < nb_monomials; i++) {
151 cout << "expression_parser_domain::parse_and_evaluate Monomial " << i << " : ";
152 if (Subtrees[i]) {
153 Subtrees[i]->print_expression(cout);
154 cout << " * ";
155 Poly->print_monomial(cout, i);
156 cout << endl;
157 }
158 else {
159 cout << "expression_parser_domain::parse_and_evaluate no subtree" << endl;
160 }
161 }
162 }
163
164 if (f_evaluate) {
165
166 if (f_v) {
167 cout << "expression_parser_domain::parse_and_evaluate before evaluate" << endl;
168 }
169
170 const char *p = parameters.c_str();
171 //char str[1000];
172
173 std::map<std::string, std::string> symbol_table;
174 //vector<string> symbols;
175 //vector<string> values;
176
177 while (TRUE) {
178 if (!ST.s_scan_token_comma_separated(&p, str)) {
179 break;
180 }
181 string assignment;
182 int len;
183
184 assignment.assign(str);
185 len = strlen(str);
186
187 std::size_t found;
188
189 found = assignment.find('=');
190 if (found == std::string::npos) {
191 cout << "expression_parser_domain::parse_and_evaluate did not find '=' in variable assignment" << endl;
192 exit(1);
193 }
194 std::string symb = assignment.substr (0, found);
195 std::string val = assignment.substr (found + 1, len - found - 1);
196
197
198
199 if (f_v) {
200 cout << "expression_parser_domain::parse_and_evaluate adding symbol " << symb << " = " << val << endl;
201 }
202
203 symbol_table[symb] = val;
204 //symbols.push_back(symb);
205 //values.push_back(val);
206
207 }
208
209#if 0
210 if (f_v) {
211 cout << "expression_parser_domain::parse_and_evaluate symbol table:" << endl;
212 for (i = 0; i < symbol_table.size(); i++) {
213 cout << i << " : " << symbol_table[i] << " = " << values[i] << endl;
214 }
215 }
216#endif
217 int a;
218 int *Values;
219
220 Values = NEW_int(nb_monomials);
221
222 for (i = 0; i < nb_monomials; i++) {
223 cout << "expression_parser_domain::parse_and_evaluate Monomial " << i << " : ";
224 if (Subtrees[i]) {
225 //Subtrees[i]->print_expression(cout);
226 a = Subtrees[i]->evaluate(symbol_table, F, verbose_level);
227 Values[i] = a;
228 cout << a << " * ";
229 Poly->print_monomial(cout, i);
230 cout << endl;
231 }
232 else {
233 cout << "expression_parser_domain::parse_and_evaluate no subtree" << endl;
234 Values[i] = 0;
235 }
236 }
237 if (f_v) {
238 cout << "expression_parser_domain::parse_and_evaluate evaluated polynomial:" << endl;
239 for (i = 0; i < nb_monomials; i++) {
240 cout << Values[i] << " * ";
241 Poly->print_monomial(cout, i);
242 cout << endl;
243 }
244 cout << "expression_parser_domain::parse_and_evaluate coefficient vector: ";
245 Int_vec_print(cout, Values, nb_monomials);
246 cout << endl;
247 }
248
249 }
250
251
252 FREE_OBJECT(Poly);
253 }
254 else {
255 if (f_v) {
256 cout << "expression_parser_domain::parse_and_evaluate not homogeneous" << endl;
257 }
258
259
260 if (f_evaluate) {
261
262 if (f_v) {
263 cout << "expression_parser_domain::parse_and_evaluate before evaluate" << endl;
264 }
265
266 const char *p = parameters.c_str();
267 //char str[1000];
268
269 std::map<std::string, std::string> symbol_table;
270 //vector<string> symbols;
271 //vector<string> values;
272
273 while (TRUE) {
274 if (!ST.s_scan_token_comma_separated(&p, str)) {
275 break;
276 }
277 string assignment;
278 int len;
279
280 assignment.assign(str);
281 len = strlen(str);
282
283 std::size_t found;
284
285 found = assignment.find('=');
286 if (found == std::string::npos) {
287 cout << "expression_parser_domain::parse_and_evaluate did not find '=' in variable assignment" << endl;
288 exit(1);
289 }
290 std::string symb = assignment.substr (0, found);
291 std::string val = assignment.substr (found + 1, len - found - 1);
292
293
294
295 if (f_v) {
296 cout << "expression_parser_domain::parse_and_evaluate adding symbol " << symb << " = " << val << endl;
297 }
298
299 symbol_table[symb] = val;
300 //symbols.push_back(symb);
301 //values.push_back(val);
302
303 }
304
305#if 0
306 cout << "expression_parser_domain::parse_and_evaluate symbol table:" << endl;
307 for (i = 0; i < symbol_table.size(); i++) {
308 cout << i << " : " << symbol_table[i] << " = " << values[i] << endl;
309 }
310#endif
311 int a;
312
313 a = tree->Root->evaluate(symbol_table, F, verbose_level);
314 if (f_v) {
315 cout << "expression_parser_domain::parse_and_evaluate the formula evaluates to " << a << endl;
316 }
317
318 }
319
320
321 }
322
323 if (f_v) {
324 cout << "expression_parser_domain::parse_and_evaluate done" << endl;
325 }
326}
327
330 std::string &formula_label,
331 std::string &parameters,
332 int verbose_level)
333{
334 int f_v = (verbose_level >= 1);
335
336 if (f_v) {
337 cout << "expression_parser_domain::evaluate" << endl;
338 }
339
340
341
342 int idx;
343 idx = orbiter_kernel_system::Orbiter->find_symbol(formula_label);
344
345 if (idx < 0) {
346 cout << "could not find symbol " << formula_label << endl;
347 exit(1);
348 }
349
350 if (orbiter_kernel_system::Orbiter->Orbiter_symbol_table->Table[idx].type != orbiter_kernel_system::t_object) {
351 cout << "symbol table entry must be of type t_object" << endl;
352 exit(1);
353 }
354
355
356
357
358 if (orbiter_kernel_system::Orbiter->Orbiter_symbol_table->Table[idx].object_type == t_collection) {
359 cout << "symbol table entry is a collection" << endl;
360
361 vector<string> *List;
362
363 List = (vector<string> *) orbiter_kernel_system::Orbiter->Orbiter_symbol_table->Table[idx].ptr;
364 int i;
365 int *Values;
366
367 Values = NEW_int(List->size());
368
369 for (i = 0; i < List->size(); i++) {
370 int idx1;
371
373 if (idx1 < 0) {
374 cout << "could not find symbol " << (*List)[i] << endl;
375 exit(1);
376 }
377 formula *F;
378 F = (formula *) orbiter_kernel_system::Orbiter->Orbiter_symbol_table->Table[idx1].ptr;
379
380 Values[i] = evaluate_formula(
381 F,
382 Fq,
383 parameters,
384 verbose_level);
385 }
386 cout << "The values of the formulae are:" << endl;
387 for (i = 0; i < List->size(); i++) {
388 cout << i << " : " << Values[i] << endl;
389 }
390
391 }
392 else if (orbiter_kernel_system::Orbiter->Orbiter_symbol_table->Table[idx].object_type == t_formula) {
393 cout << "symbol table entry is a formula" << endl;
394
395 formula *F;
396 F = (formula *) orbiter_kernel_system::Orbiter->Orbiter_symbol_table->Table[idx].ptr;
397
398 int a;
399
401 F,
402 Fq,
403 parameters,
404 verbose_level);
405 cout << "The formula evaluates to " << a << endl;
406 }
407 else {
408 cout << "symbol table entry must be either a formula or a collection" << endl;
409 exit(1);
410 }
411
412
413 if (f_v) {
414 cout << "expression_parser_domain::evaluate done" << endl;
415 }
416}
417
419 formula *F,
421 std::string &parameters,
422 int verbose_level)
423{
424 int f_v = (verbose_level >= 1);
426
427 if (f_v) {
428 cout << "expression_parser_domain::evaluate_formula" << endl;
429 }
430
431 if (f_v) {
432 cout << "expression_parser_domain::evaluate_formula before F->get_subtrees" << endl;
433 }
434
435 int ret, degree;
436 ret = F->tree->is_homogeneous(degree, verbose_level - 3);
437 if (ret) {
438 cout << "expression_parser_domain::evaluate_formula homogeneous of degree " << degree << endl;
439
441
443
444 if (f_v) {
445 cout << "expression_parser_domain::evaluate_formula before Poly->init" << endl;
446 }
447 Poly->init(Fq,
448 F->nb_managed_vars /* nb_vars */, degree,
449 FALSE /* f_init_incidence_structure */,
450 t_PART,
451 verbose_level - 3);
452 if (f_v) {
453 cout << "expression_parser_domain::evaluate_formula after Poly->init" << endl;
454 }
455
456 syntax_tree_node **Subtrees;
457 int nb_monomials;
458 int i;
459
460 nb_monomials = Poly->get_nb_monomials();
461
462 F->tree->split_by_monomials(Poly, Subtrees, verbose_level);
463
464 for (i = 0; i < nb_monomials; i++) {
465 cout << "expression_parser_domain::evaluate_formula Monomial " << i << " : ";
466 if (Subtrees[i]) {
467 Subtrees[i]->print_expression(cout);
468 cout << " * ";
469 Poly->print_monomial(cout, i);
470 cout << endl;
471 }
472 else {
473 cout << "expression_parser_domain::evaluate_formula no subtree" << endl;
474 }
475 }
476
477 cout << "expression_parser_domain::evaluate_formula before evaluate" << endl;
478
479 const char *p = parameters.c_str();
480 char str[1000];
481
482 std::map<std::string, std::string> symbol_table;
483 //vector<string> symbols;
484 //vector<string> values;
485
486 while (TRUE) {
487 if (!ST.s_scan_token_comma_separated(&p, str)) {
488 break;
489 }
490 string assignment;
491 int len;
492
493 assignment.assign(str);
494 len = strlen(str);
495
496 std::size_t found;
497
498 found = assignment.find('=');
499 if (found == std::string::npos) {
500 cout << "expression_parser_domain::evaluate_formula did not find '=' in variable assignment" << endl;
501 exit(1);
502 }
503 std::string symb = assignment.substr (0, found);
504 std::string val = assignment.substr (found + 1, len - found - 1);
505
506
507
508 cout << "expression_parser_domain::evaluate_formula adding symbol " << symb << " = " << val << endl;
509
510 symbol_table[symb] = val;
511 //symbols.push_back(symb);
512 //values.push_back(val);
513
514 }
515
516#if 0
517 cout << "expression_parser_domain::evaluate_formula symbol table:" << endl;
518 for (i = 0; i < symbol_table.size(); i++) {
519 cout << i << " : " << symbol_table[i] << " = " << values[i] << endl;
520 }
521#endif
522 int a;
523 int *Values;
524
525 Values = NEW_int(nb_monomials);
526
527 for (i = 0; i < nb_monomials; i++) {
528 cout << "expression_parser_domain::evaluate_formula Monomial " << i << " : ";
529 if (Subtrees[i]) {
530 //Subtrees[i]->print_expression(cout);
531 a = Subtrees[i]->evaluate(symbol_table, Fq, verbose_level);
532 Values[i] = a;
533 cout << a << " * ";
534 Poly->print_monomial(cout, i);
535 cout << endl;
536 }
537 else {
538 cout << "expression_parser_domain::evaluate_formula no subtree" << endl;
539 Values[i] = 0;
540 }
541 }
542 cout << "expression_parser_domain::evaluate_formula evaluated polynomial:" << endl;
543 for (i = 0; i < nb_monomials; i++) {
544 cout << Values[i] << " * ";
545 Poly->print_monomial(cout, i);
546 cout << endl;
547 }
548 cout << "expression_parser_domain::evaluate_formula coefficient vector: ";
549 Int_vec_print(cout, Values, nb_monomials);
550 cout << endl;
551
552
553
554 FREE_OBJECT(Poly);
555 }
556 else {
557 cout << "expression_parser_domain::evaluate_formula not homogeneous" << endl;
558
559
560 cout << "expression_parser_domain::evaluate_formula before evaluate" << endl;
561
562 const char *p = parameters.c_str();
563 char str[1000];
564
565 std::map<std::string, std::string> symbol_table;
566 //vector<string> symbols;
567 //vector<string> values;
568
569 while (TRUE) {
570 if (!ST.s_scan_token_comma_separated(&p, str)) {
571 break;
572 }
573 string assignment;
574 int len;
575
576 assignment.assign(str);
577 len = strlen(str);
578
579 std::size_t found;
580
581 found = assignment.find('=');
582 if (found == std::string::npos) {
583 cout << "expression_parser_domain::evaluate_formula did not find '=' in variable assignment" << endl;
584 exit(1);
585 }
586 std::string symb = assignment.substr (0, found);
587 std::string val = assignment.substr (found + 1, len - found - 1);
588
589
590
591 cout << "expression_parser_domain::evaluate_formula adding symbol " << symb << " = " << val << endl;
592
593 symbol_table[symb] = val;
594 //symbols.push_back(symb);
595 //values.push_back(val);
596
597 }
598
599#if 0
600 cout << "expression_parser_domain::evaluate_formula symbol table:" << endl;
601 for (i = 0; i < symbol_table.size(); i++) {
602 cout << i << " : " << symbol_table[i] << " = " << values[i] << endl;
603 }
604#endif
605 int a;
606
607 a = F->tree->Root->evaluate(symbol_table, Fq, verbose_level);
608 cout << "expression_parser_domain::evaluate_formula the formula evaluates to " << a << endl;
609
610
611 return a;
612
613 }
614
615
616 if (f_v) {
617 cout << "expression_parser_domain::evaluate_formula done" << endl;
618 }
619 return 0;
620}
621
622
623
624}}}
625
626
627
functions related to strings and character arrays
void parse_and_evaluate(field_theory::finite_field *F, std::string &name_of_formula, std::string &formula_text, std::string &managed_variables, int f_evaluate, std::string &parameters, int verbose_level)
int evaluate_formula(formula *F, field_theory::finite_field *Fq, std::string &parameters, int verbose_level)
void evaluate(field_theory::finite_field *Fq, std::string &formula_label, std::string &parameters, int verbose_level)
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
orbiter_kernel_system::orbiter_symbol_table * Orbiter_symbol_table
homogeneous polynomials of a given degree in a given number of variables over a finite field GF(q)
Definition: ring_theory.h:88
void init(field_theory::finite_field *F, int nb_vars, int degree, int f_init_incidence_structure, monomial_ordering_type Monomial_ordering_type, int verbose_level)
#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
orbiter_kernel_system::orbiter_session * Orbiter
global Orbiter session
the orbiter library for the classification of combinatorial objects