1 // (C) Copyright John Maddock 2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_TOOLS_REMEZ_HPP
7 #define BOOST_MATH_TOOLS_REMEZ_HPP
13 #include <boost/math/tools/solve.hpp>
14 #include <boost/math/tools/minima.hpp>
15 #include <boost/math/tools/roots.hpp>
16 #include <boost/math/tools/polynomial.hpp>
17 #include <boost/function/function1.hpp>
18 #include <boost/scoped_array.hpp>
19 #include <boost/math/constants/constants.hpp>
20 #include <boost/math/policies/policy.hpp>
22 namespace boost{ namespace math{ namespace tools{
27 // The error function: the difference between F(x) and
28 // the current approximation. This is the function
29 // for which we must find the extema.
32 struct remez_error_function
34 typedef boost::function1<T, T const &> function_type;
38 const polynomial<T>& n,
39 const polynomial<T>& d,
41 : f(f_), numerator(n), denominator(d), rel_error(rel_err) {}
43 T operator()(const T& z)const
46 T abs = y - (numerator.evaluate(z) / denominator.evaluate(z));
54 // we must be at a root, or it's not recoverable:
55 BOOST_ASSERT(0 == abs);
60 // We have a divide by zero!
61 // Lets assume that f(x) is zero as a result of
62 // internal cancellation error that occurs as a result
63 // of shifting a root at point z to the origin so that
64 // the approximation can be "pinned" to pass through
65 // the origin: in that case it really
66 // won't matter what our approximation calculates here
67 // as long as it's a small number, return the absolute error:
77 polynomial<T> numerator;
78 polynomial<T> denominator;
82 // This function adapts the error function so that it's minima
83 // are the extema of the error function. We can find the minima
84 // with standard techniques.
87 struct remez_max_error_function
89 remez_max_error_function(const remez_error_function<T>& f)
92 T operator()(const T& x)
95 return -fabs(func(x));
98 remez_error_function<T> func;
107 typedef boost::function1<T, T const &> function_type;
108 typedef boost::numeric::ublas::vector<T> vector_type;
109 typedef boost::numeric::ublas::matrix<T> matrix_type;
111 remez_minimax(function_type f, unsigned oN, unsigned oD, T a, T b, bool pin = true, bool rel_err = false, int sk = 0, int bits = 0);
112 remez_minimax(function_type f, unsigned oN, unsigned oD, T a, T b, bool pin, bool rel_err, int sk, int bits, const vector_type& points);
114 void reset(unsigned oN, unsigned oD, T a, T b, bool pin = true, bool rel_err = false, int sk = 0, int bits = 0);
115 void reset(unsigned oN, unsigned oD, T a, T b, bool pin, bool rel_err, int sk, int bits, const vector_type& points);
117 void set_brake(int b)
119 BOOST_ASSERT(b < 100);
120 BOOST_ASSERT(b >= 0);
126 polynomial<T> denominator()const;
127 polynomial<T> numerator()const;
129 vector_type const& chebyshev_points()const
131 return control_points;
134 vector_type const& zero_points()const
141 return solution[solution.size() - 1];
156 void rescale(T a, T b)
158 T scale = (b - a) / (max - min);
159 for(unsigned i = 0; i < control_points.size(); ++i)
161 control_points[i] = (control_points[i] - min) * scale + a;
168 void init_chebyshev();
170 function_type func; // The function to approximate.
171 vector_type control_points; // Current control points to be used for the next iteration.
172 vector_type solution; // Solution from the last iteration contains all unknowns including the error term.
173 vector_type zeros; // Location of points of zero error from last iteration, plus the two end points.
174 vector_type maxima; // Location of maxima of the error function, actually contains the control points used for the last iteration.
175 T m_max_error; // Maximum error found in last approximation.
176 T m_max_change; // Maximum change in location of control points after last iteration.
177 unsigned orderN; // Order of the numerator polynomial.
178 unsigned orderD; // Order of the denominator polynomial.
179 T min, max; // End points of the range to optimise over.
180 bool rel_error; // If true optimise for relative not absolute error.
181 bool pinned; // If true the approximation is "pinned" to go through the origin.
182 unsigned unknowns; // Total number of unknowns.
183 int m_precision; // Number of bits precision to which the zeros and maxima are found.
184 T m_max_change_history[2]; // Past history of changes to control points.
185 int m_brake; // amount to break by in percentage points.
186 int m_skew; // amount to skew starting points by in percentage points: -100-100
197 void remez_minimax<T>::init_chebyshev()
201 // Fill in the zeros:
203 unsigned terms = pinned ? orderD + orderN : orderD + orderN + 1;
205 for(unsigned i = 0; i < terms; ++i)
207 T cheb = cos((2 * terms - 1 - 2 * i) * constants::pi<T>() / (2 * terms));
212 T p = static_cast<T>(200 + m_skew) / 200;
220 zeros[unknowns] = max;
221 // perform a regular interpolation fit:
222 matrix_type A(terms, terms);
223 vector_type b(terms);
224 // fill in the y values:
225 for(unsigned i = 0; i < b.size(); ++i)
227 b[i] = func(zeros[i+1]);
229 // fill in powers of x evaluated at each of the control points:
230 unsigned offsetN = pinned ? 0 : 1;
231 unsigned offsetD = offsetN + orderN;
232 unsigned maxorder = (std::max)(orderN, orderD);
233 for(unsigned i = 0; i < b.size(); ++i)
239 for(unsigned j = 0; j < maxorder; ++j)
242 A(i, j + offsetN) = x;
245 A(i, j + offsetD) = -x * b[i];
251 // Now go ahead and solve the expression to get our solution:
253 vector_type l_solution = boost::math::tools::solve(A, b);
254 // need to add a "fake" error term:
255 l_solution.resize(unknowns);
256 l_solution[unknowns-1] = 0;
257 solution = l_solution;
259 // Now find all the extrema of the error function:
261 detail::remez_error_function<T> Err(func, this->numerator(), this->denominator(), rel_error);
262 detail::remez_max_error_function<T> Ex(Err);
264 //int max_err_location = 0;
265 for(unsigned i = 0; i < unknowns; ++i)
267 std::pair<T, T> r = brent_find_minima(Ex, zeros[i], zeros[i+1], m_precision);
269 T rel_err = fabs(r.second);
270 if(rel_err > m_max_error)
272 m_max_error = fabs(r.second);
273 //max_err_location = i;
276 control_points = maxima;
280 void remez_minimax<T>::reset(
290 control_points = vector_type(oN + oD + (pin ? 1 : 2));
291 solution = control_points;
292 zeros = vector_type(oN + oD + (pin ? 2 : 3));
293 maxima = control_points;
302 unknowns = orderN + orderD + (pinned ? 1 : 2);
303 // guess our initial control points:
304 control_points[0] = min;
305 control_points[unknowns - 1] = max;
306 T interval = (max - min) / (unknowns - 1);
307 T spot = min + interval;
308 for(unsigned i = 1; i < control_points.size(); ++i)
310 control_points[i] = spot;
313 solution[unknowns - 1] = 0;
317 // don't bother about more than float precision:
318 m_precision = (std::min)(24, (boost::math::policies::digits<T, boost::math::policies::policy<> >() / 2) - 2);
322 // can't be more accurate than half the bits of T:
323 m_precision = (std::min)(bits, (boost::math::policies::digits<T, boost::math::policies::policy<> >() / 2) - 2);
325 m_max_change_history[0] = m_max_change_history[1] = 1;
327 // do one iteration whatever:
332 inline remez_minimax<T>::remez_minimax(
333 typename remez_minimax<T>::function_type f,
345 reset(oN, oD, a, b, pin, rel_err, sk, bits);
349 void remez_minimax<T>::reset(
358 const vector_type& points)
360 control_points = vector_type(oN + oD + (pin ? 1 : 2));
361 solution = control_points;
362 zeros = vector_type(oN + oD + (pin ? 2 : 3));
363 maxima = control_points;
372 unknowns = orderN + orderD + (pinned ? 1 : 2);
373 control_points = points;
374 solution[unknowns - 1] = 0;
378 // don't bother about more than float precision:
379 m_precision = (std::min)(24, (boost::math::policies::digits<T, boost::math::policies::policy<> >() / 2) - 2);
383 // can't be more accurate than half the bits of T:
384 m_precision = (std::min)(bits, (boost::math::policies::digits<T, boost::math::policies::policy<> >() / 2) - 2);
386 m_max_change_history[0] = m_max_change_history[1] = 1;
387 // do one iteration whatever:
392 inline remez_minimax<T>::remez_minimax(
393 typename remez_minimax<T>::function_type f,
402 const vector_type& points)
406 reset(oN, oD, a, b, pin, rel_err, sk, bits, points);
410 T remez_minimax<T>::iterate()
413 matrix_type A(unknowns, unknowns);
414 vector_type b(unknowns);
416 // fill in evaluation of f(x) at each of the control points:
417 for(unsigned i = 0; i < b.size(); ++i)
419 // take care that none of our control points are at the origin:
420 if(pinned && (control_points[i] == 0))
423 control_points[i] = control_points[i-1] / 3;
425 control_points[i] = control_points[i+1] / 3;
427 b[i] = func(control_points[i]);
431 unsigned convergence_count = 0;
433 // fill in powers of x evaluated at each of the control points:
435 unsigned offsetN = pinned ? 0 : 1;
436 unsigned offsetD = offsetN + orderN;
437 unsigned maxorder = (std::max)(orderN, orderD);
438 T Elast = solution[unknowns - 1];
440 for(unsigned i = 0; i < b.size(); ++i)
442 T x0 = control_points[i];
446 for(unsigned j = 0; j < maxorder; ++j)
449 A(i, j + offsetN) = x;
452 T mult = rel_error ? T(b[i] - sign * fabs(b[i]) * Elast): T(b[i] - sign * Elast);
453 A(i, j + offsetD) = -x * mult;
457 // The last variable to be solved for is the error term,
458 // sign changes with each control point:
459 T E = rel_error ? T(sign * fabs(b[i])) : T(sign);
460 A(i, unknowns - 1) = E;
464 #ifdef BOOST_MATH_INSTRUMENT
465 for(unsigned i = 0; i < b.size(); ++i)
466 std::cout << b[i] << " ";
468 for(unsigned i = 0; i < b.size(); ++i)
470 for(unsigned j = 0; j < b.size(); ++ j)
471 std::cout << A(i, j) << " ";
474 std::cout << std::endl;
477 // Now go ahead and solve the expression to get our solution:
479 solution = boost::math::tools::solve(A, b);
481 err_err = (Elast != 0) ? T(fabs((fabs(solution[unknowns-1]) - fabs(Elast)) / fabs(Elast))) : T(1);
482 }while(orderD && (convergence_count++ < 80) && (err_err > 0.001));
485 // Perform a sanity check to verify that the solution to the equations
486 // is not so much in error as to be useless. The matrix inversion can
487 // be very close to singular, so this can be a real problem.
489 vector_type sanity = prod(A, solution);
490 for(unsigned i = 0; i < b.size(); ++i)
492 T err = fabs((b[i] - sanity[i]) / fabs(b[i]));
493 if(err > sqrt(epsilon<T>()))
495 std::cerr << "Sanity check failed: more than half the digits in the found solution are in error." << std::endl;
500 // Next comes another sanity check, we want to verify that all the control
501 // points do actually alternate in sign, in practice we may have
502 // additional roots in the error function that cause this to fail.
503 // Failure here is always fatal: even though this code attempts to correct
504 // the problem it usually only postpones the inevitable.
506 polynomial<T> num, denom;
507 num = this->numerator();
508 denom = this->denominator();
509 T e1 = b[0] - num.evaluate(control_points[0]) / denom.evaluate(control_points[0]);
510 #ifdef BOOST_MATH_INSTRUMENT
513 for(unsigned i = 1; i < b.size(); ++i)
515 T e2 = b[i] - num.evaluate(control_points[i]) / denom.evaluate(control_points[i]);
516 #ifdef BOOST_MATH_INSTRUMENT
517 std::cout << " " << e2;
521 std::cerr << std::flush << "Basic sanity check failed: Error term does not alternate in sign, non-recoverable error may follow..." << std::endl;
522 T perturbation = 0.05;
524 T point = control_points[i] * (1 - perturbation) + control_points[i-1] * perturbation;
525 e2 = func(point) - num.evaluate(point) / denom.evaluate(point);
528 control_points[i] = point;
531 perturbation += 0.05;
532 }while(perturbation < 0.8);
534 if((e2 * e1 > 0) && (i + 1 < b.size()))
538 T point = control_points[i] * (1 - perturbation) + control_points[i+1] * perturbation;
539 e2 = func(point) - num.evaluate(point) / denom.evaluate(point);
542 control_points[i] = point;
545 perturbation += 0.05;
546 }while(perturbation < 0.8);
553 #ifdef BOOST_MATH_INSTRUMENT
554 for(unsigned i = 0; i < solution.size(); ++i)
555 std::cout << solution[i] << " ";
556 std::cout << std::endl << this->numerator() << std::endl;
557 std::cout << this->denominator() << std::endl;
558 std::cout << std::endl;
562 // The next step is to find all the intervals in which our maxima
565 detail::remez_error_function<T> Err(func, this->numerator(), this->denominator(), rel_error);
567 zeros[unknowns] = max;
568 for(unsigned i = 1; i < control_points.size(); ++i)
570 eps_tolerance<T> tol(m_precision);
571 boost::uintmax_t max_iter = 1000;
572 std::pair<T, T> p = toms748_solve(
578 zeros[i] = (p.first + p.second) / 2;
579 //zeros[i] = bisect(Err, control_points[i-1], control_points[i], m_precision);
582 // Now find all the extrema of the error function:
584 detail::remez_max_error_function<T> Ex(Err);
586 //int max_err_location = 0;
587 for(unsigned i = 0; i < unknowns; ++i)
589 std::pair<T, T> r = brent_find_minima(Ex, zeros[i], zeros[i+1], m_precision);
591 T rel_err = fabs(r.second);
592 if(rel_err > m_max_error)
594 m_max_error = fabs(r.second);
595 //max_err_location = i;
599 // Almost done now! we just need to set our control points
600 // to the extrema, and calculate how much each point has changed
601 // (this will be our termination condition):
603 swap(control_points, maxima);
605 //int max_change_location = 0;
606 for(unsigned i = 0; i < unknowns; ++i)
608 control_points[i] = (control_points[i] * (100 - m_brake) + maxima[i] * m_brake) / 100;
609 T change = fabs((control_points[i] - maxima[i]) / control_points[i]);
611 if(change > m_max_change_history[1])
613 // divergence!!! try capping the change:
614 std::cerr << "Possible divergent step, change will be capped!!" << std::endl;
615 change = m_max_change_history[1];
616 if(control_points[i] < maxima[i])
617 control_points[i] = maxima[i] - change * maxima[i];
619 control_points[i] = maxima[i] + change * maxima[i];
622 if(change > m_max_change)
624 m_max_change = change;
625 //max_change_location = i;
629 // store max change information:
631 m_max_change_history[0] = m_max_change_history[1];
632 m_max_change_history[1] = fabs(m_max_change);
638 polynomial<T> remez_minimax<T>::numerator()const
640 boost::scoped_array<T> a(new T[orderN + 1]);
643 unsigned terms = pinned ? orderN : orderN + 1;
644 for(unsigned i = 0; i < terms; ++i)
645 a[pinned ? i+1 : i] = solution[i];
646 return boost::math::tools::polynomial<T>(&a[0], orderN);
650 polynomial<T> remez_minimax<T>::denominator()const
652 unsigned terms = orderD + 1;
653 unsigned offsetD = pinned ? orderN : (orderN + 1);
654 boost::scoped_array<T> a(new T[terms]);
656 for(unsigned i = 0; i < orderD; ++i)
657 a[i+1] = solution[i + offsetD];
658 return boost::math::tools::polynomial<T>(&a[0], orderD);
664 #endif // BOOST_MATH_TOOLS_REMEZ_HPP