void lubksbfloat a, int n, int indx, float b
int i,ii0,ip,j;
float sum;
for i1;in;i
ipindxi;
sumbip;
bipbi;
if ii
for jii;ji1;j sum aijbj;
else if sum iii;
bisum;
for in;i1;i
sumbi;
for ji1;jn;j sum aijbj;
bisumaii;
include math.h
define NRANSI
include nrutil.h
define TINY 1.0e20
void ludcmpfloat a, int n, int indx, float d
int i,imax,j,k;
float big,dum,sum,temp;
float vv;
vvvector1,n;
d1.0;
for i1;in;i
big0.0;
for j1;jn;j
if tempfabsaij big bigtemp;
if big 0.0 nrerrorSingular matrix in routine ludcmp;
vvi1.0big;
for j1;jn;j
for i1;ij;i
sumaij;
for k1;ki;k sum aikakj;
aijsum;
big0.0;
for ij;in;i
sumaij;
for k1;kj;k
sum aikakj;
aijsum;
if dumvvifabssum big
bigdum;
imaxi;
if j ! imax
for k1;kn;k
dumaimaxk;
aimaxkajk;
ajkdum;
d d;
vvimaxvvj;
indxjimax;
if ajj 0.0 ajjTINY;
if j ! n
dum1.0ajj;
for ij1;in;i aij dum;
freevectorvv,1,n;
undef TINY
undef NRANSI
define NRANSI
include nrutil.h
void mprovefloat a, float alud, int n, int indx, float b, float x
void lubksbfloat a, int n, int indx, float b;
int j,i;
double sdp;
float r;
rvector1,n;
for i1;in;i
sdp bi;
for j1;jn;j sdp aijxj;
risdp;
lubksbalud,n,indx,r;
for i1;in;i xi ri;
freevectorr,1,n;
undef NRANSI
if definedSTDC definedANSI definedNRANSI ANSI
include stdio.h
include stddef.h
include stdlib.h
define NREND 1
define FREEARG char
void nrerrorchar errortext
Numerical Recipes standard error handler
fprintfstderr,Numerical Recipes runtime error…n;
fprintfstderr,sn,errortext;
fprintfstderr,…now exiting to system…n;
exit1;
float vectorlong nl, long nh
allocate a float vector with subscript range vnl..nh
float v;
vfloat mallocsizet nhnl1NRENDsizeoffloat;
if !v nrerrorallocation failure in vector;
return vnlNREND;
int ivectorlong nl, long nh
allocate an int vector with subscript range vnl..nh
int v;
vint mallocsizet nhnl1NRENDsizeofint;
if !v nrerrorallocation failure in ivector;
return vnlNREND;
unsigned char cvectorlong nl, long nh
allocate an unsigned char vector with subscript range vnl..nh
unsigned char v;
vunsigned char mallocsizet nhnl1NRENDsizeofunsigned char;
if !v nrerrorallocation failure in cvector;
return vnlNREND;
unsigned long lvectorlong nl, long nh
allocate an unsigned long vector with subscript range vnl..nh
unsigned long v;
vunsigned long mallocsizet nhnl1NRENDsizeoflong;
if !v nrerrorallocation failure in lvector;
return vnlNREND;
double dvectorlong nl, long nh
allocate a double vector with subscript range vnl..nh
double v;
vdouble mallocsizet nhnl1NRENDsizeofdouble;
if !v nrerrorallocation failure in dvector;
return vnlNREND;
float matrixlong nrl, long nrh, long ncl, long nch
allocate a float matrix with subscript range mnrl..nrhncl..nch
long i, nrownrhnrl1,ncolnchncl1;
float m;
allocate pointers to rows
mfloat mallocsizetnrowNRENDsizeoffloat;
if !m nrerrorallocation failure 1 in matrix;
m NREND;
m nrl;
allocate rows and set pointers to them
mnrlfloat mallocsizetnrowncolNRENDsizeoffloat;
if !mnrl nrerrorallocation failure 2 in matrix;
mnrl NREND;
mnrl ncl;
forinrl1;inrh;i mimi1ncol;
return pointer to array of pointers to rows
return m;
double dmatrixlong nrl, long nrh, long ncl, long nch
allocate a double matrix with subscript range mnrl..nrhncl..nch
long i, nrownrhnrl1,ncolnchncl1;
double m;
allocate pointers to rows
mdouble mallocsizetnrowNRENDsizeofdouble;
if !m nrerrorallocation failure 1 in matrix;
m NREND;
m nrl;
allocate rows and set pointers to them
mnrldouble mallocsizetnrowncolNRENDsizeofdouble;
if !mnrl nrerrorallocation failure 2 in matrix;
mnrl NREND;
mnrl ncl;
forinrl1;inrh;i mimi1ncol;
return pointer to array of pointers to rows
return m;
int imatrixlong nrl, long nrh, long ncl, long nch
allocate a int matrix with subscript range mnrl..nrhncl..nch
long i, nrownrhnrl1,ncolnchncl1;
int m;
allocate pointers to rows
mint mallocsizetnrowNRENDsizeofint;
if !m nrerrorallocation failure 1 in matrix;
m NREND;
m nrl;
allocate rows and set pointers to them
mnrlint mallocsizetnrowncolNRENDsizeofint;
if !mnrl nrerrorallocation failure 2 in matrix;
mnrl NREND;
mnrl ncl;
forinrl1;inrh;i mimi1ncol;
return pointer to array of pointers to rows
return m;
float submatrixfloat a, long oldrl, long oldrh, long oldcl, long oldch,
long newrl, long newcl
point a submatrix newrl..newcl.. to aoldrl..oldrholdcl..oldch
long i,j,nrowoldrholdrl1,ncololdclnewcl;
float m;
allocate array of pointers to rows
mfloat mallocsizet nrowNRENDsizeoffloat;
if !m nrerrorallocation failure in submatrix;
m NREND;
m newrl;
set pointers to rows
forioldrl,jnewrl;ioldrh;i,j mjaincol;
return pointer to array of pointers to rows
return m;
float convertmatrixfloat a, long nrl, long nrh, long ncl, long nch
allocate a float matrix mnrl..nrhncl..nch that points to the matrix
declared in the standard C manner as anrowncol, where nrownrhnrl1
and ncolnchncl1. The routine should be called with the address
a00 as the first argument.
long i,j,nrownrhnrl1,ncolnchncl1;
float m;
allocate pointers to rows
mfloat mallocsizet nrowNRENDsizeoffloat;
if !m nrerrorallocation failure in convertmatrix;
m NREND;
m nrl;
set pointers to rows
mnrlancl;
fori1,jnrl1;inrow;i,j mjmj1ncol;
return pointer to array of pointers to rows
return m;
float f3tensorlong nrl, long nrh, long ncl, long nch, long ndl, long ndh
allocate a float 3tensor with range tnrl..nrhncl..nchndl..ndh
long i,j,nrownrhnrl1,ncolnchncl1,ndepndhndl1;
float t;
allocate pointers to pointers to rows
tfloat mallocsizetnrowNRENDsizeoffloat;
if !t nrerrorallocation failure 1 in f3tensor;
t NREND;
t nrl;
allocate pointers to rows and set pointers to them
tnrlfloat mallocsizetnrowncolNRENDsizeoffloat;
if !tnrl nrerrorallocation failure 2 in f3tensor;
tnrl NREND;
tnrl ncl;
allocate rows and set pointers to them
tnrlnclfloat mallocsizetnrowncolndepNRENDsizeoffloat;
if !tnrlncl nrerrorallocation failure 3 in f3tensor;
tnrlncl NREND;
tnrlncl ndl;
forjncl1;jnch;j tnrljtnrlj1ndep;
forinrl1;inrh;i
titi1ncol;
tinclti1nclncolndep;
forjncl1;jnch;j tijtij1ndep;
return pointer to array of pointers to rows
return t;
void freevectorfloat v, long nl, long nh
free a float vector allocated with vector
freeFREEARG vnlNREND;
void freeivectorint v, long nl, long nh
free an int vector allocated with ivector
freeFREEARG vnlNREND;
void freecvectorunsigned char v, long nl, long nh
free an unsigned char vector allocated with cvector
freeFREEARG vnlNREND;
void freelvectorunsigned long v, long nl, long nh
free an unsigned long vector allocated with lvector
freeFREEARG vnlNREND;
void freedvectordouble v, long nl, long nh
free a double vector allocated with dvector
freeFREEARG vnlNREND;
void freematrixfloat m, long nrl, long nrh, long ncl, long nch
free a float matrix allocated by matrix
freeFREEARG mnrlnclNREND;
freeFREEARG mnrlNREND;
void freedmatrixdouble m, long nrl, long nrh, long ncl, long nch
free a double matrix allocated by dmatrix
freeFREEARG mnrlnclNREND;
freeFREEARG mnrlNREND;
void freeimatrixint m, long nrl, long nrh, long ncl, long nch
free an int matrix allocated by imatrix
freeFREEARG mnrlnclNREND;
freeFREEARG mnrlNREND;
void freesubmatrixfloat b, long nrl, long nrh, long ncl, long nch
free a submatrix allocated by submatrix
freeFREEARG bnrlNREND;
void freeconvertmatrixfloat b, long nrl, long nrh, long ncl, long nch
free a matrix allocated by convertmatrix
freeFREEARG bnrlNREND;
void freef3tensorfloat t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh
free a float f3tensor allocated by f3tensor
freeFREEARG tnrlnclndlNREND;
freeFREEARG tnrlnclNREND;
freeFREEARG tnrlNREND;
else ANSI
traditional KR
include stdio.h
define NREND 1
define FREEARG char
void nrerrorerrortext
char errortext;
Numerical Recipes standard error handler
void exit;
fprintfstderr,Numerical Recipes runtime error…n;
fprintfstderr,sn,errortext;
fprintfstderr,…now exiting to system…n;
exit1;
float vectornl,nh
long nh,nl;
allocate a float vector with subscript range vnl..nh
float v;
vfloat mallocunsigned int nhnl1NRENDsizeoffloat;
if !v nrerrorallocation failure in vector;
return vnlNREND;
int ivectornl,nh
long nh,nl;
allocate an int vector with subscript range vnl..nh
int v;
vint mallocunsigned int nhnl1NRENDsizeofint;
if !v nrerrorallocation failure in ivector;
return vnlNREND;
unsigned char cvectornl,nh
long nh,nl;
allocate an unsigned char vector with subscript range vnl..nh
unsigned char v;
vunsigned char mallocunsigned int nhnl1NRENDsizeofunsigned char;
if !v nrerrorallocation failure in cvector;
return vnlNREND;
unsigned long lvectornl,nh
long nh,nl;
allocate an unsigned long vector with subscript range vnl..nh
unsigned long v;
vunsigned long mallocunsigned int nhnl1NRENDsizeoflong;
if !v nrerrorallocation failure in lvector;
return vnlNREND;
double dvectornl,nh
long nh,nl;
allocate a double vector with subscript range vnl..nh
double v;
vdouble mallocunsigned int nhnl1NRENDsizeofdouble;
if !v nrerrorallocation failure in dvector;
return vnlNREND;
float matrixnrl,nrh,ncl,nch
long nch,ncl,nrh,nrl;
allocate a float matrix with subscript range mnrl..nrhncl..nch
long i, nrownrhnrl1,ncolnchncl1;
float m;
allocate pointers to rows
mfloat mallocunsigned intnrowNRENDsizeoffloat;
if !m nrerrorallocation failure 1 in matrix;
m NREND;
m nrl;
allocate rows and set pointers to them
mnrlfloat mallocunsigned intnrowncolNRENDsizeoffloat;
if !mnrl nrerrorallocation failure 2 in matrix;
mnrl NREND;
mnrl ncl;
forinrl1;inrh;i mimi1ncol;
return pointer to array of pointers to rows
return m;
double dmatrixnrl,nrh,ncl,nch
long nch,ncl,nrh,nrl;
allocate a double matrix with subscript range mnrl..nrhncl..nch
long i, nrownrhnrl1,ncolnchncl1;
double m;
allocate pointers to rows
mdouble mallocunsigned intnrowNRENDsizeofdouble;
if !m nrerrorallocation failure 1 in matrix;
m NREND;
m nrl;
allocate rows and set pointers to them
mnrldouble mallocunsigned intnrowncolNRENDsizeofdouble;
if !mnrl nrerrorallocation failure 2 in matrix;
mnrl NREND;
mnrl ncl;
forinrl1;inrh;i mimi1ncol;
return pointer to array of pointers to rows
return m;
int imatrixnrl,nrh,ncl,nch
long nch,ncl,nrh,nrl;
allocate a int matrix with subscript range mnrl..nrhncl..nch
long i, nrownrhnrl1,ncolnchncl1;
int m;
allocate pointers to rows
mint mallocunsigned intnrowNRENDsizeofint;
if !m nrerrorallocation failure 1 in matrix;
m NREND;
m nrl;
allocate rows and set pointers to them
mnrlint mallocunsigned intnrowncolNRENDsizeofint;
if !mnrl nrerrorallocation failure 2 in matrix;
mnrl NREND;
mnrl ncl;
forinrl1;inrh;i mimi1ncol;
return pointer to array of pointers to rows
return m;
float submatrixa,oldrl,oldrh,oldcl,oldch,newrl,newcl
float a;
long newcl,newrl,oldch,oldcl,oldrh,oldrl;
point a submatrix newrl..newcl.. to aoldrl..oldrholdcl..oldch
long i,j,nrowoldrholdrl1,ncololdclnewcl;
float m;
allocate array of pointers to rows
mfloat mallocunsigned int nrowNRENDsizeoffloat;
if !m nrerrorallocation failure in submatrix;
m NREND;
m newrl;
set pointers to rows
forioldrl,jnewrl;ioldrh;i,j mjaincol;
return pointer to array of pointers to rows
return m;
float convertmatrixa,nrl,nrh,ncl,nch
float a;
long nch,ncl,nrh,nrl;
allocate a float matrix mnrl..nrhncl..nch that points to the matrix
declared in the standard C manner as anrowncol, where nrownrhnrl1
and ncolnchncl1. The routine should be called with the address
a00 as the first argument.
long i,j,nrownrhnrl1,ncolnchncl1;
float m;
allocate pointers to rows
mfloat mallocunsigned int nrowNRENDsizeoffloat;
if !m nrerrorallocation failure in convertmatrix;
m NREND;
m nrl;
set pointers to rows
mnrlancl;
fori1,jnrl1;inrow;i,j mjmj1ncol;
return pointer to array of pointers to rows
return m;
float f3tensornrl,nrh,ncl,nch,ndl,ndh
long nch,ncl,ndh,ndl,nrh,nrl;
allocate a float 3tensor with range tnrl..nrhncl..nchndl..ndh
long i,j,nrownrhnrl1,ncolnchncl1,ndepndhndl1;
float t;
allocate pointers to pointers to rows
tfloat mallocunsigned intnrowNRENDsizeoffloat;
if !t nrerrorallocation failure 1 in f3tensor;
t NREND;
t nrl;
allocate pointers to rows and set pointers to them
tnrlfloat mallocunsigned intnrowncolNRENDsizeoffloat;
if !tnrl nrerrorallocation failure 2 in f3tensor;
tnrl NREND;
tnrl ncl;
allocate rows and set pointers to them
tnrlnclfloat mallocunsigned intnrowncolndepNRENDsizeoffloat;
if !tnrlncl nrerrorallocation failure 3 in f3tensor;
tnrlncl NREND;
tnrlncl ndl;
forjncl1;jnch;j tnrljtnrlj1ndep;
forinrl1;inrh;i
titi1ncol;
tinclti1nclncolndep;
forjncl1;jnch;j tijtij1ndep;
return pointer to array of pointers to rows
return t;
void freevectorv,nl,nh
float v;
long nh,nl;
free a float vector allocated with vector
freeFREEARG vnlNREND;
void freeivectorv,nl,nh
int v;
long nh,nl;
free an int vector allocated with ivector
freeFREEARG vnlNREND;
void freecvectorv,nl,nh
long nh,nl;
unsigned char v;
free an unsigned char vector allocated with cvector
freeFREEARG vnlNREND;
void freelvectorv,nl,nh
long nh,nl;
unsigned long v;
free an unsigned long vector allocated with lvector
freeFREEARG vnlNREND;
void freedvectorv,nl,nh
double v;
long nh,nl;
free a double vector allocated with dvector
freeFREEARG vnlNREND;
void freematrixm,nrl,nrh,ncl,nch
float m;
long nch,ncl,nrh,nrl;
free a float matrix allocated by matrix
freeFREEARG mnrlnclNREND;
freeFREEARG mnrlNREND;
void freedmatrixm,nrl,nrh,ncl,nch
double m;
long nch,ncl,nrh,nrl;
free a double matrix allocated by dmatrix
freeFREEARG mnrlnclNREND;
freeFREEARG mnrlNREND;
void freeimatrixm,nrl,nrh,ncl,nch
int m;
long nch,ncl,nrh,nrl;
free an int matrix allocated by imatrix
freeFREEARG mnrlnclNREND;
freeFREEARG mnrlNREND;
void freesubmatrixb,nrl,nrh,ncl,nch
float b;
long nch,ncl,nrh,nrl;
free a submatrix allocated by submatrix
freeFREEARG bnrlNREND;
void freeconvertmatrixb,nrl,nrh,ncl,nch
float b;
long nch,ncl,nrh,nrl;
free a matrix allocated by convertmatrix
freeFREEARG bnrlNREND;
void freef3tensort,nrl,nrh,ncl,nch,ndl,ndh
float t;
long nch,ncl,ndh,ndl,nrh,nrl;
free a float f3tensor allocated by f3tensor
freeFREEARG tnrlnclndlNREND;
freeFREEARG tnrlnclNREND;
freeFREEARG tnrlNREND;
endif ANSI
include math.h
define NRANSI
include nrutil.h
define BIG 1.0e30
void padedouble cof, int n, float resid
void lubksbfloat a, int n, int indx, float b;
void ludcmpfloat a, int n, int indx, float d;
void mprovefloat a, float alud, int n, int indx, float b,
float x;
int j,k,indx;
float d,rr,rrold,sum,q,qlu,x,y,z;
indxivector1,n;
qmatrix1,n,1,n;
qlumatrix1,n,1,n;
xvector1,n;
yvector1,n;
zvector1,n;
for j1;jn;j
yjxjcofnj;
for k1;kn;k
qjkcofjkn;
qlujkqjk;
ludcmpqlu,n,indx,d;
lubksbqlu,n,indx,x;
rrBIG;
do
rroldrr;
for j1;jn;j zjxj;
mproveq,qlu,n,indx,y,x;
rr0.0;
for j1;jn;j
rr SQRzjxj;
while rr rrold;
residsqrtrrold;
for k1;kn;k
for sumcofk,j1;jk;j sum zjcofkj;
yksum;
for j1;jn;j
cofjyj;
cofjn zj;
freevectorz,1,n;
freevectory,1,n;
freevectorx,1,n;
freematrixqlu,1,n,1,n;
freematrixq,1,n,1,n;
freeivectorindx,1,n;
undef BIG
undef NRANSI
double ratvaldouble x, double cof, int mm, int kk
int j;
double sumd,sumn;
for sumncofmm,jmm1;j0;j sumnsumnxcofj;
for sumd0.0,jmmkk;jmm1;j sumdsumdcofjx;
return sumn1.0sumd;
Driver for routine pade
include stdlib.h
include string.h
include stdio.h
include math.h
define NRANSI
include nr.h
include nrutil.h
double fndouble x
return x 0.0 ? 1.0 : log1.0 x x;
int mainint argc, char argv
int j, k, n 2048, verbose 0;
float resid;
double b, d, fac, x, c;
for j 0; j argc; j
if strcmpargvj, verbose 0
verbose 1;
else
n atoiargvj;
c dvector0, 2 n 1;
fac 1;
for j 1; j 2 n 1; j
cj 1 fac doublej;
fac fac;
padec, n, resid;
if verbose
printfNorm of residual vector 16.8en, resid;
printfpoint, func. value, pade series:n;
for j 1; j 21; j
x j 1 0.25;
d ratvalx, c, n, n;
printf16.8ft16.8ft16.8fn, x, fnx, d;
freedvectorc, 0, 2 n 1;
return 0;
undef NRANSI