Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_MATH_OPTIMIZATION_RELATIVE_CONVERGENCE_H
00002 #define OPENTISSUE_CORE_MATH_OPTIMIZATION_RELATIVE_CONVERGENCE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_value_traits.h>
00013 #include <OpenTissue/core/math/math_is_number.h>
00014
00015 #include <cmath>
00016
00017 namespace OpenTissue
00018 {
00019 namespace math
00020 {
00021 namespace optimization
00022 {
00023
00033 template < typename T >
00034 inline bool relative_convergence(
00035 T const & f_old
00036 , T const & f
00037 , T const & tolerance
00038 )
00039 {
00040 using std::fabs;
00041
00042 typedef OpenTissue::math::ValueTraits<T> value_traits;
00043
00044 assert( tolerance >= value_traits::zero() || !"relative_convergence(): tolerance must be non-negative");
00045 assert( is_number( tolerance ) || !"relative_convergence(): internal error, NAN is encountered?");
00046 assert( is_number( f_old ) || !"relative_convergence(): internal error, NAN is encountered?");
00047 assert( is_number( f ) || !"relative_convergence(): internal error, NAN is encountered?");
00048
00049
00050 if(f_old == value_traits::zero())
00051 {
00052
00053 if(f == value_traits::zero())
00054 return true;
00055
00056 return false;
00057 }
00058
00059 T const relative_test = fabs(f - f_old) / fabs(f_old);
00060
00061 assert( is_number( relative_test ) || !"relative_convergence(): internal error, NAN is encountered?");
00062
00063 if(relative_test <= tolerance)
00064 return true;
00065
00066 return false;
00067 }
00068
00069 }
00070 }
00071 }
00072
00073
00074 #endif