19 #define SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;} 21 #define TOL 1.0e-5 //Default value for single precision and variables scaled to order unity. 23 void covsrt(
float **covar,
int ma,
int ia[],
int mfit)
29 for (i=mfit+1;i<=ma;i++)
30 for (j=1;j<=i;j++) covar[i][j]=covar[j][i]=0.0;
36 for (i=1;i<=ma;i++)
SWAP(covar[i][k],covar[i][j])
37 for (i=1;i<=ma;i++)
SWAP(covar[k][i],covar[j][i])
47 void svdfit(
float x[],
float y[],
float sig[],
int ndata,
float a[],
int ma,
48 float **u,
float **v,
float w[],
float *chisq,
49 void (*funcs)(
float,
float [],
int))
62 void svbksb(
long double **u,
long double w[],
long double **v,
int m,
int n,
long double b[],
64 void svdcmp(
long double **a,
int m,
int n,
long double w[],
long double **v);
66 long double wmax,tmp,thresh,sum,*b,*afunc;
70 for (i=1;i<=ndata;i++)
72 (*funcs)(x[i],(
float *)afunc,ma);
74 for (j=1;j<=ma;j++) u[i][j]=afunc[j]*tmp;
81 if (w[j] > wmax) wmax=w[j];
84 if (w[j] < thresh) w[j]=0.0;
85 svbksb(u,w,v,ndata,ma,b,a);
87 for (i=1;i<=ndata;i++)
89 (*funcs)(x[i],afunc,ma);
90 for (sum=0.0,j=1;j<=ma;j++) sum += a[j]*afunc[j];
91 *chisq += (float)(tmp=(y[i]-sum)/sig[i],tmp*tmp);
99 void svbksb(
long double **u,
long double w[],
long double **v,
100 int m,
int n,
long double b[],
long double x[])
116 for (i=1;i<=m;i++) s += u[i][j]*b[i];
124 for (jj=1;jj<=n;jj++) s += v[j][jj]*tmp[jj];
132 void svdcmp(
long double **a,
int m,
int n,
long double w[],
long double **v)
141 long double pythag(
long double a,
long double b);
142 int flag,i,its,j,jj,k,l,nm;
143 long double anorm,c,f,g,h,s,scale,x,y,z,*rv1;
154 scale += fabs(a[k][i]);
160 s += a[k][i]*a[k][i];
163 g = -
SIGN(sqrt(s),f);
168 for (s=0.0,k=i;k<=m;k++) s += a[k][i]*a[k][j];
170 for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
172 for (k=i;k<=m;k++) a[k][i] *= scale;
177 if (i <= m && i != n)
179 for (k=l;k<=n;k++) scale += fabs(a[i][k]);
185 s += a[i][k]*a[i][k];
188 g = -
SIGN(sqrt(s),f);
191 for (k=l;k<=n;k++) rv1[k]=a[i][k]/h;
194 for (s=0.0,k=l;k<=n;k++) s += a[j][k]*a[i][k];
195 for (k=l;k<=n;k++) a[j][k] += s*rv1[k];
197 for (k=l;k<=n;k++) a[i][k] *= scale;
200 anorm=
DMAX(anorm,(fabs(w[i])+fabs(rv1[i])));
209 v[j][i]=(a[i][j]/a[i][l])/g;
212 for (s=0.0,k=l;k<=n;k++) s += a[i][k]*v[k][j];
213 for (k=l;k<=n;k++) v[k][j] += s*v[k][i];
216 for (j=l;j<=n;j++) v[i][j]=v[j][i]=0.0;
222 for (i=
IMIN(m,n);i>=1;i--)
226 for (j=l;j<=n;j++) a[i][j]=0.0;
232 for (s=0.0,k=l;k<=m;k++) s += a[k][i]*a[k][j];
234 for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
236 for (j=i;j<=m;j++) a[j][i] *= g;
239 for (j=i;j<=m;j++) a[j][i]=0.0;
245 for (its=1;its<=30;its++)
251 if ((
float)(fabs(rv1[l])+anorm) == anorm)
256 if ((
float)(fabs(w[nm])+anorm) == anorm)
break;
266 if ((
float)(fabs(f)+anorm) == anorm)
break;
288 for (j=1;j<=n;j++) v[j][k] = -v[j][k];
292 if (its == 30)
nrerror(
"no convergence in 30 svdcmp iterations");
298 f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
300 f=((x-z)*(x+z)+h*((y/(f+
SIGN(g,f)))-h))/x;
317 for (jj=1;jj<=n;jj++)
334 for (jj=1;jj<=m;jj++)
348 fpp = fopen(
"matUInSVD.dat",
"w");
353 fprintf(fpp,
"%7.6e ",a[k][l]) ;
366 long double pythag(
long double a,
long double b)
369 long double absa,absb;
372 if (absa > absb)
return absa*sqrt(1.0+
DSQR(absb/absa));
373 else return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+
DSQR(absa/absb)));
void svdfit(float x[], float y[], float sig[], int ndata, float a[], int ma, float **u, float **v, float w[], float *chisq, void(*funcs)(float, float [], int))
long double pythag(long double a, long double b)
void svbksb(long double **u, long double w[], long double **v, int m, int n, long double b[], long double x[])
void svdcmp(long double **a, int m, int n, long double w[], long double **v)
void covsrt(float **covar, int ma, int ia[], int mfit)
struct flag flag
Control Flags.