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

View File

@@ -0,0 +1,12 @@
#include "nr.h"
void NR::addint(Mat_O_DP &uf, Mat_I_DP &uc, Mat_O_DP &res)
{
int i,j;
int nf=uf.nrows();
interp(res,uc);
for (j=0;j<nf;j++)
for (i=0;i<nf;i++)
uf[i][j] += res[i][j];
}

View File

@@ -0,0 +1,34 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::airy(const DP x, DP &ai, DP &bi, DP &aip, DP &bip)
{
const DP PI=3.141592653589793238, ONOVRT=0.577350269189626;
const DP THIRD=(1.0/3.0), TWOTHR=2.0*THIRD;
DP absx,ri,rip,rj,rjp,rk,rkp,rootx,ry,ryp,z;
absx=fabs(x);
rootx=sqrt(absx);
z=TWOTHR*absx*rootx;
if (x > 0.0) {
bessik(z,THIRD,ri,rk,rip,rkp);
ai=rootx*ONOVRT*rk/PI;
bi=rootx*(rk/PI+2.0*ONOVRT*ri);
bessik(z,TWOTHR,ri,rk,rip,rkp);
aip = -x*ONOVRT*rk/PI;
bip=x*(rk/PI+2.0*ONOVRT*ri);
} else if (x < 0.0) {
bessjy(z,THIRD,rj,ry,rjp,ryp);
ai=0.5*rootx*(rj-ONOVRT*ry);
bi = -0.5*rootx*(ry+ONOVRT*rj);
bessjy(z,TWOTHR,rj,ry,rjp,ryp);
aip=0.5*absx*(ONOVRT*ry+rj);
bip=0.5*absx*(ONOVRT*rj-ry);
} else {
ai=0.355028053887817;
bi=ai/ONOVRT;
aip = -0.258819403792807;
bip = -aip/ONOVRT;
}
}

View File

@@ -0,0 +1,89 @@
#include <cmath>
#include "nr.h"
using namespace std;
namespace {
inline void get_psum(Mat_I_DP &p, Vec_O_DP &psum)
{
int n,m;
DP sum;
int mpts=p.nrows();
int ndim=p.ncols();
for (n=0;n<ndim;n++) {
for (sum=0.0,m=0;m<mpts;m++) sum += p[m][n];
psum[n]=sum;
}
}
}
extern int idum;
DP tt;
void NR::amebsa(Mat_IO_DP &p, Vec_IO_DP &y, Vec_O_DP &pb, DP &yb, const DP ftol,
DP funk(Vec_I_DP &), int &iter, const DP temptr)
{
int i,ihi,ilo,j,n;
DP rtol,yhi,ylo,ynhi,ysave,yt,ytry;
int mpts=p.nrows();
int ndim=p.ncols();
Vec_DP psum(ndim);
tt = -temptr;
get_psum(p,psum);
for (;;) {
ilo=0;
ihi=1;
ynhi=ylo=y[0]+tt*log(ran1(idum));
yhi=y[1]+tt*log(ran1(idum));
if (ylo > yhi) {
ihi=0;
ilo=1;
ynhi=yhi;
yhi=ylo;
ylo=ynhi;
}
for (i=3;i<=mpts;i++) {
yt=y[i-1]+tt*log(ran1(idum));
if (yt <= ylo) {
ilo=i-1;
ylo=yt;
}
if (yt > yhi) {
ynhi=yhi;
ihi=i-1;
yhi=yt;
} else if (yt > ynhi) {
ynhi=yt;
}
}
rtol=2.0*fabs(yhi-ylo)/(fabs(yhi)+fabs(ylo));
if (rtol < ftol || iter < 0) {
SWAP(y[0],y[ilo]);
for (n=0;n<ndim;n++)
SWAP(p[0][n],p[ilo][n]);
break;
}
iter -= 2;
ytry=amotsa(p,y,psum,pb,yb,funk,ihi,yhi,-1.0);
if (ytry <= ylo) {
ytry=amotsa(p,y,psum,pb,yb,funk,ihi,yhi,2.0);
} else if (ytry >= ynhi) {
ysave=yhi;
ytry=amotsa(p,y,psum,pb,yb,funk,ihi,yhi,0.5);
if (ytry >= ysave) {
for (i=0;i<mpts;i++) {
if (i != ilo) {
for (j=0;j<ndim;j++) {
psum[j]=0.5*(p[i][j]+p[ilo][j]);
p[i][j]=psum[j];
}
y[i]=funk(psum);
}
}
iter -= ndim;
get_psum(p,psum);
}
} else ++iter;
}
}

View File

@@ -0,0 +1,71 @@
#include <cmath>
#include "nr.h"
using namespace std;
namespace {
inline void get_psum(Mat_I_DP &p, Vec_O_DP &psum)
{
int i,j;
DP sum;
int mpts=p.nrows();
int ndim=p.ncols();
for (j=0;j<ndim;j++) {
for (sum=0.0,i=0;i<mpts;i++)
sum += p[i][j];
psum[j]=sum;
}
}
}
void NR::amoeba(Mat_IO_DP &p, Vec_IO_DP &y, const DP ftol, DP funk(Vec_I_DP &),
int &nfunk)
{
const int NMAX=5000;
const DP TINY=1.0e-10;
int i,ihi,ilo,inhi,j;
DP rtol,ysave,ytry;
int mpts=p.nrows();
int ndim=p.ncols();
Vec_DP psum(ndim);
nfunk=0;
get_psum(p,psum);
for (;;) {
ilo=0;
ihi = y[0]>y[1] ? (inhi=1,0) : (inhi=0,1);
for (i=0;i<mpts;i++) {
if (y[i] <= y[ilo]) ilo=i;
if (y[i] > y[ihi]) {
inhi=ihi;
ihi=i;
} else if (y[i] > y[inhi] && i != ihi) inhi=i;
}
rtol=2.0*fabs(y[ihi]-y[ilo])/(fabs(y[ihi])+fabs(y[ilo])+TINY);
if (rtol < ftol) {
SWAP(y[0],y[ilo]);
for (i=0;i<ndim;i++) SWAP(p[0][i],p[ilo][i]);
break;
}
if (nfunk >= NMAX) nrerror("NMAX exceeded");
nfunk += 2;
ytry=amotry(p,y,psum,funk,ihi,-1.0);
if (ytry <= y[ilo])
ytry=amotry(p,y,psum,funk,ihi,2.0);
else if (ytry >= y[inhi]) {
ysave=y[ihi];
ytry=amotry(p,y,psum,funk,ihi,0.5);
if (ytry >= ysave) {
for (i=0;i<mpts;i++) {
if (i != ilo) {
for (j=0;j<ndim;j++)
p[i][j]=psum[j]=0.5*(p[i][j]+p[ilo][j]);
y[i]=funk(psum);
}
}
nfunk += ndim;
get_psum(p,psum);
}
} else --nfunk;
}
}

View File

@@ -0,0 +1,24 @@
#include "nr.h"
DP NR::amotry(Mat_IO_DP &p, Vec_O_DP &y, Vec_IO_DP &psum, DP funk(Vec_I_DP &),
const int ihi, const DP fac)
{
int j;
DP fac1,fac2,ytry;
int ndim=p.ncols();
Vec_DP ptry(ndim);
fac1=(1.0-fac)/ndim;
fac2=fac1-fac;
for (j=0;j<ndim;j++)
ptry[j]=psum[j]*fac1-p[ihi][j]*fac2;
ytry=funk(ptry);
if (ytry < y[ihi]) {
y[ihi]=ytry;
for (j=0;j<ndim;j++) {
psum[j] += ptry[j]-p[ihi][j];
p[ihi][j]=ptry[j];
}
}
return ytry;
}

View File

@@ -0,0 +1,35 @@
#include <cmath>
#include "nr.h"
using namespace std;
extern int idum;
extern DP tt;
DP NR::amotsa(Mat_IO_DP &p, Vec_O_DP &y, Vec_IO_DP &psum, Vec_O_DP &pb, DP &yb,
DP funk(Vec_I_DP &), const int ihi, DP &yhi, const DP fac)
{
int j;
DP fac1,fac2,yflu,ytry;
int ndim=p.ncols();
Vec_DP ptry(ndim);
fac1=(1.0-fac)/ndim;
fac2=fac1-fac;
for (j=0;j<ndim;j++)
ptry[j]=psum[j]*fac1-p[ihi][j]*fac2;
ytry=funk(ptry);
if (ytry <= yb) {
for (j=0;j<ndim;j++) pb[j]=ptry[j];
yb=ytry;
}
yflu=ytry-tt*log(ran1(idum));
if (yflu < yhi) {
y[ihi]=ytry;
yhi=yflu;
for (j=0;j<ndim;j++) {
psum[j] += ptry[j]-p[ihi][j];
p[ihi][j]=ptry[j];
}
}
return yflu;
}

View File

@@ -0,0 +1,77 @@
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include "nr.h"
using namespace std;
namespace {
inline DP alen(const DP a, const DP b, const DP c, const DP d)
{
return sqrt((b-a)*(b-a)+(d-c)*(d-c));
}
}
void NR::anneal(Vec_I_DP &x, Vec_I_DP &y, Vec_IO_INT &iorder)
{
const DP TFACTR=0.9;
bool ans;
int i,i1,i2,idec,idum,j,k,nn,nover,nlimit,nsucc;
static Vec_INT n(6);
unsigned long iseed;
DP path,de,t;
int ncity=x.size();
nover=100*ncity;
nlimit=10*ncity;
path=0.0;
t=0.5;
for (i=0;i<ncity-1;i++) {
i1=iorder[i];
i2=iorder[i+1];
path += alen(x[i1],x[i2],y[i1],y[i2]);
}
i1=iorder[ncity-1];
i2=iorder[0];
path += alen(x[i1],x[i2],y[i1],y[i2]);
idum = -1;
iseed=111;
cout << fixed << setprecision(6);
for (j=0;j<100;j++) {
nsucc=0;
for (k=0;k<nover;k++) {
do {
n[0]=int(ncity*ran3(idum));
n[1]=int((ncity-1)*ran3(idum));
if (n[1] >= n[0]) ++n[1];
nn=(n[0]-n[1]+ncity-1) % ncity;
} while (nn<2);
idec=irbit1(iseed);
if (idec == 0) {
n[2]=n[1]+int(abs(nn-1)*ran3(idum))+1;
n[2] %= ncity;
de=trncst(x,y,iorder,n);
ans=metrop(de,t);
if (ans) {
++nsucc;
path += de;
trnspt(iorder,n);
}
} else {
de=revcst(x,y,iorder,n);
ans=metrop(de,t);
if (ans) {
++nsucc;
path += de;
reverse(iorder,n);
}
}
if (nsucc >= nlimit) break;
}
cout << endl << "T = " << setw(12) << t;
cout << " Path Length = " << setw(12) << path << endl;
cout << "Successful Moves: " << nsucc << endl;
t *= TFACTR;
if (nsucc == 0) return;
}
}

View File

@@ -0,0 +1,15 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::anorm2(Mat_I_DP &a)
{
int i,j;
DP sum=0.0;
int n=a.nrows();
for (j=0;j<n;j++)
for (i=0;i<n;i++)
sum += a[i][j]*a[i][j];
return sqrt(sum)/n;
}

View File

@@ -0,0 +1,22 @@
#include <limits>
#include "nr.h"
using namespace std;
void NR::arcmak(Vec_I_ULNG &nfreq, unsigned long nchh, unsigned long nradd,
arithcode &acode)
{
const unsigned long MAXULNG=numeric_limits<unsigned long>::max();
unsigned long j;
unsigned long MC=acode.ncumfq.size()-2;
if (nchh > MC) nrerror("input radix may not exceed MC in arcmak.");
if (nradd > 256) nrerror("output radix may not exceed 256 in arcmak.");
acode.minint=MAXULNG/nradd;
acode.nch=nchh;
acode.nrad=nradd;
acode.ncumfq[0]=0;
for (j=1;j<=acode.nch;j++)
acode.ncumfq[j]=acode.ncumfq[j-1]+MAX(nfreq[j-1],(unsigned long) 1);
acode.ncum=acode.ncumfq[acode.nch+1]=acode.ncumfq[acode.nch]+1;
}

View File

@@ -0,0 +1,72 @@
#include "nr.h"
namespace {
inline unsigned long JTRY(const unsigned long j, const unsigned long k,
const unsigned long m)
{
return (unsigned long) (DP(j)*DP(k)/DP (m));
}
}
void NR::arcode(unsigned long &ich, string &code, unsigned long &lcd,
const int isign, arithcode &acode)
{
int j,k;
unsigned long ihi,ja,jh,jl,m;
int NWK=acode.ilob.size();
if (isign == 0) {
acode.jdif=acode.nrad-1;
for (j=NWK-1;j>=0;j--) {
acode.iupb[j]=acode.nrad-1;
acode.ilob[j]=0;
acode.nc=j;
if (acode.jdif > acode.minint) return;
acode.jdif=(acode.jdif+1)*acode.nrad-1;
}
nrerror("NWK too small in arcode.");
} else {
if (isign > 0) {
if (ich > acode.nch) nrerror("bad ich in arcode.");
} else {
ja=(unsigned char) code[lcd]-acode.ilob[acode.nc];
for (j=acode.nc+1;j<NWK;j++) {
ja *= acode.nrad;
ja += ((unsigned char) code[lcd+j-acode.nc]-acode.ilob[j]);
}
ihi=acode.nch+1;
ich=0;
while (ihi-ich > 1) {
m=(ich+ihi)>>1;
if (ja >= JTRY(acode.jdif,acode.ncumfq[m],acode.ncum))
ich=m;
else ihi=m;
}
if (ich == acode.nch) return;
}
jh=JTRY(acode.jdif,acode.ncumfq[ich+1],acode.ncum);
jl=JTRY(acode.jdif,acode.ncumfq[ich],acode.ncum);
acode.jdif=jh-jl;
arcsum(acode.ilob,acode.iupb,jh,NWK,acode.nrad,acode.nc);
arcsum(acode.ilob,acode.ilob,jl,NWK,acode.nrad,acode.nc);
for (j=acode.nc;j<NWK;j++) {
if (ich != acode.nch && acode.iupb[j] != acode.ilob[j]) break;
if (isign > 0) code += (unsigned char)acode.ilob[j];
lcd++;
}
if (j+1 > NWK) return;
acode.nc=j;
for(j=0;acode.jdif<acode.minint;j++)
acode.jdif *= acode.nrad;
if (j > acode.nc) nrerror("NWK too small in arcode.");
if (j != 0) {
for (k=acode.nc;k<NWK;k++) {
acode.iupb[k-j]=acode.iupb[k];
acode.ilob[k-j]=acode.ilob[k];
}
}
acode.nc -= j;
for (k=NWK-j;k<NWK;k++) acode.iupb[k]=acode.ilob[k]=0;
}
return;
}

View File

@@ -0,0 +1,19 @@
#include "nr.h"
void NR::arcsum(Vec_I_ULNG &iin, Vec_O_ULNG &iout, unsigned long ja,
const int nwk, const unsigned long nrad, const unsigned long nc)
{
int karry=0;
unsigned long j,jtmp;
for (j=nwk-1;j>nc;j--) {
jtmp=ja;
ja /= nrad;
iout[j]=iin[j]+(jtmp-ja*nrad)+karry;
if (iout[j] >= nrad) {
iout[j] -= nrad;
karry=1;
} else karry=0;
}
iout[nc]=iin[nc]+ja+karry;
}

View File

@@ -0,0 +1,12 @@
#include "nr.h"
extern Vec_INT *ija_p;
extern Vec_DP *sa_p;
void NR::asolve(Vec_I_DP &b, Vec_O_DP &x, const int itrnsp)
{
int i;
int n=b.size();
for(i=0;i<n;i++) x[i]=((*sa_p)[i] != 0.0 ? b[i]/(*sa_p)[i] : b[i]);
}

View File

@@ -0,0 +1,10 @@
#include "nr.h"
extern Vec_INT *ija_p;
extern Vec_DP *sa_p;
void NR::atimes(Vec_I_DP &x, Vec_O_DP &r, const int itrnsp)
{
if (itrnsp) sprstx(*sa_p,*ija_p,x,r);
else sprsax(*sa_p,*ija_p,x,r);
}

View File

@@ -0,0 +1,19 @@
#include "nr.h"
void NR::avevar(Vec_I_DP &data, DP &ave, DP &var)
{
DP s,ep;
int j;
int n=data.size();
ave=0.0;
for (j=0;j<n;j++) ave += data[j];
ave /= n;
var=ep=0.0;
for (j=0;j<n;j++) {
s=data[j]-ave;
ep += s;
var += s*s;
}
var=(var-ep*ep/n)/(n-1);
}

View File

@@ -0,0 +1,53 @@
#include <iostream>
#include <iomanip>
#include <cmath>
#include "nr.h"
using namespace std;
int main(void) // Program badluk
{
const int IYBEG=2000,IYEND=2100;
const DP ZON=-5.0;
int ic,icon,idwk,im,iyyy,jd,jday,n;
DP timzon=ZON/24.0,frac;
cout << endl << "Full moons on Friday the 13th from ";
cout << setw(5) << IYBEG << " to " << setw(5) << IYEND << endl;
for (iyyy=IYBEG;iyyy<=IYEND;iyyy++) {
for (im=1;im<=12;im++) {
jday=NR::julday(im,13,iyyy);
idwk=int((jday+1) % 7);
if (idwk == 5) {
n=int(12.37*(iyyy-1900+(im-0.5)/12.0));
icon=0;
for (;;) {
NR::flmoon(n,2,jd,frac);
frac=24.0*(frac+timzon);
if (frac < 0.0) {
--jd;
frac += 24.0;
}
if (frac > 12.0) {
++jd;
frac -= 12.0;
} else
frac += 12.0;
if (jd == jday) {
cout << endl << setw(2) << im;
cout << "/13/" << setw(4) << iyyy << endl;
cout << fixed << setprecision(1);
cout << "Full moon" << setw(6) << frac;
cout << " hrs after midnight (EST)" << endl;
break;
} else {
ic=(jday >= jd ? 1 : -1);
if (ic == (-icon)) break;
icon=ic;
n += ic;
}
}
}
}
}
return 0;
}

View File

@@ -0,0 +1,45 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
void NR::balanc(Mat_IO_DP &a)
{
const DP RADIX = numeric_limits<DP>::radix;
int i,j,last=0;
DP s,r,g,f,c,sqrdx;
int n=a.nrows();
sqrdx=RADIX*RADIX;
while (last == 0) {
last=1;
for (i=0;i<n;i++) {
r=c=0.0;
for (j=0;j<n;j++)
if (j != i) {
c += fabs(a[j][i]);
r += fabs(a[i][j]);
}
if (c != 0.0 && r != 0.0) {
g=r/RADIX;
f=1.0;
s=c+r;
while (c<g) {
f *= RADIX;
c *= sqrdx;
}
g=r*RADIX;
while (c>g) {
f /= RADIX;
c /= sqrdx;
}
if ((c+r)/f < 0.95*s) {
last=0;
g=1.0/f;
for (j=0;j<n;j++) a[i][j] *= g;
for (j=0;j<n;j++) a[j][i] *= f;
}
}
}
}
}

View File

@@ -0,0 +1,25 @@
#include "nr.h"
void NR::banbks(Mat_I_DP &a, const int m1, const int m2, Mat_I_DP &al,
Vec_I_INT &indx, Vec_IO_DP &b)
{
int i,j,k,l,mm;
DP dum;
int n=a.nrows();
mm=m1+m2+1;
l=m1;
for (k=0;k<n;k++) {
j=indx[k]-1;
if (j!=k) SWAP(b[k],b[j]);
if (l<n) l++;
for (j=k+1;j<l;j++) b[j] -= al[k][j-k-1]*b[k];
}
l=1;
for (i=n-1;i>=0;i--) {
dum=b[i];
for (k=1;k<l;k++) dum -= a[i][k]*b[k+i];
b[i]=dum/a[i][0];
if (l<mm) l++;
}
}

View File

@@ -0,0 +1,45 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::bandec(Mat_IO_DP &a, const int m1, const int m2, Mat_O_DP &al,
Vec_O_INT &indx, DP &d)
{
const DP TINY=1.0e-20;
int i,j,k,l,mm;
DP dum;
int n=a.nrows();
mm=m1+m2+1;
l=m1;
for (i=0;i<m1;i++) {
for (j=m1-i;j<mm;j++) a[i][j-l]=a[i][j];
l--;
for (j=mm-l-1;j<mm;j++) a[i][j]=0.0;
}
d=1.0;
l=m1;
for (k=0;k<n;k++) {
dum=a[k][0];
i=k;
if (l<n) l++;
for (j=k+1;j<l;j++) {
if (fabs(a[j][0]) > fabs(dum)) {
dum=a[j][0];
i=j;
}
}
indx[k]=i+1;
if (dum == 0.0) a[k][0]=TINY;
if (i != k) {
d = -d;
for (j=0;j<mm;j++) SWAP(a[k][j],a[i][j]);
}
for (i=k+1;i<l;i++) {
dum=a[i][0]/a[k][0];
al[k][i-k-1]=dum;
for (j=1;j<mm;j++) a[i][j-1]=a[i][j]-dum*a[k][j];
a[i][mm-1]=0.0;
}
}
}

View File

@@ -0,0 +1,15 @@
#include "nr.h"
void NR::banmul(Mat_I_DP &a, const int m1, const int m2, Vec_I_DP &x,
Vec_O_DP &b)
{
int i,j,k,tmploop;
int n=a.nrows();
for (i=0;i<n;i++) {
k=i-m1;
tmploop=MIN(m1+m2+1,int(n-k));
b[i]=0.0;
for (j=MAX(0,-k);j<tmploop;j++) b[i] += a[i][j]*x[j+k];
}
}

View File

@@ -0,0 +1,43 @@
#include "nr.h"
void NR::bcucof(Vec_I_DP &y, Vec_I_DP &y1, Vec_I_DP &y2, Vec_I_DP &y12,
const DP d1, const DP d2, Mat_O_DP &c)
{
static int wt_d[16*16]=
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
-3, 0, 0, 3, 0, 0, 0, 0,-2, 0, 0,-1, 0, 0, 0, 0,
2, 0, 0,-2, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0,-3, 0, 0, 3, 0, 0, 0, 0,-2, 0, 0,-1,
0, 0, 0, 0, 2, 0, 0,-2, 0, 0, 0, 0, 1, 0, 0, 1,
-3, 3, 0, 0,-2,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,-3, 3, 0, 0,-2,-1, 0, 0,
9,-9, 9,-9, 6, 3,-3,-6, 6,-6,-3, 3, 4, 2, 1, 2,
-6, 6,-6, 6,-4,-2, 2, 4,-3, 3, 3,-3,-2,-1,-1,-2,
2,-2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 2,-2, 0, 0, 1, 1, 0, 0,
-6, 6,-6, 6,-3,-3, 3, 3,-4, 4, 2,-2,-2,-2,-1,-1,
4,-4, 4,-4, 2, 2,-2,-2, 2,-2,-2, 2, 1, 1, 1, 1};
int l,k,j,i;
DP xx,d1d2;
Vec_DP cl(16),x(16);
static Mat_INT wt(wt_d,16,16);
d1d2=d1*d2;
for (i=0;i<4;i++) {
x[i]=y[i];
x[i+4]=y1[i]*d1;
x[i+8]=y2[i]*d2;
x[i+12]=y12[i]*d1d2;
}
for (i=0;i<16;i++) {
xx=0.0;
for (k=0;k<16;k++) xx += wt[i][k]*x[k];
cl[i]=xx;
}
l=0;
for (i=0;i<4;i++)
for (j=0;j<4;j++) c[i][j]=cl[l++];
}

View File

@@ -0,0 +1,26 @@
#include "nr.h"
void NR::bcuint(Vec_I_DP &y, Vec_I_DP &y1, Vec_I_DP &y2, Vec_I_DP &y12,
const DP x1l, const DP x1u, const DP x2l, const DP x2u,
const DP x1, const DP x2, DP &ansy, DP &ansy1, DP &ansy2)
{
int i;
DP t,u,d1,d2;
Mat_DP c(4,4);
d1=x1u-x1l;
d2=x2u-x2l;
bcucof(y,y1,y2,y12,d1,d2,c);
if (x1u == x1l || x2u == x2l)
nrerror("Bad input in routine bcuint");
t=(x1-x1l)/d1;
u=(x2-x2l)/d2;
ansy=ansy2=ansy1=0.0;
for (i=3;i>=0;i--) {
ansy=t*ansy+((c[i][3]*u+c[i][2])*u+c[i][1])*u+c[i][0];
ansy2=t*ansy2+(3.0*c[i][3]*u+2.0*c[i][2])*u+c[i][1];
ansy1=u*ansy1+(3.0*c[3][i]*t+2.0*c[2][i])*t+c[1][i];
}
ansy1 /= d1;
ansy2 /= d2;
}

View File

@@ -0,0 +1,22 @@
#include "nr.h"
void NR::beschb(const DP x, DP &gam1, DP &gam2, DP &gampl, DP &gammi)
{
const int NUSE1=7, NUSE2=8;
static const DP c1_d[7] = {
-1.142022680371168e0,6.5165112670737e-3,
3.087090173086e-4,-3.4706269649e-6,6.9437664e-9,
3.67795e-11,-1.356e-13};
static const DP c2_d[8] = {
1.843740587300905e0,-7.68528408447867e-2,
1.2719271366546e-3,-4.9717367042e-6,-3.31261198e-8,
2.423096e-10,-1.702e-13,-1.49e-15};
DP xx;
static Vec_DP c1(c1_d,7),c2(c2_d,8);
xx=8.0*x*x-1.0;
gam1=chebev(-1.0,1.0,c1,NUSE1,xx);
gam2=chebev(-1.0,1.0,c2,NUSE2,xx);
gampl= gam2-x*gam1;
gammi= gam2+x*gam1;
}

View File

@@ -0,0 +1,34 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
DP NR::bessi(const int n, const DP x)
{
const DP ACC=200.0;
const int IEXP=numeric_limits<DP>::max_exponent/2;
int j,k;
DP bi,bim,bip,dum,tox,ans;
if (n < 2) nrerror("Index n less than 2 in bessi");
if (x*x <= 8.0*numeric_limits<DP>::min()) return 0.0;
else {
tox=2.0/fabs(x);
bip=ans=0.0;
bi=1.0;
for (j=2*(n+int(sqrt(ACC*n)));j>0;j--) {
bim=bip+j*tox*bi;
bip=bi;
bi=bim;
dum=frexp(bi,&k);
if (k > IEXP) {
ans=ldexp(ans,-IEXP);
bi=ldexp(bi,-IEXP);
bip=ldexp(bip,-IEXP);
}
if (j == n) ans=bip;
}
ans *= bessi0(x)/bi;
return x < 0.0 && (n & 1) ? -ans : ans;
}
}

View File

@@ -0,0 +1,22 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bessi0(const DP x)
{
DP ax,ans,y;
if ((ax=fabs(x)) < 3.75) {
y=x/3.75;
y*=y;
ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492
+y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
} else {
y=3.75/ax;
ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1
+y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2
+y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1
+y*0.392377e-2))))))));
}
return ans;
}

View File

@@ -0,0 +1,23 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bessi1(const DP x)
{
DP ax,ans,y;
if ((ax=fabs(x)) < 3.75) {
y=x/3.75;
y*=y;
ans=ax*(0.5+y*(0.87890594+y*(0.51498869+y*(0.15084934
+y*(0.2658733e-1+y*(0.301532e-2+y*0.32411e-3))))));
} else {
y=3.75/ax;
ans=0.2282967e-1+y*(-0.2895312e-1+y*(0.1787654e-1
-y*0.420059e-2));
ans=0.39894228+y*(-0.3988024e-1+y*(-0.362018e-2
+y*(0.163801e-2+y*(-0.1031555e-1+y*ans))));
ans *= (exp(ax)/sqrt(ax));
}
return x < 0.0 ? -ans : ans;
}

View File

@@ -0,0 +1,121 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
void NR::bessik(const DP x, const DP xnu, DP &ri, DP &rk, DP &rip, DP &rkp)
{
const int MAXIT=10000;
const DP EPS=numeric_limits<DP>::epsilon();
const DP FPMIN=numeric_limits<DP>::min()/EPS;
const DP XMIN=2.0, PI=3.141592653589793;
DP a,a1,b,c,d,del,del1,delh,dels,e,f,fact,fact2,ff,gam1,gam2,
gammi,gampl,h,p,pimu,q,q1,q2,qnew,ril,ril1,rimu,rip1,ripl,
ritemp,rk1,rkmu,rkmup,rktemp,s,sum,sum1,x2,xi,xi2,xmu,xmu2;
int i,l,nl;
if (x <= 0.0 || xnu < 0.0) nrerror("bad arguments in bessik");
nl=int(xnu+0.5);
xmu=xnu-nl;
xmu2=xmu*xmu;
xi=1.0/x;
xi2=2.0*xi;
h=xnu*xi;
if (h < FPMIN) h=FPMIN;
b=xi2*xnu;
d=0.0;
c=h;
for (i=0;i<MAXIT;i++) {
b += xi2;
d=1.0/(b+d);
c=b+1.0/c;
del=c*d;
h=del*h;
if (fabs(del-1.0) <= EPS) break;
}
if (i >= MAXIT)
nrerror("x too large in bessik; try asymptotic expansion");
ril=FPMIN;
ripl=h*ril;
ril1=ril;
rip1=ripl;
fact=xnu*xi;
for (l=nl-1;l >= 0;l--) {
ritemp=fact*ril+ripl;
fact -= xi;
ripl=fact*ritemp+ril;
ril=ritemp;
}
f=ripl/ril;
if (x < XMIN) {
x2=0.5*x;
pimu=PI*xmu;
fact = (fabs(pimu) < EPS ? 1.0 : pimu/sin(pimu));
d = -log(x2);
e=xmu*d;
fact2 = (fabs(e) < EPS ? 1.0 : sinh(e)/e);
beschb(xmu,gam1,gam2,gampl,gammi);
ff=fact*(gam1*cosh(e)+gam2*fact2*d);
sum=ff;
e=exp(e);
p=0.5*e/gampl;
q=0.5/(e*gammi);
c=1.0;
d=x2*x2;
sum1=p;
for (i=1;i<=MAXIT;i++) {
ff=(i*ff+p+q)/(i*i-xmu2);
c *= (d/i);
p /= (i-xmu);
q /= (i+xmu);
del=c*ff;
sum += del;
del1=c*(p-i*ff);
sum1 += del1;
if (fabs(del) < fabs(sum)*EPS) break;
}
if (i > MAXIT) nrerror("bessk series failed to converge");
rkmu=sum;
rk1=sum1*xi2;
} else {
b=2.0*(1.0+x);
d=1.0/b;
h=delh=d;
q1=0.0;
q2=1.0;
a1=0.25-xmu2;
q=c=a1;
a = -a1;
s=1.0+q*delh;
for (i=1;i<MAXIT;i++) {
a -= 2*i;
c = -a*c/(i+1.0);
qnew=(q1-b*q2)/a;
q1=q2;
q2=qnew;
q += c*qnew;
b += 2.0;
d=1.0/(b+a*d);
delh=(b*d-1.0)*delh;
h += delh;
dels=q*delh;
s += dels;
if (fabs(dels/s) <= EPS) break;
}
if (i >= MAXIT) nrerror("bessik: failure to converge in cf2");
h=a1*h;
rkmu=sqrt(PI/(2.0*x))*exp(-x)/s;
rk1=rkmu*(xmu+x+0.5-h)*xi;
}
rkmup=xmu*xi*rkmu-rk1;
rimu=xi/(f*rkmu-rkmup);
ri=(rimu*ril1)/ril;
rip=(rimu*rip1)/ril;
for (i=1;i <= nl;i++) {
rktemp=(xmu+i)*xi2*rk1+rkmu;
rkmu=rk1;
rk1=rktemp;
}
rk=rkmu;
rkp=xnu*xi*rkmu-rk1;
}

View File

@@ -0,0 +1,52 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
DP NR::bessj(const int n, const DP x)
{
const DP ACC=160.0;
const int IEXP=numeric_limits<DP>::max_exponent/2;
bool jsum;
int j,k,m;
DP ax,bj,bjm,bjp,dum,sum,tox,ans;
if (n < 2) nrerror("Index n less than 2 in bessj");
ax=fabs(x);
if (ax*ax <= 8.0*numeric_limits<DP>::min()) return 0.0;
else if (ax > DP(n)) {
tox=2.0/ax;
bjm=bessj0(ax);
bj=bessj1(ax);
for (j=1;j<n;j++) {
bjp=j*tox*bj-bjm;
bjm=bj;
bj=bjp;
}
ans=bj;
} else {
tox=2.0/ax;
m=2*((n+int(sqrt(ACC*n)))/2);
jsum=false;
bjp=ans=sum=0.0;
bj=1.0;
for (j=m;j>0;j--) {
bjm=j*tox*bj-bjp;
bjp=bj;
bj=bjm;
dum=frexp(bj,&k);
if (k > IEXP) {
bj=ldexp(bj,-IEXP);
bjp=ldexp(bjp,-IEXP);
ans=ldexp(ans,-IEXP);
sum=ldexp(sum,-IEXP);
}
if (jsum) sum += bj;
jsum=!jsum;
if (j == n) ans=bjp;
}
sum=2.0*sum-bj;
ans /= sum;
}
return x < 0.0 && (n & 1) ? -ans : ans;
}

View File

@@ -0,0 +1,28 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bessj0(const DP x)
{
DP ax,z,xx,y,ans,ans1,ans2;
if ((ax=fabs(x)) < 8.0) {
y=x*x;
ans1=57568490574.0+y*(-13362590354.0+y*(651619640.7
+y*(-11214424.18+y*(77392.33017+y*(-184.9052456)))));
ans2=57568490411.0+y*(1029532985.0+y*(9494680.718
+y*(59272.64853+y*(267.8532712+y*1.0))));
ans=ans1/ans2;
} else {
z=8.0/ax;
y=z*z;
xx=ax-0.785398164;
ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4
+y*(-0.2073370639e-5+y*0.2093887211e-6)));
ans2 = -0.1562499995e-1+y*(0.1430488765e-3
+y*(-0.6911147651e-5+y*(0.7621095161e-6
-y*0.934945152e-7)));
ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
}
return ans;
}

View File

@@ -0,0 +1,29 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bessj1(const DP x)
{
DP ax,z,xx,y,ans,ans1,ans2;
if ((ax=fabs(x)) < 8.0) {
y=x*x;
ans1=x*(72362614232.0+y*(-7895059235.0+y*(242396853.1
+y*(-2972611.439+y*(15704.48260+y*(-30.16036606))))));
ans2=144725228442.0+y*(2300535178.0+y*(18583304.74
+y*(99447.43394+y*(376.9991397+y*1.0))));
ans=ans1/ans2;
} else {
z=8.0/ax;
y=z*z;
xx=ax-2.356194491;
ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4
+y*(0.2457520174e-5+y*(-0.240337019e-6))));
ans2=0.04687499995+y*(-0.2002690873e-3
+y*(0.8449199096e-5+y*(-0.88228987e-6
+y*0.105787412e-6)));
ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
if (x < 0.0) ans = -ans;
}
return ans;
}

View File

@@ -0,0 +1,150 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
void NR::bessjy(const DP x, const DP xnu, DP &rj, DP &ry, DP &rjp, DP &ryp)
{
const int MAXIT=10000;
const DP EPS=numeric_limits<DP>::epsilon();
const DP FPMIN=numeric_limits<DP>::min()/EPS;
const DP XMIN=2.0, PI=3.141592653589793;
DP a,b,br,bi,c,cr,ci,d,del,del1,den,di,dlr,dli,dr,e,f,fact,fact2,
fact3,ff,gam,gam1,gam2,gammi,gampl,h,p,pimu,pimu2,q,r,rjl,
rjl1,rjmu,rjp1,rjpl,rjtemp,ry1,rymu,rymup,rytemp,sum,sum1,
temp,w,x2,xi,xi2,xmu,xmu2;
int i,isign,l,nl;
if (x <= 0.0 || xnu < 0.0)
nrerror("bad arguments in bessjy");
nl=(x < XMIN ? int(xnu+0.5) : MAX(0,int(xnu-x+1.5)));
xmu=xnu-nl;
xmu2=xmu*xmu;
xi=1.0/x;
xi2=2.0*xi;
w=xi2/PI;
isign=1;
h=xnu*xi;
if (h < FPMIN) h=FPMIN;
b=xi2*xnu;
d=0.0;
c=h;
for (i=0;i<MAXIT;i++) {
b += xi2;
d=b-d;
if (fabs(d) < FPMIN) d=FPMIN;
c=b-1.0/c;
if (fabs(c) < FPMIN) c=FPMIN;
d=1.0/d;
del=c*d;
h=del*h;
if (d < 0.0) isign = -isign;
if (fabs(del-1.0) <= EPS) break;
}
if (i >= MAXIT)
nrerror("x too large in bessjy; try asymptotic expansion");
rjl=isign*FPMIN;
rjpl=h*rjl;
rjl1=rjl;
rjp1=rjpl;
fact=xnu*xi;
for (l=nl-1;l>=0;l--) {
rjtemp=fact*rjl+rjpl;
fact -= xi;
rjpl=fact*rjtemp-rjl;
rjl=rjtemp;
}
if (rjl == 0.0) rjl=EPS;
f=rjpl/rjl;
if (x < XMIN) {
x2=0.5*x;
pimu=PI*xmu;
fact = (fabs(pimu) < EPS ? 1.0 : pimu/sin(pimu));
d = -log(x2);
e=xmu*d;
fact2 = (fabs(e) < EPS ? 1.0 : sinh(e)/e);
beschb(xmu,gam1,gam2,gampl,gammi);
ff=2.0/PI*fact*(gam1*cosh(e)+gam2*fact2*d);
e=exp(e);
p=e/(gampl*PI);
q=1.0/(e*PI*gammi);
pimu2=0.5*pimu;
fact3 = (fabs(pimu2) < EPS ? 1.0 : sin(pimu2)/pimu2);
r=PI*pimu2*fact3*fact3;
c=1.0;
d = -x2*x2;
sum=ff+r*q;
sum1=p;
for (i=1;i<=MAXIT;i++) {
ff=(i*ff+p+q)/(i*i-xmu2);
c *= (d/i);
p /= (i-xmu);
q /= (i+xmu);
del=c*(ff+r*q);
sum += del;
del1=c*p-i*del;
sum1 += del1;
if (fabs(del) < (1.0+fabs(sum))*EPS) break;
}
if (i > MAXIT)
nrerror("bessy series failed to converge");
rymu = -sum;
ry1 = -sum1*xi2;
rymup=xmu*xi*rymu-ry1;
rjmu=w/(rymup-f*rymu);
} else {
a=0.25-xmu2;
p = -0.5*xi;
q=1.0;
br=2.0*x;
bi=2.0;
fact=a*xi/(p*p+q*q);
cr=br+q*fact;
ci=bi+p*fact;
den=br*br+bi*bi;
dr=br/den;
di = -bi/den;
dlr=cr*dr-ci*di;
dli=cr*di+ci*dr;
temp=p*dlr-q*dli;
q=p*dli+q*dlr;
p=temp;
for (i=1;i<MAXIT;i++) {
a += 2*i;
bi += 2.0;
dr=a*dr+br;
di=a*di+bi;
if (fabs(dr)+fabs(di) < FPMIN) dr=FPMIN;
fact=a/(cr*cr+ci*ci);
cr=br+cr*fact;
ci=bi-ci*fact;
if (fabs(cr)+fabs(ci) < FPMIN) cr=FPMIN;
den=dr*dr+di*di;
dr /= den;
di /= -den;
dlr=cr*dr-ci*di;
dli=cr*di+ci*dr;
temp=p*dlr-q*dli;
q=p*dli+q*dlr;
p=temp;
if (fabs(dlr-1.0)+fabs(dli) <= EPS) break;
}
if (i >= MAXIT) nrerror("cf2 failed in bessjy");
gam=(p-f)/q;
rjmu=sqrt(w/((p-f)*gam+q));
rjmu=SIGN(rjmu,rjl);
rymu=rjmu*gam;
rymup=rymu*(p+q/gam);
ry1=xmu*xi*rymu-rymup;
}
fact=rjmu/rjl;
rj=rjl1*fact;
rjp=rjp1*fact;
for (i=1;i<=nl;i++) {
rytemp=(xmu+i)*xi2*ry1-rymu;
rymu=ry1;
ry1=rytemp;
}
ry=rymu;
ryp=xnu*xi*rymu-ry1;
}

View File

@@ -0,0 +1,18 @@
#include "nr.h"
DP NR::bessk(const int n, const DP x)
{
int j;
DP bk,bkm,bkp,tox;
if (n < 2) nrerror("Index n less than 2 in bessk");
tox=2.0/x;
bkm=bessk0(x);
bk=bessk1(x);
for (j=1;j<n;j++) {
bkp=bkm+j*tox*bk;
bkm=bk;
bk=bkp;
}
return bk;
}

View File

@@ -0,0 +1,21 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bessk0(const DP x)
{
DP y,ans;
if (x <= 2.0) {
y=x*x/4.0;
ans=(-log(x/2.0)*bessi0(x))+(-0.57721566+y*(0.42278420
+y*(0.23069756+y*(0.3488590e-1+y*(0.262698e-2
+y*(0.10750e-3+y*0.74e-5))))));
} else {
y=2.0/x;
ans=(exp(-x)/sqrt(x))*(1.25331414+y*(-0.7832358e-1
+y*(0.2189568e-1+y*(-0.1062446e-1+y*(0.587872e-2
+y*(-0.251540e-2+y*0.53208e-3))))));
}
return ans;
}

View File

@@ -0,0 +1,21 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bessk1(const DP x)
{
DP y,ans;
if (x <= 2.0) {
y=x*x/4.0;
ans=(log(x/2.0)*bessi1(x))+(1.0/x)*(1.0+y*(0.15443144
+y*(-0.67278579+y*(-0.18156897+y*(-0.1919402e-1
+y*(-0.110404e-2+y*(-0.4686e-4)))))));
} else {
y=2.0/x;
ans=(exp(-x)/sqrt(x))*(1.25331414+y*(0.23498619
+y*(-0.3655620e-1+y*(0.1504268e-1+y*(-0.780353e-2
+y*(0.325614e-2+y*(-0.68245e-3)))))));
}
return ans;
}

View File

@@ -0,0 +1,18 @@
#include "nr.h"
DP NR::bessy(const int n, const DP x)
{
int j;
DP by,bym,byp,tox;
if (n < 2) nrerror("Index n less than 2 in bessy");
tox=2.0/x;
by=bessy1(x);
bym=bessy0(x);
for (j=1;j<n;j++) {
byp=j*tox*by-bym;
bym=by;
by=byp;
}
return by;
}

View File

@@ -0,0 +1,28 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bessy0(const DP x)
{
DP z,xx,y,ans,ans1,ans2;
if (x < 8.0) {
y=x*x;
ans1 = -2957821389.0+y*(7062834065.0+y*(-512359803.6
+y*(10879881.29+y*(-86327.92757+y*228.4622733))));
ans2=40076544269.0+y*(745249964.8+y*(7189466.438
+y*(47447.26470+y*(226.1030244+y*1.0))));
ans=(ans1/ans2)+0.636619772*bessj0(x)*log(x);
} else {
z=8.0/x;
y=z*z;
xx=x-0.785398164;
ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4
+y*(-0.2073370639e-5+y*0.2093887211e-6)));
ans2 = -0.1562499995e-1+y*(0.1430488765e-3
+y*(-0.6911147651e-5+y*(0.7621095161e-6
+y*(-0.934945152e-7))));
ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2);
}
return ans;
}

View File

@@ -0,0 +1,30 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bessy1(const DP x)
{
DP z,xx,y,ans,ans1,ans2;
if (x < 8.0) {
y=x*x;
ans1=x*(-0.4900604943e13+y*(0.1275274390e13
+y*(-0.5153438139e11+y*(0.7349264551e9
+y*(-0.4237922726e7+y*0.8511937935e4)))));
ans2=0.2499580570e14+y*(0.4244419664e12
+y*(0.3733650367e10+y*(0.2245904002e8
+y*(0.1020426050e6+y*(0.3549632885e3+y)))));
ans=(ans1/ans2)+0.636619772*(bessj1(x)*log(x)-1.0/x);
} else {
z=8.0/x;
y=z*z;
xx=x-2.356194491;
ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4
+y*(0.2457520174e-5+y*(-0.240337019e-6))));
ans2=0.04687499995+y*(-0.2002690873e-3
+y*(0.8449199096e-5+y*(-0.88228987e-6
+y*0.105787412e-6)));
ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2);
}
return ans;
}

View File

@@ -0,0 +1,8 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::beta(const DP z, const DP w)
{
return exp(gammln(z)+gammln(w)-gammln(z+w));
}

View File

@@ -0,0 +1,43 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
DP NR::betacf(const DP a, const DP b, const DP x)
{
const int MAXIT=100;
const DP EPS=numeric_limits<DP>::epsilon();
const DP FPMIN=numeric_limits<DP>::min()/EPS;
int m,m2;
DP aa,c,d,del,h,qab,qam,qap;
qab=a+b;
qap=a+1.0;
qam=a-1.0;
c=1.0;
d=1.0-qab*x/qap;
if (fabs(d) < FPMIN) d=FPMIN;
d=1.0/d;
h=d;
for (m=1;m<=MAXIT;m++) {
m2=2*m;
aa=m*(b-m)*x/((qam+m2)*(a+m2));
d=1.0+aa*d;
if (fabs(d) < FPMIN) d=FPMIN;
c=1.0+aa/c;
if (fabs(c) < FPMIN) c=FPMIN;
d=1.0/d;
h *= d*c;
aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
d=1.0+aa*d;
if (fabs(d) < FPMIN) d=FPMIN;
c=1.0+aa/c;
if (fabs(c) < FPMIN) c=FPMIN;
d=1.0/d;
del=d*c;
h *= del;
if (fabs(del-1.0) <= EPS) break;
}
if (m > MAXIT) nrerror("a or b too big, or MAXIT too small in betacf");
return h;
}

View File

@@ -0,0 +1,17 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::betai(const DP a, const DP b, const DP x)
{
DP bt;
if (x < 0.0 || x > 1.0) nrerror("Bad x in routine betai");
if (x == 0.0 || x == 1.0) bt=0.0;
else
bt=exp(gammln(a+b)-gammln(a)-gammln(b)+a*log(x)+b*log(1.0-x));
if (x < (a+1.0)/(a+b+2.0))
return bt*betacf(a,b,x)/a;
else
return 1.0-bt*betacf(b,a,1.0-x)/b;
}

View File

@@ -0,0 +1,8 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bico(const int n, const int k)
{
return floor(0.5+exp(factln(n)-factln(k)-factln(n-k)));
}

View File

@@ -0,0 +1,25 @@
#include "nr.h"
void NR::bksub(const int ne, const int nb, const int jf, const int k1,
const int k2, Mat3D_IO_DP &c)
{
int nbf,im,kp,k,j,i;
DP xx;
nbf=ne-nb;
im=1;
for (k=k2-1;k>=k1;k--) {
if (k == k1) im=nbf+1;
kp=k+1;
for (j=0;j<nbf;j++) {
xx=c[j][jf][kp];
for (i=im-1;i<ne;i++)
c[i][jf][k] -= c[i][j][k]*xx;
}
}
for (k=k1;k<k2;k++) {
kp=k+1;
for (i=0;i<nb;i++) c[i][0][k]=c[i+nbf][jf][k];
for (i=0;i<nbf;i++) c[i+nb][0][k]=c[i][jf][kp];
}
}

View File

@@ -0,0 +1,53 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::bnldev(const DP pp, const int n, int &idum)
{
const DP PI=3.141592653589793238;
int j;
static int nold=(-1);
DP am,em,g,angle,p,bnl,sq,t,y;
static DP pold=(-1.0),pc,plog,pclog,en,oldg;
p=(pp <= 0.5 ? pp : 1.0-pp);
am=n*p;
if (n < 25) {
bnl=0.0;
for (j=0;j<n;j++)
if (ran1(idum) < p) ++bnl;
} else if (am < 1.0) {
g=exp(-am);
t=1.0;
for (j=0;j<=n;j++) {
t *= ran1(idum);
if (t < g) break;
}
bnl=(j <= n ? j : n);
} else {
if (n != nold) {
en=n;
oldg=gammln(en+1.0);
nold=n;
} if (p != pold) {
pc=1.0-p;
plog=log(p);
pclog=log(pc);
pold=p;
}
sq=sqrt(2.0*am*pc);
do {
do {
angle=PI*ran1(idum);
y=tan(angle);
em=sq*y+am;
} while (em < 0.0 || em >= (en+1.0));
em=floor(em);
t=1.2*sq*(1.0+y*y)*exp(oldg-gammln(em+1.0)
-gammln(en-em+1.0)+em*plog+(en-em)*pclog);
} while (ran1(idum) > t);
bnl=em;
}
if (p != pp) bnl=n-bnl;
return bnl;
}

View File

@@ -0,0 +1,79 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
namespace {
inline void shft3(DP &a, DP &b, DP &c, const DP d)
{
a=b;
b=c;
c=d;
}
}
DP NR::brent(const DP ax, const DP bx, const DP cx, DP f(const DP),
const DP tol, DP &xmin)
{
const int ITMAX=100;
const DP CGOLD=0.3819660;
const DP ZEPS=numeric_limits<DP>::epsilon()*1.0e-3;
int iter;
DP a,b,d=0.0,etemp,fu,fv,fw,fx;
DP p,q,r,tol1,tol2,u,v,w,x,xm;
DP e=0.0;
a=(ax < cx ? ax : cx);
b=(ax > cx ? ax : cx);
x=w=v=bx;
fw=fv=fx=f(x);
for (iter=0;iter<ITMAX;iter++) {
xm=0.5*(a+b);
tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
if (fabs(x-xm) <= (tol2-0.5*(b-a))) {
xmin=x;
return fx;
}
if (fabs(e) > tol1) {
r=(x-w)*(fx-fv);
q=(x-v)*(fx-fw);
p=(x-v)*q-(x-w)*r;
q=2.0*(q-r);
if (q > 0.0) p = -p;
q=fabs(q);
etemp=e;
e=d;
if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
d=CGOLD*(e=(x >= xm ? a-x : b-x));
else {
d=p/q;
u=x+d;
if (u-a < tol2 || b-u < tol2)
d=SIGN(tol1,xm-x);
}
} else {
d=CGOLD*(e=(x >= xm ? a-x : b-x));
}
u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
fu=f(u);
if (fu <= fx) {
if (u >= x) a=x; else b=x;
shft3(v,w,x,u);
shft3(fv,fw,fx,fu);
} else {
if (u < x) a=u; else b=u;
if (fu <= fw || w == x) {
v=w;
w=u;
fv=fw;
fw=fu;
} else if (fu <= fv || v == x || v == w) {
v=u;
fv=fu;
}
}
}
nrerror("Too many iterations in brent");
xmin=x;
return fx;
}

View File

@@ -0,0 +1,143 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
Vec_DP *fvec_p;
void (*nrfuncv)(Vec_I_DP &v, Vec_O_DP &f);
void NR::broydn(Vec_IO_DP &x, bool &check, void vecfunc(Vec_I_DP &, Vec_O_DP &))
{
const int MAXITS=200;
const DP EPS=numeric_limits<DP>::epsilon();
const DP TOLF=1.0e-8, TOLX=EPS, STPMX=100.0, TOLMIN=1.0e-12;
bool restrt,sing,skip;
int i,its,j,k;
DP den,f,fold,stpmax,sum,temp,test;
int n=x.size();
Mat_DP qt(n,n),r(n,n);
Vec_DP c(n),d(n),fvcold(n),g(n),p(n),s(n),t(n),w(n),xold(n);
fvec_p=new Vec_DP(n);
nrfuncv=vecfunc;
Vec_DP &fvec=*fvec_p;
f=fmin(x);
test=0.0;
for (i=0;i<n;i++)
if (fabs(fvec[i]) > test) test=fabs(fvec[i]);
if (test < 0.01*TOLF) {
check=false;
delete fvec_p;
return;
}
for (sum=0.0,i=0;i<n;i++) sum += SQR(x[i]);
stpmax=STPMX*MAX(sqrt(sum),DP(n));
restrt=true;
for (its=1;its<=MAXITS;its++) {
if (restrt) {
fdjac(x,fvec,r,vecfunc);
qrdcmp(r,c,d,sing);
if (sing) nrerror("singular Jacobian in broydn");
for (i=0;i<n;i++) {
for (j=0;j<n;j++) qt[i][j]=0.0;
qt[i][i]=1.0;
}
for (k=0;k<n-1;k++) {
if (c[k] != 0.0) {
for (j=0;j<n;j++) {
sum=0.0;
for (i=k;i<n;i++)
sum += r[i][k]*qt[i][j];
sum /= c[k];
for (i=k;i<n;i++)
qt[i][j] -= sum*r[i][k];
}
}
}
for (i=0;i<n;i++) {
r[i][i]=d[i];
for (j=0;j<i;j++) r[i][j]=0.0;
}
} else {
for (i=0;i<n;i++) s[i]=x[i]-xold[i];
for (i=0;i<n;i++) {
for (sum=0.0,j=i;j<n;j++) sum += r[i][j]*s[j];
t[i]=sum;
}
skip=true;
for (i=0;i<n;i++) {
for (sum=0.0,j=0;j<n;j++) sum += qt[j][i]*t[j];
w[i]=fvec[i]-fvcold[i]-sum;
if (fabs(w[i]) >= EPS*(fabs(fvec[i])+fabs(fvcold[i]))) skip=false;
else w[i]=0.0;
}
if (!skip) {
for (i=0;i<n;i++) {
for (sum=0.0,j=0;j<n;j++) sum += qt[i][j]*w[j];
t[i]=sum;
}
for (den=0.0,i=0;i<n;i++) den += SQR(s[i]);
for (i=0;i<n;i++) s[i] /= den;
qrupdt(r,qt,t,s);
for (i=0;i<n;i++) {
if (r[i][i] == 0.0) nrerror("r singular in broydn");
d[i]=r[i][i];
}
}
}
for (i=0;i<n;i++) {
for (sum=0.0,j=0;j<n;j++) sum += qt[i][j]*fvec[j];
p[i] = -sum;
}
for (i=n-1;i>=0;i--) {
for (sum=0.0,j=0;j<=i;j++) sum -= r[j][i]*p[j];
g[i]=sum;
}
for (i=0;i<n;i++) {
xold[i]=x[i];
fvcold[i]=fvec[i];
}
fold=f;
rsolv(r,d,p);
lnsrch(xold,fold,g,p,x,f,stpmax,check,fmin);
test=0.0;
for (i=0;i<n;i++)
if (fabs(fvec[i]) > test) test=fabs(fvec[i]);
if (test < TOLF) {
check=false;
delete fvec_p;
return;
}
if (check) {
if (restrt) {
delete fvec_p;
return;
} else {
test=0.0;
den=MAX(f,0.5*n);
for (i=0;i<n;i++) {
temp=fabs(g[i])*MAX(fabs(x[i]),1.0)/den;
if (temp > test) test=temp;
}
if (test < TOLMIN) {
delete fvec_p;
return;
}
else restrt=true;
}
} else {
restrt=false;
test=0.0;
for (i=0;i<n;i++) {
temp=(fabs(x[i]-xold[i]))/MAX(fabs(x[i]),1.0);
if (temp > test) test=temp;
}
if (test < TOLX) {
delete fvec_p;
return;
}
}
}
nrerror("MAXITS exceeded in broydn");
return;
}

View File

@@ -0,0 +1,118 @@
#include <cmath>
#include "nr.h"
using namespace std;
Vec_DP *x_p;
Mat_DP *d_p;
void NR::bsstep(Vec_IO_DP &y, Vec_IO_DP &dydx, DP &xx, const DP htry,
const DP eps, Vec_I_DP &yscal, DP &hdid, DP &hnext,
void derivs(const DP, Vec_I_DP &, Vec_O_DP &))
{
const int KMAXX=8, IMAXX=(KMAXX+1);
const DP SAFE1=0.25, SAFE2=0.7, REDMAX=1.0e-5, REDMIN=0.7;
const DP TINY=1.0e-30, SCALMX=0.1;
static const int nseq_d[IMAXX]={2,4,6,8,10,12,14,16,18};
static int first=1,kmax,kopt;
static DP epsold = -1.0,xnew;
static Vec_DP a(IMAXX);
static Mat_DP alf(KMAXX,KMAXX);
bool exitflag=false;
int i,iq,k,kk,km,reduct;
DP eps1,errmax,fact,h,red,scale,work,wrkmin,xest;
Vec_INT nseq(nseq_d,IMAXX);
Vec_DP err(KMAXX);
int nv=y.size();
Vec_DP yerr(nv),ysav(nv),yseq(nv);
x_p=new Vec_DP(KMAXX);
d_p=new Mat_DP(nv,KMAXX);
if (eps != epsold) {
hnext = xnew = -1.0e29;
eps1=SAFE1*eps;
a[0]=nseq[0]+1;
for (k=0;k<KMAXX;k++) a[k+1]=a[k]+nseq[k+1];
for (iq=1;iq<KMAXX;iq++) {
for (k=0;k<iq;k++)
alf[k][iq]=pow(eps1,(a[k+1]-a[iq+1])/
((a[iq+1]-a[0]+1.0)*(2*k+3)));
}
epsold=eps;
for (kopt=1;kopt<KMAXX-1;kopt++)
if (a[kopt+1] > a[kopt]*alf[kopt-1][kopt]) break;
kmax=kopt;
}
h=htry;
for (i=0;i<nv;i++) ysav[i]=y[i];
if (xx != xnew || h != hnext) {
first=1;
kopt=kmax;
}
reduct=0;
for (;;) {
for (k=0;k<=kmax;k++) {
xnew=xx+h;
if (xnew == xx) nrerror("step size underflow in bsstep");
mmid(ysav,dydx,xx,h,nseq[k],yseq,derivs);
xest=SQR(h/nseq[k]);
pzextr(k,xest,yseq,y,yerr);
if (k != 0) {
errmax=TINY;
for (i=0;i<nv;i++) errmax=MAX(errmax,fabs(yerr[i]/yscal[i]));
errmax /= eps;
km=k-1;
err[km]=pow(errmax/SAFE1,1.0/(2*km+3));
}
if (k != 0 && (k >= kopt-1 || first)) {
if (errmax < 1.0) {
exitflag=true;
break;
}
if (k == kmax || k == kopt+1) {
red=SAFE2/err[km];
break;
}
else if (k == kopt && alf[kopt-1][kopt] < err[km]) {
red=1.0/err[km];
break;
}
else if (kopt == kmax && alf[km][kmax-1] < err[km]) {
red=alf[km][kmax-1]*SAFE2/err[km];
break;
}
else if (alf[km][kopt] < err[km]) {
red=alf[km][kopt-1]/err[km];
break;
}
}
}
if (exitflag) break;
red=MIN(red,REDMIN);
red=MAX(red,REDMAX);
h *= red;
reduct=1;
}
xx=xnew;
hdid=h;
first=0;
wrkmin=1.0e35;
for (kk=0;kk<=km;kk++) {
fact=MAX(err[kk],SCALMX);
work=fact*a[kk+1];
if (work < wrkmin) {
scale=fact;
wrkmin=work;
kopt=kk+1;
}
}
hnext=h/scale;
if (kopt >= k && kopt != kmax && !reduct) {
fact=MAX(scale/alf[kopt-1][kopt],SCALMX);
if (a[kopt+1]*fact <= wrkmin) {
hnext=h/fact;
kopt++;
}
}
delete d_p;
delete x_p;
}

View File

@@ -0,0 +1,28 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::caldat(const int julian, int &mm, int &id, int &iyyy)
{
const int IGREG=2299161;
int ja,jalpha,jb,jc,jd,je;
if (julian >= IGREG) {
jalpha=int((DP(julian-1867216)-0.25)/36524.25);
ja=julian+1+jalpha-int(0.25*jalpha);
} else if (julian < 0) {
ja=julian+36525*(1-julian/36525);
} else
ja=julian;
jb=ja+1524;
jc=int(6680.0+(DP(jb-2439870)-122.1)/365.25);
jd=int(365*jc+(0.25*jc));
je=int((jb-jd)/30.6001);
id=jb-jd-int(30.6001*je);
mm=je-1;
if (mm > 12) mm -= 12;
iyyy=jc-4715;
if (mm > 2) --iyyy;
if (iyyy <= 0) --iyyy;
if (julian < 0) iyyy -= 100*(1-julian/36525);
}

View File

@@ -0,0 +1,15 @@
#include "nr.h"
void NR::chder(const DP a, const DP b, Vec_I_DP &c, Vec_O_DP &cder, const int n)
{
int j;
DP con;
cder[n-1]=0.0;
cder[n-2]=2*(n-1)*c[n-1];
for (j=n-2;j>0;j--)
cder[j-1]=cder[j+1]+2*j*c[j];
con=2.0/(b-a);
for (j=0;j<n;j++)
cder[j] *= con;
}

View File

@@ -0,0 +1,17 @@
#include "nr.h"
DP NR::chebev(const DP a, const DP b, Vec_I_DP &c, const int m, const DP x)
{
DP d=0.0,dd=0.0,sv,y,y2;
int j;
if ((x-a)*(x-b) > 0.0)
nrerror("x not in range in routine chebev");
y2=2.0*(y=(2.0*x-a-b)/(b-a));
for (j=m-1;j>0;j--) {
sv=d;
d=y2*d-dd+c[j];
dd=sv;
}
return y*d-dd+0.5*c[0];
}

View File

@@ -0,0 +1,26 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::chebft(const DP a, const DP b, Vec_O_DP &c, DP func(const DP))
{
const DP PI=3.141592653589793;
int k,j;
DP fac,bpa,bma,y,sum;
int n=c.size();
Vec_DP f(n);
bma=0.5*(b-a);
bpa=0.5*(b+a);
for (k=0;k<n;k++) {
y=cos(PI*(k+0.5)/n);
f[k]=func(y*bma+bpa);
}
fac=2.0/n;
for (j=0;j<n;j++) {
sum=0.0;
for (k=0;k<n;k++)
sum += f[k]*cos(PI*j*(k+0.5)/n);
c[j]=fac*sum;
}
}

View File

@@ -0,0 +1,25 @@
#include "nr.h"
void NR::chebpc(Vec_I_DP &c, Vec_O_DP &d)
{
int k,j;
DP sv;
int n=c.size();
Vec_DP dd(n);
for (j=0;j<n;j++) d[j]=dd[j]=0.0;
d[0]=c[n-1];
for (j=n-2;j>0;j--) {
for (k=n-j;k>0;k--) {
sv=d[k];
d[k]=2.0*d[k-1]-dd[k];
dd[k]=sv;
}
sv=d[0];
d[0] = -dd[0]+c[j];
dd[0]=sv;
}
for (j=n-1;j>0;j--)
d[j]=d[j-1]-dd[j];
d[0] = -dd[0]+0.5*c[0];
}

View File

@@ -0,0 +1,17 @@
#include "nr.h"
void NR::chint(const DP a, const DP b, Vec_I_DP &c, Vec_O_DP &cint, const int n)
{
int j;
DP sum=0.0,fac=1.0,con;
con=0.25*(b-a);
for (j=1;j<n-1;j++) {
cint[j]=con*(c[j-1]-c[j+1])/j;
sum += fac*cint[j];
fac = -fac;
}
cint[n-1]=con*c[n-2]/(n-1);
sum += fac*cint[n-1];
cint[0]=2.0*sum;
}

View File

@@ -0,0 +1,30 @@
#include <cmath>
#include "nr.h"
using namespace std;
extern Vec_DP *xx_p,*yy_p,*sx_p,*sy_p,*ww_p;
extern DP aa,offs;
DP NR::chixy(const DP bang)
{
const DP BIG=1.0e30;
int j;
DP ans,avex=0.0,avey=0.0,sumw=0.0,b;
Vec_DP &xx=*xx_p, &yy=*yy_p;
Vec_DP &sx=*sx_p, &sy=*sy_p, &ww=*ww_p;
int nn=xx.size();
b=tan(bang);
for (j=0;j<nn;j++) {
ww[j] = SQR(b*sx[j])+SQR(sy[j]);
sumw += (ww[j]=(ww[j] < 1.0/BIG ? BIG : 1.0/ww[j]));
avex += ww[j]*xx[j];
avey += ww[j]*yy[j];
}
avex /= sumw;
avey /= sumw;
aa=avey-b*avex;
for (ans = -offs,j=0;j<nn;j++)
ans += ww[j]*SQR(yy[j]-aa-b*xx[j]);
return ans;
}

View File

@@ -0,0 +1,21 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::choldc(Mat_IO_DP &a, Vec_O_DP &p)
{
int i,j,k;
DP sum;
int n=a.nrows();
for (i=0;i<n;i++) {
for (j=i;j<n;j++) {
for (sum=a[i][j],k=i-1;k>=0;k--) sum -= a[i][k]*a[j][k];
if (i == j) {
if (sum <= 0.0)
nrerror("choldc failed");
p[i]=sqrt(sum);
} else a[j][i]=sum/p[i];
}
}
}

View File

@@ -0,0 +1,17 @@
#include "nr.h"
void NR::cholsl(Mat_I_DP &a, Vec_I_DP &p, Vec_I_DP &b, Vec_O_DP &x)
{
int i,k;
DP sum;
int n=a.nrows();
for (i=0;i<n;i++) {
for (sum=b[i],k=i-1;k>=0;k--) sum -= a[i][k]*x[k];
x[i]=sum/p[i];
}
for (i=n-1;i>=0;i--) {
for (sum=x[i],k=i+1;k<n;k++) sum -= a[k][i]*x[k];
x[i]=sum/p[i];
}
}

View File

@@ -0,0 +1,18 @@
#include "nr.h"
void NR::chsone(Vec_I_DP &bins, Vec_I_DP &ebins, const int knstrn, DP &df,
DP &chsq, DP &prob)
{
int j;
DP temp;
int nbins=bins.size();
df=nbins-knstrn;
chsq=0.0;
for (j=0;j<nbins;j++) {
if (ebins[j] <= 0.0) nrerror("Bad expected number in chsone");
temp=bins[j]-ebins[j];
chsq += temp*temp/ebins[j];
}
prob=gammq(0.5*df,0.5*chsq);
}

View File

@@ -0,0 +1,20 @@
#include "nr.h"
void NR::chstwo(Vec_I_DP &bins1, Vec_I_DP &bins2, const int knstrn, DP &df,
DP &chsq, DP &prob)
{
int j;
DP temp;
int nbins=bins1.size();
df=nbins-knstrn;
chsq=0.0;
for (j=0;j<nbins;j++)
if (bins1[j] == 0.0 && bins2[j] == 0.0)
--df;
else {
temp=bins1[j]-bins2[j];
chsq += temp*temp/(bins1[j]+bins2[j]);
}
prob=gammq(0.5*df,0.5*chsq);
}

View File

@@ -0,0 +1,69 @@
#include <cmath>
#include <complex>
#include <limits>
#include "nr.h"
using namespace std;
void NR::cisi(const DP x, complex<DP> &cs)
{
const int MAXIT=100;
const DP EULER=0.577215664901533, PIBY2=1.570796326794897, TMIN=2.0;
const DP EPS=numeric_limits<DP>::epsilon();
const DP FPMIN=numeric_limits<DP>::min()*4.0;
const DP BIG=numeric_limits<DP>::max()*EPS;
int i,k;
bool odd;
DP a,err,fact,sign,sum,sumc,sums,t,term;
complex<DP> h,b,c,d,del;
t=fabs(x);
if (t == 0.0) {
cs= -BIG;
return;
}
if (t > TMIN) {
b=complex<DP>(1.0,t);
c=complex<DP>(BIG,0.0);
d=h=1.0/b;
for (i=1;i<MAXIT;i++) {
a= -i*i;
b += 2.0;
d=1.0/(a*d+b);
c=b+a/c;
del=c*d;
h *= del;
if (fabs(real(del)-1.0)+fabs(imag(del)) <= EPS) break;
}
if (i >= MAXIT) nrerror("cf failed in cisi");
h=complex<DP>(cos(t),-sin(t))*h;
cs= -conj(h)+complex<DP>(0.0,PIBY2);
} else {
if (t < sqrt(FPMIN)) {
sumc=0.0;
sums=t;
} else {
sum=sums=sumc=0.0;
sign=fact=1.0;
odd=true;
for (k=1;k<=MAXIT;k++) {
fact *= t/k;
term=fact/k;
sum += sign*term;
err=term/fabs(sum);
if (odd) {
sign = -sign;
sums=sum;
sum=sumc;
} else {
sumc=sum;
sum=sums;
}
if (err < EPS) break;
odd=!odd;
}
if (k > MAXIT) nrerror("maxits exceeded in cisi");
}
cs=complex<DP>(sumc+log(t)+EULER,sums);
}
if (x < 0.0) cs = conj(cs);
}

View File

@@ -0,0 +1,42 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::cntab1(Mat_I_INT &nn, DP &chisq, DP &df, DP &prob, DP &cramrv, DP &ccc)
{
const DP TINY=1.0e-30;
int i,j,nnj,nni,minij;
DP sum=0.0,expctd,temp;
int ni=nn.nrows();
int nj=nn.ncols();
Vec_DP sumi(ni),sumj(nj);
nni=ni;
nnj=nj;
for (i=0;i<ni;i++) {
sumi[i]=0.0;
for (j=0;j<nj;j++) {
sumi[i] += nn[i][j];
sum += nn[i][j];
}
if (sumi[i] == 0.0) --nni;
}
for (j=0;j<nj;j++) {
sumj[j]=0.0;
for (i=0;i<ni;i++) sumj[j] += nn[i][j];
if (sumj[j] == 0.0) --nnj;
}
df=nni*nnj-nni-nnj+1;
chisq=0.0;
for (i=0;i<ni;i++) {
for (j=0;j<nj;j++) {
expctd=sumj[j]*sumi[i]/sum;
temp=nn[i][j]-expctd;
chisq += temp*temp/(expctd+TINY);
}
}
prob=gammq(0.5*df,0.5*chisq);
minij = nni < nnj ? nni-1 : nnj-1;
cramrv=sqrt(chisq/(sum*minij));
ccc=sqrt(chisq/(chisq+sum));
}

View File

@@ -0,0 +1,51 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::cntab2(Mat_I_INT &nn, DP &h, DP &hx, DP &hy, DP &hygx, DP &hxgy,
DP &uygx, DP &uxgy, DP &uxy)
{
const DP TINY=1.0e-30;
int i,j;
DP sum=0.0,p;
int ni=nn.nrows();
int nj=nn.ncols();
Vec_DP sumi(ni),sumj(nj);
for (i=0;i<ni;i++) {
sumi[i]=0.0;
for (j=0;j<nj;j++) {
sumi[i] += nn[i][j];
sum += nn[i][j];
}
}
for (j=0;j<nj;j++) {
sumj[j]=0.0;
for (i=0;i<ni;i++)
sumj[j] += nn[i][j];
}
hx=0.0;
for (i=0;i<ni;i++)
if (sumi[i] != 0.0) {
p=sumi[i]/sum;
hx -= p*log(p);
}
hy=0.0;
for (j=0;j<nj;j++)
if (sumj[j] != 0.0) {
p=sumj[j]/sum;
hy -= p*log(p);
}
h=0.0;
for (i=0;i<ni;i++)
for (j=0;j<nj;j++)
if (nn[i][j] != 0) {
p=nn[i][j]/sum;
h -= p*log(p);
}
hygx=h-hx;
hxgy=h-hy;
uygx=(hy-hygx)/(hy+TINY);
uxgy=(hx-hxgy)/(hx+TINY);
uxy=2.0*(hx+hy-h)/(hx+hy+TINY);
}

View File

@@ -0,0 +1,46 @@
#include "nr.h"
void NR::convlv(Vec_I_DP &data, Vec_I_DP &respns, const int isign,
Vec_O_DP &ans)
{
int i,no2;
DP mag2,tmp;
int n=data.size();
int m=respns.size();
Vec_DP temp(n);
temp[0]=respns[0];
for (i=1;i<(m+1)/2;i++) {
temp[i]=respns[i];
temp[n-i]=respns[m-i];
}
for (i=(m+1)/2;i<n-(m-1)/2;i++)
temp[i]=0.0;
for (i=0;i<n;i++)
ans[i]=data[i];
realft(ans,1);
realft(temp,1);
no2=n>>1;
if (isign == 1) {
for (i=2;i<n;i+=2) {
tmp=ans[i];
ans[i]=(ans[i]*temp[i]-ans[i+1]*temp[i+1])/no2;
ans[i+1]=(ans[i+1]*temp[i]+tmp*temp[i+1])/no2;
}
ans[0]=ans[0]*temp[0]/no2;
ans[1]=ans[1]*temp[1]/no2;
} else if (isign == -1) {
for (i=2;i<n;i+=2) {
if ((mag2=SQR(temp[i])+SQR(temp[i+1])) == 0.0)
nrerror("Deconvolving at response zero in convlv");
tmp=ans[i];
ans[i]=(ans[i]*temp[i]+ans[i+1]*temp[i+1])/mag2/no2;
ans[i+1]=(ans[i+1]*temp[i]-tmp*temp[i+1])/mag2/no2;
}
if (temp[0] == 0.0 || temp[1] == 0.0)
nrerror("Deconvolving at response zero in convlv");
ans[0]=ans[0]/temp[0]/no2;
ans[1]=ans[1]/temp[1]/no2;
} else nrerror("No meaning for isign in convlv");
realft(ans,-1);
}

View File

@@ -0,0 +1,12 @@
#include "nr.h"
void NR::copy(Mat_O_DP &aout, Mat_I_DP &ain)
{
int i,j;
int n=ain.nrows();
for (i=0;i<n;i++)
for (j=0;j<n;j++)
aout[j][i]=ain[j][i];
}

View File

@@ -0,0 +1,25 @@
#include "nr.h"
void NR::correl(Vec_I_DP &data1, Vec_I_DP &data2, Vec_O_DP &ans)
{
int no2,i;
DP tmp;
int n=data1.size();
Vec_DP temp(n);
for (i=0;i<n;i++) {
ans[i]=data1[i];
temp[i]=data2[i];
}
realft(ans,1);
realft(temp,1);
no2=n>>1;
for (i=2;i<n;i+=2) {
tmp=ans[i];
ans[i]=(ans[i]*temp[i]+ans[i+1]*temp[i+1])/no2;
ans[i+1]=(ans[i+1]*temp[i]-tmp*temp[i+1])/no2;
}
ans[0]=ans[0]*temp[0]/no2;
ans[1]=ans[1]*temp[1]/no2;
realft(ans,-1);
}

View File

@@ -0,0 +1,37 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::cosft1(Vec_IO_DP &y)
{
const DP PI=3.141592653589793238;
int j;
DP sum,y1,y2,theta,wi=0.0,wpi,wpr,wr=1.0,wtemp;
int n=y.size()-1;
Vec_DP yy(n);
theta=PI/n;
wtemp=sin(0.5*theta);
wpr = -2.0*wtemp*wtemp;
wpi=sin(theta);
sum=0.5*(y[0]-y[n]);
yy[0]=0.5*(y[0]+y[n]);
for (j=1;j<n/2;j++) {
wr=(wtemp=wr)*wpr-wi*wpi+wr;
wi=wi*wpr+wtemp*wpi+wi;
y1=0.5*(y[j]+y[n-j]);
y2=(y[j]-y[n-j]);
yy[j]=y1-wi*y2;
yy[n-j]=y1+wi*y2;
sum += wr*y2;
}
yy[n/2]=y[n/2];
realft(yy,1);
for (j=0;j<n;j++) y[j]=yy[j];
y[n]=y[1];
y[1]=sum;
for (j=3;j<n;j+=2) {
sum += y[j];
y[j]=sum;
}
}

View File

@@ -0,0 +1,64 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::cosft2(Vec_IO_DP &y, const int isign)
{
const DP PI=3.141592653589793238;
int i;
DP sum,sum1,y1,y2,ytemp,theta,wi=0.0,wi1,wpi,wpr,wr=1.0,wr1,wtemp;
int n=y.size();
theta=0.5*PI/n;
wr1=cos(theta);
wi1=sin(theta);
wpr = -2.0*wi1*wi1;
wpi=sin(2.0*theta);
if (isign == 1) {
for (i=0;i<n/2;i++) {
y1=0.5*(y[i]+y[n-1-i]);
y2=wi1*(y[i]-y[n-1-i]);
y[i]=y1+y2;
y[n-1-i]=y1-y2;
wr1=(wtemp=wr1)*wpr-wi1*wpi+wr1;
wi1=wi1*wpr+wtemp*wpi+wi1;
}
realft(y,1);
for (i=2;i<n;i+=2) {
wr=(wtemp=wr)*wpr-wi*wpi+wr;
wi=wi*wpr+wtemp*wpi+wi;
y1=y[i]*wr-y[i+1]*wi;
y2=y[i+1]*wr+y[i]*wi;
y[i]=y1;
y[i+1]=y2;
}
sum=0.5*y[1];
for (i=n-1;i>0;i-=2) {
sum1=sum;
sum += y[i];
y[i]=sum1;
}
} else if (isign == -1) {
ytemp=y[n-1];
for (i=n-1;i>2;i-=2)
y[i]=y[i-2]-y[i];
y[1]=2.0*ytemp;
for (i=2;i<n;i+=2) {
wr=(wtemp=wr)*wpr-wi*wpi+wr;
wi=wi*wpr+wtemp*wpi+wi;
y1=y[i]*wr+y[i+1]*wi;
y2=y[i+1]*wr-y[i]*wi;
y[i]=y1;
y[i+1]=y2;
}
realft(y,-1);
for (i=0;i<n/2;i++) {
y1=y[i]+y[n-1-i];
y2=(0.5/wi1)*(y[i]-y[n-1-i]);
y[i]=0.5*(y1+y2);
y[n-1-i]=0.5*(y1-y2);
wr1=(wtemp=wr1)*wpr-wi1*wpi+wr1;
wi1=wi1*wpr+wtemp*wpi+wi1;
}
}
}

View File

@@ -0,0 +1,18 @@
#include "nr.h"
void NR::covsrt(Mat_IO_DP &covar, Vec_I_BOOL &ia, const int mfit)
{
int i,j,k;
int ma=ia.size();
for (i=mfit;i<ma;i++)
for (j=0;j<i+1;j++) covar[i][j]=covar[j][i]=0.0;
k=mfit-1;
for (j=ma-1;j>=0;j--) {
if (ia[j]) {
for (i=0;i<ma;i++) SWAP(covar[i][k],covar[i][j]);
for (i=0;i<ma;i++) SWAP(covar[k][i],covar[j][i]);
k--;
}
}
}

View File

@@ -0,0 +1,25 @@
#include "nr.h"
void NR::crank(Vec_IO_DP &w, DP &s)
{
int j=1,ji,jt;
DP t,rank;
int n=w.size();
s=0.0;
while (j < n) {
if (w[j] != w[j-1]) {
w[j-1]=j;
++j;
} else {
for (jt=j+1;jt<=n && w[jt-1]==w[j-1];jt++);
rank=0.5*(j+jt-1);
for (ji=j;ji<=(jt-1);ji++)
w[ji-1]=rank;
t=jt-j;
s += (t*t*t-t);
j=jt;
}
}
if (j == n) w[n-1]=n;
}

View File

@@ -0,0 +1,24 @@
#include "nr.h"
void NR::cyclic(Vec_I_DP &a, Vec_I_DP &b, Vec_I_DP &c, const DP alpha,
const DP beta, Vec_I_DP &r, Vec_O_DP &x)
{
int i;
DP fact,gamma;
int n=a.size();
if (n <= 2) nrerror("n too small in cyclic");
Vec_DP bb(n),u(n),z(n);
gamma = -b[0];
bb[0]=b[0]-gamma;
bb[n-1]=b[n-1]-alpha*beta/gamma;
for (i=1;i<n-1;i++) bb[i]=b[i];
tridag(a,bb,c,r,x);
u[0]=gamma;
u[n-1]=alpha;
for (i=1;i<n-1;i++) u[i]=0.0;
tridag(a,bb,c,u,z);
fact=(x[0]+beta*x[n-1]/gamma)/
(1.0+z[0]+beta*z[n-1]/gamma);
for (i=0;i<n;i++) x[i] -= fact*z[i];
}

View File

@@ -0,0 +1,28 @@
#include "nr.h"
void NR::daub4(Vec_IO_DP &a, const int n, const int isign)
{
const DP C0=0.4829629131445341,C1=0.8365163037378079,
C2=0.2241438680420134,C3=-0.1294095225512604;
int nh,i,j;
if (n < 4) return;
Vec_DP wksp(n);
nh=n >> 1;
if (isign >= 0) {
for (i=0,j=0;j<n-3;j+=2,i++) {
wksp[i]=C0*a[j]+C1*a[j+1]+C2*a[j+2]+C3*a[j+3];
wksp[i+nh]=C3*a[j]-C2*a[j+1]+C1*a[j+2]-C0*a[j+3];
}
wksp[i]=C0*a[n-2]+C1*a[n-1]+C2*a[0]+C3*a[1];
wksp[i+nh]=C3*a[n-2]-C2*a[n-1]+C1*a[0]-C0*a[1];
} else {
wksp[0]=C2*a[nh-1]+C1*a[n-1]+C0*a[0]+C3*a[nh];
wksp[1]=C3*a[nh-1]-C0*a[n-1]+C1*a[0]-C2*a[nh];
for (i=0,j=2;i<nh-1;i++) {
wksp[j++]=C2*a[i]+C1*a[i+nh]+C0*a[i+1]+C3*a[i+nh+1];
wksp[j++]=C3*a[i]-C0*a[i+nh]+C1*a[i+1]-C2*a[i+nh+1];
}
}
for (i=0;i<n;i++) a[i]=wksp[i];
}

View File

@@ -0,0 +1,35 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::dawson(const DP x)
{
const int NMAX=6;
const DP H=0.4, A1=2.0/3.0, A2=0.4, A3=2.0/7.0;
int i,n0;
static bool init = true;
DP d1,d2,e1,e2,sum,x2,xp,xx,ans;
static Vec_DP c(NMAX);
if (init) {
init=false;
for (i=0;i<NMAX;i++) c[i]=exp(-SQR((2.0*i+1.0)*H));
}
if (fabs(x) < 0.2) {
x2=x*x;
ans=x*(1.0-A1*x2*(1.0-A2*x2*(1.0-A3*x2)));
} else {
xx=fabs(x);
n0=2*int(0.5*xx/H+0.5);
xp=xx-n0*H;
e1=exp(2.0*xp*H);
e2=e1*e1;
d1=n0+1;
d2=d1-2.0;
sum=0.0;
for (i=0;i<NMAX;i++,d1+=2.0,d2-=2.0,e1*=e2)
sum += c[i]*(e1/d1+1.0/(d2*e1));
ans=0.5641895835*SIGN(exp(-xp*xp),x)*sum;
}
return ans;
}

View File

@@ -0,0 +1,97 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
namespace {
inline void mov3(DP &a, DP &b, DP &c, const DP d, const DP e,
const DP f)
{
a=d; b=e; c=f;
}
}
DP NR::dbrent(const DP ax, const DP bx, const DP cx, DP f(const DP),
DP df(const DP), const DP tol, DP &xmin)
{
const int ITMAX=100;
const DP ZEPS=numeric_limits<DP>::epsilon()*1.0e-3;
bool ok1,ok2;
int iter;
DP a,b,d=0.0,d1,d2,du,dv,dw,dx,e=0.0;
DP fu,fv,fw,fx,olde,tol1,tol2,u,u1,u2,v,w,x,xm;
a=(ax < cx ? ax : cx);
b=(ax > cx ? ax : cx);
x=w=v=bx;
fw=fv=fx=f(x);
dw=dv=dx=df(x);
for (iter=0;iter<ITMAX;iter++) {
xm=0.5*(a+b);
tol1=tol*fabs(x)+ZEPS;
tol2=2.0*tol1;
if (fabs(x-xm) <= (tol2-0.5*(b-a))) {
xmin=x;
return fx;
}
if (fabs(e) > tol1) {
d1=2.0*(b-a);
d2=d1;
if (dw != dx) d1=(w-x)*dx/(dx-dw);
if (dv != dx) d2=(v-x)*dx/(dx-dv);
u1=x+d1;
u2=x+d2;
ok1 = (a-u1)*(u1-b) > 0.0 && dx*d1 <= 0.0;
ok2 = (a-u2)*(u2-b) > 0.0 && dx*d2 <= 0.0;
olde=e;
e=d;
if (ok1 || ok2) {
if (ok1 && ok2)
d=(fabs(d1) < fabs(d2) ? d1 : d2);
else if (ok1)
d=d1;
else
d=d2;
if (fabs(d) <= fabs(0.5*olde)) {
u=x+d;
if (u-a < tol2 || b-u < tol2)
d=SIGN(tol1,xm-x);
} else {
d=0.5*(e=(dx >= 0.0 ? a-x : b-x));
}
} else {
d=0.5*(e=(dx >= 0.0 ? a-x : b-x));
}
} else {
d=0.5*(e=(dx >= 0.0 ? a-x : b-x));
}
if (fabs(d) >= tol1) {
u=x+d;
fu=f(u);
} else {
u=x+SIGN(tol1,d);
fu=f(u);
if (fu > fx) {
xmin=x;
return fx;
}
}
du=df(u);
if (fu <= fx) {
if (u >= x) a=x; else b=x;
mov3(v,fv,dv,w,fw,dw);
mov3(w,fw,dw,x,fx,dx);
mov3(x,fx,dx,u,fu,du);
} else {
if (u < x) a=u; else b=u;
if (fu <= fw || w == x) {
mov3(v,fv,dv,w,fw,dw);
mov3(w,fw,dw,u,fu,du);
} else if (fu < fv || v == x || v == w) {
mov3(v,fv,dv,u,fu,du);
}
}
}
nrerror("Too many iterations in routine dbrent");
return 0.0;
}

View File

@@ -0,0 +1,22 @@
#include "nr.h"
void NR::ddpoly(Vec_I_DP &c, const DP x, Vec_O_DP &pd)
{
int nnd,j,i;
DP cnst=1.0;
int nc=c.size()-1;
int nd=pd.size()-1;
pd[0]=c[nc];
for (j=1;j<nd+1;j++) pd[j]=0.0;
for (i=nc-1;i>=0;i--) {
nnd=(nd < (nc-i) ? nd : nc-i);
for (j=nnd;j>0;j--)
pd[j]=pd[j]*x+pd[j-1];
pd[0]=pd[0]*x+c[i];
}
for (i=2;i<nd+1;i++) {
cnst *= i;
pd[i] *= cnst;
}
}

View File

@@ -0,0 +1,26 @@
#include "nr.h"
bool NR::decchk(string str, char &ch)
{
char c;
int j,k=0,m=0;
static int ip[10][8]={{0,1,5,8,9,4,2,7},{1,5,8,9,4,2,7,0},
{2,7,0,1,5,8,9,4},{3,6,3,6,3,6,3,6},{4,2,7,0,1,5,8,9},
{5,8,9,4,2,7,0,1},{6,3,6,3,6,3,6,3},{7,0,1,5,8,9,4,2},
{8,9,4,2,7,0,1,5},{9,4,2,7,0,1,5,8}};
static int ij[10][10]={{0,1,2,3,4,5,6,7,8,9},{1,2,3,4,0,6,7,8,9,5},
{2,3,4,0,1,7,8,9,5,6},{3,4,0,1,2,8,9,5,6,7},{4,0,1,2,3,9,5,6,7,8},
{5,9,8,7,6,0,4,3,2,1},{6,5,9,8,7,1,0,4,3,2},{7,6,5,9,8,2,1,0,4,3},
{8,7,6,5,9,3,2,1,0,4},{9,8,7,6,5,4,3,2,1,0}};
int n=str.length();
for (j=0;j<n;j++) {
c=str[j];
if (c >= 48 && c <= 57)
k=ij[k][ip[(c+2) % 10][7 & m++]];
}
for (j=0;j<10;j++)
if (ij[k][ip[j][m & 7]] == 0) break;
ch=char(j+48);
return k==0;
}

View File

@@ -0,0 +1,19 @@
#include "nr.h"
extern int ncom;
extern DP (*nrfunc)(Vec_I_DP &);
extern void (*nrdfun)(Vec_I_DP &, Vec_O_DP &);
extern Vec_DP *pcom_p,*xicom_p;
DP NR::df1dim(const DP x)
{
int j;
DP df1=0.0;
Vec_DP xt(ncom),df(ncom);
Vec_DP &pcom=*pcom_p,&xicom=*xicom_p;
for (j=0;j<ncom;j++) xt[j]=pcom[j]+x*xicom[j];
nrdfun(xt,df);
for (j=0;j<ncom;j++) df1 += df[j]*xicom[j];
return df1;
}

View File

@@ -0,0 +1,83 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
void NR::dfpmin(Vec_IO_DP &p, const DP gtol, int &iter, DP &fret,
DP func(Vec_I_DP &), void dfunc(Vec_I_DP &, Vec_O_DP &))
{
const int ITMAX=200;
const DP EPS=numeric_limits<DP>::epsilon();
const DP TOLX=4*EPS,STPMX=100.0;
bool check;
int i,its,j;
DP den,fac,fad,fae,fp,stpmax,sum=0.0,sumdg,sumxi,temp,test;
int n=p.size();
Vec_DP dg(n),g(n),hdg(n),pnew(n),xi(n);
Mat_DP hessin(n,n);
fp=func(p);
dfunc(p,g);
for (i=0;i<n;i++) {
for (j=0;j<n;j++) hessin[i][j]=0.0;
hessin[i][i]=1.0;
xi[i] = -g[i];
sum += p[i]*p[i];
}
stpmax=STPMX*MAX(sqrt(sum),DP(n));
for (its=0;its<ITMAX;its++) {
iter=its;
lnsrch(p,fp,g,xi,pnew,fret,stpmax,check,func);
fp=fret;
for (i=0;i<n;i++) {
xi[i]=pnew[i]-p[i];
p[i]=pnew[i];
}
test=0.0;
for (i=0;i<n;i++) {
temp=fabs(xi[i])/MAX(fabs(p[i]),1.0);
if (temp > test) test=temp;
}
if (test < TOLX)
return;
for (i=0;i<n;i++) dg[i]=g[i];
dfunc(p,g);
test=0.0;
den=MAX(fret,1.0);
for (i=0;i<n;i++) {
temp=fabs(g[i])*MAX(fabs(p[i]),1.0)/den;
if (temp > test) test=temp;
}
if (test < gtol)
return;
for (i=0;i<n;i++) dg[i]=g[i]-dg[i];
for (i=0;i<n;i++) {
hdg[i]=0.0;
for (j=0;j<n;j++) hdg[i] += hessin[i][j]*dg[j];
}
fac=fae=sumdg=sumxi=0.0;
for (i=0;i<n;i++) {
fac += dg[i]*xi[i];
fae += dg[i]*hdg[i];
sumdg += SQR(dg[i]);
sumxi += SQR(xi[i]);
}
if (fac > sqrt(EPS*sumdg*sumxi)) {
fac=1.0/fac;
fad=1.0/fae;
for (i=0;i<n;i++) dg[i]=fac*xi[i]-fad*hdg[i];
for (i=0;i<n;i++) {
for (j=i;j<n;j++) {
hessin[i][j] += fac*xi[i]*xi[j]
-fad*hdg[i]*hdg[j]+fae*dg[i]*dg[j];
hessin[j][i]=hessin[i][j];
}
}
}
for (i=0;i<n;i++) {
xi[i]=0.0;
for (j=0;j<n;j++) xi[i] -= hessin[i][j]*g[j];
}
}
nrerror("too many iterations in dfpmin");
}

View File

@@ -0,0 +1,36 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
DP NR::dfridr(DP func(const DP), const DP x, const DP h, DP &err)
{
const int NTAB=10;
const DP CON=1.4, CON2=(CON*CON);
const DP BIG=numeric_limits<DP>::max();
const DP SAFE=2.0;
int i,j;
DP errt,fac,hh,ans;
Mat_DP a(NTAB,NTAB);
if (h == 0.0) nrerror("h must be nonzero in dfridr.");
hh=h;
a[0][0]=(func(x+hh)-func(x-hh))/(2.0*hh);
err=BIG;
for (i=1;i<NTAB;i++) {
hh /= CON;
a[0][i]=(func(x+hh)-func(x-hh))/(2.0*hh);
fac=CON2;
for (j=1;j<=i;j++) {
a[j][i]=(a[j-1][i]*fac-a[j-1][i-1])/(fac-1.0);
fac=CON2*fac;
errt=MAX(fabs(a[j][i]-a[j-1][i]),fabs(a[j][i]-a[j-1][i-1]));
if (errt <= err) {
err=errt;
ans=a[j][i];
}
}
if (fabs(a[i][i]-a[i-1][i-1]) >= SAFE*err) break;
}
return ans;
}

View File

@@ -0,0 +1,58 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::dftcor(const DP w, const DP delta, const DP a, const DP b,
Vec_I_DP &endpts, DP &corre, DP &corim, DP &corfac)
{
DP a0i,a0r,a1i,a1r,a2i,a2r,a3i,a3r,arg,c,cl,cr,s,sl,sr,t,t2,t4,t6,
cth,ctth,spth2,sth,sth4i,stth,th,th2,th4,tmth2,tth4i;
th=w*delta;
if (a >= b || th < 0.0e0 || th > 3.1416e0)
nrerror("bad arguments to dftcor");
if (fabs(th) < 5.0e-2) {
t=th;
t2=t*t;
t4=t2*t2;
t6=t4*t2;
corfac=1.0-(11.0/720.0)*t4+(23.0/15120.0)*t6;
a0r=(-2.0/3.0)+t2/45.0+(103.0/15120.0)*t4-(169.0/226800.0)*t6;
a1r=(7.0/24.0)-(7.0/180.0)*t2+(5.0/3456.0)*t4-(7.0/259200.0)*t6;
a2r=(-1.0/6.0)+t2/45.0-(5.0/6048.0)*t4+t6/64800.0;
a3r=(1.0/24.0)-t2/180.0+(5.0/24192.0)*t4-t6/259200.0;
a0i=t*(2.0/45.0+(2.0/105.0)*t2-(8.0/2835.0)*t4+(86.0/467775.0)*t6);
a1i=t*(7.0/72.0-t2/168.0+(11.0/72576.0)*t4-(13.0/5987520.0)*t6);
a2i=t*(-7.0/90.0+t2/210.0-(11.0/90720.0)*t4+(13.0/7484400.0)*t6);
a3i=t*(7.0/360.0-t2/840.0+(11.0/362880.0)*t4-(13.0/29937600.0)*t6);
} else {
cth=cos(th);
sth=sin(th);
ctth=cth*cth-sth*sth;
stth=2.0e0*sth*cth;
th2=th*th;
th4=th2*th2;
tmth2=3.0e0-th2;
spth2=6.0e0+th2;
sth4i=1.0/(6.0e0*th4);
tth4i=2.0e0*sth4i;
corfac=tth4i*spth2*(3.0e0-4.0e0*cth+ctth);
a0r=sth4i*(-42.0e0+5.0e0*th2+spth2*(8.0e0*cth-ctth));
a0i=sth4i*(th*(-12.0e0+6.0e0*th2)+spth2*stth);
a1r=sth4i*(14.0e0*tmth2-7.0e0*spth2*cth);
a1i=sth4i*(30.0e0*th-5.0e0*spth2*sth);
a2r=tth4i*(-4.0e0*tmth2+2.0e0*spth2*cth);
a2i=tth4i*(-12.0e0*th+2.0e0*spth2*sth);
a3r=sth4i*(2.0e0*tmth2-spth2*cth);
a3i=sth4i*(6.0e0*th-spth2*sth);
}
cl=a0r*endpts[0]+a1r*endpts[1]+a2r*endpts[2]+a3r*endpts[3];
sl=a0i*endpts[0]+a1i*endpts[1]+a2i*endpts[2]+a3i*endpts[3];
cr=a0r*endpts[7]+a1r*endpts[6]+a2r*endpts[5]+a3r*endpts[4];
sr= -a0i*endpts[7]-a1i*endpts[6]-a2i*endpts[5]-a3i*endpts[4];
arg=w*(b-a);
c=cos(arg);
s=sin(arg);
corre=cl+c*cr-s*sr;
corim=sl+s*cr+c*sr;
}

View File

@@ -0,0 +1,53 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::dftint(DP func(const DP), const DP a, const DP b, const DP w,
DP &cosint, DP &sinint)
{
static int init=0;
static DP (*funcold)(const DP);
static DP aold = -1.e30,bold = -1.e30,delta;
const int M=64,NDFT=1024,MPOL=6;
const DP TWOPI=6.283185307179586476;
int j,nn;
DP c,cdft,cerr,corfac,corim,corre,en,s,sdft,serr;
static Vec_DP data(NDFT),endpts(8);
Vec_DP cpol(MPOL),spol(MPOL),xpol(MPOL);
if (init != 1 || a != aold || b != bold || func != funcold) {
init=1;
aold=a;
bold=b;
funcold=func;
delta=(b-a)/M;
for (j=0;j<M+1;j++)
data[j]=func(a+j*delta);
for (j=M+1;j<NDFT;j++)
data[j]=0.0;
for (j=0;j<4;j++) {
endpts[j]=data[j];
endpts[j+4]=data[M-3+j];
}
realft(data,1);
data[1]=0.0;
}
en=w*delta*NDFT/TWOPI+1.0;
nn=MIN(MAX(int(en-0.5*MPOL+1.0),1),NDFT/2-MPOL+1);
for (j=0;j<MPOL;j++,nn++) {
cpol[j]=data[2*nn-2];
spol[j]=data[2*nn-1];
xpol[j]=nn;
}
polint(xpol,cpol,en,cdft,cerr);
polint(xpol,spol,en,sdft,serr);
dftcor(w,delta,a,b,endpts,corre,corim,corfac);
cdft *= corfac;
sdft *= corfac;
cdft += corre;
sdft += corim;
c=delta*cos(w*a);
s=delta*sin(w*a);
cosint=c*cdft-s*sdft;
sinint=s*cdft+c*sdft;
}

View File

@@ -0,0 +1,64 @@
#include "nr.h"
extern int mm,n,mpt;
extern DP h,c2,anorm;
extern Vec_DP *x_p;
void NR::difeq(const int k, const int k1, const int k2, const int jsf,
const int is1, const int isf, Vec_I_INT &indexv, Mat_O_DP &s,
Mat_I_DP &y)
{
DP temp,temp1,temp2;
Vec_DP &x=*x_p;
if (k == k1) {
if ((n+mm & 1) != 0) {
s[2][3+indexv[0]]=1.0;
s[2][3+indexv[1]]=0.0;
s[2][3+indexv[2]]=0.0;
s[2][jsf]=y[0][0];
} else {
s[2][3+indexv[0]]=0.0;
s[2][3+indexv[1]]=1.0;
s[2][3+indexv[2]]=0.0;
s[2][jsf]=y[1][0];
}
} else if (k > k2-1) {
s[0][3+indexv[0]] = -(y[2][mpt-1]-c2)/(2.0*(mm+1.0));
s[0][3+indexv[1]]=1.0;
s[0][3+indexv[2]] = -y[0][mpt-1]/(2.0*(mm+1.0));
s[0][jsf]=y[1][mpt-1]-(y[2][mpt-1]-c2)*y[0][mpt-1]/
(2.0*(mm+1.0));
s[1][3+indexv[0]]=1.0;
s[1][3+indexv[1]]=0.0;
s[1][3+indexv[2]]=0.0;
s[1][jsf]=y[0][mpt-1]-anorm;
} else {
s[0][indexv[0]] = -1.0;
s[0][indexv[1]] = -0.5*h;
s[0][indexv[2]]=0.0;
s[0][3+indexv[0]]=1.0;
s[0][3+indexv[1]] = -0.5*h;
s[0][3+indexv[2]]=0.0;
temp1=x[k]+x[k-1];
temp=h/(1.0-temp1*temp1*0.25);
temp2=0.5*(y[2][k]+y[2][k-1])-c2*0.25*temp1*temp1;
s[1][indexv[0]]=temp*temp2*0.5;
s[1][indexv[1]] = -1.0-0.5*temp*(mm+1.0)*temp1;
s[1][indexv[2]]=0.25*temp*(y[0][k]+y[0][k-1]);
s[1][3+indexv[0]]=s[1][indexv[0]];
s[1][3+indexv[1]]=2.0+s[1][indexv[1]];
s[1][3+indexv[2]]=s[1][indexv[2]];
s[2][indexv[0]]=0.0;
s[2][indexv[1]]=0.0;
s[2][indexv[2]] = -1.0;
s[2][3+indexv[0]]=0.0;
s[2][3+indexv[1]]=0.0;
s[2][3+indexv[2]]=1.0;
s[0][jsf]=y[0][k]-y[0][k-1]-0.5*h*(y[1][k]+y[1][k-1]);
s[1][jsf]=y[1][k]-y[1][k-1]-temp*((x[k]+x[k-1])
*0.5*(mm+1.0)*(y[1][k]+y[1][k-1])-temp2
*0.5*(y[0][k]+y[0][k-1]));
s[2][jsf]=y[2][k]-y[2][k-1];
}
}

View File

@@ -0,0 +1,36 @@
#include "nr.h"
int ncom;
DP (*nrfunc)(Vec_I_DP &);
void (*nrdfun)(Vec_I_DP &, Vec_O_DP &);
Vec_DP *pcom_p,*xicom_p;
void NR::dlinmin(Vec_IO_DP &p, Vec_IO_DP &xi, DP &fret, DP func(Vec_I_DP &),
void dfunc(Vec_I_DP &, Vec_O_DP &))
{
const DP TOL=2.0e-8;
int j;
DP xx,xmin,fx,fb,fa,bx,ax;
int n=p.size();
ncom=n;
pcom_p=new Vec_DP(n);
xicom_p=new Vec_DP(n);
nrfunc=func;
nrdfun=dfunc;
Vec_DP &pcom=*pcom_p,&xicom=*xicom_p;
for (j=0;j<n;j++) {
pcom[j]=p[j];
xicom[j]=xi[j];
}
ax=0.0;
xx=1.0;
mnbrak(ax,xx,bx,fa,fx,fb,f1dim);
fret=dbrent(ax,xx,bx,f1dim,df1dim,TOL,xmin);
for (j=0;j<n;j++) {
xi[j] *= xmin;
p[j] += xi[j];
}
delete xicom_p;
delete pcom_p;
}

View File

@@ -0,0 +1,19 @@
#include "nr.h"
void NR::eclass(Vec_O_INT &nf, Vec_I_INT &lista, Vec_I_INT &listb)
{
int l,k,j;
int n=nf.size();
int m=lista.size();
for (k=0;k<n;k++) nf[k]=k;
for (l=0;l<m;l++) {
j=lista[l];
while (nf[j] != j) j=nf[j];
k=listb[l];
while (nf[k] != k) k=nf[k];
if (j != k) nf[j]=k;
}
for (j=0;j<n;j++)
while (nf[j] != nf[nf[j]]) nf[j]=nf[nf[j]];
}

View File

@@ -0,0 +1,17 @@
#include "nr.h"
void NR::eclazz(Vec_O_INT &nf, bool equiv(const int, const int))
{
int kk,jj;
int n=nf.size();
nf[0]=0;
for (jj=1;jj<n;jj++) {
nf[jj]=jj;
for (kk=0;kk<jj;kk++) {
nf[kk]=nf[nf[kk]];
if (equiv(jj+1,kk+1)) nf[nf[nf[kk]]]=jj;
}
}
for (jj=0;jj<n;jj++) nf[jj]=nf[nf[jj]];
}

43
lib/nr/cpp/recipes/ei.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
DP NR::ei(const DP x)
{
const int MAXIT=100;
const DP EULER=0.577215664901533;
const DP EPS=numeric_limits<DP>::epsilon();
const DP FPMIN=numeric_limits<DP>::min()/EPS;
int k;
DP fact,prev,sum,term;
if (x <= 0.0) nrerror("Bad argument in ei");
if (x < FPMIN) return log(x)+EULER;
if (x <= -log(EPS)) {
sum=0.0;
fact=1.0;
for (k=1;k<=MAXIT;k++) {
fact *= x/k;
term=fact/k;
sum += term;
if (term < EPS*sum) break;
}
if (k > MAXIT) nrerror("Series failed in ei");
return sum+log(x)+EULER;
} else {
sum=0.0;
term=1.0;
for (k=1;k<=MAXIT;k++) {
prev=term;
term *= k/x;
if (term < EPS) break;
if (term < prev) sum += term;
else {
sum -= prev;
break;
}
}
return exp(x)*(1.0+sum)/x;
}
}

View File

@@ -0,0 +1,23 @@
#include "nr.h"
void NR::eigsrt(Vec_IO_DP &d, Mat_IO_DP &v)
{
int i,j,k;
DP p;
int n=d.size();
for (i=0;i<n-1;i++) {
p=d[k=i];
for (j=i;j<n;j++)
if (d[j] >= p) p=d[k=j];
if (k != i) {
d[k]=d[i];
d[i]=p;
for (j=0;j<n;j++) {
p=v[j][i];
v[j][i]=v[j][k];
v[j][k]=p;
}
}
}
}

View File

@@ -0,0 +1,13 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::elle(const DP phi, const DP ak)
{
DP cc,q,s;
s=sin(phi);
cc=SQR(cos(phi));
q=(1.0-s*ak)*(1.0+s*ak);
return s*(rf(cc,q,1.0)-(SQR(s*ak))*rd(cc,q,1.0)/3.0);
}

View File

@@ -0,0 +1,11 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::ellf(const DP phi, const DP ak)
{
DP s;
s=sin(phi);
return s*rf(SQR(cos(phi)),(1.0-s*ak)*(1.0+s*ak),1.0);
}

View File

@@ -0,0 +1,14 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::ellpi(const DP phi, const DP en, const DP ak)
{
DP cc,enss,q,s;
s=sin(phi);
enss=en*s*s;
cc=SQR(cos(phi));
q=(1.0-s*ak)*(1.0+s*ak);
return s*(rf(cc,q,1.0)-enss*rj(cc,q,1.0,1.0+enss)/3.0);
}

View File

@@ -0,0 +1,35 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::elmhes(Mat_IO_DP &a)
{
int i,j,m;
DP y,x;
int n=a.nrows();
for (m=1;m<n-1;m++) {
x=0.0;
i=m;
for (j=m;j<n;j++) {
if (fabs(a[j][m-1]) > fabs(x)) {
x=a[j][m-1];
i=j;
}
}
if (i != m) {
for (j=m-1;j<n;j++) SWAP(a[i][j],a[m][j]);
for (j=0;j<n;j++) SWAP(a[j][i],a[j][m]);
}
if (x != 0.0) {
for (i=m+1;i<n;i++) {
if ((y=a[i][m-1]) != 0.0) {
y /= x;
a[i][m-1]=y;
for (j=m;j<n;j++) a[i][j] -= y*a[m][j];
for (j=0;j<n;j++) a[j][m] += y*a[j][i];
}
}
}
}
}

View File

@@ -0,0 +1,15 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::erfcc(const DP x)
{
DP t,z,ans;
z=fabs(x);
t=1.0/(1.0+0.5*z);
ans=t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+
t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+
t*(-0.82215223+t*0.17087277)))))))));
return (x >= 0.0 ? ans : 2.0-ans);
}

View File

@@ -0,0 +1,6 @@
#include "nr.h"
DP NR::erff(const DP x)
{
return x < 0.0 ? -gammp(0.5,x*x) : gammp(0.5,x*x);
}

View File

@@ -0,0 +1,6 @@
#include "nr.h"
DP NR::erffc(const DP x)
{
return x < 0.0 ? 1.0+gammp(0.5,x*x) : gammq(0.5,x*x);
}

View File

@@ -0,0 +1,29 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::eulsum(DP &sum, const DP term, const int jterm, Vec_IO_DP &wksp)
{
int j;
static int nterm;
DP tmp,dum;
if (jterm == 0) {
nterm=1;
sum=0.5*(wksp[0]=term);
} else {
if (nterm+1 > wksp.size()) nrerror("wksp too small in euler");
tmp=wksp[0];
wksp[0]=term;
for (j=1;j<nterm;j++) {
dum=wksp[j];
wksp[j]=0.5*(wksp[j-1]+tmp);
tmp=dum;
}
wksp[nterm]=0.5*(wksp[nterm-1]+tmp);
if (fabs(wksp[nterm]) <= fabs(wksp[nterm-1]))
sum += (0.5*wksp[nterm++]);
else
sum += wksp[nterm];
}
}

View File

@@ -0,0 +1,21 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::evlmem(const DP fdt, Vec_I_DP &d, const DP xms)
{
int i;
DP sumr=1.0,sumi=0.0,wr=1.0,wi=0.0,wpr,wpi,wtemp,theta;
int m=d.size();
theta=6.28318530717959*fdt;
wpr=cos(theta);
wpi=sin(theta);
for (i=0;i<m;i++) {
wr=(wtemp=wr)*wpr-wi*wpi;
wi=wi*wpr+wtemp*wpi;
sumr -= d[i]*wr;
sumi -= d[i]*wi;
}
return xms/(sumr*sumr+sumi*sumi);
}

View File

@@ -0,0 +1,13 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::expdev(int &idum)
{
DP dum;
do
dum=ran1(idum);
while (dum == 0.0);
return -log(dum);
}

View File

@@ -0,0 +1,61 @@
#include <cmath>
#include <limits>
#include "nr.h"
using namespace std;
DP NR::expint(const int n, const DP x)
{
const int MAXIT=100;
const DP EULER=0.577215664901533;
const DP EPS=numeric_limits<DP>::epsilon();
const DP BIG=numeric_limits<DP>::max()*EPS;
int i,ii,nm1;
DP a,b,c,d,del,fact,h,psi,ans;
nm1=n-1;
if (n < 0 || x < 0.0 || (x==0.0 && (n==0 || n==1)))
nrerror("bad arguments in expint");
else {
if (n == 0) ans=exp(-x)/x;
else {
if (x == 0.0) ans=1.0/nm1;
else {
if (x > 1.0) {
b=x+n;
c=BIG;
d=1.0/b;
h=d;
for (i=1;i<=MAXIT;i++) {
a = -i*(nm1+i);
b += 2.0;
d=1.0/(a*d+b);
c=b+a/c;
del=c*d;
h *= del;
if (fabs(del-1.0) <= EPS) {
ans=h*exp(-x);
return ans;
}
}
nrerror("continued fraction failed in expint");
} else {
ans = (nm1!=0 ? 1.0/nm1 : -log(x)-EULER);
fact=1.0;
for (i=1;i<=MAXIT;i++) {
fact *= -x/i;
if (i != nm1) del = -fact/(i-nm1);
else {
psi = -EULER;
for (ii=1;ii<=nm1;ii++) psi += 1.0/ii;
del=fact*(-log(x)+psi);
}
ans += del;
if (fabs(del) < fabs(ans)*EPS) return ans;
}
nrerror("series failed in expint");
}
}
}
}
return ans;
}

View File

@@ -0,0 +1,16 @@
#include "nr.h"
extern int ncom;
extern DP (*nrfunc)(Vec_I_DP &);
extern Vec_DP *pcom_p,*xicom_p;
DP NR::f1dim(const DP x)
{
int j;
Vec_DP xt(ncom);
Vec_DP &pcom=*pcom_p,&xicom=*xicom_p;
for (j=0;j<ncom;j++)
xt[j]=pcom[j]+x*xicom[j];
return nrfunc(xt);
}

View File

@@ -0,0 +1,12 @@
#include "nr.h"
DP NR::factln(const int n)
{
static DP a[101];
if (n < 0) nrerror("Negative factorial in routine factln");
if (n <= 1) return 0.0;
if (n <= 100)
return (a[n] != 0.0 ? a[n] : (a[n]=gammln(n+1.0)));
else return gammln(n+1.0);
}

View File

@@ -0,0 +1,18 @@
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::factrl(const int n)
{
static int ntop=4;
static DP a[33]={1.0,1.0,2.0,6.0,24.0};
int j;
if (n < 0) nrerror("Negative factorial in routine factrl");
if (n > 32) return exp(gammln(n+1.0));
while (ntop<n) {
j=ntop++;
a[ntop]=a[j]*ntop;
}
return a[n];
}

View File

@@ -0,0 +1,63 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::fasper(Vec_I_DP &x, Vec_I_DP &y, const DP ofac, const DP hifac,
Vec_O_DP &wk1, Vec_O_DP &wk2, int &nout, int &jmax, DP &prob)
{
const int MACC=4;
int j,k,ndim,nfreq,nfreqt;
DP ave,ck,ckk,cterm,cwt,den,df,effm,expy,fac,fndim,hc2wt,hs2wt,
hypo,pmax,sterm,swt,var,xdif,xmax,xmin;
int n=x.size();
int nwk=wk1.size();
nout=0.5*ofac*hifac*n;
nfreqt=ofac*hifac*n*MACC;
nfreq=64;
while (nfreq < nfreqt) nfreq <<= 1;
ndim=nfreq << 1;
if (ndim > nwk) nrerror("workspaces too small in fasper");
avevar(y,ave,var);
if (var == 0.0) nrerror("zero variance in fasper");
xmin=x[0];
xmax=xmin;
for (j=1;j<n;j++) {
if (x[j] < xmin) xmin=x[j];
if (x[j] > xmax) xmax=x[j];
}
xdif=xmax-xmin;
Vec_DP wk1_t(0.0,ndim);
Vec_DP wk2_t(0.0,ndim);
fac=ndim/(xdif*ofac);
fndim=ndim;
for (j=0;j<n;j++) {
ck=fmod((x[j]-xmin)*fac,fndim);
ckk=2.0*(ck++);
ckk=fmod(ckk,fndim);
++ckk;
spread(y[j]-ave,wk1_t,ck,MACC);
spread(1.0,wk2_t,ckk,MACC);
}
realft(wk1_t,1);
realft(wk2_t,1);
df=1.0/(xdif*ofac);
pmax = -1.0;
for (k=2,j=0;j<nout;j++,k+=2) {
hypo=sqrt(wk2_t[k]*wk2_t[k]+wk2_t[k+1]*wk2_t[k+1]);
hc2wt=0.5*wk2_t[k]/hypo;
hs2wt=0.5*wk2_t[k+1]/hypo;
cwt=sqrt(0.5+hc2wt);
swt=SIGN(sqrt(0.5-hc2wt),hs2wt);
den=0.5*n+hc2wt*wk2_t[k]+hs2wt*wk2_t[k+1];
cterm=SQR(cwt*wk1_t[k]+swt*wk1_t[k+1])/den;
sterm=SQR(cwt*wk1_t[k+1]-swt*wk1_t[k])/(n-den);
wk1[j]=(j+1)*df;
wk2[j]=(cterm+sterm)/(2.0*var);
if (wk2[j] > pmax) pmax=wk2[jmax=j];
}
expy=exp(-pmax);
effm=2.0*nout/ofac;
prob=effm*expy;
if (prob > 0.01) prob=1.0-pow(1.0-expy,effm);
}

View File

@@ -0,0 +1,25 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::fdjac(Vec_IO_DP &x, Vec_I_DP &fvec, Mat_O_DP &df,
void vecfunc(Vec_I_DP &, Vec_O_DP &))
{
const DP EPS=1.0e-8;
int i,j;
DP h,temp;
int n=x.size();
Vec_DP f(n);
for (j=0;j<n;j++) {
temp=x[j];
h=EPS*fabs(temp);
if (h == 0.0) h=EPS;
x[j]=temp+h;
h=x[j]-temp;
vecfunc(x,f);
x[j]=temp;
for (i=0;i<n;i++)
df[i][j]=(f[i]-fvec[i])/h;
}
}

View File

@@ -0,0 +1,21 @@
#include <cmath>
#include "nr.h"
using namespace std;
void NR::fgauss(const DP x, Vec_I_DP &a, DP &y, Vec_O_DP &dyda)
{
int i;
DP fac,ex,arg;
int na=a.size();
y=0.0;
for (i=0;i<na-1;i+=3) {
arg=(x-a[i+1])/a[i+2];
ex=exp(-arg*arg);
fac=a[i]*ex*2.0*arg;
y += a[i]*ex;
dyda[i]=ex;
dyda[i+1]=fac/a[i+2];
dyda[i+2]=fac*arg/a[i+2];
}
}

Some files were not shown because too many files have changed in this diff Show More