Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_MATH_OPTIMIZATION_STAGNATION_H
00002 #define OPENTISSUE_CORE_MATH_OPTIMIZATION_STAGNATION_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 #include <OpenTissue/core/math/big/big_types.h>
00015
00016 #include <cmath>
00017
00018 namespace OpenTissue
00019 {
00020 namespace math
00021 {
00022 namespace optimization
00023 {
00024
00035 template < typename T >
00036 inline bool stagnation(
00037 ublas::vector<T> const & x_old
00038 , ublas::vector<T> const & x
00039 , T const & tolerance
00040 )
00041 {
00042 using std::fabs;
00043 using std::max;
00044
00045
00046 typedef OpenTissue::math::ValueTraits<T> value_traits;
00047
00048 assert( tolerance >= value_traits::zero() || !"stagnation(): tolerance must be non-negative");
00049 assert( is_number( tolerance ) || !"stagnation(): internal error, NAN is encountered?");
00050
00051 size_t const m = x.size();
00052
00053 assert( m>0 || !"stagnation(): zero size of x");
00054 assert( x.size() == x_old.size() || !"stagnation(): incompatible dimensions of x and x_old");
00055
00056 T max_dx = value_traits::zero();
00057
00058 for (size_t i = 0; i < m; ++ i)
00059 max_dx = max( max_dx, fabs( x(i) - x_old(i) ) );
00060
00061 assert( is_number( max_dx ) || !"stagnation(): internal error, NAN is encountered?");
00062
00063 if(max_dx <= tolerance)
00064 return true;
00065 return false;
00066 }
00067
00068 }
00069 }
00070 }
00071
00072
00073 #endif