add numerical recipes library

This commit is contained in:
2025-09-12 18:55:25 +09:00
parent d4dff245bd
commit 2c75620ec9
1344 changed files with 63869 additions and 0 deletions

61
lib/nr/ansi/recipes/fit.c Normal file
View File

@@ -0,0 +1,61 @@
#include <math.h>
#define NRANSI
#include "nrutil.h"
void fit(float x[], float y[], int ndata, float sig[], int mwt, float *a,
float *b, float *siga, float *sigb, float *chi2, float *q)
{
float gammq(float a, float x);
int i;
float wt,t,sxoss,sx=0.0,sy=0.0,st2=0.0,ss,sigdat;
*b=0.0;
if (mwt) {
ss=0.0;
for (i=1;i<=ndata;i++) {
wt=1.0/SQR(sig[i]);
ss += wt;
sx += x[i]*wt;
sy += y[i]*wt;
}
} else {
for (i=1;i<=ndata;i++) {
sx += x[i];
sy += y[i];
}
ss=ndata;
}
sxoss=sx/ss;
if (mwt) {
for (i=1;i<=ndata;i++) {
t=(x[i]-sxoss)/sig[i];
st2 += t*t;
*b += t*y[i]/sig[i];
}
} else {
for (i=1;i<=ndata;i++) {
t=x[i]-sxoss;
st2 += t*t;
*b += t*y[i];
}
}
*b /= st2;
*a=(sy-sx*(*b))/ss;
*siga=sqrt((1.0+sx*sx/(ss*st2))/ss);
*sigb=sqrt(1.0/st2);
*chi2=0.0;
*q=1.0;
if (mwt == 0) {
for (i=1;i<=ndata;i++)
*chi2 += SQR(y[i]-(*a)-(*b)*x[i]);
sigdat=sqrt((*chi2)/(ndata-2));
*siga *= sigdat;
*sigb *= sigdat;
} else {
for (i=1;i<=ndata;i++)
*chi2 += SQR((y[i]-(*a)-(*b)*x[i])/sig[i]);
if (ndata>2) *q=gammq(0.5*(ndata-2),0.5*(*chi2));
}
}
#undef NRANSI