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,40 @@
/* Driver for routine airy */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float ai,bi,aip,bip,x,xai,xbi,xaip,xbip;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Airy Functions",14)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s\n%14s %16s %17s %16s\n%14s %16s %17s %16s\n",
"x","ai","bi","aip","bip","xai","xbi","xaip","xbip");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f %f %f",&x,&ai,&bi,&aip,&bip);
airy(x,&xai,&xbi,&xaip,&xbip);
printf("%5.2f\n\t%16.6e %16.6e %16.6e %16.6e\n",x,ai,bi,aip,bip);
printf("\t%16.6e %16.6e %16.6e %16.6e\n",xai,xbi,xaip,xbip);
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,90 @@
/* Driver for routine amebsa */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NP 4
#define MP 5
#define FTOL 1.0E-6
#define N 4
#define RAD 0.3
#define AUG 2.0
long idum=(-64);
float tfunk(float p[])
{
int j;
float q,r,sumd=0.0,sumr=0.0;
static float wid[N+1]={0.0,1.0,3.0,10.0,30.0};
for (j=1;j<=N;j++) {
q=p[j]*wid[j];
r=(float)(q >= 0 ? (int)(q+0.5) : -(int)(0.5-q));
sumr += q*q;
sumd += (q-r)*(q-r);
}
return 1+sumr*(1+(sumd > RAD*RAD ? AUG : AUG*sumd/(RAD*RAD)));
}
int main(void)
{
int i,iiter,iter,j,jiter,ndim=NP,nit;
float temptr,yb,ybb;
float **p,*x,*y,*pb;
static float xoff[NP+1]={0.0,10.0,10.0,10.0,10.0};
p=matrix(1,MP,1,NP);
x=vector(1,NP);
y=vector(1,MP);
pb=vector(1,NP);
for (i=1;i<=MP;i++)
for (j=1;j<=NP;j++) p[i][j]=0.0;
for (;;) {
for (j=2;j<=MP;j++) p[j][j-1]=1.0;
for (i=1;i<=MP;i++) {
for (j=1;j<=NP;j++) x[j]=(p[i][j] += xoff[j]);
y[i]=tfunk(x);
}
yb=1.0e30;
printf("Input t, iiter:\n");
if (scanf("%f %d",&temptr,&iiter) == EOF) break;
ybb=1.0e30;
nit=0;
for (jiter=1;jiter<=100;jiter++) {
iter=iiter;
temptr *= 0.8;
amebsa(p,y,ndim,pb,&yb,FTOL,tfunk,&iter,temptr);
nit += iiter-iter;
if (yb < ybb) {
ybb=yb;
printf("%6d %10.3e ",nit,temptr);
for (j=1;j<=NP;j++) printf("%10.5f ",pb[j]);
printf("%15.7e\n",yb);
}
if (iter > 0) break;
}
printf("Vertices of final 3-D simplex and\n");
printf("float values at the vertices:\n");
printf("%3s %10s %12s %12s %14s\n\n",
"i","x[i]","y[i]","z[i]","function");
for (i=1;i<=MP;i++) {
printf("%3d ",i);
for (j=1;j<=NP;j++) printf("%12.6f ",p[i][j]);
printf("%15.7e\n",y[i]);
}
printf("%3d ",99);
for (j=1;j<=NP;j++) printf("%12.6f ",pb[j]);
printf("%15.7e\n",yb);
}
free_vector(pb,1,NP);
free_vector(y,1,MP);
free_vector(x,1,NP);
free_matrix(p,1,MP,1,NP);
printf("Normal completion\n");
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,49 @@
/* Driver for routine amoeba */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MP 4
#define NP 3
#define FTOL 1.0e-6
float func(float x[])
{
return 0.6-bessj0(SQR(x[1]-0.5)+SQR(x[2]-0.6)+SQR(x[3]-0.7));
}
int main(void)
{
int i,nfunc,j,ndim=NP;
float *x,*y,**p;
x=vector(1,NP);
y=vector(1,MP);
p=matrix(1,MP,1,NP);
for (i=1;i<=MP;i++) {
for (j=1;j<=NP;j++)
x[j]=p[i][j]=(i == (j+1) ? 1.0 : 0.0);
y[i]=func(x);
}
amoeba(p,y,ndim,FTOL,func,&nfunc);
printf("\nNumber of function evaluations: %3d\n",nfunc);
printf("Vertices of final 3-d simplex and\n");
printf("function values at the vertices:\n\n");
printf("%3s %10s %12s %12s %14s\n\n",
"i","x[i]","y[i]","z[i]","function");
for (i=1;i<=MP;i++) {
printf("%3d ",i);
for (j=1;j<=NP;j++) printf("%12.6f ",p[i][j]);
printf("%12.6f\n",y[i]);
}
printf("\nTrue minimum is at (0.5,0.6,0.7)\n");
free_matrix(p,1,MP,1,NP);
free_vector(y,1,MP);
free_vector(x,1,NP);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,38 @@
/* Driver for routine anneal */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NCITY 10
int main(void)
{
int i,ii,*iorder;
long idum=(-111);
float *x,*y;
iorder=ivector(1,NCITY);
x=vector(1,NCITY);
y=vector(1,NCITY);
for (i=1;i<=NCITY;i++) {
x[i]=ran3(&idum);
y[i]=ran3(&idum);
iorder[i]=i;
}
anneal(x,y,iorder,NCITY);
printf("*** System Frozen ***\n");
printf("Final path:\n");
printf("%8s %9s %12s\n","city","x","y");
for (i=1;i<=NCITY;i++) {
ii=iorder[i];
printf("%4d %10.4f %10.4f\n",ii,x[ii],y[ii]);
}
free_vector(y,1,NCITY);
free_vector(x,1,NCITY);
free_ivector(iorder,1,NCITY);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,80 @@
/* Driver for routines arcmak and arcode */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MC 512
#define NWK 20
#define MAXLINE 256
int main(void)
{
int k;
unsigned long i,j,lc,lcode=MAXLINE,n,nch,nrad,nt,nfreq[257],tmp,zero=0;
unsigned char *code,mess[MAXLINE],ness[MAXLINE];
arithcode acode;
FILE *fp;
code=cvector(0,MAXLINE);
acode.ilob=lvector(1,NWK);
acode.iupb=lvector(1,NWK);
acode.ncumfq=lvector(1,MC+2);
if ((fp = fopen("text.dat","r")) == NULL)
nrerror("Input file text.dat not found.\n");
for (j=1;j<=256;j++) nfreq[j]=0;
while ((k=getc(fp)) != EOF) {
if ((k -= 31) >= 1) nfreq[k]++;
}
fclose(fp);
nch=96;
nrad=256;
/* here is the initialization that constructs the code */
arcmak(nfreq,(int)nch,(int)nrad,&acode);
/* now ready to prompt for lines to encode */
for (;;) {
printf("Enter a line:\n");
if (gets((char *)&mess[1]) == NULL) break;
n=strlen((char *)&mess[1]);
/* shift from 256 character alphabet to 96 printing characters */
for (j=1;j<=n;j++) mess[j] -= 32;
/* message initialization */
lc=1;
arcode(&zero,&code,&lcode,&lc,0,&acode);
/* here we arithmetically encode mess(1:n) */
for (j=1;j<=n;j++) {
tmp=mess[j];
arcode(&tmp,&code,&lcode,&lc,1,&acode);
}
/* message termination */
arcode(&nch,&code,&lcode,&lc,1,&acode);
printf("Length of line input, coded= %lu %lu\n",n,lc-1);
/* here we decode the message, hopefully to get the original back */
lc=1;
arcode(&zero,&code,&lcode,&lc,0,&acode);
for (j=1;j<=lcode;j++) {
arcode(&i,&code,&lcode,&lc,-1,&acode);
if (i == nch) break;
else ness[j]=(unsigned char)i;
}
if (j > lcode) nrerror("Arith. coding: Never get here");
nt=j-1;
printf("Decoded output:\n");
for (j=1;j<=nt;j++) printf("%c",(char)(ness[j]+32));
printf("\n");
if (nt != n) printf("Error ! j decoded != n input.\n");
}
free_cvector(code,0,MAXLINE);
free_lvector(acode.ncumfq,1,MC+2);
free_lvector(acode.iupb,1,NWK);
free_lvector(acode.ilob,1,NWK);
printf("Normal completion\n");
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,31 @@
/* Driver for routine avevar */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NPTS 1000
#define EPS 0.1
int main(void)
{
int i,j;
long idum=(-5);
float ave,shift,vrnce,*data;
data=vector(1,NPTS);
/* generate gaussian distributed data */
printf("\n%9s %11s %12s\n","shift","average","variance");
for (i=1;i<=11;i++) {
shift=(i-1)*EPS;
for (j=1;j<=NPTS;j++)
data[j]=shift+i*gasdev(&idum);
avevar(data,NPTS,&ave,&vrnce);
printf("%8.2f %11.2f %12.2f\n",shift,ave,vrnce);
}
free_vector(data,1,NPTS);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,55 @@
/* Driver for routine balanc */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NP 5
int main(void)
{
int i,j;
float *c,*r,**a;
c=vector(1,NP);
r=vector(1,NP);
a=matrix(1,NP,1,NP);
for (i=1;i<=NP;i++)
for (j=1;j<=NP;j++)
a[i][j] = (((i & 1) && !(j & 1)) ? 100.0 : 1.0);
/* Write norms */
for (i=1;i<=NP;i++) {
r[i]=c[i]=0.0;
for (j=1;j<=NP;j++) {
r[i] += fabs(a[i][j]);
c[i] += fabs(a[j][i]);
}
}
printf("rows:\n");
for (i=1;i<=NP;i++) printf("%12.2f",r[i]);
printf("\ncolumns:\n");
for (i=1;i<=NP;i++) printf("%12.2f",c[i]);
printf("\n\n***** Balancing matrix *****\n\n");
balanc(a,NP);
/* Write norms */
for (i=1;i<=NP;i++) {
r[i]=c[i]=0.0;
for (j=1;j<=NP;j++) {
r[i] += fabs(a[i][j]);
c[i] += fabs(a[j][i]);
}
}
printf("rows:\n");
for (i=1;i<=NP;i++) printf("%12.2f",r[i]);
printf("\ncolumns:\n");
for (i=1;i<=NP;i++) printf("%12.2f",c[i]);
printf("\n");
free_matrix(a,1,NP,1,NP);
free_vector(r,1,NP);
free_vector(c,1,NP);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,38 @@
/* Driver for routine bandec */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
int main(void)
{
float d,**a,**al,*b,*x;
unsigned long i,j,*indx;
long idum=(-1);
a=matrix(1,7,1,4);
x=vector(1,7);
b=vector(1,7);
al=matrix(1,7,1,2);
indx=lvector(1,7);
for (i=1;i<=7;i++) {
x[i]=ran1(&idum);
for (j=1;j<=4;j++) {
a[i][j]=ran1(&idum);
}
}
banmul(a,7,2,1,x,b);
for (i=1;i<=7;i++) printf("%ld %12.6f %12.6f\n",i,b[i],x[i]);
bandec(a,7,2,1,al,indx,&d);
banbks(a,7,2,1,al,indx,b);
for (i=1;i<=7;i++) printf("%ld %12.6f %12.6f\n",i,b[i],x[i]);
free_lvector(indx,1,7);
free_matrix(al,1,7,1,2);
free_vector(b,1,7);
free_vector(x,1,7);
free_matrix(a,1,7,1,4);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,52 @@
/* Driver for routine banmul */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NP 7
#define M1 2
#define M2 1
#define MP (M1+1+M2)
int main(void)
{
unsigned long i,j,k;
float **a,**aa,*ax,*b,*x;
a=matrix(1,NP,1,MP);
aa=matrix(1,NP,1,NP);
ax=vector(1,NP);
b=vector(1,NP);
x=vector(1,NP);
for (i=1;i<=M1;i++) for (j=1;j<=NP;j++) a[j][i]=10.0*j+i;
/* Lower band */
for (i=1;i<=NP;i++) a[i][M1+1]=i;
/* Diagonal */
for (i=1;i<=M2;i++) for (j=1;j<=NP;j++) a[j][M1+1+i]=0.1*j+i;
/* Upper band */
for (i=1;i<=NP;i++) {
for (j=1;j<=NP;j++) {
k=i-M1-1;
if (j>=LMAX(1,1+k) && j<=LMIN(M1+M2+1+k,NP))
aa[i][j]=a[i][j-k];
else aa[i][j]=0.0;
}
}
for (i=1;i<=NP;i++) x[i]=i/10.0;
banmul(a,NP,M1,M2,x,b);
for (i=1;i<=NP;i++) {
for (ax[i]=0.0,j=1;j<=NP;j++) ax[i] += aa[i][j]*x[j];
}
printf("\tReference vector\tbanmul vector\n");
for (i=1;i<=NP;i++) printf("\t%12.4f\t%12.4f\n",ax[i],b[i]);
free_vector(x,1,NP);
free_vector(b,1,NP);
free_vector(ax,1,NP);
free_matrix(aa,1,NP,1,NP);
free_matrix(a,1,NP,1,MP);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,38 @@
/* Driver for routine bcucof */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
int main(void)
{
int i,j;
float d1,d2,ee,x1x2;
float y[5],y1[5],y2[5],y12[5],**c;
static float x1[]={0.0,0.0,2.0,2.0,0.0};
static float x2[]={0.0,0.0,0.0,2.0,2.0};
c=matrix(1,4,1,4);
d1=x1[2]-x1[1];
d2=x2[4]-x2[1];
for (i=1;i<=4;i++) {
x1x2=x1[i]*x2[i];
ee=exp(-x1x2);
y[i]=x1x2*ee;
y1[i]=x2[i]*(1.0-x1x2)*ee;
y2[i]=x1[i]*(1.0-x1x2)*ee;
y12[i]=(1.0-3.0*x1x2+x1x2*x1x2)*ee;
}
bcucof(y,y1,y2,y12,d1,d2,c);
printf("\nCoefficients for bicubic interpolation:\n\n");
for (i=1;i<=4;i++) {
for (j=1;j<=4;j++) printf("%12.6f",c[i][j]);
printf("\n");
}
free_matrix(c,1,4,1,4);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,42 @@
/* Driver for routine bcuint */
#include <stdio.h>
#define NRANSI
#include "nr.h"
int main(void)
{
int i;
float ansy,ansy1,ansy2,ey,ey1,ey2;
float x1,x1l,x1u,x1x2,x2,x2l,x2u,xxyy;
float y[5],y1[5],y12[5],y2[5];
static float xx[]={0.0,0.0,2.0,2.0,0.0};
static float yy[]={0.0,0.0,0.0,2.0,2.0};
x1l=xx[1];
x1u=xx[2];
x2l=yy[1];
x2u=yy[4];
for (i=1;i<=4;i++) {
xxyy=xx[i]*yy[i];
y[i]=xxyy*xxyy;
y1[i]=2.0*yy[i]*xxyy;
y2[i]=2.0*xx[i]*xxyy;
y12[i]=4.0*xxyy;
}
printf("\n%6s %8s %7s %11s %6s %10s %6s %10s \n\n",
"x1","x2","y","expect","y1","expect","y2","expect");
for (i=1;i<=10;i++) {
x2=(x1=0.2*i);
bcuint(y,y1,y2,y12,x1l,x1u,x2l,x2u,x1,x2,&ansy,&ansy1,&ansy2);
x1x2=x1*x2;
ey=x1x2*x1x2;
ey1=2.0*x2*x1x2;
ey2=2.0*x1*x1x2;
printf("%8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f\n",
x1,x2,ansy,ey,ansy1,ey1,ansy2,ey2);
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,29 @@
/* Driver for routine beschb */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
int main(void)
{
double gam1,gam2,gampl,gammi,x,xgam1,xgam2,xgampl,xgammi;
for (;;) {
printf("Enter x:\n");
if (scanf("%lf",&x) == EOF) break;
beschb(x,&xgam1,&xgam2,&xgampl,&xgammi);
printf("%5s\n%17s %16s %17s %15s\n%17s %16s %17s %15s\n",
"x","gam1","gam2","gampl","gammi","xgam1","xgam2","xgampl","xgammi");
gampl=1/exp(gammln((float)(1+x)));
gammi=1/exp(gammln((float)(1-x)));
gam1=(gammi-gampl)/(2*x);
gam2=(gammi+gampl)/2;
printf("%5.2f\n\t%16.6e %16.6e %16.6e %16.6e\n",x,gam1,gam2,gampl,gammi);
printf("\t%16.6e %16.6e %16.6e %16.6e\n",xgam1,xgam2,xgampl,xgammi);
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessi */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval,n;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Modified Bessel Function In",27)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%4s %7s %15s %20s\n","n","x","actual","bessi(n,x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%d %f %f",&n,&x,&val);
printf("%4d %8.2f %18.7e %18.7e\n",n,x,val,bessi(n,x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessi0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Modified Bessel Function I0",27)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %13s \n","x","actual","bessi0(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.7f %12.7f\n",x,val,bessi0(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessi1 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Modified Bessel Function I1",27)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %13s \n","x","actual","bessi1(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.7f %12.7f\n",x,val,bessi1(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,40 @@
/* Driver for routine bessik */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float ri,rk,rip,rkp,x,xnu,xri,xrk,xrip,xrkp;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Modified Bessel Functions",25)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %7s\n%14s %16s %17s %16s\n%14s %16s %17s %16s\n",
"xnu","x","ri","rk","rip","rkp","xri","xrk","xrip","xrkp");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f %f %f %f",&xnu,&x,&ri,&rk,&rip,&rkp);
bessik(x,xnu,&xri,&xrk,&xrip,&xrkp);
printf("%5.2f %5.2f\n\t%16.6e %16.6e %16.6e %16.6e\n",xnu,x,ri,rk,rip,rkp);
printf("\t%16.6e %16.6e %16.6e %16.6e\n",xri,xrk,xrip,xrkp);
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessj */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval,n;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Bessel Function Jn",18)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%4s %7s %15s %20s \n","n","x","actual","bessj(n,x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%d %f %f",&n,&x,&val);
printf("%4d %8.2f %18.6e %18.6e\n",n,x,val,bessj(n,x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessj0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Bessel Function J0",18)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %13s \n","x","actual","bessj0(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.7f %12.7f \n",x,val,bessj0(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessj1 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Bessel Function J1",18)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %13s \n","x","actual","bessj1(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.7f %12.7f\n",x,val,bessj1(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,40 @@
/* Driver for routine bessjy */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float rj,ry,rjp,ryp,x,xnu,xrj,xry,xrjp,xryp;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Ordinary Bessel Functions",25)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %7s\n%14s %16s %17s %16s\n%14s %16s %17s %16s\n",
"xnu","x","rj","ry","rjp","ryp","xrj","xry","xrjp","xryp");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f %f %f %f",&xnu,&x,&rj,&ry,&rjp,&ryp);
bessjy(x,xnu,&xrj,&xry,&xrjp,&xryp);
printf("%5.2f %5.2f\n\t%16.6e %16.6e %16.6e %16.6e\n",xnu,x,rj,ry,rjp,ryp);
printf("\t%16.6e %16.6e %16.6e %16.6e\n",xrj,xry,xrjp,xryp);
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessk */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval,n;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Modified Bessel Function Kn",27)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%4s %7s %14s %19s\n","n","x","actual","bessk(n,x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%d %f %f",&n,&x,&val);
printf("%4d %8.2f %18.7e %16.7e \n",n,x,val,bessk(n,x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessk0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Modified Bessel Function K0",27)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %13s %18s \n","x","actual","bessk0(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %16.7e %16.7e\n",x,val,bessk0(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessk1 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Modified Bessel Function K1",27)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %13s %17s \n","x","actual","bessk1(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %16.7e %16.7e\n",x,val,bessk1(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessy */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval,n;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Bessel Function Yn",18)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%4s %7s %15s %20s \n","n","x","actual","bessy(n,x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%d %f %f",&n,&x,&val);
printf("%4d %8.2f %18.6e %18.6e\n",n,x,val,bessy(n,x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessy0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Bessel Function Y0",18)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %13s \n","x","actual","bessy0(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.7f %12.7f\n",x,val,bessy0(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bessy1 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Bessel Function Y1",18)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %13s \n","x","actual","bessy1(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.7f %12.7f\n",x,val,bessy1(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine beta */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,w,z;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Beta Function",13)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %6s %16s %20s\n","w","z","actual","beta(w,z)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f",&w,&z,&val);
printf("%6.2f %6.2f %18.6e %18.6e\n",w,z,val,beta(w,z));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,39 @@
/* Driver for routine betai */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float a,b,x,val;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Incomplete Beta Function",24)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %10s %12s %14s %13s \n",
"a","b","x","actual","betai(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f %f",&a,&b,&x,&val);
printf("%6.2f %12.6f %12.6f %12.6f %12.6f\n",
a,b,x,val,betai(a,b,x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine bico */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,k,n,nval;
float binco;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Binomial Coefficients",21)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%6s %6s %12s %12s \n","n","k","actual","bico(n,k)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%d %d %f ",&n,&k,&binco);
printf("%6d %6d %12.0f %12.0f \n",n,k,binco,bico(n,k));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,49 @@
/* Driver for routine bnldev */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#define N 20
#define NPTS 1000
#define ISCAL 200
#define NN 100
#define LLEN 50
int main(void)
{
char txt[LLEN+1];
int i,j,k,klim,dist[N+1];
long idum=(-133);
float pp,xm,dd;
for (;;) {
for (j=0;j<=N;j++) dist[j]=0;
do {
printf("Mean of binomial distribution (0.0 to %d.0)",N);
printf(" - Negative to end:\n");
scanf("%f",&xm);
} while (xm > N);
if (xm < 0.0) break;
pp=xm/NN;
for (i=1;i<=NPTS;i++) {
j=bnldev(pp,NN,&idum);
if (j >= 0 && j <= N) ++dist[j];
}
printf("Binomial-distributed deviate, mean %5.2f of %6d points\n",
xm,NPTS);
printf("%4s %8s %10s\n","x","p(x)","graph:");
for (j=0;j<N;j++) {
for (k=0;k<=LLEN;k++) txt[k]=' ';
dd=(float) dist[j]/NPTS;
klim=(int) (ISCAL*dd+1);
if (klim > LLEN) klim=LLEN;
for (k=1;k<=klim;k++) txt[k]='*';
txt[LLEN]='\0';
printf("%4d %9.4f %s\n",j,dd,txt);
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,47 @@
/* Driver for routine brent */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#define TOL 1.0e-6
#define EQL 1.0e-4
float func(float x)
{
return bessj0(x);
}
int main(void)
{
int i,iflag,j,nmin=0;
float ax,bx,cx,fa,fb,fc,xmin,bren,amin[21];
printf("\nMinima of the function bessj0\n");
printf("%10s %8s %17s %12s\n","min. #","x","bessj0(x)","bessj1(x)");
for (i=1;i<=100;i++) {
ax=i;
bx=i+1.0;
mnbrak(&ax,&bx,&cx,&fa,&fb,&fc,func);
bren=brent(ax,bx,cx,func,TOL,&xmin);
if (nmin == 0) {
amin[1]=xmin;
nmin=1;
printf("%7d %15.6f %12.6f %12.6f\n",
nmin,xmin,bessj0(xmin),bessj1(xmin));
} else {
iflag=0;
for (j=1;j<=nmin;j++)
if (fabs(xmin-amin[j]) <= (EQL*xmin)) iflag=1;
if (iflag == 0) {
amin[++nmin]=xmin;
printf("%7d %15.6f %12.6f %12.6f\n",
nmin,xmin,bessj0(xmin),bessj1(xmin));
}
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,36 @@
/* Driver for routine broydn */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
void funcv(int n,float x[],float f[])
{
f[1]=SQR(x[1])+SQR(x[2])-2.0;
f[2]=exp(x[1]-1.0)+x[2]*SQR(x[2])-2.0;
}
#define N 2
int main(void)
{
int i,check;
float *x,*f;
x=vector(1,N);
f=vector(1,N);
x[1]=2.0;
x[2]=0.5;
broydn(x,N,&check,funcv);
funcv(N,x,f);
if (check) printf("Convergence problems.\n");
printf("%7s %3s %12s\n","Index","x","f");
for (i=1;i<=N;i++) printf("%5d %12.6f %12.6f\n",i,x[i],f[i]);
free_vector(f,1,N);
free_vector(x,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,54 @@
/* Driver for routine bsstep */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 4
float dxsav,*xp,**yp; /* defining declarations */
int kmax,kount;
int nrhs; /* counts function evaluations */
void derivs(float x,float y[],float dydx[])
{
nrhs++;
dydx[1] = -y[2];
dydx[2]=y[1]-(1.0/x)*y[2];
dydx[3]=y[2]-(2.0/x)*y[3];
dydx[4]=y[3]-(3.0/x)*y[4];
}
int main(void)
{
int i,nbad,nok;
float eps=1.0e-4,h1=0.1,hmin=0.0,x1=1.0,x2=10.0,*ystart;
ystart=vector(1,N);
xp=vector(1,200);
yp=matrix(1,10,1,200);
ystart[1]=bessj0(x1);
ystart[2]=bessj1(x1);
ystart[3]=bessj(2,x1);
ystart[4]=bessj(3,x1);
nrhs=0;
kmax=100;
dxsav=(x2-x1)/20.0;
odeint(ystart,N,x1,x2,eps,h1,hmin,&nok,&nbad,derivs,bsstep);
printf("\n%s %13s %3d\n","successful steps:"," ",nok);
printf("%s %20s %3d\n","bad steps:"," ",nbad);
printf("%s %9s %3d\n","function evaluations:"," ",nrhs);
printf("\n%s %3d\n","stored intermediate values: ",kount);
printf("\n%8s %18s %15s\n","x","integral","bessj(3,x)");
for (i=1;i<=kount;i++)
printf("%10.4f %16.6f %14.6f\n",
xp[i],yp[4][i],bessj(3,xp[i]));
free_matrix(yp,1,10,1,200);
free_vector(xp,1,200);
free_vector(ystart,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,41 @@
/* Driver for routine caldat */
#include <stdio.h>
#include <stdlib.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
int i,id,idd,im,imm,iy,iyy,n;
long j;
char dummy[MAXSTR];
static char *name[]={"","january","february","march",
"april","may","june","july","august",
"september","october","november","december"};
FILE *fp;
/* Check whether caldat properly undoes the operation of julday */
if ((fp = fopen("dates1.dat","r")) == NULL)
nrerror("Data file dates1.dat not found\n");
fgets(dummy,MAXSTR,fp);
fscanf(fp,"%d %*s ",&n);
printf("\n %14s %43s\n","original date:","reconstructed date");
printf("%8s %5s %6s %15s %12s %5s %6s\n","month","day","year",
"julian day","month","day","year");
for (i=1;i<=n;i++) {
fscanf(fp,"%d %d %d ",&im,&id,&iy);
fgets(dummy,MAXSTR,fp);
j=julday(im,id,iy);
caldat(j,&imm,&idd,&iyy);
printf("%10s %3d %6d %13ld %16s %3d %6d\n",name[im],
id,iy,j,name[imm],idd,iyy);
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,45 @@
/* Driver for routine chder */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#define NVAL 40
#define PIO2 1.5707963
float func(float x)
{
return x*x*(x*x-2.0)*sin(x);
}
float fder(float x)
{
return 4.0*x*(x*x-1.0)*sin(x)+x*x*(x*x-2.0)*cos(x);
}
int main(void)
{
int i,mval;
float a=(-PIO2),b=PIO2,x;
float c[NVAL],cder[NVAL];
chebft(a,b,c,NVAL,func);
/* Test derivative */
for (;;) {
printf("\nHow many terms in Chebyshev evaluation?\n");
printf("Enter n between 6 and %2d. (n=0 to end).\n",NVAL);
scanf("%d",&mval);
if ((mval <= 0) || (mval > NVAL)) break;
chder(a,b,c,cder,mval);
printf("\n%9s %14s %16s\n","x","actual","Cheby. deriv.");
for (i = -8;i<=8;i++) {
x=i*PIO2/10.0;
printf("%12.6f %12.6f %12.6f\n",
x,fder(x),chebev(a,b,cder,mval,x));
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,38 @@
/* Driver for routine chebev */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#define NVAL 40
#define PIO2 1.5707963
float func(float x)
{
return x*x*(x*x-2.0)*sin(x);
}
int main(void)
{
int i,mval;
float a=(-PIO2),b=PIO2,x,c[NVAL];
chebft(a,b,c,NVAL,func);
/* Test Chebyshev evaluation routine */
for (;;) {
printf("\nHow many terms in Chebyshev evaluation?\n");
printf("Enter n between 6 and %2d. (n=0 to end).\n",NVAL);
scanf("%d",&mval);
if ((mval <= 0) || (mval > NVAL)) break;
printf("\n%9s %14s %16s \n","x","actual","chebyshev fit");
for (i = -8;i<=8;i++) {
x=i*PIO2/10.0;
printf("%12.6f %12.6f %12.6f\n",
x,func(x),chebev(a,b,c,mval,x));
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,50 @@
/* Driver for routine chebft */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#define NVAL 40
#define PIO2 1.5707963
float func(float x)
{
return x*x*(x*x-2.0)*sin(x);
}
int main(void)
{
float a=(-PIO2),b=PIO2,dum,f;
float t0,t1,term,x,y,c[NVAL];
int i,j,mval;
chebft(a,b,c,NVAL,func);
/* test result */
for (;;) {
printf("\nHow many terms in Chebyshev evaluation?\n");
printf("Enter n between 6 and %2d. (n=0 to end).\n",NVAL);
scanf("%d",&mval);
if ((mval <= 0) || (mval > NVAL)) break;
printf("\n%9s %14s %16s\n","x","actual","chebyshev fit");
for (i = -8;i<=8;i++) {
x=i*PIO2/10.0;
y=(x-0.5*(b+a))/(0.5*(b-a));
/* Evaluate Chebyshev polynomial without CHEBEV */
t0=1.0;
t1=y;
f=c[1]*t1+c[0]*0.5;
for (j=2;j<mval;j++) {
dum=t1;
t1=2.0*y*t1-t0;
t0=dum;
term=c[j]*t1;
f += term;
}
printf("%12.6f %12.6f %12.6f\n",x,func(x),f);
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,42 @@
/* Driver for routine chebpc */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#define NVAL 40
#define PIO2 1.5707963
float func(float x)
{
return x*x*(x*x-2.0)*sin(x);
}
int main(void)
{
int i,j,mval;
float a=(-PIO2),b=PIO2,poly,x,y;
float c[NVAL],d[NVAL];
chebft(a,b,c,NVAL,func);
for (;;) {
printf("\nHow many terms in Chebyshev evaluation?\n");
printf("Enter n between 6 and %2d. (n=0 to end).\n",NVAL);
scanf("%d",&mval);
if ((mval <= 0) || (mval > NVAL)) break;
chebpc(c,d,mval);
/* Test polynomial */
printf("\n%9s %14s %14s\n","x","actual","polynomial");
for (i = -8;i<=8;i++) {
x=i*PIO2/10.0;
y=(x-0.5*(b+a))/(0.5*(b-a));
poly=d[mval-1];
for (j=mval-2;j>=0;j--) poly=poly*y+d[j];
printf("%12.6f %12.6f %12.6f\n",x,func(x),poly);
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,45 @@
/* Driver for routine chint */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#define NVAL 40
#define PIO2 1.5707963
float func(float x)
{
return x*x*(x*x-2.0)*sin(x);
}
float fint(float x)
{
return 4.0*x*(x*x-7.0)*sin(x)-(x*x*(x*x-14.0)+28.0)*cos(x);
}
int main(void)
{
int i,mval;
float a=(-PIO2),b=PIO2,x;
float c[NVAL],cint[NVAL];
chebft(a,b,c,NVAL,func);
/* test integral */
for (;;) {
printf("\nHow many terms in Chebyshev evaluation?\n");
printf("Enter n between 6 and %2d. (n=0 to end).\n",NVAL);
scanf("%d",&mval);
if ((mval <= 0) || (mval > NVAL)) break;
chint(a,b,c,cint,mval);
printf("\n%9s %14s %16s\n","x","actual","Cheby. integ.");
for (i = -8;i<=8;i++) {
x=i*PIO2/10.0;
printf("%12.6f %12.6f %12.6f\n",
x,fint(x)-fint(-PIO2),chebev(a,b,cint,mval,x));
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,65 @@
/* Driver for routine cholsl */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 3
int main(void)
{
int i,j,k;
float sum,**a,**atest,**chol,*p,*x;
static float aorig[N+1][N+1]=
{0.0,0.0,0.0,0.0,
0.0,100.0,15.0,0.01,
0.0,15.0,2.3,0.01,
0.0,0.01,0.01,1.0};
static float b[N+1]={0.0,0.4,0.02,99.0};
a=matrix(1,N,1,N);
atest=matrix(1,N,1,N);
chol=matrix(1,N,1,N);
p=vector(1,N);
x=vector(1,N);
for (i=1;i<=N;i++)
for (j=1;j<=N;j++) a[i][j]=aorig[i][j];
choldc(a,N,p);
printf("Original matrix:\n");
for (i=1;i<=N;i++) {
for (j=1;j<=N;j++) {
chol[i][j]=((i > j) ? a[i][j] : (i == j ? p[i] : 0.0));
if (i > j) chol[i][j]=a[i][j];
else chol[i][j]=(i == j ? p[i] : 0.0);
printf("%16.6e",aorig[i][j]);
}
printf("\n");
}
printf("\n");
printf("Product of Cholesky factors:\n");
for (i=1;i<=N;i++) {
for (j=1;j<=N;j++) {
for (sum=0.0,k=1;k<=N;k++) sum += chol[i][k]*chol[j][k];
atest[i][j]=sum;
printf("%16.6e",atest[i][j]);
}
printf("\n");
}
printf("\n");
printf("Check solution vector:\n");
cholsl(a,N,p,b,x);
for (i=1;i<=N;i++) {
for (sum=0.0,j=1;j<=N;j++) sum += aorig[i][j]*x[j];
p[i]=sum;
printf("%16.6e%16.6e\n",p[i],b[i]);
}
free_vector(x,1,N);
free_vector(p,1,N);
free_matrix(chol,1,N,1,N);
free_matrix(atest,1,N,1,N);
free_matrix(a,1,N,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,39 @@
/* Driver for routine chsone */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NBINS 10
#define NPTS 2000
int main(void)
{
long idum=(-15);
int i,ibin,j;
float chsq,df,prob,x,*bins,*ebins;
bins=vector(1,NBINS);
ebins=vector(1,NBINS);
for (j=1;j<=NBINS;j++) bins[j]=0.0;
for (i=1;i<=NPTS;i++) {
x=expdev(&idum);
ibin=(int) (x*NBINS/3.0)+1;
if (ibin <= NBINS) ++bins[ibin];
}
for (i=1;i<=NBINS;i++)
ebins[i]=3.0*NPTS/NBINS*exp(-3.0*(i-0.5)/NBINS);
chsone(bins,ebins,NBINS,0,&df,&chsq,&prob);
printf("%15s %15s\n","expected","observed");
for (i=1;i<=NBINS;i++)
printf("%14.2f %15.2f\n",ebins[i],bins[i]);
printf("\n%19s %10.4f\n","chi-squared:",chsq);
printf("%19s %10.4f\n","probability:",prob);
free_vector(ebins,1,NBINS);
free_vector(bins,1,NBINS);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,43 @@
/* Driver for routine chstwo */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NBINS 10
#define NPTS 2000
int main(void)
{
long idum=(-17);
int i,ibin,j;
float chsq,df,prob,x,*bins1,*bins2;
bins1=vector(1,NBINS);
bins2=vector(1,NBINS);
for (j=1;j<=NBINS;j++) {
bins1[j]=0.0;
bins2[j]=0.0;
}
for (i=1;i<=NPTS;i++) {
x=expdev(&idum);
ibin=(int) (x*NBINS/3.0+1);
if (ibin <= NBINS) ++bins1[ibin];
x=expdev(&idum);
ibin=(int) (x*NBINS/3.0+1);
if (ibin <= NBINS) ++bins2[ibin];
}
chstwo(bins1,bins2,NBINS,0,&df,&chsq,&prob);
printf("\n%15s %15s\n","dataset 1","dataset 2");
for (i=1;i<=NBINS;i++)
printf("%13.2f %15.2f\n",bins1[i],bins2[i]);
printf("\n%18s %12.4f\n","chi-squared:",chsq);
printf("%18s %12.4f\n","probability:",prob);
free_vector(bins2,1,NBINS);
free_vector(bins1,1,NBINS);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,40 @@
/* Driver for routine cisi */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float ci,si,x,xci,xsi;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Cosine and Sine Integrals",25)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %12s %12s %12s \n",
"x","actual","ci(x)","actual","si(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f",&x,&xci,&xsi);
cisi(x,&ci,&si);
printf("%6.2f %12.6f %12.6f %12.6f %12.6f\n",
x,xci,ci,xsi,si);
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,55 @@
/* Driver for routine cntab1 */
#include <stdio.h>
#include <stdlib.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NDAT 9
#define NMON 12
#define MAXSTR 80
int main(void)
{
int i,j,**nmbr;
float ccc,chisq,cramrv,df,prob;
char dummy[MAXSTR],fate[NDAT+1][16],mon[NMON+1][6],txt[16];
FILE *fp;
nmbr=imatrix(1,NDAT,1,NMON);
if ((fp = fopen("table1.dat","r")) == NULL)
nrerror("Data file table1.dat not found\n");
fgets(dummy,MAXSTR,fp);
fgets(dummy,MAXSTR,fp);
fscanf(fp,"%16c",txt);
txt[15]='\0';
for (i=1;i<=12;i++) fscanf(fp," %s",mon[i]);
fgets(dummy,MAXSTR,fp);
fgets(dummy,MAXSTR,fp);
for (i=1;i<=NDAT;i++) {
fscanf(fp,"%16[^0123456789]",fate[i]);
fate[i][15]='\0';
for (j=1;j<=12;j++)
fscanf(fp,"%d ",&nmbr[i][j]);
}
fclose(fp);
printf("\n%s",txt);
for (i=1;i<=12;i++) printf("%5s",mon[i]);
printf("\n\n");
for (i=1;i<=NDAT;i++) {
printf("%s",fate[i]);
for (j=1;j<=12;j++) printf("%5d",nmbr[i][j]);
printf("\n");
}
cntab1(nmbr,NDAT,NMON,&chisq,&df,&prob,&cramrv,&ccc);
printf("\n%15s chi-squared %20.2f\n"," ",chisq);
printf("%15s degrees of freedom%20.2f\n"," ",df);
printf("%15s probability %20.4f\n"," ",prob);
printf("%15s cramer-v %20.4f\n"," ",cramrv);
printf("%15s contingency coeff.%20.4f\n"," ",ccc);
free_imatrix(nmbr,1,NDAT,1,NMON);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,58 @@
/* Driver for routine cntab2 */
#include <stdio.h>
#include <stdlib.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NI 9
#define NMON 12
#define MAXSTR 80
int main(void)
{
float h,hx,hxgy,hy,hygx,uxgy,uxy,uygx;
int i,j,**nmbr;
char dummy[MAXSTR],fate[NI+1][16],mon[NMON+1][6],txt[16];
FILE *fp;
nmbr=imatrix(1,NI,1,NMON);
if ((fp = fopen("table1.dat","r")) == NULL)
nrerror("Data file table1.dat not found\n");
fgets(dummy,MAXSTR,fp);
fgets(dummy,MAXSTR,fp);
fscanf(fp,"%16c",txt);
txt[15]='\0';
for (i=1;i<=12;i++) fscanf(fp," %s",mon[i]);
fgets(dummy,MAXSTR,fp);
fgets(dummy,MAXSTR,fp);
for (i=1;i<=NI;i++) {
fscanf(fp,"%16[^0123456789]",fate[i]);
fate[i][15]='\0';
for (j=1;j<=12;j++)
fscanf(fp,"%d ",&nmbr[i][j]);
}
fclose(fp);
printf("\n%s",txt);
for (i=1;i<=12;i++) printf("%5s",mon[i]);
printf("\n\n");
for (i=1;i<=NI;i++) {
printf("%s",fate[i]);
for (j=1;j<=12;j++) printf("%5d",nmbr[i][j]);
printf("\n");
}
cntab2(nmbr,NI,NMON,&h,&hx,&hy,&hygx,&hxgy,&uygx,&uxgy,&uxy);
printf("\n entropy of table %10.4f\n",h);
printf(" entropy of x-distribution %10.4f\n",hx);
printf(" entropy of y-distribution %10.4f\n",hy);
printf(" entropy of y given x %10.4f\n",hygx);
printf(" entropy of x given y %10.4f\n",hxgy);
printf(" dependency of y on x %10.4f\n",uygx);
printf(" dependency of x on y %10.4f\n",uxgy);
printf(" symmetrical dependency %10.4f\n",uxy);
free_imatrix(nmbr,1,NI,1,NMON);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,54 @@
/* Driver for routine convlv */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 16 /* data array size */
#define M 9 /* response function dimension - must be odd */
#define N2 (2*N)
int main(void)
{
unsigned long i,j;
int isign;
float cmp,*data,*respns,*resp,*ans;
data=vector(1,N);
respns=vector(1,N);
resp=vector(1,N);
ans=vector(1,N2);
for (i=1;i<=N;i++)
if ((i >= N/2-N/8) && (i <= N/2+N/8))
data[i]=1.0;
else
data[i]=0.0;
for (i=1;i<=M;i++) {
if ((i > 2) && (i < 7))
respns[i]=1.0;
else
respns[i]=0.0;
resp[i]=respns[i];
}
isign=1;
convlv(data,N,resp,M,isign,ans);
/* compare with a direct convolution */
printf("%3s %14s %13s\n","i","CONVLV","Expected");
for (i=1;i<=N;i++) {
cmp=0.0;
for (j=1;j<=M/2;j++) {
cmp += data[((i-j-1+N) % N)+1]*respns[j+1];
cmp += data[((i+j-1) % N)+1]*respns[M-j+1];
}
cmp += data[i]*respns[1];
printf("%3ld %15.6f %12.6f\n",i,ans[i],cmp);
}
free_vector(ans,1,N2);
free_vector(resp,1,N);
free_vector(respns,1,N);
free_vector(data,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,41 @@
/* Driver for routine correl */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 64
#define N2 (2*N)
int main(void)
{
unsigned long i,j;
float cmp,*data1,*data2,*ans;
data1=vector(1,N);
data2=vector(1,N);
ans=vector(1,N2);
for (i=1;i<=N;i++) {
if ((i > N/2-N/8) && (i < N/2+N/8))
data1[i]=1.0;
else
data1[i]=0.0;
data2[i]=data1[i];
}
correl(data1,data2,N,ans);
/* Calculate directly */
printf("%3s %14s %18s\n","n","CORREL","direct calc.");
for (i=0;i<=16;i++) {
cmp=0.0;
for (j=1;j<=N;j++)
cmp += data1[((i+j-1) % N)+1]*data2[j];
printf("%3ld %15.6f %15.6f\n",i,ans[i+1],cmp);
}
free_vector(ans,1,N2);
free_vector(data2,1,N);
free_vector(data1,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,61 @@
/* Driver for routine cosft1 */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define EPS 1.0e-3
#define NP 17
#define WIDTH 30.0
#define PI 3.1415926
int main(void)
{
int i,j,nlim;
float big,per,scal,small,*data;
data=vector(1,NP);
for (;;) {
printf("Period of cosine in channels (2-%2d)\n",NP);
scanf("%f",&per);
if (per <= 0.0) break;
for (i=1;i<=NP;i++)
data[i]=cos(2.0*PI*(i-1)/per);
cosft1(data,NP-1);
big = -1.0e10;
small=1.0e10;
for (i=1;i<=NP;i++) {
if (data[i] < small) small=data[i];
if (data[i] > big) big=data[i];
}
scal=WIDTH/(big-small);
for (i=1;i<=NP;i++) {
nlim=(int) (0.5+scal*(data[i]-small)+EPS);
printf("%4d %6.2f ",i,data[i]);
for (j=1;j<=nlim+1;j++) printf("*");
printf("\n");
}
printf("press RETURN to continue ...\n");
(void) getchar();
cosft1(data,NP-1);
big = -1.0e10;
small=1.0e10;
for (i=1;i<=NP;i++) {
if (data[i] < small) small=data[i];
if (data[i] > big) big=data[i];
}
scal=WIDTH/(big-small);
for (i=1;i<=NP;i++) {
nlim=(int) (0.5+scal*(data[i]-small)+EPS);
printf("%4d ",i);
for (j=1;j<=nlim+1;j++) printf("*");
printf("\n");
}
}
free_vector(data,1,NP);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,61 @@
/* Driver for routine cosft2 */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define EPS 1.0e-3
#define NP 16
#define WIDTH 30.0
#define PI 3.1415926
int main(void)
{
int i,j,nlim;
float big,per,scal,small,*data;
data=vector(1,NP);
for (;;) {
printf("Period of cosine in channels (2-%2d)\n",NP);
scanf("%f",&per);
if (per <= 0.0) break;
for (i=1;i<=NP;i++)
data[i]=cos(2.0*PI*(i-0.5)/per);
cosft2(data,NP,1);
big = -1.0e10;
small=1.0e10;
for (i=1;i<=NP;i++) {
if (data[i] < small) small=data[i];
if (data[i] > big) big=data[i];
}
scal=WIDTH/(big-small);
for (i=1;i<=NP;i++) {
nlim=(int) (0.5+scal*(data[i]-small)+EPS);
printf("%4d %6.2f ",i,data[i]);
for (j=1;j<=nlim+1;j++) printf("*");
printf("\n");
}
printf("press RETURN to continue ...\n");
(void) getchar();
cosft2(data,NP,-1);
big = -1.0e10;
small=1.0e10;
for (i=1;i<=NP;i++) {
if (data[i] < small) small=data[i];
if (data[i] > big) big=data[i];
}
scal=WIDTH/(big-small);
for (i=1;i<=NP;i++) {
nlim=(int) (0.5+scal*(data[i]-small)+EPS);
printf("%4d ",i);
for (j=1;j<=nlim+1;j++) printf("*");
printf("\n");
}
}
free_vector(data,1,NP);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,57 @@
/* Driver for routine covsrt */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MA 10
#define MFIT 5
int main(void)
{
int i,j,*ia;
float **covar;
ia=ivector(1,MA);
covar=matrix(1,MA,1,MA);
for (i=1;i<=MA;i++)
for (j=1;j<=MA;j++) {
covar[i][j]=0.0;
if ((i <= MFIT) && (j <= MFIT))
covar[i][j]=i+j-1;
}
printf("\noriginal matrix\n");
for (i=1;i<=MA;i++) {
for (j=1;j<=MA;j++) printf("%4.1f",covar[i][j]);
printf("\n");
}
printf("press RETURN to continue...\n");
(void) getchar();
printf("\nTest #1 - full fitting\n");
for (i=1;i<=MA;i++) ia[i]=1;
covsrt(covar,MA,ia,MA);
for (i=1;i<=MA;i++) {
for (j=1;j<=MA;j++) printf("%4.1f",covar[i][j]);
printf("\n");
}
printf("press RETURN to continue...\n");
(void) getchar();
printf("\nTest #2 - spread\n");
for (i=1;i<=MA;i++)
for (j=1;j<=MA;j++) {
covar[i][j]=0.0;
if ((i <= MFIT) && (j <= MFIT)) covar[i][j]=i+j-1;
}
for (i=1;i<=MA;i+=2) ia[i]=0;
covsrt(covar,MA,ia,MFIT);
for (i=1;i<=MA;i++) {
for (j=1;j<=MA;j++) printf("%4.1f",covar[i][j]);
printf("\n");
}
free_matrix(covar,1,MA,1,MA);
free_ivector(ia,1,MA);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,76 @@
/* Driver for routine crank */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NDAT 20
#define NMON 12
#define MAXSTR 80
int main(void)
{
int i,j;
float *data,*order,*s,**rays;
char dummy[MAXSTR],txt[MAXSTR],city[NDAT+1][17],mon[NMON+1][5];
FILE *fp;
data=vector(1,NDAT);
order=vector(1,NDAT);
s=vector(1,NMON);
rays=matrix(1,NDAT,1,NMON);
if ((fp = fopen("table2.dat","r")) == NULL)
nrerror("Data file table2.dat not found\n");
fgets(dummy,MAXSTR,fp);
fgets(txt,MAXSTR,fp);
fscanf(fp,"%*15c");
for (i=1;i<=NMON;i++) fscanf(fp," %s",mon[i]);
fgets(dummy,MAXSTR,fp);
fgets(dummy,MAXSTR,fp);
for (i=1;i<=NDAT;i++) {
fscanf(fp,"%[^0123456789]",city[i]);
city[i][16]='\0';
for (j=1;j<=NMON;j++) fscanf(fp,"%f",&rays[i][j]);
fgets(dummy,MAXSTR,fp);
}
fclose(fp);
printf("%s\n%16s",txt," ");
for (i=1;i<=12;i++) printf(" %s",mon[i]);
printf("\n");
for (i=1;i<=NDAT;i++) {
printf("%s",city[i]);
for (j=1;j<=12;j++)
printf("%4d",(int) (0.5+rays[i][j]));
printf("\n");
}
printf(" press return to continue ...\n");
getchar();
/* Replace solar flux in each column by rank order */
for (j=1;j<=12;j++) {
for (i=1;i<=NDAT;i++) {
data[i]=rays[i][j];
order[i]=i;
}
sort2(NDAT,data,order);
crank(NDAT,data,&s[j]);
for (i=1;i<=NDAT;i++)
rays[(int) (0.5+order[i])][j]=data[i];
}
printf("%16s"," ");
for (i=1;i<=12;i++) printf(" %s",mon[i]);
printf("\n");
for (i=1;i<=NDAT;i++) {
printf("%s",city[i]);
for (j=1;j<=12;j++)
printf("%4d",(int) (0.5+rays[i][j]));
printf("\n");
}
free_matrix(rays,1,NDAT,1,NMON);
free_vector(s,1,NMON);
free_vector(order,1,NDAT);
free_vector(data,1,NDAT);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,54 @@
/* Driver for routine cyclic */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 20
int main(void)
{
float alpha,beta,d,*a,*b,*c,*r,*x,**aa;
int i,j,*indx;
long idum=(-23);
indx=ivector(1,N);
a=vector(1,N);
b=vector(1,N);
c=vector(1,N);
r=vector(1,N);
x=vector(1,N);
aa=matrix(1,N,1,N);
for (i=1;i<=N;i++)
for (j=1;j<=N;j++) aa[i][j]=0.0;
for (i=1;i<=N;i++) {
b[i]=ran2(&idum);
aa[i][i]=b[i];
r[i]=ran2(&idum);
}
for (i=1;i<N;i++) {
a[i+1]=ran2(&idum);
aa[i+1][i]=a[i+1];
c[i]=ran2(&idum);
aa[i][i+1]=c[i];
}
alpha=ran2(&idum);
aa[N][1]=alpha;
beta=ran2(&idum);
aa[1][N]=beta;
cyclic(a,b,c,alpha,beta,r,x,N);
ludcmp(aa,N,indx,&d);
lubksb(aa,N,indx,r);
for (i=1;i<=N;i++) printf("%4d %12.6e\n",i,(x[i]-r[i])/(x[i]+r[i]));
free_matrix(aa,1,N,1,N);
free_vector(x,1,N);
free_vector(r,1,N);
free_vector(c,1,N);
free_vector(b,1,N);
free_vector(a,1,N);
free_ivector(indx,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine dawson */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Dawson integral",15)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %11s \n","x","actual","dawson(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.6f %12.6f\n",x,val,dawson(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,53 @@
/* Driver for routine dbrent */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#define TOL 1.0e-6
#define EQL 1.0e-4
float dfunc(float x)
{
return -bessj1(x);
}
float func(float x)
{
return bessj0(x);
}
int main(void)
{
int i,iflag,j,nmin=0;
float ax,bx,cx,fa,fb,fc,xmin,dbr,amin[21];
printf("\nMinima of the function bessj0\n");
printf("%10s %8s %16s %12s %11s\n",
"min. #","x","bessj0(x)","bessj1(x)","DBRENT");
for (i=1;i<=100;i++) {
ax=i;
bx=i+1.0;
mnbrak(&ax,&bx,&cx,&fa,&fb,&fc,func);
dbr=dbrent(ax,bx,cx,func,dfunc,TOL,&xmin);
if (nmin == 0) {
amin[1]=xmin;
nmin=1;
printf("%7d %15.6f %12.6f %12.6f %12.6f\n",
nmin,xmin,func(xmin),dfunc(xmin),dbr);
} else {
iflag=0;
for (j=1;j<=nmin;j++)
if (fabs(xmin-amin[j]) <= EQL*xmin) iflag=1;
if (iflag == 0) {
amin[++nmin]=xmin;
printf("%7d %15.6f %12.6f %12.6f %12.6f\n",
nmin,xmin,func(xmin),dfunc(xmin),dbr);
}
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,45 @@
/* Driver for routine ddpoly */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NC 5
#define ND NC-1
#define NP 20
int main(void)
{
int i,j,k;
float x,pwr,*pd,**d;
static float c[NC+1]={-1.0,5.0,-10.0,10.0,-5.0,1.0};
static char *a[ND+1]={"polynomial:", "first deriv:",
"second deriv:","third deriv:","fourth deriv:"};
pd=vector(0,ND);
d=matrix(0,ND,1,NP);
for (i=1;i<=NP;i++) {
x=0.1*i;
ddpoly(c,NC,x,pd,ND);
for (j=0;j<=ND;j++) d[j][i]=pd[j];
}
for (i=0;i<=ND;i++) {
printf("%6s %s \n"," ",a[i]);
printf("%12s %17s %15s\n","x","DDPOLY","actual");
for (j=1;j<=NP;j++) {
x=0.1*j;
pwr=1.0;
for (k=1;k<=NC-i;k++) pwr *= x-1.0;
printf("%15.6f %15.6f %15.6f\n",x,d[i][j],
(factrl(NC)/factrl(NC-i))*pwr);
}
printf("press ENTER to continue...\n");
(void) getchar();
}
free_matrix(d,0,ND,1,NP);
free_vector(pd,0,ND);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,59 @@
/* Driver for routine decchk */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#define MAXLINE 128
int main(void)
{
int j,k,l,n,nbad=0,ntot=0;
int iok,jok;
char lin[MAXLINE+2],ch,chh;
/* test all jump transpositions of the form 86jlk41 */
lin[0]='8';
lin[1]='6';
lin[5]='4';
lin[6]='1';
for (j=48;j<=57;j++) {
for (k=48;k<=57;k++) {
for (l=48;l<=57;l++) {
lin[3]=l;
if (j != k) {
ntot++;
lin[2]=j;
lin[4]=k;
iok=decchk(lin,7,&ch);
lin[7]=ch;
iok=decchk(lin,8,&chh);
lin[2]=k;
lin[4]=j;
jok=decchk(lin,8,&chh);
if (!iok || jok) nbad++;
}
}
}
}
printf("%s %14s %3d\n","Total tries:"," ",ntot);
printf("%s %16s %3d\n","Bad tries:"," ",nbad);
printf("%s %11s %4.2f\n","Fraction good:"," ",((float)(ntot-nbad))/ntot);
for (;;) {
printf("enter string terminated by x:\n");
if (gets(lin) == NULL) break;
for (j=0;j<MAXLINE;j++)
if (lin[j] == 'x') break;
n=j;
if (!n) break;
iok=decchk(lin,n,&ch);
lin[n]=ch;
jok=decchk(lin,n+1,&chh);
lin[n+1]=0;
printf("%s checks as %c\n",lin,jok ? 'T' : 'F');
}
printf("Normal completion\n");
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,38 @@
/* Driver for routine df1dim */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NDIM 3
int ncom; /* defining declarations */
float *pcom,*xicom;
void (*nrdfun)(float [], float []);
void dfunc(float x[], float df[])
{
int i;
for (i=1;i<=3;i++) df[i]=(x[i]-1.0)*(x[i]-1.0);
}
int main(void)
{
ncom=NDIM;
pcom=vector(1,ncom);
xicom=vector(1,ncom);
nrdfun=dfunc;
printf("\nEnter vector direction along which to\n");
printf("plot the function. Minimum is in the\n");
printf("direction 1.0 1.0 1.0 - enter x y z:\n\n");
pcom[1]=pcom[2]=pcom[3]=0.0;
scanf("%f %f %f",&xicom[1],&xicom[2],&xicom[3]);
scrsho(df1dim);
free_vector(xicom,1,ncom);
free_vector(pcom,1,ncom);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,55 @@
/* Driver for routine dfpmin */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
static int nfunc,ndfunc;
float func(float x[])
{
float x1p2sqr=SQR(2.0+x[1]);
nfunc++;
return 10.0*
SQR(SQR(x[2])*(3.0-x[1])-SQR(x[1])*(3.0+x[1]))+
x1p2sqr/(1.0+x1p2sqr);
}
void dfunc(float x[],float df[])
{
float x1sqr=SQR(x[1]),x2sqr=SQR(x[2]),x1p2=x[1]+2.0;
float x1p2sqr=SQR(x1p2);
ndfunc++;
df[1]=20.0*(x2sqr*(3.0-x[1])-x1sqr*(3.0+x[1]))*(-x2sqr-6.0*x[1]-3.0*x1sqr)+
2.0*x1p2/(1.0+x1p2sqr)-2.0*x1p2*x1p2sqr/SQR((1.0+x1p2sqr));
df[2]=40.0*(x2sqr*(3.0-x[1])-x1sqr*(3.0+x[1]))*x[2]*(3.0-x[1]);
}
#define NDIM 2
#define GTOL 1.0e-4
int main(void)
{
int iter;
float *p,fret;
p=vector(1,NDIM);
printf("True minimum is at (-2.0,+-0.89442719)\n");
nfunc=ndfunc=0;
p[1]=0.1;
p[2]=4.2;
printf("Starting vector: (%7.4f,%7.4f)\n",p[1],p[2]);
dfpmin(p,NDIM,GTOL,&iter,&fret,func,dfunc);
printf("Iterations: %3d\n",iter);
printf("Func. evals: %3d\n",nfunc);
printf("Deriv. evals: %3d\n",ndfunc);
printf("Solution vector: (%9.6f,%9.6f)\n",p[1],p[2]);
printf("Func. value at solution %14.6g\n",fret);
free_vector(p,1,NDIM);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,27 @@
/* Driver for routine dfridr */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
float func(float x)
{
return tan(x);
}
int main(void)
{
float x,h,dx,err;
printf("input x, h\n");
while (scanf("%f %f",&x,&h) != EOF) {
dx=dfridr(func,x,h,&err);
printf("dfridr=%12.6f %12.6f %12.6f\n",dx,1.0/SQR(cos(x)),err);
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,52 @@
/* Driver for routine dftint */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
static float c,d;
float coscxd(float x)
{
return cos(c*x+d);
}
#define ci(x) (sin((w-c)*x-d)/(2.0*(w-c))+sin((w+c)*x+d)/(2.0*(w+c)))
#define si(x) (-cos((w-c)*x-d)/(2.0*(w-c))-cos((w+c)*x+d)/(2.0*(w+c)))
void getans(float w,float a,float b,float *cans,float *sans)
{
*cans=ci(b)-ci(a);
*sans=si(b)-si(a);
}
int main(void)
{
float a,b,cans,cosint,sans,sinint,w;
printf(" Omega Integral cosine*test func Err");
printf(" Integral sine*test func Err\n");
for (;;) {
printf("input c,d:\n");
if (scanf("%f %f",&c,&d) == EOF) break;
for (;;) {
printf("input a,b:\n");
if (scanf("%f %f",&a,&b) == EOF) break;
if (a == b) break;
for (;;) {
printf("input w:\n");
if (scanf("%f",&w) == EOF) break;
if (w < 0.0) break;
dftint(coscxd,a,b,w,&cosint,&sinint);
getans(w,a,b,&cans,&sans);
printf("%15.6e %15.6e %15.6e %15.6e %15.6e\n",
w,cans,cosint-cans,sans,sinint-sans);
}
}
}
printf("normal completion\n");
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,55 @@
/* Driver for routine dlinmin */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NDIM 3
#define PIO2 1.5707963
float func(float x[])
{
int i;
float f=0.0;
for (i=1;i<=3;i++) f += (x[i]-1.0)*(x[i]-1.0);
return f;
}
void dfunc(float x[],float df[])
{
int i;
for (i=1;i<=3;i++) df[i]=2.0*(x[i]-1.0);
}
int main(void)
{
int i,j;
float fret,sr2,x,*p,*xi;
p=vector(1,NDIM);
xi=vector(1,NDIM);
printf("\nMinimum of a 3-d quadratic centered\n");
printf("at (1.0,1.0,1.0). Minimum is found\n");
printf("along a series of radials.\n\n");
printf("%9s %12s %12s %14s \n","x","y","z","minimum");
for (i=0;i<=10;i++) {
x=PIO2*i/10.0;
sr2=sqrt(2.0);
xi[1]=sr2*cos(x);
xi[2]=sr2*sin(x);
xi[3]=1.0;
p[1]=p[2]=p[3]=0.0;
dlinmin(p,xi,NDIM,&fret,func,dfunc);
for (j=1;j<=3;j++) printf("%12.6f ",p[j]);
printf("%12.6f\n",fret);
}
free_vector(xi,1,NDIM);
free_vector(p,1,NDIM);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,43 @@
/* Driver for routine eclass */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define M 11
#define N 15
int main(void)
{
int i,j,k,lclas=0,nclass,*nf,*nflag,*nsav;
static int lista[]={0,1,1,5,2,6,2,7,11,3,4,12},
listb[]={0,5,9,13,6,10,14,3,7,15,8,4};
nf=ivector(1,N);
nflag=ivector(1,N);
nsav=ivector(1,N);
eclass(nf,N,lista,listb,M);
for (i=1;i<=N;i++) nflag[i]=1;
printf("\nNumbers from 1-%d divided according to\n",N);
printf("their value modulo 4:\n\n");
for (i=1;i<=N;i++) {
nclass=nf[i];
if (nflag[nclass]) {
nflag[nclass]=0;
lclas++;
k=0;
for (j=i;j<=N;j++)
if (nf[j] == nf[i]) nsav[++k]=j;
printf("Class %2d: ",lclas);
for (j=1;j<=k;j++) printf("%3d",nsav[j]);
printf("\n");
}
}
free_ivector(nsav,1,N);
free_ivector(nflag,1,N);
free_ivector(nf,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,45 @@
/* Driver for routine eclazz */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 15
int equiv(int i,int j)
{
return (i % 4) == (j % 4);
}
int main(void)
{
int i,j,k,lclas=0,nclass,*nf,*nflag,*nsav;
nf=ivector(1,N);
nflag=ivector(1,N);
nsav=ivector(1,N);
eclazz(nf,N,equiv);
for (i=1;i<=N;i++) nflag[i]=1;
printf("\nNumbers from 1-%d divided according to\n",N);
printf("their value modulo 4:\n");
for (i=1;i<=N;i++) {
nclass=nf[i];
if (nflag[nclass]) {
nflag[nclass]=0;
lclas++;
k=0;
for (j=i;j<=N;j++)
if (nf[j] == nf[i]) nsav[++k]=j;
printf("Class %2d: ",lclas);
for (j=1;j<=k;j++) printf("%3d",nsav[j]);
printf("\n");
}
}
free_ivector(nsav,1,N);
free_ivector(nflag,1,N);
free_ivector(nf,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine ei */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Exponential Integral Ei",23)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %11s \n","x","actual","ei(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.6f %12.6f\n",x,val,ei(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,59 @@
/* Driver for routine eigsrt */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NP 10
int main(void)
{
int i,j,nrot;
static float c[NP][NP]=
{5.0,4.3,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0,-4.0,
4.3,5.1,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0,
3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0,
2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,-1.0,
1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,
0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,
-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,
-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,
-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,
-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0};
float *d,**v,**e;
d=vector(1,NP);
v=matrix(1,NP,1,NP);
e=convert_matrix(&c[0][0],1,NP,1,NP);
printf("****** Finding Eigenvectors ******\n");
jacobi(e,NP,d,v,&nrot);
printf("unsorted eigenvectors:\n");
for (i=1;i<=NP;i++) {
printf("eigenvalue %3d = %12.6f\n",i,d[i]);
printf("eigenvector:\n");
for (j=1;j<=NP;j++) {
printf("%12.6f",v[j][i]);
if ((j % 5) == 0) printf("\n");
}
printf("\n");
}
printf("\n****** Sorting Eigenvectors ******\n\n");
eigsrt(d,v,NP);
printf("sorted eigenvectors:\n");
for (i=1;i<=NP;i++) {
printf("eigenvalue %3d = %12.6f\n",i,d[i]);
printf("eigenvector:\n");
for (j=1;j<=NP;j++) {
printf("%12.6f",v[j][i]);
if ((j % 5) == 0) printf("\n");
}
printf("\n");
}
free_convert_matrix(e,1,NP,1,NP);
free_matrix(v,1,NP,1,NP);
free_vector(d,1,NP);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,43 @@
/* Driver for routine elle */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
#define FAC (3.1415926535/180.0)
int main(void)
{
char txt[MAXSTR];
int i,nval;
float ak,alpha,phi,val;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Legendre Elliptic Integral Second Kind",38)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %10s %11s %22s\n","phi","sin(alpha)","actual","elle(phi,ak)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f",&phi,&alpha,&val);
alpha=alpha*FAC;
ak=sin(alpha);
phi=phi*FAC;
printf("%6.2f %6.2f %18.6e %18.6e\n",phi,ak,val,elle(phi,ak));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,43 @@
/* Driver for routine ellf */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
#define FAC (3.1415926535/180.0)
int main(void)
{
char txt[MAXSTR];
int i,nval;
float ak,alpha,phi,val;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Legendre Elliptic Integral First Kind",37)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %10s %11s %22s\n","phi","sin(alpha)","actual","ellf(phi,ak)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f",&phi,&alpha,&val);
alpha=alpha*FAC;
ak=sin(alpha);
phi=phi*FAC;
printf("%6.2f %6.2f %18.6e %18.6e\n",phi,ak,val,ellf(phi,ak));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,47 @@
/* Driver for routine ellpi */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
#define FAC (3.1415926535/180.0)
int main(void)
{
char txt[MAXSTR];
int i,nval;
float ak,alpha,en,phi,val;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Legendre Elliptic Integral Third Kind",37)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%6s %6s %6s %10s %23s\n",
"phi","-en","sin(alpha)","actual","ellpi(phi,ak)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f %f",&phi,&en,&alpha,&val);
alpha=alpha*FAC;
ak=sin(alpha);
en = -en;
phi=phi*FAC;
printf("%6.2f %6.2f %6.2f %18.6e %18.6e\n",
phi,en,ak,val,ellpi(phi,en,ak));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,46 @@
/* Driver for routine elmhes */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NP 5
int main(void)
{
int i,j;
static float b[NP][NP]=
{1.0,2.0,300.0,4.0,5.0,
2.0,3.0,400.0,5.0,6.0,
3.0,4.0,5.0,6.0,7.0,
4.0,5.0,600.0,7.0,8.0,
5.0,6.0,700.0,8.0,9.0};
float **a;
a=convert_matrix(&b[0][0],1,NP,1,NP);
printf("***** original matrix *****\n");
for (i=1;i<=NP;i++) {
for (j=1;j<=NP;j++) printf("%12.2f",a[i][j]);
printf("\n");
}
printf("***** balance matrix *****\n");
balanc(a,NP);
for (i=1;i<=NP;i++) {
for (j=1;j<=NP;j++) printf("%12.2f",a[i][j]);
printf("\n");
}
printf("***** reduce to hessenberg form *****\n");
elmhes(a,NP);
for (j=1;j<=NP-2;j++)
for (i=j+2;i<=NP;i++)
a[i][j]=0.0;
for (i=1;i<=NP;i++) {
for (j=1;j<=NP;j++) printf("%12.2e",a[i][j]);
printf("\n");
}
free_convert_matrix(a,1,NP,1,NP);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,38 @@
/* Driver for routine erfcc */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float x,val;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Error Function",14)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\ncomplementary error function\n");
printf("%5s %12s %13s\n","x","actual","erfcc(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
val=1.0-val;
printf("%6.2f %12.7f %12.7f\n",x,val,erfcc(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine erff */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Error Function",14)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%4s %12s %12s\n","x","actual","erf(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
printf("%6.2f %12.7f %12.7f\n",x,val,erff(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,38 @@
/* Driver for routine erffc */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float x,val;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Error Function",14)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\ncomplementary error function\n");
printf("%5s %12s %13s\n","x","actual","erfc(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&val);
val=1.0-val;
printf("%6.2f %12.7f %12.7f\n",x,val,erffc(x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,41 @@
/* Driver for routine eulsum */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NVAL 40
int main(void)
{
int i,j,mval;
float sum,term,x,xpower,*wksp;
wksp=vector(1,NVAL);
/* evaluate ln(1+x)=x-x^2/2+x^3/3-x^4/4 ... for -1<x<1 */
for (;;) {
printf("\nHow many terms in polynomial?\n");
printf("Enter n between 1 and %2d. (n=0 to end) ",NVAL);
scanf("%d",&mval);
printf("\n");
if ((mval <= 0) || (mval > NVAL)) break;
printf("%9s %14s %14s\n","x","actual","polynomial");
for (i = -8;i<=8;i++) {
x=i/10.0;
sum=0.0;
xpower = -1;
for (j=1;j<=mval;j++) {
xpower *= (-x);
term=xpower/j;
eulsum(&sum,term,j,wksp);
}
printf("%12.6f %12.6f %12.6f\n",x,log(1.0+x),sum);
}
}
free_vector(wksp,1,NVAL);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,35 @@
/* Driver for routine evlmem */
#include <stdio.h>
#include <stdlib.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 1000
#define M 10
#define NFDT 16
int main(void)
{
int i;
float fdt,pm,*cof,*data;
FILE *fp;
cof=vector(1,M);
data=vector(1,N);
if ((fp = fopen("spctrl.dat","r")) == NULL)
nrerror("Data file spctrl.dat not found\n");
for (i=1;i<=N;i++) fscanf(fp,"%f",&data[i]);
fclose(fp);
memcof(data,N,M,&pm,cof);
printf("Power spectum estimate of data in spctrl.dat\n");
printf(" f*delta power\n");
for (fdt=0.0;fdt<=0.5;fdt+=0.5/NFDT)
printf("%12.6f %12.6f\n",fdt,evlmem(fdt,cof,M,pm));
free_vector(data,1,N);
free_vector(cof,1,M);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,39 @@
/* Driver for routine expdev */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#define NPTS 10000
#define EE 2.718281828
int main(void)
{
long idum=(1);
int i,j,total=0,x[21];
float expect,xx,y,trig[21];
for (i=0;i<=20;i++) {
trig[i]=i/20.0;
x[i]=0;
}
for (i=1;i<=NPTS;i++) {
y=expdev(&idum);
for (j=1;j<=20;j++)
if ((y < trig[j]) && (y > trig[j-1])) ++x[j];
}
for (i=1;i<=20;i++) total += x[i];
printf("\nexponential distribution with %7d points\n",NPTS);
printf(" interval observed expected\n\n");
for (i=1;i<=20;i++) {
xx=(float) x[i]/total;
expect=exp(-(trig[i-1]+trig[i])/2.0);
expect *= (0.05*EE/(EE-1));
printf("%6.2f %6.2f %12.6f %12.6f \n",
trig[i-1],trig[i],xx,expect);
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine expint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval,n;
float val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Exponential Integral En",23)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%4s %7s %15s %21s \n","n","x","actual","expint(n,x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%d %f %f",&n,&x,&val);
printf("%4d %8.2f %18.6e %18.6e\n",n,x,val,expint(n,x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,39 @@
/* Driver for routine f1dim */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
float func(float x[])
{
int i;
float f=0.0;
for (i=1;i<=3;i++) f += (x[i]-1.0)*(x[i]-1.0);
return f;
}
#define NDIM 3
int ncom; /* defining declarations */
float *pcom,*xicom,(*nrfunc)(float []);
int main(void)
{
ncom=NDIM;
pcom=vector(1,ncom);
xicom=vector(1,ncom);
nrfunc=func;
pcom[1]=pcom[2]=pcom[3]=0.0;
printf("\nEnter vector direction along which to\n");
printf("plot the function. Minimum is in the\n");
printf("direction 1.0 1.0 1.0 - enter x y z:\n");
scanf(" %f %f %f",&xicom[1],&xicom[2],&xicom[3]);
scrsho(f1dim);
free_vector(xicom,1,ncom);
free_vector(pcom,1,ncom);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,38 @@
/* Driver for routine factln */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,n,nval;
float val;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"N-factorial",11)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\nlog of n_factorial\n");
printf("\n%6s %19s %21s\n","n","actual","factln(n)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%d %f",&n,&val);
printf("%6d %20.7f %20.7f\n",n,log(val),factln(n));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,41 @@
/* Driver for routine factrl */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
float actual;
int i,n,nval;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"N-factorial",11)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%6s %18s %20s \n","n","actual","factrl(n)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%d %f ",&n,&actual);
if (actual < 1.0e10)
printf("%6d %20.0f %20.0f\n",n,actual,factrl(n));
else
printf("%6d %20e %20e \n",n,actual,factrl(n));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,44 @@
/* Driver for routine fasper */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NP 90
#define MP 4096
#define NPR 11
#define TWOPI 6.2831853
int main(void)
{
long idum=(-4);
unsigned long j=0,jmax,n,nout;
float prob,*px,*py,*x,*y;
x=vector(1,NP);
y=vector(1,NP);
px=vector(1,MP);
py=vector(1,MP);
for (n=1;n<=NP+10;n++) {
if (n != 3 && n != 4 && n != 6 && n != 21 &&
n != 38 && n != 51 && n != 67 && n != 68 &&
n != 83 && n != 93) {
x[++j]=n;
y[j]=0.75*cos(0.6*x[j])+gasdev(&idum);
}
}
fasper(x,y,j,4.0,1.0,px,py,MP,&nout,&jmax,&prob);
printf("fasper results for test signal (cos(0.6x) + noise):\n");
printf("nout,jmax,prob=%ld %ld %12.6g\n",nout,jmax,prob);
for (n=LMAX(1,jmax-NPR/2);n<=LMIN(nout,jmax+NPR/2);n++)
printf("%ld %12.6f %12.6f\n",n,TWOPI*px[n],py[n]);
free_vector(py,1,MP);
free_vector(px,1,MP);
free_vector(y,1,NP);
free_vector(x,1,NP);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,45 @@
/* Driver for routine fgauss */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NPT 3
#define NLIN 2
#define NA 3*NLIN
int main(void)
{
int i,j;
float e1,e2,f,x,y;
static float a[NA+1]={0.0,3.0,0.2,0.5,1.0,0.7,0.3};
float dyda[NA+1],df[NA+1];
printf("\n%6s %8s %8s %7s %7s %7s %7s %7s\n",
"x","y","dyda1","dyda2","dyda3","dyda4","dyda5","dyda6");
for (i=1;i<=NPT;i++) {
x=0.3*i;
fgauss(x,a,&y,dyda,NA);
e1=exp(-SQR((x-a[2])/a[3]));
e2=exp(-SQR((x-a[5])/a[6]));
f=a[1]*e1+a[4]*e2;
df[1]=e1;
df[4]=e2;
df[2]=a[1]*e1*2.0*(x-a[2])/(a[3]*a[3]);
df[5]=a[4]*e2*2.0*(x-a[5])/(a[6]*a[6]);
df[3]=a[1]*e1*2.0*SQR(x-a[2])/(a[3]*a[3]*a[3]);
df[6]=a[4]*e2*2.0*SQR(x-a[5])/(a[6]*a[6]*a[6]);
printf("from FGAUSS\n");
printf("%8.4f %8.4f",x,y);
for (j=1;j<=6;j++) printf("%8.4f",dyda[j]);
printf("\nindependent calc.\n");
printf("%8.4f %8.4f",x,f);
for (j=1;j<=6;j++) printf("%8.4f",df[j]);
printf("\n\n");
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,44 @@
/* Driver for routine fit */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NPT 100
#define SPREAD 0.5
int main(void)
{
long idum=(-117);
int i,mwt;
float a,b,chi2,q,siga,sigb,*x,*y,*sig;
x=vector(1,NPT);
y=vector(1,NPT);
sig=vector(1,NPT);
for (i=1;i<=NPT;i++) {
x[i]=0.1*i;
y[i] = -2.0*x[i]+1.0+SPREAD*gasdev(&idum);
sig[i]=SPREAD;
}
for (mwt=0;mwt<=1;mwt++) {
fit(x,y,NPT,sig,mwt,&a,&b,&siga,&sigb,&chi2,&q);
if (mwt == 0)
printf("\nIgnoring standard deviations\n");
else
printf("\nIncluding standard deviations\n");
printf("%12s %9.6f %18s %9.6f \n",
"a = ",a,"uncertainty:",siga);
printf("%12s %9.6f %18s %9.6f \n",
"b = ",b,"uncertainty:",sigb);
printf("%19s %14.6f \n","chi-squared: ",chi2);
printf("%23s %10.6f \n","goodness-of-fit: ",q);
}
free_vector(sig,1,NPT);
free_vector(y,1,NPT);
free_vector(x,1,NPT);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,62 @@
/* Driver for routine fitexy */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NPT 30
int main(void)
{
long idum=(-1);
int j;
float a,b,chi2,q,sa,sb,siga,sigb;
float *x,*y,*dx,*dy,*dz;
x=vector(1,NPT);
y=vector(1,NPT);
dx=vector(1,NPT);
dy=vector(1,NPT);
dz=vector(1,NPT);
for (j=1;j<=NPT;j++) {
dx[j]=0.1+ran1(&idum);
dy[j]=0.1+ran1(&idum);
dz[j]=0.0;
x[j]=10.0+10.0*gasdev(&idum);
y[j]=2.0*x[j]-5.0+dy[j]*gasdev(&idum);
x[j] += dx[j]*gasdev(&idum);
}
printf("Values of a,b,siga,sigb,chi2,q:\n");
printf("Fit with x and y errors gives:\n");
fitexy(x,y,NPT,dx,dy,&a,&b,&siga,&sigb,&chi2,&q);
printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n\n",
a,b,siga,sigb,chi2,q);
printf("Setting x errors to zero gives:\n");
fitexy(x,y,NPT,dz,dy,&a,&b,&siga,&sigb,&chi2,&q);
printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n",
a,b,siga,sigb,chi2,q);
printf("...to be compared with fit result:\n");
fit(x,y,NPT,dy,1,&a,&b,&siga,&sigb,&chi2,&q);
printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n\n",
a,b,siga,sigb,chi2,q);
printf("Setting y errors to zero gives:\n");
fitexy(x,y,NPT,dx,dz,&a,&b,&siga,&sigb,&chi2,&q);
printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n",
a,b,siga,sigb,chi2,q);
printf("...to be compared with fit result:\n");
fit(y,x,NPT,dx,1,&a,&b,&siga,&sigb,&chi2,&q);
sa=sqrt(siga*siga+SQR(sigb*(a/b)))/b;
sb=sigb/(b*b);
printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n",
-a/b,1.0/b,sa,sb,chi2,q);
free_vector(dz,1,NPT);
free_vector(dy,1,NPT);
free_vector(dx,1,NPT);
free_vector(y,1,NPT);
free_vector(x,1,NPT);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,56 @@
/* Driver for routine fixrts */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "complex.h"
#define NPOLES 6
#define NP1 (NPOLES+1)
#define ONE Complex(1.0,0.0)
#define TRUE 1
int main(void)
{
int i,polish=TRUE;
static float d[NP1]=
{0.0,6.0,-15.0,20.0,-15.0,6.0,0.0};
fcomplex zcoef[NP1],zeros[NP1],z1,z2;
/* finding roots of (z-1.0)^6=1.0 */
/* first write roots */
zcoef[NPOLES]=ONE;
for (i=NPOLES-1;i>=0;i--)
zcoef[i] = Complex(-d[NPOLES-i],0.0);
zroots(zcoef,NPOLES,zeros,polish);
printf("Roots of (z-1.0)^6 = 1.0\n");
printf("%24s %27s \n","Root","(z-1.0)^6");
for (i=1;i<=NPOLES;i++) {
z1=Csub(zeros[i],ONE);
z2=Cmul(z1,z1);
z1=Cmul(z1,z2);
z1=Cmul(z1,z1);
printf("%6d %12.6f %12.6f %12.6f %12.6f\n",
i,zeros[i].r,zeros[i].i,z1.r,z1.i);
}
/* now fix them to lie within unit circle */
fixrts(d,NPOLES);
/* check results */
zcoef[NPOLES]=ONE;
for (i=NPOLES-1;i>=0;i--)
zcoef[i] = Complex(-d[NPOLES-i],0.0);
zroots(zcoef,NPOLES,zeros,polish);
printf("\nRoots reflected in unit circle\n");
printf("%24s %27s \n","Root","(z-1.0)^6");
for (i=1;i<=NPOLES;i++) {
z1=Csub(zeros[i],ONE);
z2=Cmul(z1,z1);
z1=Cmul(z1,z2);
z1=Cmul(z1,z1);
printf("%6d %12.6f %12.6f %12.6f %12.6f\n",
i,zeros[i].r,zeros[i].i,z1.r,z1.i);
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,33 @@
/* Driver for routine fleg */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NVAL 5
#define DX 0.2
#define NPOLY 5
int main(void)
{
int i,j;
float x,*afunc;
afunc=vector(1,NPOLY);
printf("\n%3s\n","Legendre polynomials");
printf("%9s %9s %9s %9s %9s\n","n=1","n=2","n=3","n=4","n=5");
for (i=1;i<=NVAL;i++) {
x=i*DX;
fleg(x,afunc,NPOLY);
printf("x =%5.2f\n",x);
for (j=1;j<=NPOLY;j++) printf("%10.4f",afunc[j]);
printf(" routine FLEG\n");
for (j=1;j<=NPOLY;j++) printf("%10.4f",plgndr(j-1,0,x));
printf(" routine PLGNDR\n\n");
}
free_vector(afunc,1,NPOLY);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,54 @@
/* Driver for routine flmoon */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#define ZON (-5.0)
int main(void)
{
int i,i1,i2,i3,id,im,iy,n,nph=2;
float timzon=ZON/24.0,frac,secs;
long j1,j2;
static char *phase[]={"new moon","first quarter",
"full moon","last quarter"};
printf("Date of the next few phases of the moon\n");
printf("Enter today\'s date (e.g. 12 15 1992) : \n");
scanf("%d %d %d",&im,&id,&iy);
/* Approximate number of full moons since january 1900 */
n=(int)(12.37*(iy-1900+((im-0.5)/12.0)));
j1=julday(im,id,iy);
flmoon(n,nph,&j2,&frac);
n += (int) ((j1-j2)/29.53 + (j1 >= j2 ? 0.5 : -0.5));
printf("\n%10s %19s %9s\n","date","time(EST)","phase");
for (i=1;i<=20;i++) {
flmoon(n,nph,&j2,&frac);
frac=24.0*(frac+timzon);
if (frac < 0.0) {
--j2;
frac += 24.0;
}
if (frac > 12.0) {
++j2;
frac -= 12.0;
} else
frac += 12.0;
i1=(int) frac;
secs=3600.0*(frac-i1);
i2=(int) (secs/60.0);
i3=(int) (secs-60*i2+0.5);
caldat(j2,&im,&id,&iy);
printf("%5d %3d %5d %7d:%2d:%2d %s\n",
im,id,iy,i1,i2,i3,phase[nph]);
if (nph == 3) {
nph=0;
++n;
} else
++nph;
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,98 @@
/* Driver for routine four1 */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
void prntft(float data[],unsigned long nn)
{
unsigned long n;
printf("%4s %13s %13s %12s %13s\n",
"n","real(n)","imag.(n)","real(N-n)","imag.(N-n)");
printf(" 0 %14.6f %12.6f %12.6f %12.6f\n",
data[1],data[2],data[1],data[2]);
for (n=3;n<=nn+1;n+=2) {
printf("%4lu %14.6f %12.6f %12.6f %12.6f\n",
((n-1)/2),data[n],data[n+1],
data[2*nn+2-n],data[2*nn+3-n]);
}
printf(" press return to continue ...\n");
(void) getchar();
return;
}
#define NN 32
#define NN2 (2*NN)
int main(void)
{
long i;
int isign;
float *data,*dcmp;
data=vector(1,NN2);
dcmp=vector(1,NN2);
printf("h(t)=real-valued even-function\n");
printf("h(n)=h(N-n) and real?\n");
for (i=1;i<NN2;i+=2) {
data[i]=1.0/(SQR((float) (i-NN-1)/NN)+1.0);
data[i+1]=0.0;
}
isign=1;
four1(data,NN,isign);
prntft(data,NN);
printf("h(t)=imaginary-valued even-function\n");
printf("h(n)=h(N-n) and imaginary?\n");
for (i=1;i<NN2;i+=2) {
data[i+1]=1.0/(SQR((float) (i-NN-1)/NN)+1.0);
data[i]=0.0;
}
isign=1;
four1(data,NN,isign);
prntft(data,NN);
printf("h(t)=real-valued odd-function\n");
printf("h(n) = -h(N-n) and imaginary?\n");
for (i=1;i<NN2;i+=2) {
data[i]=((float) (i-NN-1)/NN)/(SQR((float) (i-NN-1)/NN)+1.0);
data[i+1]=0.0;
}
data[1]=0.0;
isign=1;
four1(data,NN,isign);
prntft(data,NN);
printf("h(t)=imaginary-valued odd-function\n");
printf("h(n) = -h(N-n) and real?\n");
for (i=1;i<NN2;i+=2) {
data[i+1]=((float) (i-NN-1)/NN)/(SQR((float) (i-NN-1)/NN)+1.0);
data[i]=0.0;
}
data[2]=0.0;
isign=1;
four1(data,NN,isign);
prntft(data,NN);
/* transform, inverse-transform test */
for (i=1;i<NN2;i+=2) {
data[i]=1.0/(SQR(0.5*(i-NN-1.0)/NN)+1.0);
dcmp[i]=data[i];
data[i+1]=(0.25*(i-NN-1.0)/NN)*exp(-SQR(0.5*(i-NN-1)/NN));
dcmp[i+1]=data[i+1];
}
isign=1;
four1(data,NN,isign);
isign = -1;
four1(data,NN,isign);
printf("%23s %33s \n","original data:","double fourier transform:");
printf("\n %3s %15s %12s %12s %12s \n",
"k","real h(k)","imag h(k)","real h(k)","imag h(k)");
for (i=1;i<NN;i+=2)
printf("%4lu %14.6f %12.6f %12.6f %12.6f\n",
(i+1)/2,dcmp[i],dcmp[i+1],data[i]/NN,data[i+1]/NN);
free_vector(dcmp,1,NN2);
free_vector(data,1,NN2);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,132 @@
/* Driver for routine fourfs */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define SWAP(a,b) {iswap=(a);(a)=(b);(b)=iswap;}
#define FSWAP(a,b) {flswap=(a);(a)=(b);(b)=flswap;}
#define NX 8
#define NY 32
#define NZ 4
#define NDAT (2*NX*NY*NZ)
#define TEMPFILE1 "frfstmp1"
#define TEMPFILE2 "frfstmp2"
#define TEMPFILE3 "frfstmp3"
#define TEMPFILE4 "frfstmp4"
#if defined(MSDOS) || defined(_MSDOS) || defined(_MSDOS_) || defined(__MSDOS__)
#define BINREADWRITE "wb+"
#else
#define BINREADWRITE "w+"
#endif
char *fnames[4]={TEMPFILE1,TEMPFILE2,TEMPFILE3,TEMPFILE4};
int main(void)
{
int cc,nnv=3,nwrite;
long idum=(-23);
unsigned long dim[4],i,j,k,l,ll,iswap;
float diff,smax,sum,sum1=0.0,sum2=0.0,tot,*data1,*data2,*data1p,*data2p;
FILE *flswap,*file[5];
data1=vector(1,NDAT);
data2=vector(1,NDAT);
dim[1]=NX;
dim[2]=NY;
dim[3]=NZ;
tot=(float)NX*(float)NY*(float)NZ;
for (j=1;j<=4;j++)
if ((file[j]=fopen(fnames[j-1],BINREADWRITE)) == NULL)
nrerror("Couldn't open temporary file");
for (i=1;i<=dim[3];i++)
for (j=1;j<=dim[2];j++)
for (k=1;k<=dim[1];k++) {
l=k+(j-1)*dim[1]+(i-1)*dim[2]*dim[1];
l=(l<<1)-1;
data2[l]=data1[l]=2*ran1(&idum)-1;
l++;
data2[l]=data1[l]=2*ran1(&idum)-1;
}
nwrite=NDAT >> 1;
cc=fwrite(&data1[1],sizeof(float),nwrite,file[1]);
if (cc != nwrite) nrerror("write error in xfourfs");
cc=fwrite(&data1[nwrite+1],sizeof(float),nwrite,file[2]);
if (cc != nwrite) nrerror("write error in xfourfs");
rewind(file[1]);
rewind(file[2]);
printf("**************** now doing fourfs *********\n");
fourfs(file,dim,nnv,1);
for (j=1;j<=4;j++) rewind(file[j]);
cc=fread(&data1[1],sizeof(float),nwrite,file[3]);
if (cc != nwrite) nrerror("read error in xfourfs");
cc=fread(&data1[nwrite+1],sizeof(float),nwrite,file[4]);
if (cc != nwrite) nrerror("read error in xfourfs");
printf("**************** now doing fourn *********\n");
fourn(data2,dim,nnv,1);
sum=smax=0.0;
for (i=1;i<=dim[3];i++)
for (j=1;j<=dim[2];j++)
for (k=1;k<=dim[1];k++) {
l=k+(j-1)*dim[1]+(i-1)*dim[2]*dim[1];
l=(l<<1)-1;
ll=i+(j-1)*dim[3]+(k-1)*dim[3]*dim[2];
ll=(ll<<1)-1;
diff=sqrt(SQR(data2[ll]-data1[l])+SQR(data2[ll+1]-data1[l+1]));
sum2 += SQR(data1[l])+SQR(data1[l+1]);
sum += diff;
if (diff > smax) smax=diff;
}
sum2=sqrt(sum2/tot);
sum=sum/tot;
printf("(r.m.s.) value, (max,ave) discrepancy= %12.7f %12.7f %12.7f\n",
sum2,smax,sum);
/* now check the inverse transforms */
SWAP(dim[1],dim[3]);
/* This step swap step is conceptually a reversal, but for
three dimensions a swap accomplishes that. */
FSWAP(file[1],file[3])
FSWAP(file[4],file[2])
for (j=1;j<=4;j++) rewind(file[j]);
printf("**************** now doing fourfs *********\n");
fourfs(file,dim,nnv,-1);
for (j=1;j<=4;j++) rewind(file[j]);
cc=fread(&data1[1],sizeof(float),nwrite,file[3]);
if (cc != nwrite) nrerror("read error in xfourfs");
cc=fread(&data1[nwrite+1],sizeof(float),nwrite,file[4]);
if (cc != nwrite) nrerror("read error in xfourfs");
SWAP(dim[1],dim[3]);
printf("**************** now doing fourn *********\n");
fourn(data2,dim,nnv,-1);
sum=smax=0.0;
data1p=data1;
data2p=data2;
for (j=1;j<=NDAT;j+=2) {
sum1 += SQR(data2p[1])+SQR(data2p[2]);
diff=sqrt(SQR(data2p[1]-data1p[1])+SQR(data2p[2]-data1p[2]));
sum += diff;
if (diff > smax) smax=diff;
data1p += 2;
data2p += 2;
}
sum=sum/tot;
sum1=sqrt(sum1/tot);
printf("(r.m.s.) value, (max,ave) discrepancy= %12.7f %12.7f %12.7f\n",
sum1,smax,sum);
printf("ratio of r.m.s. values, expected ratio= %12.6f %12.6f\n",
sum1/sum2,sqrt(tot));
for (j=1;j<=4;j++)
if (fclose(file[j]) == EOF) nrerror("Couldn't close temporary file");
free_vector(data2,1,NDAT);
free_vector(data1,1,NDAT);
for (j=1;j<=4;j++)
if (remove(fnames[j-1])) nrerror("Couldn't delete temporary file");
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,58 @@
/* Driver for routine fourn */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NDIM 3
#define NDAT2 1024
int main(void)
{
int isign;
long idum=(-23);
unsigned long i,j,k,l,ndum=2,*nn;
float *data1,*data2;
nn=lvector(1,NDIM);
data1=vector(1,NDAT2);
data2=vector(1,NDAT2);
for (i=1;i<=NDIM;i++) nn[i]=(ndum <<= 1);
for (i=1;i<=nn[3];i++)
for (j=1;j<=nn[2];j++)
for (k=1;k<=nn[1];k++) {
l=k+(j-1)*nn[1]+(i-1)*nn[2]*nn[1];
l=(l<<1)-1;
/* real part of component */
data2[l]=data1[l]=2*ran1(&idum)-1;
/* imaginary part of component */
l++;
data2[l]=data1[l]=2*ran1(&idum)-1;
}
isign=1;
fourn(data2,nn,NDIM,isign);
/* here would be any processing to be done in Fourier space */
isign = -1;
fourn(data2,nn,NDIM,isign);
printf("Double 3-dimensional transform\n\n");
printf("%22s %24s %20s\n",
"Double transf.","Original data","Ratio");
printf("%10s %13s %12s %13s %11s %13s\n\n",
"real","imag.","real","imag.","real","imag.");
for (i=1;i<=4;i++) {
k=2*(j=2*i);
l=k+(j-1)*nn[1]+(i-1)*nn[2]*nn[1];
l=(l<<1)-1;
printf("%12.2f %12.2f %10.2f %12.2f %14.2f %12.2f\n",
data2[l],data2[l+1],data1[l],data1[l+1],
data2[l]/data1[l],data2[l+1]/data1[l+1]);
}
printf("\nThe product of transform lengths is: %4lu\n",nn[1]*nn[2]*nn[3]);
free_vector(data2,1,NDAT2);
free_vector(data1,1,NDAT2);
free_lvector(nn,1,NDIM);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,32 @@
/* Driver for routine fpoly */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NVAL 15
#define DX 0.1
#define NPOLY 5
int main(void)
{
int i,j;
float x,*afunc;
afunc=vector(1,NPOLY);
printf("\n%38s\n","powers of x");
printf("%8s %10s %9s %9s %9s %9s\n",
"x","x**0","x**1","x**2","x**3","x**4");
for (i=1;i<=NVAL;i++) {
x=i*DX;
fpoly(x,afunc,NPOLY);
printf("%10.4f",x);
for (j=1;j<=NPOLY;j++) printf("%10.4f",afunc[j]);
printf("\n");
}
free_vector(afunc,1,NPOLY);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,41 @@
/* Driver for routine fred2 */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 8
#define PI 3.1415927
float g(float t)
{
return sqrt(t)-pow(PI/2.0,2.25)*pow(t,0.75)/2.25;
}
float ak(float t,float s)
{
return pow(t*s,0.75);
}
int main(void)
{
int i;
float a=0.0,b=PI/2.0,*f;
float *t,*w;
t=vector(1,N);
f=vector(1,N);
w=vector(1,N);
fred2(N,a,b,t,f,w,g,ak);
/* Compare with exact solution */
printf("Abscissa, Calc soln, True soln\n");
for (i=1;i<=N;i++) printf("%10.6f %10.6f %10.6f\n",t[i],f[i],sqrt(t[i]));
free_vector(w,1,N);
free_vector(f,1,N);
free_vector(t,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,44 @@
/* Driver for routine fredin */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define N 8
#define PI 3.1415927
float g(float t)
{
return sqrt(t)-pow(PI/2.0,2.25)*pow(t,0.75)/2.25;
}
float ak(float t,float s)
{
return pow(t*s,0.75);
}
int main(void)
{
float a=0.0,ans,b=PI/2.0,x,*f;
float *t,*w;
t=vector(1,N);
f=vector(1,N);
w=vector(1,N);
fred2(N,a,b,t,f,w,g,ak);
for (;;) {
printf("Enter T between 0 and PI/2\n");
if (scanf("%f",&x) == EOF) break;
ans=fredin(x,N,a,b,t,f,w,g,ak);
printf("T, Calculated answer, True answer\n");
printf("%10.6f %10.6f %10.6f\n",x,ans,sqrt(x));
}
free_vector(w,1,N);
free_vector(f,1,N);
free_vector(t,1,N);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,40 @@
/* Driver for routine frenel */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float c,s,x,xc,xs;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Fresnel Integrals",17)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%5s %12s %14s %16s %14s \n",
"x","actual","c(x)","actual","s(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f",&x,&xs,&xc);
frenel(x,&s,&c);
printf("%6.2f %15.6e %15.6e %15.6e %15.6e\n",
x,xs,s,xc,c);
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,51 @@
/* Driver for routine frprmn */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NDIM 3
#define FTOL 1.0e-6
#define PIO2 1.5707963
float func(float x[])
{
return 1.0-bessj0(x[1]-0.5)*bessj0(x[2]-0.5)*bessj0(x[3]-0.5);
}
void dfunc(float x[],float df[])
{
df[1]=bessj1(x[1]-0.5)*bessj0(x[2]-0.5)*bessj0(x[3]-0.5);
df[2]=bessj0(x[1]-0.5)*bessj1(x[2]-0.5)*bessj0(x[3]-0.5);
df[3]=bessj0(x[1]-0.5)*bessj0(x[2]-0.5)*bessj1(x[3]-0.5);
}
int main(void)
{
int iter,k;
float angl,fret,*p;
p=vector(1,NDIM);
printf("Program finds the minimum of a function\n");
printf("with different trial starting vectors.\n");
printf("True minimum is (0.5,0.5,0.5)\n");
for (k=0;k<=4;k++) {
angl=PIO2*k/4.0;
p[1]=2.0*cos(angl);
p[2]=2.0*sin(angl);
p[3]=0.0;
printf("\nStarting vector: (%6.4f,%6.4f,%6.4f)\n",
p[1],p[2],p[3]);
frprmn(p,NDIM,FTOL,&iter,&fret,func,dfunc);
printf("Iterations: %3d\n",iter);
printf("Solution vector: (%6.4f,%6.4f,%6.4f)\n",
p[1],p[2],p[3]);
printf("Func. value at solution %14f\n",fret);
}
free_vector(p,1,NDIM);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,41 @@
/* Driver for routine ftest */
#include <stdio.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define NPTS 1000
#define MPTS 500
#define EPS 0.01
#define NVAL 11
int main(void)
{
long idum=(-13);
int i,j;
float f,factor,prob,vrnce,*data1,*data2,*data3;
data1=vector(1,NPTS);
data2=vector(1,MPTS);
data3=vector(1,MPTS);
/* Generate two gaussian distributions with different variances */
printf("\n%16s %5.2f\n","Variance 1 = ",1.0);
printf("%13s %11s %16s\n","Variance 2","Ratio","Probability");
for (j=1;j<=NPTS;j++) data1[j]=gasdev(&idum);
for (j=1;j<=MPTS;j++) data2[j]=gasdev(&idum);
for (i=1;i<=NVAL;i++) {
vrnce=1.0+(i-1)*EPS;
factor=sqrt(vrnce);
for (j=1;j<=MPTS;j++) data3[j]=factor*data2[j];
ftest(data1,NPTS,data3,MPTS,&f,&prob);
printf("%11.4f %13.4f %13.4f\n",vrnce,f,prob);
}
free_vector(data3,1,MPTS);
free_vector(data2,1,MPTS);
free_vector(data1,1,NPTS);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,47 @@
/* Driver for routine gamdev */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#define N 20
#define NPTS 10000
#define ISCAL 200
#define LLEN 50
int main(void)
{
char words[LLEN+1];
long idum=(-13);
int i,ia,j,k,klim,dist[N+1];
float dd;
for (;;) {
for (j=0;j<=N;j++) dist[j]=0;
do {
printf("Select order of Gamma distribution (n=1..%d), -1 to end\n",N);
scanf("%d",&ia);
} while (ia > N);
if (ia < 0) break;
for (i=1;i<=NPTS;i++) {
j=(int) gamdev(ia,&idum);
if ((j >= 0) && (j <= N)) ++dist[j];
}
printf("\ngamma-distribution deviate, order %2d of %6d points\n",
ia,NPTS);
printf("%6s %7s %9s \n","x","p(x)","graph:");
for (j=0;j<N;j++) {
dd=(float) dist[j]/NPTS;
for (k=1;k<=50;k++) words[k]=' ';
klim=(int) (ISCAL*dd);
if (klim > LLEN) klim=LLEN;
for (k=1;k<=klim;k++) words[k]='*';
printf("%6d %8.4f ",j,dd);
for (k=1;k<=klim;k++) printf("%c",words[k]);
printf("\n");
}
}
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,42 @@
/* Driver for routine gammln */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float actual,calc,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Gamma Function",14)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%10s %21s %21s\n","x","actual","gammln(x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f",&x,&actual);
if (x > 0.0) {
calc=(x<1.0 ? gammln(x+1.0)-log(x) : gammln(x));
printf("%12.2f %20.6f %20.6f\n",x,
log(actual),calc);
}
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine gammp */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float a,val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Incomplete Gamma Function",25)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%4s %11s %14s %14s \n","a","x","actual","gammp(a,x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f",&a,&x,&val);
printf("%6.2f %12.6f %12.6f %12.6f \n",a,x,val,gammp(a,x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,37 @@
/* Driver for routine gammq */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NRANSI
#include "nr.h"
#include "nrutil.h"
#define MAXSTR 80
int main(void)
{
char txt[MAXSTR];
int i,nval;
float a,val,x;
FILE *fp;
if ((fp = fopen("fncval.dat","r")) == NULL)
nrerror("Data file fncval.dat not found\n");
fgets(txt,MAXSTR,fp);
while (strncmp(txt,"Incomplete Gamma Function",25)) {
fgets(txt,MAXSTR,fp);
if (feof(fp)) nrerror("Data not found in fncval.dat\n");
}
fscanf(fp,"%d %*s",&nval);
printf("\n%s\n",txt);
printf("%4s %11s %14s %14s \n","a","x","actual","gammq(a,x)");
for (i=1;i<=nval;i++) {
fscanf(fp,"%f %f %f",&a,&x,&val);
printf("%6.2f %12.6f %12.6f %12.6f\n",a,x,(1.0-val),gammq(a,x));
}
fclose(fp);
return 0;
}
#undef NRANSI

View File

@@ -0,0 +1,41 @@
/* Driver for routine gasdev */
#include <stdio.h>
#define NRANSI
#include "nr.h"
#define N 20
#define NOVER2 (N/2)
#define NPTS 10000
#define ISCAL 400
#define LLEN 50
int main(void)
{
char words[LLEN+1];
int i,j,k,klim,dist[N+1];
long idum=(-13);
float dd,x;
for (j=0;j<=N;j++) dist[j]=0;
for (i=1;i<=NPTS;i++) {
x=0.25*N*gasdev(&idum);
j=(int)(x > 0 ? x+0.5 : x-0.5);
if ((j >= -NOVER2) && (j <= NOVER2)) ++dist[j+NOVER2];
}
printf("Normally distributed deviate of %6d points\n",NPTS);
printf ("%5s %10s %9s\n","x","p(x)","graph:");
for (j=0;j<=N;j++) {
dd=(float) dist[j]/NPTS;
for (k=1;k<=LLEN;k++) words[k]=' ';
klim=(int) (ISCAL*dd);
if (klim > LLEN) klim=LLEN;
for (k=1;k<=klim;k++) words[k]='*';
printf("%8.4f %8.4f ",j/(0.25*N),dd);
for (k=1;k<=LLEN;k++) printf("%c",words[k]);
printf("\n");
}
return 0;
}
#undef NRANSI

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