/*************************************************************************** * aon_maximize.h - wrapper around gsl minimize functions * * Begun: Thu Apr 15 00:51:04 2004 * Copyright 2004 Andrew C. O'Neill * Email oneill@phys.columbia.edu ****************************************************************************/ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _AON_MAXIMIZE_H #define _AON_MAXIMIZE_H #include "aon_debug.h" #include "gsl/gsl_min.h" #include "gsl/gsl_errno.h" // NOTE: to implement a minimization, should do a class like this then derive // the maximization case from that. /*========================================================================= * The GSL only provides minimization functions * In order to maximize, we need to invert the sign and minimize * * This is achieved by the somewhat convoluted method of setting * gsl_function.function = &InvertedGSLFunction * Where InvertedGSLFunction takes as it's parameters the function to be * maximized and that functions parameters * It then returns the negative value of that function ===========================================================================*/ struct InvertedGSLFunctionParams { double (* OriginalFunction) (double, void*); void* OriginalParams; }; double InvertedGSLFunction (double Argument, void* AllParams); // we want to know whether we are ready to get the maximum of a function, // and if we already know it, we don't need to calculate it! enum MaximizationStatus { NOT_READY, READY, DONE }; /*============================================================================= class Maximization: Wrapper around the GSL minimization routines which finds the maximum of a function. Usage: double myFunction(double Arg, void* Params) { // blah blah } myParamStruct myParams; myParams.param1 = 0.5; myParams.param2 = 77.3; Maximization myMax; if (myMax.setFunction (&myFunction, &myParams) ) log_error(); cout << "maximum: " << myMax.FindMaximum(25, 0, 50) << endl; =============================================================================*/ class Maximization { public: Maximization(); ~Maximization(); int setFunction(double (*OriginalFunction)(double, void*), void* OriginalParams); double FindMaximum(const double& Guess, const double& Lower, const double& Upper); // FindArgumentOfMaximum () not implemented! void setPrecision(const double& Precision) { itsPrecision = Precision; if (itsStatus == DONE) { itsStatus = READY; } } double getPrecision(void) {return itsPrecision;} protected: // gsl stuff gsl_function itsGSLFunction; gsl_min_fminimizer *itsMinimizer; // this is complicated because we need to pass the original function pointer // and the original parameters to the function InvertedGSLFunction // which will call the original function and return its negative. InvertedGSLFunctionParams *itsInvertedParams; // internal status flag MaximizationStatus itsStatus; // these should be settable by clients, but hasn't been implemented yet int MaxIterations; double itsPrecision; }; // max algo double QuickMax(const double& x, const double& y); #endif /* _AON_MAXIMIZE_H */