forked from opengazer/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LeastSquares.cpp
46 lines (36 loc) · 929 Bytes
/
LeastSquares.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "LeastSquares.h"
#include <vnl/algo/vnl_cholesky.h>
#include <assert.h>
void LeastSquares::addSample(double xs[], double y) {
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
X[i][j] += xs[i]*xs[j];
for(int i=0; i<n; i++)
Y[i] += xs[i]*y;
}
void LeastSquares::addSample(double x1, double x2, double y) {
assert(n == 2);
double xs[2] = {x1, x2};
addSample(xs, y);
}
void LeastSquares::addSample(double x1, double x2, double x3, double y) {
assert(n == 3);
double xs[3] = {x1, x2, x3};
addSample(xs, y);
}
Vector LeastSquares::solve(void) {
return vnl_cholesky(X).solve(Y);
}
void LeastSquares::solve(double &a0, double &a1) {
assert(n == 2);
Vector vec = solve();
a0 = vec[0];
a1 = vec[1];
}
void LeastSquares::solve(double &a0, double &a1, double &a2) {
assert(n == 3);
Vector vec = solve();
a0 = vec[0];
a1 = vec[1];
a2 = vec[2];
}