-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRSGISLinearFit.cpp
154 lines (122 loc) · 4.26 KB
/
RSGISLinearFit.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* RSGISLinearFit.cpp
* RSGIS_LIB
*
* Created by Daniel Clewley on 17/05/2010.
* Copyright 2010 RSGISLib. All rights reserved.
* This file is part of RSGISLib.
*
* RSGISLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RSGISLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RSGISLib. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "RSGISLinearFit.h"
namespace rsgis {namespace math{
RSGISLinearFit::RSGISLinearFit()
{
}
double RSGISLinearFit::linearFit(gsl_matrix *a, gsl_vector *x, gsl_vector *b)
{
RSGISVectors vectorUtils;
// Perform Least Squared Fit
gsl_multifit_linear_workspace *workspace;
workspace = gsl_multifit_linear_alloc(a->size1, a->size2);
gsl_matrix *cov;
double chisq;
cov = gsl_matrix_alloc(a->size2, a->size2);
gsl_multifit_linear(a, b, x, cov, &chisq, workspace);
// Clean up
gsl_multifit_linear_free(workspace);
gsl_matrix_free(cov);
return chisq;
}
double RSGISLinearFit::linearFitPrint(gsl_matrix *a, gsl_vector *x, gsl_vector *b)
{
RSGISVectors vectorUtils;
RSGISMatrices matrixUtils;
// Perform Least Squared Fit
gsl_multifit_linear_workspace *workspace;
workspace = gsl_multifit_linear_alloc(a->size1, a->size2);
gsl_matrix *cov;
double chisq;
cov = gsl_matrix_alloc(a->size2, a->size2);
gsl_multifit_linear(a, b, x, cov, &chisq, workspace);
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << "coefficients are : ";
vectorUtils.printGSLVector(x);
std::cout << " chisq = " << chisq << std::endl;
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << std::endl;
// Clean up
gsl_multifit_linear_free(workspace);
gsl_matrix_free(cov);
return chisq;
}
double RSGISLinearFit::linearFitW(gsl_matrix *a, gsl_vector *x, gsl_vector *b, gsl_vector *var)
{
RSGISVectors vectorUtils;
// Invert variance to get weights
gsl_vector *weights;
weights = gsl_vector_alloc(var->size);
for(unsigned int i = 0; i < var->size; i++)
{
double weightElement = 1 / gsl_vector_get(var, i);
gsl_vector_set(weights, i, weightElement);
}
// Perform Least Squared Fit
gsl_multifit_linear_workspace *workspace;
workspace = gsl_multifit_linear_alloc(a->size1, a->size2);
gsl_matrix *cov;
double chisq;
cov = gsl_matrix_alloc(a->size2, a->size2);
gsl_multifit_wlinear(a, weights, b, x, cov, &chisq, workspace);
// Clean up
gsl_multifit_linear_free(workspace);
gsl_matrix_free(cov);
gsl_vector_free(weights);
return chisq;
}
double RSGISLinearFit::linearFitWPrint(gsl_matrix *a, gsl_vector *x, gsl_vector *b, gsl_vector *var)
{
RSGISVectors vectorUtils;
// Invert variance to get weights
gsl_vector *weights;
weights = gsl_vector_alloc(var->size);
for(unsigned int i = 0; i < var->size; i++)
{
double weightElement = 1 / gsl_vector_get(var, i);
gsl_vector_set(weights, i, weightElement);
}
// Perform Least Squared Fit
gsl_multifit_linear_workspace *workspace;
workspace = gsl_multifit_linear_alloc(a->size1, a->size2);
gsl_matrix *cov;
double chisq;
cov = gsl_matrix_alloc(a->size2, a->size2);
gsl_multifit_wlinear(a, weights, b, x, cov, &chisq, workspace);
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << "coefficients are : ";
vectorUtils.printGSLVector(x);
std::cout << " chisq = " << chisq << std::endl;
std::cout << "----------------------------------------------------------------------------" << std::endl;
std::cout << std::endl;
// Clean up
gsl_multifit_linear_free(workspace);
gsl_matrix_free(cov);
gsl_vector_free(weights);
return chisq;
}
RSGISLinearFit::~RSGISLinearFit()
{
}
}}