tesseract  3.03
QLSQ Class Reference

#include <quadlsq.h>

List of all members.

Public Member Functions

 QLSQ ()
void clear ()
void add (double x, double y)
void remove (double x, double y)
inT32 count ()
void fit (int degree)
double get_a ()
double get_b ()
double get_c ()

Detailed Description

Definition at line 25 of file quadlsq.h.


Constructor & Destructor Documentation

QLSQ::QLSQ ( ) [inline]

Definition at line 28 of file quadlsq.h.

           {  //constructor
      clear();  //set to zeros
    }

Member Function Documentation

void QLSQ::add ( double  x,
double  y 
)

Definition at line 56 of file quadlsq.cpp.

                                 {
  n++;                           // Count elements.
  sigx += x;                     // Update accumulators.
  sigy += y;
  sigxx += x * x;
  sigxy += x * y;
  sigyy += y * y;
  sigxxx += static_cast<long double>(x) * x * x;
  sigxxy += static_cast<long double>(x) * x * y;
  sigxxxx += static_cast<long double>(x) * x * x * x;
}
void QLSQ::clear ( )

Definition at line 34 of file quadlsq.cpp.

                 {  // initialize
  a = 0.0;
  b = 0.0;
  c = 0.0;
  n = 0;                           // No elements.
  sigx = 0.0;                      // Zero accumulators.
  sigy = 0.0;
  sigxx = 0.0;
  sigxy = 0.0;
  sigyy = 0.0;
  sigxxx = 0.0;
  sigxxy = 0.0;
  sigxxxx = 0.0;
}
inT32 QLSQ::count ( ) [inline]

Definition at line 39 of file quadlsq.h.

                  {  //no of elements
      return n;
    }
void QLSQ::fit ( int  degree)

Definition at line 100 of file quadlsq.cpp.

                         {
  long double x_variance = static_cast<long double>(sigxx) * n -
      static_cast<long double>(sigx) * sigx;

  // Note: for computational efficiency, we do not normalize the variance,
  // covariance and cube variance here as they are in the same order in both
  // nominators and denominators. However, we need be careful in value range
  // check.
  if (x_variance < kMinVariance * n * n || degree < 1 || n < 2) {
    // We cannot calculate b reliably so forget a and b, and just work on c.
    a = b = 0.0;
    if (n >= 1 && degree >= 0) {
      c = sigy / n;
    } else {
      c = 0.0;
    }
    return;
  }
  long double top96 = 0.0;       // Accurate top.
  long double bottom96 = 0.0;    // Accurate bottom.
  long double cubevar = sigxxx * n - static_cast<long double>(sigxx) * sigx;
  long double covariance = static_cast<long double>(sigxy) * n -
      static_cast<long double>(sigx) * sigy;

  if (n >= 4 && degree >= 2) {
    top96 = cubevar * covariance;
    top96 += x_variance * (static_cast<long double>(sigxx) * sigy - sigxxy * n);

    bottom96 = cubevar * cubevar;
    bottom96 -= x_variance *
        (sigxxxx * n - static_cast<long double>(sigxx) * sigxx);
  }
  if (bottom96 >= kMinVariance * n * n * n * n) {
    // Denominators looking good
    a = top96 / bottom96;
    top96 = covariance - cubevar * a;
    b = top96 / x_variance;
  } else {
    // Forget a, and concentrate on b.
    a = 0.0;
    b = covariance / x_variance;
  }
  c = (sigy - a * sigxx - b * sigx) / n;
}
double QLSQ::get_a ( ) [inline]

Definition at line 45 of file quadlsq.h.

                   {  //get x squard
      return a;
    }
double QLSQ::get_b ( ) [inline]

Definition at line 48 of file quadlsq.h.

                   {  //get x squard
      return b;
    }
double QLSQ::get_c ( ) [inline]

Definition at line 51 of file quadlsq.h.

                   {  //get x squard
      return c;
    }
void QLSQ::remove ( double  x,
double  y 
)

Definition at line 75 of file quadlsq.cpp.

                                    {
  if (n <= 0) {
    tprintf("Can't remove an element from an empty QLSQ accumulator!\n");
    return;
  }
  n--;                           // Count elements.
  sigx -= x;                     // Update accumulators.
  sigy -= y;
  sigxx -= x * x;
  sigxy -= x * y;
  sigyy -= y * y;
  sigxxx -= static_cast<long double>(x) * x * x;
  sigxxy -= static_cast<long double>(x) * x * y;
  sigxxxx -= static_cast<long double>(x) * x * x * x;
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines