Package com.nr

Examples of com.nr.Complex


  @Test
  public void test() {
    intW its = new intW(0);
    int i,M=5,N=20;
    double sbeps=1.e-14;
    Complex x = new Complex(),re1 = new Complex(1.0,0.0),im1=new Complex(0.0,1.0);
    Complex a[]={
        re1.mul(2.0).add(im1),
        im1.mul(2.0),
        re1.mul(3.0).add(im1),
        re1.add(im1.mul(2.0)),
        re1.sub(im1),
        new Complex(1.0)
        };
    Complex[] aa = new Complex[M+1];System.arraycopy(a, 0, aa, 0 , M+1);
    double[] dy = new double[N];
    boolean localflag, globalflag=false;

   

    // Test laguer
    System.out.println("Testing laguer");
    // Roots of polynomial x^5+(1-i)x^4+(1+2i)x^3+(3+i)x^2+(2i)x+(2+i)"
    // Roots are x=i, x=-i, x=sqrt(2i), x=-i*sqrt(2i), x=(i-1)
    Ran myran =new Ran(17);
    for (i=0;i<N;i++) {
      x=new Complex(-2.0+4.0*myran.doub(),-2.0+4.0*myran.doub());
      x = laguer(aa,x,its);
      Complex r = a[4].add(x.mul(a[5]));
      r = a[3].add(x.mul(r));
      r = a[2].add(x.mul(r));
      r = a[1].add(x.mul(r));
      r = a[0].add(x.mul(r));
      dy[i] = r.abs();
      //dy[i]=abs(a[0]+x*(a[1]+x*(a[2]+x*(a[3]+x*(a[4]+x*a[5])))));
     
    }
    System.out.printf("laguer: Maximum discrepancy = %f\n", maxel(dy));
    localflag = maxel(dy) > sbeps;
View Full Code Here


      FPMIN=Double.MIN_NORMAL,
      BIG=Double.MAX_VALUE*EPS;
    boolean odd;
    int k,n;
    double a,ax,fact,pix2,sign,sum,sumc,sums,term,test;
    Complex b,cc,d,h,del,cs;
    if ((ax=abs(x)) < sqrt(FPMIN)) {
      cs=new Complex(ax,0);
    } else if (ax <= XMIN) {
      sum=sums=0.0;
      sumc=ax;
      sign=1.0;
      fact=PIBY2*ax*ax;
      odd=true;
      term=ax;
      n=3;
      for (k=1;k<=MAXIT;k++) {
        term *= fact/k;
        sum += sign*term/n;
        test=abs(sum)*EPS;
        if (odd) {
          sign = -sign;
          sums=sum;
          sum=sumc;
        } else {
          sumc=sum;
          sum=sums;
        }
        if (term < test) break;
        odd=!odd;
        n += 2;
      }
      if (k > MAXIT) throw new IllegalArgumentException("series failed in frenel");
      cs=new Complex(sumc,sums);
    } else {
      pix2=PI*ax*ax;
      b=new Complex(1.0,-pix2);
      cc=new Complex(BIG,0);
      d=new Complex(1.0,0).div(b);
      h=new Complex(1.0,0).div(b);
     
      n = -1;
      for (k=2;k<=MAXIT;k++) {
        n += 2;
        a = -n*(n+1);
        b = b.add(new Complex(4.0,0));
        d= new Complex(1.0,0).div(new Complex(a,0).mul(d).add(b)); // d=1.0/(a*d+b);
        cc=b.add(new Complex(a,0).div(cc));
        del=cc.mul(d);
        h =h.mul(del);
        if (abs(del.re()-1.0)+abs(del.im()) <= EPS) break;
      }
      if (k > MAXIT) throw new IllegalArgumentException("cf failed in frenel");
      h = h.mul(new Complex(ax,-ax));
      cs=new Complex(0.5,0.5).mul(
        (new Complex(1.0,0).sub((new Complex(cos(0.5*pix2),sin(0.5*pix2)).mul(h)))));
    }
    if (x < 0.0) cs = cs.neg();
    return cs;
  }
View Full Code Here

      FPMIN=Double.MIN_NORMAL*4.0,
      BIG=Double.MAX_VALUE*EPS;
    int i,k;
    boolean odd;
    double a,err,fact,sign,sum,sumc,sums,t,term;
    Complex h,b,c,d,del,cs;
    if ((t=abs(x)) == 0.0) return new Complex(-BIG,0);
    if (t > TMIN) {
      b=new Complex(1.0,t);
      c=new Complex(BIG,0.0);
      d=new Complex(1.0,0).div(b);
      h=new Complex(1.0,0).div(b);
      for (i=1;i<MAXIT;i++) {
        a= -i*i;
        b = b.add(new Complex(2.0,0));
        d= new Complex(1.0,0).div(new Complex(a,0).mul(d).add(b)); // d=1.0/(a*d+b);
        c=b.add(new Complex(a,0).div(c)); // c=b+a/c;
        del=c.mul(d);
        h = h.mul(del);
        if (abs(del.re()-1.0)+abs(del.im()) <= EPS) break;
      }
      if (i >= MAXIT) throw new IllegalArgumentException("cf failed in cisi");
      h=new Complex(cos(t),-sin(t)).mul(h);
      //cs= -conj(h)+Complex(0.0,PIBY2);
      cs = new Complex(0.0,PIBY2).sub(h.conj());
    } else {
      if (t < sqrt(FPMIN)) {
        sumc=0.0;
        sums=t;
      } else {
        sum=sums=sumc=0.0;
        sign=fact=1.0;
        odd=true;
        for (k=1;k<=MAXIT;k++) {
          fact *= t/k;
          term=fact/k;
          sum += sign*term;
          err=term/abs(sum);
          if (odd) {
            sign = -sign;
            sums=sum;
            sum=sumc;
          } else {
            sumc=sum;
            sum=sums;
          }
          if (err < EPS) break;
          odd=!odd;
        }
        if (k > MAXIT) throw new IllegalArgumentException("maxits exceeded in cisi");
      }
      cs=new Complex(sumc+log(t)+EULER,sums);
    }
    if (x < 0.0) cs = cs.conj();
    return cs;
  }
View Full Code Here

public class Hypergeo {
  private Complex series, deriv;
  private Hypergeo(){}
 
  private void hypser(final Complex a, final Complex b, final Complex c, final Complex z){
    deriv = new Complex(0.0,0.0);
    Complex fac=new Complex(1.0,0.0);
    Complex temp=fac;
    Complex aa=a;
    Complex bb=b;
    Complex cc=c;
    for (int n=1;n<=1000;n++) {
      fac = fac.mul(aa.mul(bb).div(cc));
      deriv =deriv.add(fac);
      fac = fac.mul(z.mul(1.0/n));
      series=temp.add(fac);
      if (series.equals(temp)) return;
      temp=series;
      aa =aa.add(new Complex(1.0,0));
      bb =bb.add(new Complex(1.0,0));
      cc =cc.add(new Complex(1.0,0));
    }
    throw new IllegalArgumentException("convergence failure in hypser");
  }
View Full Code Here

  }
 
  private Complex hypgeo0(final Complex a, final Complex b,final Complex c,
    final Complex z){
    final double atol=1.0e-14,rtol=1.0e-14;
    Complex ans = new Complex(0,0),dz= new Complex(0,0),z0= new Complex(0,0);
    Complex[] y= new Complex[2];
    double[] yy = new double[4];
    if (z.norm() <= 0.25) {
      hypser(a,b,c,z);
      ans = series;
      y[1] = deriv;
      return ans;
    }
    else if (z.re() < 0.0)
      z0=new Complex(-0.5,0.0);
    else if (z.re() <= 1.0)
      z0=new Complex(0.5,0.0);
    else
      z0=new Complex(0.0,z.im() >= 0.0 ? 0.5 : -0.5);
    dz=z.sub(z0);
    hypser(a,b,c,z0);
    y[0] = series;
    y[1] = deriv;
    yy[0]=y[0].re();
    yy[1]=y[0].im();
    yy[2]=y[1].re();
    yy[3]=y[1].im();
    Hypderiv d = new Hypderiv(a,b,c,z0,dz);
    Output out = new Output();
    StepperBS s = new StepperBS();
    Odeint ode =new Odeint(yy,0.0,1.0,atol,rtol,0.1,0.0,out,d,s);
    ode.integrate();
    y[0]=new Complex(yy[0],yy[1]);
    return y[0];
  }
View Full Code Here

      rd[2*i] = data[i].re();
      rd[2*i+1] = data[i].im();
    }
    four1(rd, data.length,isign);
    for(int i=0;i<data.length;i++){
      data[i] = new Complex(rd[2*i], rd[2*i+1]);
    }   
  }
View Full Code Here

    v[ii + 1] = c.im();
  }

  public Complex get(int i) {
    int ii = (i & mask) << 1;
    return new Complex(v[ii], v[ii + 1]);
  }
View Full Code Here

      for (j=0;j<nn[1];j++) data1[i][j]=0.0;
    data1[5][7]=1.0;
    rlft3(data1,speq1,1);
    for (i=0;i<nn[0];i++) {
      for (j=0;j<nn[1]/2;j++) {
        Complex r1 = Complex.I.mul(2.0*pi*5.0*i/nn[0]);
        Complex r2 = Complex.I.mul(2.0*pi*7.0*j/nn[1]);
        Complex r = r1.exp().mul(r2.exp());         
        data2[i][2*j] = r.re();
        data2[i][2*j+1]= r.im();
        /*
        data2[i][2*j]= real(exp(2.0*pi*Complex(0.0,1.0)*5.0*double(i)/double(nn[0]))
          *exp(2.0*pi*Complex(0.0,1.0)*7.0*double(j)/double(nn[1])));
        data2[i][2*j+1]= imag(exp(2.0*pi*Complex(0.0,1.0)*5.0*double(i)/double(nn[0]))
          *exp(2.0*pi*Complex(0.0,1.0)*7.0*double(j)/double(nn[1])));
          */
      }
      Complex r1 = Complex.I.mul(2.0*pi*5.0*i/nn[0]);
      Complex r2 = Complex.I.mul(pi*7.0);
      Complex r = r1.exp().mul(r2.exp());
      speq2[2*i]=r.re();
      speq2[2*i+1]=r.im();
      /*
      speq2[2*i]=real(exp(2.0*pi*Complex(0.0,1.0)*5.0*double(i)/double(nn[0]))*exp(pi*Complex(0.0,1.0)*7.0));
      speq2[2*i+1]=imag(exp(2.0*pi*Complex(0.0,1.0)*5.0*double(i)/double(nn[0]))*exp(pi*Complex(0.0,1.0)*7.0));
      */
    }
//    System.out.printf(maxel(matsub(data1,data2)));
    localflag = localflag || maxel(matsub(data1,data2)) > sbeps;
//    System.out.printf(maxel(vecsub(speq1,speq2)));
    localflag = localflag || maxel(vecsub(speq1,speq2)) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** rlft3: Forward transform of a chosen delta function did not give expected result");
     
    }


    // Test rlft3, interface 2 (3 Dimensions)
    System.out.println("Testing rlft3, interface2");
    N=1;
    for (i=0;i<3;i++) N *= nn[i];
    double[][][] data3=new double[nn[0]][nn[1]][nn[2]];
    double[][][] data4=new double[nn[0]][nn[1]][nn[2]];
    double[][] speq3=new double[nn[0]][2*nn[1]];
    double[][] speq4=new double[nn[0]][2*nn[1]];
    // Round-trip test for random numbers
    for (i=0;i<nn[0];i++) {
      for (j=0;j<nn[1];j++) {
        for (k=0;k<nn[2];k++) {
          data3[i][j][k] = myran.doub();
          data4[i][j][k] = (2.0/N)*data3[i][j][k];
        }
      }
    }
    rlft3(data4,speq4,1);
    rlft3(data4,speq4,-1);
    double max=0.0;
    for (i=0;i<nn[0];i++) {
      for (j=0;j<nn[1];j++) {
        for (k=0;k<nn[2];k++) {
          temp=abs(data3[i][j][k]-data4[i][j][k]);
          max = (temp > max ? temp : max);
        }
      }
    }
//    System.out.printf(max);
    localflag = localflag || max > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** rlft3, interface2: Round-trip test for random real values failed");
     
    }


    // Test delta-function in to sine-wave out, forward transform
    for (i=0;i<nn[0];i++)
      for (j=0;j<nn[1];j++)
        for (k=0;k<nn[2];k++) data3[i][j][k]=0.0;
    data3[5][7][9]=1.0;
    rlft3(data3,speq3,1);

    for (i=0;i<nn[0];i++) {
      for (j=0;j<nn[1];j++) {
        for (k=0;k<nn[2]/2;k++) {
          Complex r1 = Complex.I.mul(2.0*pi*5.0*i/nn[0]);
          Complex r2 = Complex.I.mul(2.0*pi*7.0*j/nn[1]);
          Complex r3 = Complex.I.mul(2.0*pi*9.0*k/nn[2]);
          Complex r = r1.exp().mul(r2.exp()).mul(r3.exp());
          data4[i][j][2*k] = r.re();
          data4[i][j][2*k+1] = r.im();
          /*
          data4[i][j][2*k]=real(exp(2.0*Complex(0.0,1.0)*pi*5.0*double(i)/double(nn[0]))
            *exp(2.0*Complex(0.0,1.0)*pi*7.0*double(j)/double(nn[1]))
            *exp(2.0*Complex(0.0,1.0)*pi*9.0*double(k)/double(nn[2])));
          data4[i][j][2*k+1]=imag(exp(2.0*Complex(0.0,1.0)*pi*5.0*double(i)/double(nn[0]))
            *exp(2.0*Complex(0.0,1.0)*pi*7.0*double(j)/double(nn[1]))
            *exp(2.0*Complex(0.0,1.0)*pi*9.0*double(k)/double(nn[2])));
            **/
        }
        Complex r1 = Complex.I.mul(2.0*pi*5.0*i/nn[0]);
        Complex r2 = Complex.I.mul(2.0*pi*7.0*j/nn[1]);
        Complex r3 = Complex.I.mul(pi*9.0);
        Complex r = r1.exp().mul(r2.exp()).mul(r3.exp());
        speq4[i][2*j]=r.re();
        speq4[i][2*j+1]=r.im();
        /*
        speq4[i][2*j]=real(exp(2.0*Complex(0.0,1.0)*pi*5.0*double(i)/double(nn[0]))
            *exp(2.0*Complex(0.0,1.0)*pi*7.0*double(j)/double(nn[1]))
            *exp(Complex(0.0,1.0)*pi*9.0));
        speq4[i][2*j+1]=imag(exp(2.0*Complex(0.0,1.0)*pi*5.0*double(i)/double(nn[0]))
 
View Full Code Here

    n = aa.length;
    a = buildMatrix(aa);
    zz = new double[n][n];
    wri = new Complex[n];
    for(int i=0;i<n;i++)
      wri[i] = new Complex(0,0);
    scale = buildVector(n,1.0);
    perm = new int[n];
    yesvecs = yesvec;
    hessen = hessenb;
   
View Full Code Here

            break;
          }
        }
        x=a[nn][nn];
        if (l == nn) {
          wri[nn--]=new Complex(x+t,0);
        } else {
          y=a[nn-1][nn-1];
          w=a[nn][nn-1]*a[nn-1][nn];
          if (l == nn-1) {
            p=0.5*(y-x);
            q=p*p+w;
            z=sqrt(abs(q));
            x += t;
            if (q >= 0.0) {
              z=p+SIGN(z,p);
              wri[nn-1]=new Complex(x+z,0);
              wri[nn]=new Complex(x+z,0);
              if (z != 0.0) wri[nn]=new Complex(x-w/z,0);
            } else {
              wri[nn]=new Complex(x+p,-z);
              wri[nn-1]=wri[nn].conj();
            }
            nn -= 2;
          } else {
            if (its == 30) throw new IllegalArgumentException("Too many iterations in hqr");
View Full Code Here

TOP

Related Classes of com.nr.Complex

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.