All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
QLSQ Class Reference

#include <quadlsq.h>

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.

28  { //constructor
29  clear(); //set to zeros
30  }
void clear()
Definition: quadlsq.cpp:34

Member Function Documentation

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

Definition at line 56 of file quadlsq.cpp.

56  {
57  n++; // Count elements.
58  sigx += x; // Update accumulators.
59  sigy += y;
60  sigxx += x * x;
61  sigxy += x * y;
62  sigyy += y * y;
63  sigxxx += static_cast<long double>(x) * x * x;
64  sigxxy += static_cast<long double>(x) * x * y;
65  sigxxxx += static_cast<long double>(x) * x * x * x;
66 }
void QLSQ::clear ( )

Definition at line 34 of file quadlsq.cpp.

34  { // initialize
35  a = 0.0;
36  b = 0.0;
37  c = 0.0;
38  n = 0; // No elements.
39  sigx = 0.0; // Zero accumulators.
40  sigy = 0.0;
41  sigxx = 0.0;
42  sigxy = 0.0;
43  sigyy = 0.0;
44  sigxxx = 0.0;
45  sigxxy = 0.0;
46  sigxxxx = 0.0;
47 }
inT32 QLSQ::count ( )
inline

Definition at line 39 of file quadlsq.h.

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

Definition at line 100 of file quadlsq.cpp.

100  {
101  long double x_variance = static_cast<long double>(sigxx) * n -
102  static_cast<long double>(sigx) * sigx;
103 
104  // Note: for computational efficiency, we do not normalize the variance,
105  // covariance and cube variance here as they are in the same order in both
106  // nominators and denominators. However, we need be careful in value range
107  // check.
108  if (x_variance < kMinVariance * n * n || degree < 1 || n < 2) {
109  // We cannot calculate b reliably so forget a and b, and just work on c.
110  a = b = 0.0;
111  if (n >= 1 && degree >= 0) {
112  c = sigy / n;
113  } else {
114  c = 0.0;
115  }
116  return;
117  }
118  long double top96 = 0.0; // Accurate top.
119  long double bottom96 = 0.0; // Accurate bottom.
120  long double cubevar = sigxxx * n - static_cast<long double>(sigxx) * sigx;
121  long double covariance = static_cast<long double>(sigxy) * n -
122  static_cast<long double>(sigx) * sigy;
123 
124  if (n >= 4 && degree >= 2) {
125  top96 = cubevar * covariance;
126  top96 += x_variance * (static_cast<long double>(sigxx) * sigy - sigxxy * n);
127 
128  bottom96 = cubevar * cubevar;
129  bottom96 -= x_variance *
130  (sigxxxx * n - static_cast<long double>(sigxx) * sigxx);
131  }
132  if (bottom96 >= kMinVariance * n * n * n * n) {
133  // Denominators looking good
134  a = top96 / bottom96;
135  top96 = covariance - cubevar * a;
136  b = top96 / x_variance;
137  } else {
138  // Forget a, and concentrate on b.
139  a = 0.0;
140  b = covariance / x_variance;
141  }
142  c = (sigy - a * sigxx - b * sigx) / n;
143 }
const double kMinVariance
Definition: quadlsq.cpp:26
double QLSQ::get_a ( )
inline

Definition at line 45 of file quadlsq.h.

45  { //get x squard
46  return a;
47  }
double QLSQ::get_b ( )
inline

Definition at line 48 of file quadlsq.h.

48  { //get x squard
49  return b;
50  }
double QLSQ::get_c ( )
inline

Definition at line 51 of file quadlsq.h.

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

Definition at line 75 of file quadlsq.cpp.

75  {
76  if (n <= 0) {
77  tprintf("Can't remove an element from an empty QLSQ accumulator!\n");
78  return;
79  }
80  n--; // Count elements.
81  sigx -= x; // Update accumulators.
82  sigy -= y;
83  sigxx -= x * x;
84  sigxy -= x * y;
85  sigyy -= y * y;
86  sigxxx -= static_cast<long double>(x) * x * x;
87  sigxxy -= static_cast<long double>(x) * x * y;
88  sigxxxx -= static_cast<long double>(x) * x * x * x;
89 }
#define tprintf(...)
Definition: tprintf.h:31

The documentation for this class was generated from the following files: