File: pcomp.c

package info (click to toggle)
cufflinks 1.3.0-2
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 3,864 kB
  • sloc: cpp: 48,999; ansic: 12,297; sh: 3,381; python: 432; makefile: 209
file content (191 lines) | stat: -rw-r--r-- 4,474 bytes parent folder | download | duplicates (5)
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
/*
 *   Copyright (c) 1996-2001 Lucent Technologies.
 *   See README file for details.
 * 
 *
 * functions for computing and subtracting, adding the
 * parametric component
 */

#include "local.h"

INT noparcomp(lf)
lfit *lf;
{ 
    INT tg;
    if (lf->mi[MDEG0]<lf->mi[MDEG]) return(1);
    if (lf->mi[MUBAS]) return(1);
    tg = lf->mi[MTG] & 63;
    if (tg<=THAZ) return(1);
    if (tg==TROBT) return(1);
    if (tg==TCAUC) return(1);
    return(0);
}

INT hasparcomp(lf)
lfit *lf;
{ 
    return(lf->mi[MPC]);
}

int pc_reqd(d,p)
INT d, p;
{ 
    return(d + 2*p + jac_reqd(p));
}

void pcchk(pc,d,p,lc)
paramcomp *pc;
INT d, p, lc;
{ 
    //INT k;
    double *z;
    pc->wk = checkvarlen(pc->wk,pc_reqd(d,p),"_pcwork",VDOUBLE);
    z = vdptr(pc->wk);
    //k = 0;
    
    pc->xbar = z; z += d;
    pc->coef = z; z += p;
    pc->f    = z; z += p;
    
    z = jac_alloc(&pc->xtwx,p,z);
    pc->xtwx.p = p;
}

void compparcomp(des,lf,nopc)
design *des;
lfit *lf;
INT nopc;
{ 
    INT i, j, k;
    double wt, sw;
    paramcomp *pc;
    pc = &lf->pc;
    pcchk(pc,lf->mi[MDIM],lf->mi[MP],1);
    
    if (pc == NULL)
    {
        fprintf(stderr, "Error: locfit cannot allocate pc working memory\n");
        pcchk(pc,lf->mi[MDIM],lf->mi[MP],1);
        return;
    }
    
    for (i=0; i<lf->mi[MDIM]; i++) pc->xbar[i] = 0.0;
    sw = 0.0;
    for (i=0; i<lf->mi[MN]; i++)
    { 
        wt = prwt(lf,i);
        sw += wt;
        for (j=0; j<lf->mi[MDIM]; j++)
            pc->xbar[j] += datum(lf,j,i)*wt;
        des->ind[i] = i;
        des->w[i] = 1.0;
    }
    for (i=0; i<lf->mi[MDIM]; i++) pc->xbar[i] /= sw;
    if ((nopc) || noparcomp(lf))
    { lf->mi[MPC] = 0;
        return;
    }
    lf->mi[MPC] = 1;
    des->xev = pc->xbar;
    k = locfit(lf,des,0.0,0);

    if (lf_error) return;
    switch(k)
    { case LF_NOPT:
            ERROR(("compparcomp: no points in dataset?"));
            return;
        case LF_INFA:
            ERROR(("compparcomp: infinite parameters in param. component"));
            return;
        case LF_NCON:
            ERROR(("compparcom: not converged"));
            return;
        case LF_OOB:
            ERROR(("compparcomp: parameters out of bounds"));
            return;
        case LF_PF:
            WARN(("compparcomp: perfect fit"));
        case LF_OK:
            for (i=0; i<lf->mi[MP]; i++)
            { pc->coef[i] = des->cf[i];
                pc->xtwx.dg[i] = des->xtwx.dg[i];
                pc->xtwx.wk[i] = des->xtwx.wk[i];
            }
            for (i=0; i<lf->mi[MP]*lf->mi[MP]; i++)
            { pc->xtwx.Z[i] = des->xtwx.Z[i];
                pc->xtwx.Q[i] = des->xtwx.Q[i];
            }
            pc->xtwx.sm = des->xtwx.sm;
            pc->xtwx.st = des->xtwx.st;
            return;
        default:
            ERROR(("compparcomp: locfit unknown return status %d",k));
            return;
    }
}

void subparcomp(des,lf,coef)
design *des;
lfit *lf;
double *coef;
{ INT i, *deriv, nd;
    
    if (!hasparcomp(lf)) return;
    
    deriv = lf->deriv;
    nd = lf->nd;
    fitfun(lf,des->xev,lf->pc.xbar,des->f1,deriv,nd);
    coef[0] -= innerprod(lf->pc.coef,des->f1,lf->mi[MP]);
    if (des->ncoef == 1) return;
    
    for (i=0; i<lf->mi[MDIM]; i++)
    { deriv[nd] = i;
        fitfun(lf,des->xev,lf->pc.xbar,des->f1,deriv,nd+1);
        coef[i+1] -= innerprod(lf->pc.coef,des->f1,lf->mi[MP]);
    }
}

void subparcomp2(des,lf,vr,il)
design *des;
lfit *lf;
double *vr, *il;
{ double t0, t1;
    INT i, *deriv, nd, *mi;
    
    if (!hasparcomp(lf)) return;
    
    mi = lf->mi;
    deriv = lf->deriv;
    nd = lf->nd;
    fitfun(lf,des->xev,lf->pc.xbar,des->f1,deriv,nd);
    for (i=0; i<mi[MP]; i++) lf->pc.f[i] = des->f1[i];
    jacob_solve(&lf->pc.xtwx,des->f1);
    t0 = sqrt(innerprod(lf->pc.f,des->f1,mi[MP]));
    vr[0] -= t0;
    il[0] -= t0;
    if ((t0==0) | (des->ncoef==1)) return;
    
    for (i=0; i<mi[MDIM]; i++)
    { deriv[nd] = i;
        fitfun(lf,des->xev,lf->pc.xbar,lf->pc.f,deriv,nd+1);
        t1 = innerprod(lf->pc.f,des->f1,mi[MP])/t0;
        vr[i+1] -= t1;
        il[i+1] -= t1;
    }
}

double addparcomp(lf,x,c)
lfit *lf;
double *x;
int c;
{ double y;
    if (!hasparcomp(lf)) return(0.0);
    fitfun(lf,x,lf->pc.xbar,lf->pc.f,lf->deriv,lf->nd);
    if (c==PCOEF) return(innerprod(lf->pc.coef,lf->pc.f,lf->mi[MP]));
    if ((c==PNLX)|(c==PT0)|(c==PVARI))
    { y = sqrt(jacob_qf(&lf->pc.xtwx,lf->pc.f));
        return(y);
    }
    return(0.0);
}