⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d6r8.cpp

📁 数值计算c++源代码,包括各种算法。很有用的。
💻 CPP
字号:
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
#include <stdlib.h>

static double r[98];
static int iff,ix1,ix2,ix3;
double ran1(int& idum)
{
    double rm1,rm2,t;
	int m1,m2,m3,ia1,ia2,ia3,ic1,ic2,ic3,j;
	 
    m1 = 259200, ia1 = 7141, ic1 = 54773, rm1 = 0.0000038580247,
    m2 = 134456, ia2 = 8121, ic2 = 28411, rm2 = 0.0000074373773,
    m3 = 243000, ia3= 4561, ic3 = 51349;
	
    if ((idum < 0) || (iff == 0))
	{
        iff = 1;
        ix1 =  (ic1 - idum) % m1;
        ix1 = (ia1 * ix1 + ic1) % m1;
        ix2 = ix1 % m2;
        ix1 = (ia1 * ix1 + ic1) % m1;
        ix3 = ix1 % m3;
        for (j = 1; j<=97; j++)
		{
            ix1 = (ia1 * ix1 + ic1)% m1;
            ix2 = (ia2 * ix2 + ic2)% m2;
            r[j] = (double(ix1) + double(ix2) * rm2) * rm1;
        }
        idum = 1;
	}
      ix1 = (ia1 * ix1 + ic1) % m1;
      ix2 = (ia2 * ix2 + ic2) % m2;
      ix3 = (ia3 * ix3 + ic3) % m3;
      j = 1 + int((97 * ix3) / m3);
      if ((j > 97) || (j < 1)) 
	  {
		  cout<< "abnormal exit"<<endl;
	      return 1;
	  }
      t = r[j];
      r[j] = (float(ix1) + float(ix2) * rm2) * rm1;
	  return t;
}

double  gammln(double xx)
{
	int j;
    double cof[6],stp,half,one,fpf,x,tmp,ser,temp;
    cof[1] = 76.18009173;
    cof[2] = -86.50532033;
    cof[3] = 24.01409822;
    cof[4] = -1.231739516;
    cof[5] = 0.00120858003;
    cof[6] = -0.00000536382;
    stp = 2.50662827465;
    half = 0.5;
    one = 1.0;
    fpf = 5.5;
    x = xx - one;
    tmp = x + fpf;
    tmp = (x + half) * log(tmp) - tmp;
    ser = one;
    for (j = 1;j<=6;j++)
	{
        x = x + one;
        ser = ser + cof[j] / x;
    }
    temp = tmp + log(stp * ser);
	return temp;
}

double poidev(int xm, int &idum)
{
	double pi,g,em,t,sq,alxm,y,dum,temp;
	int oldm=0;
    pi = 3.141592654;
    if (xm < 12) 
	{
        if (xm != oldm)
		{
            oldm = xm;
            g = exp(-xm);
        }
        em = -1.0;
        t = 1.0;
        do
		{
          em = em + 1.0;
          t = t * ran1(idum);
		}
        while (t > g);
	}
    else
	{
        if (xm != oldm)
		{
            oldm = xm;
            sq =sqrt(2.0 * xm);
            alxm = log(xm);
            g = xm * alxm - gammln(xm + 1.0);
        }
        do
		{
          do
		  {
            y = tan(pi * ran1(idum));
            em = sq * y + xm;
		  }
          while (em < 0.0);
          em = int(em);
          dum = em * alxm - gammln(em + 1.0) - g;
          t = 0.9 * (1.0 + y *y) * exp(dum);
		}
        while (ran1(idum) > t);
    }
    temp = em;
	return temp;
}

void main()
{
    //program d6r8
    //driver for routine poidev
    int idum,n = 20, npts = 10000, iscal = 200, llen = 50,xm,klim,i,j,k,m;
    double dist[21];
	char text[50];
    idum = -13;
    do
	{
        for (j = 1; j<=21; j++)
            dist[j] = 0.0;
        cout<<"Order of Poisson distribution [n=1..20] -1 to end."<<endl;
        xm = 5;
        cout<<xm<<endl;
        if (xm <= 0) exit(1);
	}
    while (xm > 20);
    for (i = 1; i<=npts; i++)
	{
        j = int(poidev(xm, idum)) + 1;
        if ((j >= 1) && (j <= 21)) dist[j] = dist[j] + 1;
    }
    cout<< "Poisson distribution deviate, order "<<xm<<"of "<< npts<< " points"<<endl;
    cout<< "    x     p[x]     graph:"<<endl;
    for (j = 1; j<=20; j++)
	{
        dist[j] = dist[j] / npts;
        for (k = 0; k<=50; k++)
            text[k] =' ';
        klim = int(iscal * dist[j]);
        if (klim > llen) klim = llen;
        for (k = 1; k<=klim; k++)
            text[k] = '*';
        cout<< setw(5)<<float(j);
        cout<< setw(10)<<dist[j]<<"    ";
        
        for (m = 1; m<=50; m++)
            cout<< text[m];
		cout<<endl;
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -