From 50c2b4b46e71650907f2d1ebc903eab8bd256643 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 12 Jan 2015 18:38:51 +0000 Subject: [PATCH] lolremez: allow spaces in arithmetic expressions. --- tools/lolremez/expression.h | 38 ++++++++++++++++++++++++++----------- tools/lolremez/lolremez.cpp | 2 ++ tools/lolremez/solver.cpp | 13 +++++++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/tools/lolremez/expression.h b/tools/lolremez/expression.h index 592d8d27..61d39229 100644 --- a/tools/lolremez/expression.h +++ b/tools/lolremez/expression.h @@ -226,21 +226,32 @@ private: struct r_expr; - // Rule for unary function calls + // r_ <- * + struct _ : star {}; + + // r_call_unary <- "(" r_expr ")" template struct r_call_unary : ifapply, + _, one<'('>, + _, r_expr, + _, one<')'>>, do_op> {}; - // Rule for binary function calls + // r_call_binary <- "(" r_expr "," r_expr ")" template struct r_call_binary : ifapply, + _, one<'('>, + _, r_expr, + _, one<','>, + _, r_expr, + _, one<')'>>, do_op> {}; @@ -250,8 +261,7 @@ private: // r_var <- "x" struct r_var : ifapply, do_op> {}; - // r_call <- "(" r_expr ")" - // / "(" r_expr "," r_expr ")" + // r_call <- r_call_unary / r_call_binary struct r_call : sor, r_call_unary, r_call_unary, @@ -274,7 +284,11 @@ private: r_call_binary> {}; // r_parentheses <- "(" r_expr ")" - struct r_parentheses : seq, r_expr, one<')'>> {}; + struct r_parentheses : seq, + _, + r_expr, + _, + one<')'>> {}; // r_terminal <- r_call / r_var / r_constant / r_parentheses struct r_terminal : sor, string<'*', '*'>>, + struct r_exponent : ifapply, string<'*', '*'>>, + _, r_terminal>, do_op> {}; struct r_factor : seq> {}; @@ -292,21 +308,21 @@ private: // r_mul <- "*" r_factor // r_div <- "/" r_factor // term <- r_factor ( r_mul / r_div ) * - struct r_mul : ifapply, r_factor>, do_op> {}; - struct r_div : ifapply, r_factor>, do_op> {}; + struct r_mul : ifapply, _, r_factor>, do_op> {}; + struct r_div : ifapply, _, r_factor>, do_op> {}; struct r_term : seq>> {}; // add <- "+" r_term // sub <- "-" r_term // r_expr <- r_term ( add / sub ) * - struct r_add : ifapply, r_term>, do_op> {}; - struct r_sub : ifapply, r_term>, do_op> {}; + struct r_add : ifapply, _, r_term>, do_op> {}; + struct r_sub : ifapply, _, r_term>, do_op> {}; struct r_expr : seq>> {}; // stmt <- r_expr - struct r_stmt : ifapply, do_success> {}; + struct r_stmt : ifapply, do_success> {}; }; } /* namespace grammar */ diff --git a/tools/lolremez/lolremez.cpp b/tools/lolremez/lolremez.cpp index 6e606304..81b8565f 100644 --- a/tools/lolremez/lolremez.cpp +++ b/tools/lolremez/lolremez.cpp @@ -28,9 +28,11 @@ void FAIL() { printf("Usage:\n"); printf(" lolremez [-d degree] [-i xmin xmax] x-expression [x-error]\n"); + printf("\n"); printf("Example:\n"); printf(" lolremez -d 4 -i -1 1 \"atan(exp(1+x))\"\n"); printf(" lolremez -d 4 -i -1 1 \"atan(exp(1+x))\" \"exp(1+x)\"\n"); + printf("\n"); exit(EXIT_FAILURE); } diff --git a/tools/lolremez/solver.cpp b/tools/lolremez/solver.cpp index 1c81db01..21a22182 100644 --- a/tools/lolremez/solver.cpp +++ b/tools/lolremez/solver.cpp @@ -58,7 +58,7 @@ void RemezSolver::Run(real a, real b, char const *func, char const *weight) for (int n = 0; ; n++) { real newerror = FindExtrema(); - printf("Step %i error: ", n); + printf(" -:- error at step %i: ", n); newerror.print(m_decimals); printf("\n"); @@ -172,6 +172,8 @@ void RemezSolver::Step() void RemezSolver::FindZeroes() { + m_stats_cheby = m_stats_func = m_stats_weight = 0.f; + /* Find m_order + 1 zeroes of the error function. No need to * compute the relative error: its zeroes are at the same * place as the absolute error! */ @@ -200,6 +202,9 @@ void RemezSolver::FindZeroes() m_zeroes[i] = mid.value; } + + printf(" -:- times for zeroes: estimate %f func %f weight %f\n", + m_stats_cheby, m_stats_func, m_stats_weight); } /* XXX: this is the real costly function */ @@ -272,9 +277,9 @@ real RemezSolver::FindExtrema() } } - printf("Iterations: Rounds %d Evals %d\n", rounds, evals); - printf("Times: Estimate %f Func %f EvalWeight %f\n", + printf(" -:- times for extrema: estimate %f func %f weight %f\n", m_stats_cheby, m_stats_func, m_stats_weight); + printf(" -:- calls: %d rounds, %d evals\n", rounds, evals); return final; } @@ -289,7 +294,7 @@ void RemezSolver::PrintPoly() polynomial q ({ -m_k1 / m_k2, real(1) / m_k2 }); polynomial r = m_estimate.eval(q); - printf("Polynomial estimate: "); + printf("\n"); for (int j = 0; j < m_order + 1; j++) { if (j)