Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

326 righe
8.4 KiB

  1. //
  2. // LolRemez - Remez algorithm implementation
  3. //
  4. // Copyright © 2005-2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <functional>
  16. #include <lol/engine.h>
  17. #include <lol/math/real.h>
  18. #include <lol/math/polynomial.h>
  19. #include "matrix.h"
  20. #include "solver.h"
  21. using lol::real;
  22. RemezSolver::RemezSolver(int order, int decimals)
  23. : m_order(order),
  24. m_decimals(decimals),
  25. m_has_weight(false)
  26. {
  27. }
  28. void RemezSolver::Run(real a, real b, char const *func, char const *weight)
  29. {
  30. using std::printf;
  31. m_func.parse(func);
  32. if (weight)
  33. {
  34. m_weight.parse(weight);
  35. m_has_weight = true;
  36. }
  37. m_k1 = (b + a) / 2;
  38. m_k2 = (b - a) / 2;
  39. m_epsilon = pow((real)10, (real)-(m_decimals + 2));
  40. Init();
  41. PrintPoly();
  42. real error = -1;
  43. for (int n = 0; ; n++)
  44. {
  45. real newerror = FindExtrema();
  46. printf("Step %i error: ", n);
  47. newerror.print(m_decimals);
  48. printf("\n");
  49. Step();
  50. if (error >= (real)0 && fabs(newerror - error) < error * m_epsilon)
  51. break;
  52. error = newerror;
  53. PrintPoly();
  54. FindZeroes();
  55. }
  56. PrintPoly();
  57. }
  58. /*
  59. * This is basically the first Remez step: we solve a system of
  60. * order N+1 and get a good initial polynomial estimate.
  61. */
  62. void RemezSolver::Init()
  63. {
  64. /* m_order + 1 zeroes of the error function */
  65. m_zeroes.Resize(m_order + 1);
  66. /* m_order + 2 control points */
  67. m_control.Resize(m_order + 2);
  68. /* Initial estimates for the x_i where the error will be zero and
  69. * precompute f(x_i). */
  70. array<real> fxn;
  71. for (int i = 0; i < m_order + 1; i++)
  72. {
  73. m_zeroes[i] = (real)(2 * i - m_order) / (real)(m_order + 1);
  74. fxn.Push(EvalFunc(m_zeroes[i]));
  75. }
  76. /* We build a matrix of Chebishev evaluations: row i contains the
  77. * evaluations of x_i for polynomial order n = 0, 1, ... */
  78. LinearSystem<real> system(m_order + 1);
  79. for (int n = 0; n < m_order + 1; n++)
  80. {
  81. auto p = polynomial<real>::chebyshev(n);
  82. for (int i = 0; i < m_order + 1; i++)
  83. system[i][n] = p.eval(m_zeroes[i]);
  84. }
  85. /* Solve the system */
  86. system = system.inverse();
  87. /* Compute new Chebyshev estimate */
  88. m_estimate = polynomial<real>();
  89. for (int n = 0; n < m_order + 1; n++)
  90. {
  91. real weight = 0;
  92. for (int i = 0; i < m_order + 1; i++)
  93. weight += system[n][i] * fxn[i];
  94. m_estimate += weight * polynomial<real>::chebyshev(n);
  95. }
  96. }
  97. /*
  98. * Every subsequent iteration of the Remez algorithm: we solve a system
  99. * of order N+2 to both refine the estimate and compute the error.
  100. */
  101. void RemezSolver::Step()
  102. {
  103. /* Pick up x_i where error will be 0 and compute f(x_i) */
  104. array<real> fxn;
  105. for (int i = 0; i < m_order + 2; i++)
  106. fxn.Push(EvalFunc(m_control[i]));
  107. /* We build a matrix of Chebishev evaluations: row i contains the
  108. * evaluations of x_i for polynomial order n = 0, 1, ... */
  109. LinearSystem<real> system(m_order + 2);
  110. for (int n = 0; n < m_order + 1; n++)
  111. {
  112. auto p = polynomial<real>::chebyshev(n);
  113. for (int i = 0; i < m_order + 2; i++)
  114. system[i][n] = p.eval(m_control[i]);
  115. }
  116. /* The last line of the system is the oscillating error */
  117. for (int i = 0; i < m_order + 2; i++)
  118. {
  119. real error = fabs(EvalWeight(m_control[i]));
  120. system[i][m_order + 1] = (i & 1) ? error : -error;
  121. }
  122. /* Solve the system */
  123. system = system.inverse();
  124. /* Compute new polynomial estimate */
  125. m_estimate = polynomial<real>();
  126. for (int n = 0; n < m_order + 1; n++)
  127. {
  128. real weight = 0;
  129. for (int i = 0; i < m_order + 2; i++)
  130. weight += system[n][i] * fxn[i];
  131. m_estimate += weight * polynomial<real>::chebyshev(n);
  132. }
  133. /* Compute the error (FIXME: unused?) */
  134. real error = 0;
  135. for (int i = 0; i < m_order + 2; i++)
  136. error += system[m_order + 1][i] * fxn[i];
  137. }
  138. void RemezSolver::FindZeroes()
  139. {
  140. /* Find m_order + 1 zeroes of the error function. No need to
  141. * compute the relative error: its zeroes are at the same
  142. * place as the absolute error! */
  143. for (int i = 0; i < m_order + 1; i++)
  144. {
  145. struct { real value, error; } left, right, mid;
  146. left.value = m_control[i];
  147. left.error = EvalEstimate(left.value) - EvalFunc(left.value);
  148. right.value = m_control[i + 1];
  149. right.error = EvalEstimate(right.value) - EvalFunc(right.value);
  150. static real limit = ldexp((real)1, -500);
  151. static real zero = (real)0;
  152. while (fabs(left.value - right.value) > limit)
  153. {
  154. mid.value = (left.value + right.value) / 2;
  155. mid.error = EvalEstimate(mid.value) - EvalFunc(mid.value);
  156. if ((left.error <= zero && mid.error <= zero)
  157. || (left.error >= zero && mid.error >= zero))
  158. left = mid;
  159. else
  160. right = mid;
  161. }
  162. m_zeroes[i] = mid.value;
  163. }
  164. }
  165. /* XXX: this is the real costly function */
  166. real RemezSolver::FindExtrema()
  167. {
  168. using std::printf;
  169. /* Find m_order + 2 extrema of the error function. We need to
  170. * compute the relative error, since its extrema are at slightly
  171. * different locations than the absolute error’s. */
  172. real final = 0;
  173. m_stats_cheby = m_stats_func = m_stats_weight = 0.f;
  174. int evals = 0, rounds = 0;
  175. for (int i = 0; i < m_order + 2; i++)
  176. {
  177. real a = -1, b = 1;
  178. if (i > 0)
  179. a = m_zeroes[i - 1];
  180. if (i < m_order + 1)
  181. b = m_zeroes[i];
  182. for (int r = 0; ; ++r, ++rounds)
  183. {
  184. real maxerror = 0, maxweight = 0;
  185. int best = -1;
  186. real c = a, delta = (b - a) / 4;
  187. for (int k = 0; k <= 4; k++)
  188. {
  189. if (r == 0 || (k & 1))
  190. {
  191. ++evals;
  192. real error = fabs(EvalEstimate(c) - EvalFunc(c));
  193. real weight = fabs(EvalWeight(c));
  194. /* if error/weight >= maxerror/maxweight */
  195. if (error * maxweight >= maxerror * weight)
  196. {
  197. maxerror = error;
  198. maxweight = weight;
  199. best = k;
  200. }
  201. }
  202. c += delta;
  203. }
  204. switch (best)
  205. {
  206. case 0:
  207. b = a + delta * 2;
  208. break;
  209. case 4:
  210. a = b - delta * 2;
  211. break;
  212. default:
  213. b = a + delta * (best + 1);
  214. a = a + delta * (best - 1);
  215. break;
  216. }
  217. if (delta < m_epsilon)
  218. {
  219. real e = fabs(maxerror / maxweight);
  220. if (e > final)
  221. final = e;
  222. m_control[i] = (a + b) / 2;
  223. break;
  224. }
  225. }
  226. }
  227. printf("Iterations: Rounds %d Evals %d\n", rounds, evals);
  228. printf("Times: Estimate %f Func %f EvalWeight %f\n",
  229. m_stats_cheby, m_stats_func, m_stats_weight);
  230. return final;
  231. }
  232. void RemezSolver::PrintPoly()
  233. {
  234. using std::printf;
  235. /* Transform our polynomial in the [-1..1] range into a polynomial
  236. * in the [a..b] range by composing it with q:
  237. * q(x) = 2x / (b-a) - (b+a) / (b-a) */
  238. polynomial<real> q ({ -m_k1 / m_k2, real(1) / m_k2 });
  239. polynomial<real> r = m_estimate.eval(q);
  240. printf("Polynomial estimate: ");
  241. for (int j = 0; j < m_order + 1; j++)
  242. {
  243. if (j)
  244. printf(" + x**%i * ", j);
  245. r[j].print(m_decimals);
  246. }
  247. printf("\n\n");
  248. }
  249. real RemezSolver::EvalEstimate(real const &x)
  250. {
  251. Timer t;
  252. real ret = m_estimate.eval(x);
  253. m_stats_cheby += t.Get();
  254. return ret;
  255. }
  256. real RemezSolver::EvalFunc(real const &x)
  257. {
  258. Timer t;
  259. real ret = m_func.eval(x * m_k2 + m_k1);
  260. m_stats_func += t.Get();
  261. return ret;
  262. }
  263. real RemezSolver::EvalWeight(real const &x)
  264. {
  265. Timer t;
  266. real ret = m_has_weight ? m_weight.eval(x * m_k2 + m_k1) : real(1);
  267. m_stats_weight += t.Get();
  268. return ret;
  269. }