File: flames_lsfit.c

package info (click to toggle)
cpl-plugin-uves 6.1.3+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 23,128 kB
  • sloc: ansic: 171,056; sh: 4,359; python: 3,002; makefile: 1,322
file content (394 lines) | stat: -rw-r--r-- 11,868 bytes parent folder | download | duplicates (3)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * This file is part of the ESO Common Pipeline Library
 * Copyright (C) 2001-2008,2014 European Southern Observatory
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @defgroup cpl_matrix Matrices
 *
 * This module provides functions to create, destroy and use a @em cpl_matrix.
 * The elements of a @em cpl_matrix with M rows and N columns are counted 
 * from 0,0 to M-1,N-1. The matrix element 0,0 is the one at the upper left
 * corner of a matrix. The CPL matrix functions work properly only in the 
 * case the matrices elements do not contain garbage (such as @c NaN or 
 * infinity).
 *
 * @par Synopsis:
 * @code
 *   #include <flames_lsfit.h>
 * @endcode
 */

#include <cpl.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <flames_lsfit.h>
#include <flames_gauss_jordan.h>
#include <flames_covariance_reorder.h>
#include <flames_newmatrix.h>
#include <uves_dfs.h>
/**@{*/

cpl_matrix * cpl_matrix_product_normal_create(const cpl_matrix * self);
cpl_error_code cpl_matrix_product_transpose(cpl_matrix * self,
                                            const cpl_matrix * ma,
                                            const cpl_matrix * mb);


/*-----------------------------------------------------------------------------
                        Private function prototypes
 -----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
                              Function codes
 -----------------------------------------------------------------------------*/

/* ---------------------------------------------------------------------------*/
/**
 * @brief generic 1d vandermonde matrix
 *
 * @param sample  sampling positions
 * @param degree  degree of polynomial
 * @param func    function evaluating polynomials from [0, degree] at
 *                sampling point
 * @param offset  offset of func (1 for indexing starting with 1)
 * @return matrix containing the vandermonde matrix
 */
/* ---------------------------------------------------------------------------*/
cpl_matrix * vander1d(
                const cpl_vector * sample,
                cpl_size           degree,
                void (*func)(double, double *, int),
                size_t offset)
{
    size_t i;
    const size_t nr = cpl_vector_get_size(sample);
    const size_t nc = degree + 1;
    cpl_matrix * V = cpl_matrix_new(nr, nc);
    double * v = cpl_matrix_get_data(V);
    const double * d = cpl_vector_get_data_const(sample);
    for (i = 0; i < nr; i++) {
        if (offset != 0) {
            double tmp[nc + offset];
            func(d[i], tmp, nc);
            memcpy(&v[i * nc], &tmp[offset], nc * sizeof(*v));
        }
        else {
            func(d[i], &v[i * nc], nc);
        }
    }

    return V;
}

cpl_matrix * vander2d(
                const cpl_vector * sample_x,
                const cpl_vector * sample_y,
                cpl_size           degree,
                void (*func)(double, double, double *, int),
                size_t offset)
{
    size_t i;
    const size_t nr = cpl_vector_get_size(sample_x);
    const size_t nc = degree + 1;
    cpl_matrix * V = cpl_matrix_new(nr, nc);
    double * v = cpl_matrix_get_data(V);
    const double * dx = cpl_vector_get_data_const(sample_x);
    const double * dy = cpl_vector_get_data_const(sample_y);
    assert(cpl_vector_get_size(sample_y) == nr);
    for (i = 0; i < nr; i++) {
        if (offset != 0) {
            double tmp[nc + offset];
            func(dx[i], dy[i], tmp, nc);
            memcpy(&v[i * nc], &tmp[offset], nc * sizeof(*v));
        }
        else {
            func(dx[i], dy[i], &v[i * nc], nc);
        }
    }

    return V;
}

static void flames_polynomial(double x, double * p, int ncoefs)
{
    int i;
    p[0] = 1.;
    for (i = 1; i < ncoefs; i++) {
        p[i] = pow(x, i);
    }
}

cpl_matrix * polyvander1d(
                const cpl_vector * sample,
                cpl_size           degree)
{
    return vander1d(sample, degree, &flames_polynomial, 0);
}

void lsqfit(
                const cpl_matrix * design,
                const cpl_vector * values,
                const cpl_vector * errors,
                cpl_matrix **coef)
{
    /* weight response and design */
    cpl_vector * vrhs = cpl_vector_duplicate(errors);
    cpl_vector_power(vrhs, -1);
    cpl_matrix * wdesign = cpl_matrix_duplicate(design);
    long i, j;
    for (i = 0; i < cpl_vector_get_size(errors); i++) {
        double w = cpl_vector_get(vrhs,i);
        for (j = 0; j < cpl_matrix_get_ncol(wdesign); j++) {
            cpl_matrix_set(wdesign, i, j,
                            cpl_matrix_get(wdesign, i, j) * w);
        }
    }

    cpl_vector_multiply(vrhs, values);
    cpl_matrix * rhs = cpl_matrix_wrap(cpl_vector_get_size(vrhs), 1,
                    cpl_vector_get_data(vrhs));

    /* solve Ax = b */
    /* cpl_matrix_solve_normal(design, rhs) + covariance */
    {
        cpl_matrix * At  = cpl_matrix_transpose_create(wdesign);
        cpl_matrix * AtA = cpl_matrix_product_normal_create(At);

        /* RRt = AtA */
        cpl_matrix_decomp_chol(AtA);
        /* solve for pseudo inverse: (RRt)P=At*/
        cpl_matrix_solve_chol(AtA, At);
        /* compute solution to system Ax=b -> x=Pb */
        *coef = cpl_matrix_product_create(At, rhs);
        /* compute covariance matrix cov(b) = PPt */
        //cpl_matrix * cov = cpl_matrix_new(cpl_matrix_get_ncol(At),
        //                        cpl_matrix_get_ncol(At));
        //cpl_matrix_product_transpose(cov, At, At);

        cpl_matrix_delete(At);
        cpl_matrix_delete(AtA);
    }

    cpl_matrix_unwrap(rhs);
    cpl_vector_delete(vrhs);
    cpl_matrix_delete(wdesign);

    return;
}

void lsqfit_nr(
                double x[], double y[], double sig[], int32_t ndat, double a[],
                int ma,
                void (*funcs)(double, double [], int))
{
    int i;
    cpl_vector * vX = cpl_vector_wrap(ndat, &x[1]);
    cpl_vector * vY = cpl_vector_wrap(ndat, &y[1]);
    cpl_vector * vS;
    cpl_matrix * design = vander1d(vX, ma -1, funcs, 1);
    cpl_matrix * mcoef;

    if (sig) {
        vS = cpl_vector_wrap(ndat, &sig[1]);
    }
    else {
        vS = cpl_vector_new(ndat);
        for (i= 0; i < ndat; i++)
            cpl_vector_set(vS, i, 1.);
    }
    lsqfit(design, vY, vS, &mcoef);

    for (i = 1; i <= ma; i++) {
        a[i] = cpl_matrix_get(mcoef, i -1, 0);
    }
    cpl_vector_unwrap(vX);
    cpl_vector_unwrap(vY);
    if (sig) {
        cpl_vector_unwrap(vS);
    }
    else {
        cpl_vector_delete(vS);
    }
    cpl_matrix_delete(design);
    cpl_matrix_delete(mcoef);
}


void lsqfit2d_nr(
                double x[], double y[], double z[], double sig[], int ndat, double a[],
                int ma,
                void (*funcs)(double, double, double [], int))
{
    int i;
    cpl_vector * vX = cpl_vector_wrap(ndat, &x[1]);
    cpl_vector * vY = cpl_vector_wrap(ndat, &y[1]);
    cpl_vector * vZ = cpl_vector_wrap(ndat, &z[1]);
    cpl_vector * vS;
    cpl_matrix * design = vander2d(vX, vY, ma - 1, funcs, 1);
    cpl_matrix * mcoef;

    if (sig) {
        vS = cpl_vector_wrap(ndat, &sig[1]);
    }
    else {
        vS = cpl_vector_new(ndat);
        for (i= 0; i < ndat; i++)
            cpl_vector_set(vS, i, 1.);
    }
    lsqfit(design, vZ, vS, &mcoef);

    for (i = 1; i <= ma; i++) {
        a[i] = cpl_matrix_get(mcoef, i - 1, 0);
    }
    cpl_vector_unwrap(vX);
    cpl_vector_unwrap(vY);
    cpl_vector_unwrap(vZ);
    if (sig) {
        cpl_vector_unwrap(vS);
    }
    else {
        cpl_vector_delete(vS);
    }
    cpl_matrix_delete(design);
    cpl_matrix_delete(mcoef);
}


cpl_vector * eval_poly(
                cpl_matrix * design,
                cpl_matrix * coef)
{
    cpl_matrix * fvalues = cpl_matrix_product_create(design, coef);
    cpl_vector * res = cpl_vector_wrap(cpl_matrix_get_nrow(fvalues),
                    cpl_matrix_get_data(fvalues));
    cpl_matrix_unwrap(fvalues);
    return res;
}


/* fill upper-left matrix elements as mirror of lower-right elements
 * below matrix diagonal
 */
static void
flames_matrix_mirror_lu(double** mat,const int nraws)
{
    int j,k;
    for (j=2;j<=nraws;j++)
        for (k=1;k<j;k++)
            mat[k][j]=mat[j][k];

    return;
}


/* compute chi2 */
static double
flames_compute_chi2(double* data, double* value, double* errs, const int ndat,
                    double* par, const int npar,double* afunc,
                    void (*model)(double, double [], int)) {
    double chi2=0,sum=0;
    int i,j;
    for (i=1;i<=ndat;i++) {

        (*model)(data[i],afunc,npar);
        for (sum=0.0,j=1;j<=npar;j++) sum += par[j]*afunc[j];
        double scatter = (value[i]-sum)/errs[i];
        chi2 += scatter*scatter;
    }

    return chi2;

}

/* build design matrix: pointer to be allocated */
static void
flames_matrix_design(double* data,double* value,double* ps,const int ndat,
                     double* par,int* par_sw, const int npar,const int nfit,
                     double** covar,double** mat, double* afunc,
                     void (*model)(double, double [], int))
{
    int j,k,l,m;

    for (int i=1;i<=ndat;i++) {
        (*model)(data[i],afunc,npar);
        double value_model=value[i];

        if (nfit < npar) {
            for (j=1;j<=npar;j++)
                if (!par_sw[j]) value_model -= par[j]*afunc[j];
        }
        double sig2i=1.0/(ps[i]*ps[i]);
        for (j=0,l=1;l<=npar;l++) {
            if (par_sw[l]) {
                double weight=afunc[l]*sig2i;
                for (j++,k=0,m=1;m<=l;m++)
                    if (par_sw[m]) covar[j][++k] += weight*afunc[m];
                mat[j][1] += value_model*weight;
            }
        }
    }

    return;
}

void flames_lfit(cpl_vector* vx, cpl_vector* vy, cpl_vector* vs, int32_t ndat,
                 double a[],
                 int ia[], int ma, double **covar, double *chisq,
                 void (*funcs)(double, double [], int))
{
    int j=0;
    int l=0;
    int mfit=0;
    double **beta=0;
    double *afunc=0;

    double *px = cpl_vector_get_data(vx);
    double *py = cpl_vector_get_data(vy);
    double *ps = cpl_vector_get_data(vs);

    beta=dmatrix(1,ma,1,1);
    afunc=dvector(1,ma);
    /* Not needed as calloc also init to 0.
     for (i=1; i<=ma; i++) {
       beta[i][1] = 0;
       afunc[i] = 0;
     }
     */
    for (j=1;j<=ma;j++)
        if (ia[j]) mfit++;
    if (mfit == 0) nrerror("lfit: no parameters to be fitted");
    /* Not needed as calloc also init to 0.
     for (j=1;j<=mfit;j++) {
         for (k=1;k<=mfit;k++) covar[j][k]=0.0;
         beta[j][1]=0.0;
     }
     */
    flames_matrix_design(px,py,ps,ndat,a,ia,ma,mfit,covar,beta,afunc,funcs);
    flames_matrix_mirror_lu(covar,mfit);
    flames_gauss_jordan(covar,mfit,beta,1);

    for (j=0,l=1;l<=ma;l++)
        if (ia[l]) a[l]=beta[++j][1];
    *chisq=flames_compute_chi2(px,py,ps,ndat,a,ma,afunc,funcs);
    flames_covariance_reorder(covar,ma,ia,mfit);
    free_dvector(afunc,1,ma);
    free_dmatrix(beta,1,ma,1,1);
    return;
}