00001 /* 00002 * HippoPlot Linear 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: Linear.cxx,v 1.38 2003/02/23 19:16:44 pfkeb 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 "Linear.h" 00020 00021 #include "FunctionHelper.h" 00022 00023 #include <cassert> 00024 00025 #ifdef ITERATOR_MEMBER_DEFECT 00026 using namespace std; 00027 #else 00028 using std::vector; 00029 #endif 00030 00031 Linear::Linear ( ) 00032 { 00033 initialize (); 00034 } 00035 00036 Linear::Linear ( double intercept, double slope ) 00037 { 00038 initialize (); 00039 00040 m_parms[0] = intercept; 00041 m_parms[1] = slope; 00042 } 00043 00044 void Linear::initialize () 00045 { 00046 m_name = "Linear"; 00047 m_parm_names.push_back ( "Intercept" ); 00048 m_parm_names.push_back ( "Slope" ); 00049 00050 resize (); 00051 } 00052 00053 FunctionBase * Linear::clone () const 00054 { 00055 return new Linear ( *this ); 00056 } 00057 00058 double Linear::operator () ( double x ) const 00059 { 00060 return x * m_parms[1] + m_parms[0]; 00061 } 00062 00063 /* virtual */ 00064 void 00065 Linear:: 00066 initialParameters ( const FunctionHelper * helper ) 00067 { 00068 double min_x = helper->minCoord (); 00069 double max_x = helper->maxCoord (); 00070 00071 double min_y = helper->minValue (); 00072 double max_y = helper->maxValue (); 00073 00074 m_parms[1] = ( max_y - min_y ) / ( max_x - min_x ); 00075 m_parms[0] = max_y - m_parms[1] * max_x; 00076 } 00077 00078 double Linear::derivByParm ( int i, double x ) const 00079 { 00080 switch ( i ) { 00081 case 0 : 00082 return 1.0; 00083 break; 00084 00085 case 1 : 00086 return x; 00087 break; 00088 00089 default: 00090 assert ( false ); 00091 break; 00092 } 00093 return 0.0; 00094 }