00001
00002
00003
00004 #include <iostream>
00005
00006
00007 #include "FitPoint.h"
00008
00009 using namespace std;
00010
00011
00012 Anp::FitPoint::FitPoint()
00013 :fIndex(-1),
00014 fX(0.0),
00015 fY(0.0),
00016 fA(0.0),
00017 fB(0.0),
00018 fC(0.0),
00019 fPass(false),
00020 fDegree(0),
00021 fWeight(-1.0),
00022 fMap()
00023 {
00024 }
00025
00026
00027 Anp::FitPoint::FitPoint(const int index,
00028 const double x,
00029 const double y)
00030 :fIndex(index),
00031 fX(x),
00032 fY(y),
00033 fA(0.0),
00034 fB(0.0),
00035 fC(0.0),
00036 fPass(false),
00037 fDegree(0),
00038 fWeight(1.0),
00039 fMap()
00040 {
00041 }
00042
00043
00044 Anp::FitPoint::~FitPoint()
00045 {
00046 }
00047
00048
00049 void Anp::FitPoint::SetWeight(const double weight)
00050 {
00051 if(weight < 0.0)
00052 {
00053 cerr << "FitPoint::SetWeight - negative global weight" << endl;
00054 return;
00055 }
00056
00057 fWeight = weight;
00058 }
00059
00060
00061 bool Anp::FitPoint::SetWeight(const unsigned int position, const double weight)
00062 {
00063
00064
00065
00066
00067 if(weight < 0.0)
00068 {
00069 cerr << "FitPoint::SetWeight - negative weight at " << position << endl;
00070 return false;
00071 }
00072
00073 if(!fMap.insert(map<unsigned int, double>::value_type(position, weight)).second)
00074 {
00075 cerr << "FitPoint::SetWeight - failed to insert weight at " << position << endl;
00076 return false;
00077 }
00078
00079 return true;
00080 }
00081
00082
00083 void Anp::FitPoint::SetL(const double a, const double b, const bool pass)
00084 {
00085 fA = a;
00086 fB = b;
00087 fPass = pass;
00088 fDegree = 1;
00089 }
00090
00091
00092 void Anp::FitPoint::SetQ(const double a, const double b, const double c, const bool pass)
00093 {
00094 fA = a;
00095 fB = b;
00096 fC = c;
00097 fPass = pass;
00098 fDegree = 2;
00099 }
00100
00101
00102 double Anp::FitPoint::FitY() const
00103 {
00104 return FitY(fX);
00105 }
00106
00107
00108 double Anp::FitPoint::FitY(const double x) const
00109 {
00110 if(!fPass)
00111 {
00112 return fY;
00113 }
00114
00115 if(fDegree == 1)
00116 {
00117 return fA*x + fB;
00118 }
00119 else if(fDegree == 2)
00120 {
00121 return fA*x*x + fB*x + fC;
00122 }
00123
00124 cerr << "FitPoint::FitY - unknown degree: " << fDegree << endl;
00125
00126 return 0.0;
00127 }
00128
00129
00130 double Anp::FitPoint::Residual() const
00131 {
00132 if(!fPass)
00133 {
00134 cerr << "FitPoint::Residual - this point failed local fit" << endl;
00135 return 0.0;
00136 }
00137
00138 if(fDegree == 1)
00139 {
00140 return fY - fA * fX - fB;
00141 }
00142 else if(fDegree == 2)
00143 {
00144 return fY - fA * fX * fX - fB * fX - fC;
00145 }
00146
00147 cerr << "FitPoint::Residual - unknown degree: " << fDegree << endl;
00148
00149 return 0.0;
00150 }
00151
00152
00153 double Anp::FitPoint::Derivative(const double x) const
00154 {
00155 if(!fPass)
00156 {
00157 cerr << "FitPoint::Derivative - this point failed local fit" << endl;
00158 return 0.0;
00159 }
00160
00161 if(fDegree == 1)
00162 {
00163 return fA;
00164 }
00165 else if(fDegree == 2)
00166 {
00167 return 2.0 * fA * x + fB;
00168 }
00169
00170 cerr << "FitPoint::Derivative - unknown degree: " << fDegree << endl;
00171
00172 return 0.0;
00173 }
00174
00175
00176 void Anp::FitPoint::Print(ostream &os) const
00177 {
00178 os << "FitPoint (i, x, y, best y, weight) = ("
00179 << Index() << ", "
00180 << X() << ", "
00181 << Y() << ", "
00182 << FitY() << ", "
00183 << Weight() << ")"
00184 << endl;
00185 }
00186
00187
00188 bool Anp::operator==(const FitPoint &lhs, const FitPoint &rhs)
00189 {
00190 return (lhs.Index() == rhs.Index());
00191 }
00192
00193
00194 bool Anp::operator<(const FitPoint &lhs, const FitPoint &rhs)
00195 {
00196 return (lhs.X() < rhs.X());
00197 }
00198
00199
00200 bool Anp::operator<(const FitPoint &fit, const double value)
00201 {
00202 return (fit.X() < value);
00203 }
00204
00205
00206 bool Anp::operator<(const double value, const FitPoint &fit)
00207 {
00208 return (value < fit.X());
00209 }
00210
00211
00212 bool Anp::operator==(const int index, const FitPoint &fit)
00213 {
00214 return (index == fit.Index());
00215 }
00216
00217
00218 bool Anp::operator==(const FitPoint &fit, const int index)
00219 {
00220 return (index == fit.Index());
00221 }