00001 /* LIBGUL - Geometry Utility Library 00002 * Copyright (C) 1998-1999 Norbert Irmer 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this library; if not, write to the 00016 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 * Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #ifndef GUMA_NEWTON_H 00021 #define GUMA_NEWTON_H 00022 00023 namespace guma { 00024 00025 using gul::Ptr; 00026 00027 /* 00028 #define ALF 1.0e-4 00029 #define TOLX 1.0e-7 00030 #define MAXITS 200 00031 #define TOLF 1.0e-4 00032 #define TOLMIN 1.0e-6 00033 #define STPMAX 100.0 00034 */ 00035 00036 00037 template< class T > 00038 class newton_info : public gul::pool_object 00039 { 00040 public: 00041 int dim; 00042 T alf; // average rate of decrease 00043 T tolx; // convergence in domain 00044 00045 gul::Ptr<T> fvec; 00046 gul::Ptr< gul::Ptr<T> > fjac; 00047 00048 newton_info( int in_dim, T in_alf, T in_tolx ) 00049 { 00050 int i; 00051 00052 dim = in_dim; 00053 alf = in_alf; 00054 tolx = in_tolx; 00055 00056 fvec.reserve_pool(dim); 00057 fjac.reserve_pool(dim); 00058 for( i = 0; i < dim; i++ ) 00059 fjac[i].reserve_pool(dim); 00060 } 00061 virtual ~newton_info() { } 00062 00063 /*---------------------------------------------------------------------- 00064 user provided, has to calculate the function value and Jacobian, and 00065 store it in fvec and fjac. It has to return, if convergence is 00066 achieved, too. 00067 -----------------------------------------------------------------------*/ 00068 virtual bool evaluate( const gul::Ptr<T>& dom ) = 0; 00069 00070 /*------------------------------------ 00071 calculates F, and returns 1/2*F*F 00072 ------------------------------------*/ 00073 bool calcf( const gul::Ptr<T>& dom, T *f ) 00074 { 00075 T sum; 00076 int i; 00077 bool found; 00078 00079 found = evaluate(dom); 00080 00081 for( sum = (T)0, i = 0; i < dim; i++ ) 00082 sum += fvec[i] * fvec[i]; 00083 00084 *f = (T)0.5 * sum; 00085 00086 return found; 00087 } 00088 }; 00089 00090 /*------------------------------------------------------------------------ 00091 Searches a root of a function, with n-dimensional domain and 00092 n-dimensional function values. (see "Numerical Recipes in C") 00093 in: 00094 domain: [x1[0],x2[0]] * ... * [x1[n-1],x2[n-1]] 00095 start value: (x[0],...,x[n-1]) 00096 out: 00097 root: (x[0],...,x[n-1]) 00098 -----------------------------------------------------------------------*/ 00099 template< class T > 00100 bool Newton( 00101 Ptr<T>& x, 00102 const Ptr<T>& x1, 00103 const Ptr<T>& x2, 00104 newton_info<T>& I, 00105 int maxits ); 00106 00107 } 00108 00109 #endif