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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| #include <math.h>
#ifndef PI
#define PI 3.14159265359
#endif
#define ACCURACY 1.0e-12
/*
***********************************************************
SOLVE LINEAR SYSTEM VIA GAUSSIAN ELIMINATION
( 10.01.1979 K. BANSE )
(SACLAY - HPCCD - 1982/84)
Ported to C
Jean Souviron 2015/12/13
***********************************************************
*/
int LNSYS(double *Array, double *X, int Order )
{
int i, j, k, MaxRow, Length ;
double Save, Rmax, Mid ;
Length = Order+1 ;
for ( k = 0 ; k < (Order-1) ; k++ )
{
/*
--- Get largest pivotal element
*/
Save = 0.0 ;
for ( j = k ; j < Order ; j++ )
{
Rmax = fabs(Array[j*Length+k]);
if( Save < Rmax )
{
Save = Rmax ;
MaxRow = j ;
}
}
/*
--- Exchange rows
*/
for ( j = k ; j < Length ; j++ )
{
Save = Array[k*Length+j] ;
Array[k*Length+j] = Array[MaxRow*Length+j] ;
Array[MaxRow*Length+j]= Save ;
}
/*
--- Test if array is singular
*/
Mid = Array[k*Length+k] ;
if ( fabs(Mid) < ACCURACY )
{
for ( j = 0 ; j < Order ; j++ )
X[j] = 0.0 ;
return 0 ;
}
/*
--- Substract row from lower rows
*/
for ( i = k+1 ; i < Order ; i++ )
{
Save = Array[i*Length+k] / Mid ;
Array[i*Length+k] = 0.0 ;
for ( j = k+1 ; j < Length ; j++ )
Array[i*Length+j] = Array[i*Length+j] - (Save*Array[k*Length+j]) ;
}
}
/*
================
Calculate vector X
================
*/
X[Order-1] = Array[Length*(Order-1)+Order] / Array[Length*(Order-1)+Order-1] ;
for ( k = 1 ; k < Order ; k++ )
{
i = Order + 1 - k ;
Save = Array[i*Length+Order+1] ;
for ( j = i+1 ; j < Order ; j++ )
Save = Save - Array[i*Length+j]*X[j] ;
X[i] = Save / Array[i*Length+i] ;
}
return 1 ;
}
/*
***********************************************************************
GENERALIZED LEAST-SQUARE FOR ANY NUMBER OF PARAMETERS
(Jean SOUVIRON - DAO Victoria - 1985)
(METHOD OF R. SEDGEWICK in ALGORITHMS
ADDISON-WESLEY PUBLISHING CO.
1984)
Modified 2015/12/12 Jean Souviron
Porting to C
Upgrading to take any number of parameters
-----------------------------------------------------------------------
INPUT :
NDims = NUMBER OF DIFFERENT PARAMETERS+1 (for the data to be fitted to)
NPts = TOTAL NUMBER OF POINTS
InputMatrix = ARRAY OF PARAMETERS FOR EACH POINT
(LAST VECTOR HAS TO BE THE DATA TO BE FITTED)
OUTPUT :
OutputCoeffs = ARRAY OF COEFFICIENTS (dimension NDims-1 )
Sigma
RETURN VALUE :
1 if everything is OK
0 if an error occurred (memory or singular array)
Example : if we want to fit an ellipse with a group of points according to
equation x2/a2 + y2/b2 = 1
the input matrix will be N pts long, and 3 columns wide, with for
each point the x2, the y2, and 1.
the output coeffs will be 2 items, 1/a2 and 1/b2
the sigma will be computed by applying these coeffs to the first 2 columns
for each point, and comparing with the result (1).
***********************************************************************
*/
int LeastSquares ( double *InputMatrix, int NDims, int NPts,
double **OutputCoeffs, double *Sigma )
{
double *Coeffs=NULL ;
double T, Z1, Z2 ;
int i,j, k, io = 0, ko, NParams ;
NParams = NDims-1 ;
/*
Allocations
*/
if ( (*OutputCoeffs = (double *)calloc(NParams, sizeof(double))) == NULL )
return 0 ;
if ( (Coeffs = (double *)calloc(NParams*NDims, sizeof(double))) == NULL )
{
free (*OutputCoeffs);
*OutputCoeffs = NULL ;
return 0 ;
}
/*
Initializations
*/
io = 0 ;
for ( i = 0 ; i < NParams ; i++ )
{
(*OutputCoeffs)[i] = 0.0 ;
for ( j = 0 ; j < NDims ; j++ )
{
T = 0.0 ;
for ( k=0 ; k < NPts ; k++ )
{
ko = k * NDims ;
Z1 = InputMatrix[ko+i] ;
Z2 = InputMatrix[ko+j] ;
T = T + (Z1*Z2) ;
}
Coeffs[io+j]= T ;
}
io = io + NDims ;
}
/*
-----------------
Solving by gaussian elimination
-----------------
*/
if ( LNSYS(Coeffs, *OutputCoeffs, NParams) != 1 )
{
free ( Coeffs );
free ( *OutputCoeffs ) ;
*OutputCoeffs = NULL ;
return 0 ;
}
free ( Coeffs );
/*
-----------------
Computes sigma
-----------------
*/
*Sigma = 0.0 ;
for ( i = 0 ; i < NPts ; i++ )
{
Z1 = InputMatrix[i*NDims+NParams]; /* The value to be compared with */
for ( j = 0 ; j < NParams ; j++ )
{
Z1= Z1 - ((*OutputCoeffs)[j]*InputMatrix[i*NDims+j]) ;
}
if( Z1 < 0.0 )
Z1 = -Z1;
*Sigma = *Sigma + Z1 ;
}
*Sigma = (*Sigma/(double)NPts)*sqrt(PI/2.0) ;
return 1 ;
} |