18namespace layer1_foundations {
35 int f_v = (verbose_level >= 1);
43 cout <<
"function_polish::init" << endl;
46 cout <<
"function_polish::nb_commands = " <<
Descr->
code_sz << endl;
47 cout <<
"variables:" << endl;
51 cout <<
"constants:" << endl;
55 cout <<
"commands:" << endl;
57 cout << i <<
" : " <<
Descr->
code[i] << endl;
76 cout <<
"function_polish::init start parsing" << endl;
91 for (j = 0; j < (int)
Variables.size(); j++) {
92 if (strcmp(
Variables[j].c_str(), S.c_str()) == 0) {
99 cout <<
"push variable " << S <<
" (= " << j <<
")" << endl;
103 for (j = 0; j < (int)
Constants.size(); j++) {
105 if (strcmp(
Constants[j].first.c_str(), S.c_str()) == 0) {
112 cout <<
"push constant " << S <<
" (= " << j <<
")" << endl;
116 val = atof(S.c_str());
119 cout <<
"push immediate " << val << endl;
130 for (j = 0; j < (int)
Variables.size(); j++) {
131 if (strcmp(
Variables[j].c_str(), S.c_str()) == 0) {
138 cout <<
"store " << S <<
" (= " << j <<
")" << endl;
142 cout <<
"store command, cannot find variable with label " << S << endl;
148 cout <<
"mult" << endl;
154 cout <<
"add" << endl;
163 cout <<
"cos" << endl;
169 cout <<
"sqrt" << endl;
175 cout <<
"return" << endl;
177 Entry.push_back(entry);
179 entry =
Code.size() + 1;
183 cout <<
"unrecognized command " <<
Descr->
code[i] << endl;
190 cout <<
"function_polish::init Parsed " <<
Entry.size() <<
" functions" << endl;
198 cout <<
"function_polish::init done" << endl;
205 int f_v = (verbose_level >= 1);
208 cout <<
"function_polish::print_code_complete" << endl;
213 for (h = 0; h <
Entry.size(); h++) {
216 cout <<
"subroutine " << h <<
" starts at " << i0 <<
" and has length " << len <<
":" << endl;
228 for (j = 0; j < len; j++) {
230 if (i >=
Code.size()) {
236 cout << i <<
" : push " <<
Constants[
Code[i].arg].first << endl;
241 cout << i <<
" : push " << f << endl;
253 cout << i <<
" : mult" << endl;
257 cout << i <<
" : add" << endl;
261 cout << i <<
" : cos" << endl;
265 cout << i <<
" : sin" << endl;
269 cout << i <<
" : sqrt" << endl;
272 cout << i <<
" : return" << endl;
275 cout <<
"unknown type, error. t = " << t << endl;
282 double *variable_values,
283 double *output_values,
286 int f_v = (verbose_level >= 1);
287 vector<double > Stack;
288 int h, fst, len, i, j, t;
292 cout <<
"function_polish::evaluate" << endl;
294 for (h = 0; h < (int)
Entry.size(); h++) {
298 cout <<
"function_polish::evaluate evaluation function " << h <<
" / " <<
Entry.size() << endl;
299 cout <<
"function_polish::evaluate fst=" << fst << endl;
300 cout <<
"function_polish::evaluate len=" << len << endl;
302 if (Stack.size() != 0) {
303 cout <<
"stack must be empty before we start" << endl;
306 for (j = 0; j < len; j++) {
308 cout <<
"h=" << h <<
" j=" << j <<
" : ";
316 cout <<
"pushing constant " << f <<
" from constant " <<
Code[i].arg << endl;
324 cout <<
"pushing immediate constant " << f << endl;
330 f = variable_values[
Code[i].arg];
332 cout <<
"pushing variable " << f <<
" from variable " <<
Code[i].arg << endl;
338 f = Stack[Stack.size() - 1];
339 variable_values[
Code[i].arg] = f;
342 cout <<
"storing value " << f <<
" to variable " <<
Variables[
Code[i].arg] <<
" : stacksize = " << Stack.size() << endl;
347 if (Stack.size() < 2) {
348 cout <<
"multiplication needs at least two elements on the stack; stack size = " << Stack.size() << endl;
352 a = Stack[Stack.size() - 1];
354 b = Stack[Stack.size() - 1];
358 cout <<
"mult: " << a <<
" * " << b <<
" = " << f << endl;
364 if (Stack.size() < 2) {
365 cout <<
"addition needs at least two elements on the stack; stack size = " << Stack.size() << endl;
369 a = Stack[Stack.size() - 1];
371 b = Stack[Stack.size() - 1];
375 cout <<
"add: " << a <<
" + " << b <<
" = " << f << endl;
381 if (Stack.size() < 1) {
382 cout <<
"cos needs at least one element on the stack; stack size = " << Stack.size() << endl;
386 a = Stack[Stack.size() - 1];
389 cout <<
"cos " << a <<
" = " << f << endl;
391 Stack[Stack.size() - 1] = f;
395 if (Stack.size() < 1) {
396 cout <<
"sin needs at least one element on the stack; stack size = " << Stack.size() << endl;
400 a = Stack[Stack.size() - 1];
403 cout <<
"sin " << a <<
" = " << f << endl;
405 Stack[Stack.size() - 1] = f;
409 if (Stack.size() < 1) {
410 cout <<
"sin needs at least one element on the stack; stack size = " << Stack.size() << endl;
414 a = Stack[Stack.size() - 1];
416 cout <<
"sqrt: the argument is negative, a = " << a << endl;
422 cout <<
"sqrt " << a <<
" = " << f << endl;
424 Stack[Stack.size() - 1] = f;
428 cout <<
"return, stacksize = " << Stack.size() << endl;
430 if (Stack.size() != 1) {
431 cout <<
"Stack size is not 1 at the end of the execution of function " << h << endl;
434 output_values[h] = Stack[0];
439 cout <<
"unknown type, error. t = " << t << endl;
444 cout <<
"function_polish::evaluate function " << h <<
" / " <<
Entry.size()
445 <<
" evaluates to " << output_values[h] << endl;
450 for (h = 0; h < (int)
Entry.size(); h++) {
451 cout <<
"function " << h <<
" evaluates to " << output_values[h] << endl;
455 cout <<
"function_polish::evaluate done" << endl;
an individual command which is part of a function expressed in reverse polish notation
void init_with_argument(int type, int arg)
void init_push_immediate_constant(double val)
void init_simple(int type)
description of a function in reverse polish notation from the command line
std::vector< std::string > const_names
std::vector< std::string > variable_names
std::vector< std::string > code
std::vector< std::string > const_values
function_polish_description * Descr
void print_code(int i0, int len, int verbose_level)
std::vector< std::pair< std::string, double > > Constants
std::vector< std::string > Variables
void init(function_polish_description *Descr, int verbose_level)
void evaluate(double *variable_values, double *output_values, int verbose_level)
std::vector< function_command > Code
void print_code_complete(int verbose_level)
the orbiter library for the classification of combinatorial objects