QGpCoreTools/StatisticalPoint.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2006-09-25
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef STATISTICALPOINT_H
00029 #define STATISTICALPOINT_H
00030 
00031 #include <math.h>
00032 
00033 #include "StatisticalValue.h"
00034 #include "ComplexPointOptions.h"
00035 #include "QGpCoreToolsDLLExport.h"
00036 
00037 namespace QGpCoreTools {
00038 
00039 template <class numberType>
00040 class QGPCORETOOLS_EXPORT StatisticalPoint : public StatisticalValue<numberType>
00041 {
00042 public:
00043   StatisticalPoint() {}
00044   StatisticalPoint(double x, const numberType& y)
00045     : StatisticalValue<numberType>(y), _x(x) {}
00046 
00047   inline void operator=(const StatisticalPoint<numberType>& o);
00048   inline void operator=(const StatisticalValue<numberType>& o);
00049 
00050   inline bool operator<(const StatisticalPoint<numberType>& p) const;
00051   inline bool operator==(const StatisticalPoint<numberType>& p) const;
00052 
00053   void setX(double v) {_x=v;}
00054   double x() const {return _x;}
00055 
00056   inline void interpole(double valX,
00057                         const StatisticalPoint<numberType>& p1,
00058                         const StatisticalPoint<numberType>& p2);
00059 
00060   // I/O functions
00061   bool fromString(const StringSection& str);
00062   QString toString(int precision=6, char format='g') const;
00063 protected:
00064   void xml_writeProperties(XML_WRITEPROPERTIES_ARGS) const;
00065   XMLMember xml_member(XML_MEMBER_ARGS);
00066   bool xml_setProperty(XML_SETPROPERTY_ARGS);
00067 private:
00068   double _x;
00069 };
00070 
00071 class QGPCORETOOLS_EXPORT RealStatisticalPoint : public StatisticalPoint<double>
00072 {
00073 public:
00074   RealStatisticalPoint() {}
00075   RealStatisticalPoint(double x, const double& y) : StatisticalPoint<double>(x, y) {}
00076 
00077   virtual const QString& xml_tagName() const {return xmlRealStatisticalPointTag;}
00078   static const QString xmlRealStatisticalPointTag;
00079 
00080   inline void operator=(const RealStatisticalPoint& o);
00081   inline void operator=(const RealStatisticalValue& o);
00082 
00083   inline void setY(double v, const CurvePointOptions * options=0);
00084   inline double y(const CurvePointOptions * options=0) const;
00085 };
00086 
00087 inline void RealStatisticalPoint::operator=(const RealStatisticalPoint& o)
00088 {
00089   StatisticalPoint<double>::operator=(o);
00090 }
00091 
00092 inline void RealStatisticalPoint::operator=(const RealStatisticalValue& o)
00093 {
00094   StatisticalPoint<double>::operator=(o);
00095 }
00096 
00097 inline void RealStatisticalPoint::setY(double v, const CurvePointOptions * options)
00098 {
00099   Q_UNUSED(options);
00100   setMean(v);
00101 }
00102 
00103 inline double RealStatisticalPoint::y(const CurvePointOptions * options) const
00104 {
00105   Q_UNUSED(options);
00106   return StatisticalPoint<double>::mean();
00107 }
00108 
00109 class QGPCORETOOLS_EXPORT ComplexStatisticalPoint : public StatisticalPoint<Complex>
00110 {
00111 public:
00112   ComplexStatisticalPoint() {}
00113   ComplexStatisticalPoint(double x, const Complex& y) : StatisticalPoint<Complex>(x, y) {}
00114 
00115   virtual const QString& xml_tagName() const {return xmlComplexStatisticalPointTag;}
00116   static const QString xmlComplexStatisticalPointTag;
00117 
00118   inline void operator=(const ComplexStatisticalPoint& o);
00119   inline void operator=(const ComplexStatisticalValue& o);
00120 
00121   void setY(double v, const CurvePointOptions * options);
00122   double y(const CurvePointOptions * options) const;
00123 };
00124 
00125 inline void ComplexStatisticalPoint::operator=(const ComplexStatisticalPoint& o)
00126 {
00127   StatisticalPoint<Complex>::operator=(o);
00128 }
00129 
00130 inline void ComplexStatisticalPoint::operator=(const ComplexStatisticalValue& o)
00131 {
00132   StatisticalPoint<Complex>::operator=(o);
00133 }
00134 
00135 template <class numberType>
00136 inline void StatisticalPoint<numberType>::operator=(const StatisticalPoint<numberType>& o)
00137 {
00138   TRACE;
00139   StatisticalValue<numberType>::operator=(o);
00140   _x=o._x;
00141 }
00142 
00143 template <class numberType>
00144 inline void StatisticalPoint<numberType>::operator=(const StatisticalValue<numberType>& o)
00145 {
00146   TRACE;
00147   StatisticalValue<numberType>::operator=(o);
00148 }
00149 
00150 template <class numberType>
00151 inline bool StatisticalPoint<numberType>::operator<(const StatisticalPoint<numberType>& p) const
00152 {
00153   return _x < p._x || (_x==p._x && StatisticalValue<numberType>::mean()<p.mean());
00154 }
00155 
00156 template <class numberType>
00157 inline bool StatisticalPoint<numberType>::operator==(const StatisticalPoint<numberType>& p) const
00158 {
00159   return _x==p._x && StatisticalValue<numberType>::operator==(p);
00160 }
00161 
00162 template <class numberType>
00163 inline void StatisticalPoint<numberType>::interpole(double valX,
00164                                                     const StatisticalPoint<numberType>& p1,
00165                                                     const StatisticalPoint<numberType>& p2)
00166 {
00167   TRACE;
00168   double factor=(valX-p1.x())/(p2.x()-p1.x());
00169   setX(valX);
00170   this->setMean(p1.mean()+(p2.mean()-p1.mean())*factor);
00171   this->setStddev(p1.stddev()+(p2.stddev()-p1.stddev())*factor);
00172   this->setWeight(p1.weight()+(p2.weight()-p1.weight())*factor);
00173 }
00174 
00175 template <class numberType>
00176 void StatisticalPoint<numberType>::xml_writeProperties(XML_WRITEPROPERTIES_ARGS) const
00177 {
00178   TRACE;
00179   Q_UNUSED(context);
00180   StatisticalPoint<numberType>::writeProperty(s,"x",_x);
00181   StatisticalValue<numberType>::xml_writeProperties(s, context);
00182 }
00183 
00184 template <class numberType>
00185 XMLMember StatisticalPoint<numberType>::xml_member(XML_MEMBER_ARGS)
00186 {
00187   TRACE;
00188   Q_UNUSED(context);
00189   if(tag=="x") return XMLMember(0);
00190   else return StatisticalValue<numberType>::xml_member(tag, attributes, context)+1;
00191 }
00192 
00193 template <class numberType>
00194 bool StatisticalPoint<numberType>::xml_setProperty(XML_SETPROPERTY_ARGS)
00195 {
00196   TRACE;
00197   Q_UNUSED(context);
00198   switch (memberID) {
00199   case 0: _x=content.toDouble(); return true;
00200   default: return StatisticalValue<numberType>::xml_setProperty(memberID-1, tag, attributes, content, context);
00201   }
00202 }
00203 
00204 template <class numberType>
00205 QString StatisticalPoint<numberType>::toString(int precision, char format) const
00206 {
00207   TRACE;
00208   QString tmp;
00209   tmp+=QString::number(_x, format, precision);
00210   tmp+=" ";
00211   tmp+=QString::number(StatisticalValue<numberType>::mean(), format, precision);
00212   tmp+=" ";
00213   tmp+=QString::number(StatisticalValue<numberType>::stddev(), format, precision);
00214   if(StatisticalValue<numberType>::isValid()) {
00215     tmp+=" ";
00216     tmp+=QString::number(StatisticalValue<numberType>::weight(), format, precision);
00217   } else {
00218     tmp+=" 0";
00219   }
00220   return tmp;
00221 }
00222 
00223 template <class numberType>
00224 bool StatisticalPoint<numberType>::fromString(const StringSection& str)
00225 {
00226   TRACE;
00227   const QChar * ptr=0;
00228   StringSection f;
00229   f=str.nextField(ptr);
00230   if(f.isValid()) _x=f.toDouble(); else return false;
00231   numberType tmp;
00232   if(str.nextNumber(tmp, ptr)) {
00233     StatisticalValue<numberType>::setMean(tmp);
00234   } else return false;
00235   f=str.nextField(ptr);
00236   if(f.isValid()) StatisticalValue<numberType>::setStddev(f.toDouble()); else return false;
00237   f=str.nextField(ptr);
00238   if(f.isValid()) {
00239     double w=f.toDouble();
00240     if(w==0.0) {
00241       StatisticalValue<numberType>::setValid(false);
00242       StatisticalValue<numberType>::setWeight(1.0);
00243     } else {
00244       StatisticalValue<numberType>::setValid(true);
00245       StatisticalValue<numberType>::setWeight(w);
00246     }
00247   } else return false;
00248   return true;
00249 }
00250 
00251 } // namespace QGpCoreTools
00252 
00253 #endif // STATISTICALPOINT_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines