56 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
57 if(verbose>0) printf(
"%s()\n", __func__);
58 if(x==NULL || y==NULL || k==NULL || a==NULL || snr<1 || bnr<1) {
63 if(verbose>1) fprintf(stderr,
"invalid sample or BF number\n");
67 if(!(kmin>0.0) || kmin>=kmax) {
68 if(verbose>1) fprintf(stderr,
"invalid k range\n");
74 if(verbose>1) printf(
"computing k values\n");
77 a=log10(kmin); b=log10(kmax); c=(b-a)/(
double)(bnr-1);
78 if(verbose>3) printf(
" a := %g\n b := %g\n c := %g\n", a, b, c);
79 for(
int bi=0; bi<bnr; bi++) k[bi]=pow(10.0, (
double)bi*c+a);
83 int nnls_n, nnls_m, n, m, nnls_index[bnr];
84 double *nnls_a[bnr], *nnls_b, *nnls_zz, nnls_x[bnr], *nnls_mat,
85 nnls_wp[bnr], *dptr, nnls_rnorm;
86 if(verbose>1) printf(
"allocating memory for NNLS\n");
87 nnls_n=bnr; nnls_m=snr;
88 nnls_mat=(
double*)malloc(((nnls_n+2)*nnls_m)*
sizeof(
double));
90 if(verbose>1) fprintf(stderr,
"Error: cannot allocate memory for NNLS.\n");
94 for(n=0, dptr=nnls_mat; n<nnls_n; n++) {nnls_a[n]=dptr; dptr+=nnls_m;}
95 nnls_b=dptr; dptr+=nnls_m; nnls_zz=dptr;
97 if(verbose>1) printf(
"filling NNLS data matrix\n");
99 for(m=0; m<nnls_m; m++) nnls_b[m]=y[m];
102 for(n=0; n<nnls_n; n++)
103 for(m=0; m<nnls_m; m++)
104 nnls_a[n][m]=exp(-k[n]*x[m]);
107 if(w!=NULL)
nnlsWght(nnls_n, nnls_m, nnls_a, nnls_b, w);
110 if(verbose>1) printf(
"applying NNLS\n");
111 ret=
nnls(nnls_a, nnls_m, nnls_n, nnls_b, nnls_x, &nnls_rnorm, nnls_wp, nnls_zz, nnls_index);
113 if(verbose>1) fprintf(stderr,
"NNLS solution not possible.\n");
119 if(a!=NULL)
for(n=0; n<nnls_n; n++) a[n]=nnls_x[n];
123 if(verbose>1) printf(
"computing yfit[]\n");
124 for(m=0; m<nnls_m; m++) {
126 for(n=0; n<nnls_n; n++)
128 yfit[m]+=nnls_x[n]*exp(-k[n]*x[m]);
160 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
161 if(verbose>0) printf(
"%s(k[], a[], %d, *kmin, *kmax)\n", __func__, n);
162 if(k==NULL || a==NULL || n<1) {
166 double max=nan(
""), min=nan(
"");
167 for(
int i=0; i<n; i++)
if(a[i]>0.0 && isfinite(k[i])) {
168 if(isnan(max) || k[i]>max) max=k[i];
169 if(isnan(min) || k[i]<min) min=k[i];
172 printf(
" kmin := %g\n", min);
173 printf(
" kmax := %g\n", max);
175 if(kmin!=NULL) *kmin=min;
176 if(kmax!=NULL) *kmax=max;
177 if(isnan(min) || isnan(max)) {
242 if(k==NULL || a==NULL || n<1 || ke==NULL || ae==NULL || ne<1)
return(0);
245 double *buf=NULL, *ok, *oa;
249 buf=(
double*)malloc(2*bfn*
sizeof(
double));
if(buf==NULL)
return(0);
259 ii+=i;
if(n-ii<1)
break;
262 oa[nn]=a[ii]; ok[nn]=k[ii];
272 }
while(ii<n && nn<bfn);
274 printf(
"\n\tCluster a and k values\n");
275 for(
int i=0; i<bfn; i++) printf(
"\t%g\t%g\n", oa[i], ok[i]);
276 printf(
"\n"); fflush(stdout);
281 for(
int i=bfn; i<ne; i++) ke[i]=ae[i]=0.0;
282 if(buf!=NULL) free(buf);
289 if(0) printf(
"smallest |a[%d]|=%g\n", ami, oa[ami]);
293 else if(ami==bfn-1) ci=bfn-2;
294 else {
if(fabs(oa[ami-1])<fabs(oa[ami+1])) ci=ami-1;
else ci=ami+1;}
295 if(0) printf(
"combining clusters %d and %d\n", 1+ami, 1+ci);
297 ok[ci]=(oa[ami]*ok[ami]+oa[ci]*ok[ci])/(oa[ami]+oa[ci]);
300 for(
int i=ami+1; i<bfn; i++) oa[i-1]=oa[i];
301 for(
int i=ami+1; i<bfn; i++) ok[i-1]=ok[i];
304 printf(
"\n\tAfter reduction, cluster a and k values\n");
305 for(
int i=0; i<bfn; i++) printf(
"\t%g\t%g\n", oa[i], ok[i]);
306 printf(
"\n"); fflush(stdout);
309 for(
int i=0; i<ne; i++) ae[i]=oa[i];
310 for(
int i=0; i<ne; i++) ke[i]=ok[i];
311 if(buf!=NULL) free(buf);
int spectralDExp(const double *x, const double *y, double *w, const int snr, const double kmin, const double kmax, const int bnr, double *k, double *a, double *yfit, TPCSTATUS *status)
int nnls(double **a, int m, int n, double *b, double *x, double *rnorm, double *wp, double *zzp, int *indexp)