Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · File Structure

modules/Solve.hpp

00001 // Solve.hpp
00002 // 
00003 // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // 2008-06-28 - reformatted to comply with Stellarium's coding
00011 //              style -MNG
00012 
00013 
00014 #ifndef _SOLVE_HPP_
00015 #define _SOLVE_HPP_
00016 
00017 #include <utility>
00018 
00019 // Solve a function using the bisection method.  Returns a pair
00020 // with the solution as the first element and the error as the second.
00021 template<class T, class F> std::pair<T, T> solveBisection(F f,
00022                                                           T lower, T upper,
00023                                                           T err,
00024                                                           int maxIter = 100)
00025 {
00026     T x = 0.0;
00027 
00028     for (int i = 0; i < maxIter; i++)
00029     {
00030         x = (lower + upper) * (T) 0.5;
00031         if (upper - lower < 2 * err)
00032             break;
00033 
00034         T y = f(x);
00035         if (y < 0)
00036             lower = x;
00037         else
00038             upper = x;
00039     }
00040 
00041     return std::make_pair(x, (upper - lower) / 2);
00042 }
00043 
00044 
00045 // Solve using iteration; terminate when error is below err or the maximum
00046 // number of iterations is reached.
00047 template<class T, class F> std::pair<T, T> solveIteration(F f,
00048                                                           T x0,
00049                                                           T err,
00050                                                           int maxIter = 100)
00051 {
00052     T x = 0;
00053     T x2 = x0;
00054 
00055     for (int i = 0; i < maxIter; i++)
00056     {
00057         x = x2;
00058         x2 = f(x);
00059         if (abs(x2 - x) < err)
00060             return std::make_pair(x2, x2 - x);
00061     }
00062 
00063     return std::make_pair(x2, x2 - x);
00064 }
00065 
00066 
00067 // Solve using iteration method and a fixed number of steps.
00068 template<class T, class F> std::pair<T, T> solveIteration_fixed(F f,
00069                                                                  T x0,
00070                                                                  int maxIter)
00071 {
00072     T x = 0;
00073     T x2 = x0;
00074 
00075     for (int i = 0; i < maxIter; i++)
00076     {
00077         x = x2;
00078         x2 = f(x);
00079     }
00080 
00081     return std::make_pair(x2, x2 - x);
00082 }
00083 
00084 #endif // _SOLVE_HPP_
00085 

Generated on Mon Mar 22 09:55:38 2010 for Stellarium by  doxygen 1.5.5