00001 /* 00002 * HippoPlot Quadratic2 class implementation 00003 * 00004 * Copyright (C) 2000, 2003 The Board of Trustees of The Leland 00005 * Stanford Junior University. All Rights Reserved. 00006 * 00007 * $Id: Quadratic2.cxx,v 1.2 2004/08/13 00:26:14 jchiang Exp $ 00008 * 00009 */ 00010 00011 #ifdef HAVE_CONFIG_H 00012 #include "config.h" 00013 #else 00014 #ifdef _MSC_VER 00015 #include "msdevstudio/MSconfig.h" 00016 #endif 00017 #endif 00018 00019 #include "Quadratic2.h" 00020 00021 #include "FunctionHelper.h" 00022 00023 #include <cassert> 00024 #include <cmath> 00025 00026 #ifdef ITERATOR_MEMBER_DEFECT 00027 using namespace std; 00028 #else 00029 using std::vector; 00030 #endif 00031 00032 Quadratic2::Quadratic2 ( ) 00033 { 00034 initialize (); 00035 } 00036 00037 Quadratic2::Quadratic2 ( double yscale, double y0, double x0 ) 00038 { 00039 initialize (); 00040 00041 m_parms[0] = yscale; 00042 m_parms[1] = y0; 00043 m_parms[2] = x0; 00044 } 00045 00046 void Quadratic2::initialize () 00047 { 00048 m_name = "yscale*(x - x0)**2 + y0"; 00049 00050 m_parm_names.push_back ( "yscale" ); 00051 m_parm_names.push_back ( "y0" ); 00052 m_parm_names.push_back ( "x0" ); 00053 00054 resize (); 00055 } 00056 00057 FunctionBase * Quadratic2::clone () const 00058 { 00059 return new Quadratic2 ( *this ); 00060 } 00061 00062 double Quadratic2::operator () ( double x ) const 00063 { 00064 return m_parms[0] * ( x - m_parms[2] ) * ( x - m_parms[2] ) + m_parms[1]; 00065 } 00066 00067 /* virtual */ 00068 void 00069 Quadratic2:: 00070 initialParameters ( const FunctionHelper * helper ) 00071 { 00072 double min_x = helper->minCoord (); 00073 double max_x = helper->maxCoord (); 00074 00075 double min_y = helper->minValue (); 00076 // double max_y = helper->maxValue (); 00077 00078 m_parms[0] = 1.; 00079 m_parms[1] = min_y; 00080 m_parms[2] = std::sqrt(std::fabs(min_x*max_x)); 00081 } 00082 00083 double Quadratic2::derivByParm ( int i, double x ) const 00084 { 00085 switch ( i ) { 00086 case 0 : 00087 return (x - m_parms[2]) * (x - m_parms[2]); 00088 break; 00089 00090 case 1 : 00091 return 1.; 00092 break; 00093 00094 case 2 : 00095 return -2.*m_parms[0]*(x - m_parms[2]); 00096 break; 00097 00098 default : 00099 assert (false ); 00100 break; 00101 } 00102 return 0.0; 00103 }