Package com.nr

Examples of com.nr.Complex


            break;
          }
        }
        x=a[nn][nn];
        if (l == nn) {
          wri[nn]=new Complex(x+t,0);
          a[nn][nn]=x+t;
          nn--;
        } 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;
            a[nn][nn]=x;
            a[nn-1][nn-1]=y+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);
              x=a[nn][nn-1];
              s=abs(x)+abs(z);
              p=x/s;
              q=z/s;
              r=sqrt(p*p+q*q);
              p /= r;
              q /= r;
              for (j=nn-1;j<n;j++) {
                z=a[nn-1][j];
                a[nn-1][j]=q*z+p*a[nn][j];
                a[nn][j]=q*a[nn][j]-p*z;
              }
              for (i=0;i<=nn;i++) {
                z=a[i][nn-1];
                a[i][nn-1]=q*z+p*a[i][nn];
                a[i][nn]=q*a[i][nn]-p*z;
              }
              for (i=0;i<n;i++) {
                z=zz[i][nn-1];
                zz[i][nn-1]=q*z+p*zz[i][nn];
                zz[i][nn]=q*zz[i][nn]-p*z;
              }
            } 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");
            if (its == 10 || its == 20) {
              t += x;
              for (i=0;i<nn+1;i++) a[i][i] -= x;
              s=abs(a[nn][nn-1])+abs(a[nn-1][nn-2]);
              y=x=0.75*s;
              w = -0.4375*s*s;
            }
            ++its;
            for (m=nn-2;m>=l;m--) {
              z=a[m][m];
              r=x-z;
              s=y-z;
              p=(r*s-w)/a[m+1][m]+a[m][m+1];
              q=a[m+1][m+1]-z-r-s;
              r=a[m+2][m+1];
              s=abs(p)+abs(q)+abs(r);
              p /= s;
              q /= s;
              r /= s;
              if (m == l) break;
              u=abs(a[m][m-1])*(abs(q)+abs(r));
              v=abs(p)*(abs(a[m-1][m-1])+abs(z)+abs(a[m+1][m+1]));
              if (u <= EPS*v) break;
            }
            for (i=m;i<nn-1;i++) {
              a[i+2][i]=0.0;
              if (i != m) a[i+2][i-1]=0.0;
            }
            for (k=m;k<nn;k++) {
              if (k != m) {
                p=a[k][k-1];
                q=a[k+1][k-1];
                r=0.0;
                if (k+1 != nn) r=a[k+2][k-1];
                if ((x=abs(p)+abs(q)+abs(r)) != 0.0) {
                  p /= x;
                  q /= x;
                  r /= x;
                }
              }
              if ((s=SIGN(sqrt(p*p+q*q+r*r),p)) != 0.0) {
                if (k == m) {
                  if (l != m)
                  a[k][k-1] = -a[k][k-1];
                } else
                  a[k][k-1] = -s*x;
                p += s;
                x=p/s;
                y=q/s;
                z=r/s;
                q /= p;
                r /= p;
                for (j=k;j<n;j++) {
                  p=a[k][j]+q*a[k+1][j];
                  if (k+1 != nn) {
                    p += r*a[k+2][j];
                    a[k+2][j] -= p*z;
                  }
                  a[k+1][j] -= p*y;
                  a[k][j] -= p*x;
                }
                mmin = nn < k+3 ? nn : k+3;
                for (i=0;i<mmin+1;i++) {
                  p=x*a[i][k]+y*a[i][k+1];
                  if (k+1 != nn) {
                    p += z*a[i][k+2];
                    a[i][k+2] -= p*r;
                  }
                  a[i][k+1] -= p*q;
                  a[i][k] -= p;
                }
                for (i=0; i<n; i++) {
                  p=x*zz[i][k]+y*zz[i][k+1];
                  if (k+1 != nn) {
                    p += z*zz[i][k+2];
                    zz[i][k+2] -= p*r;
                  }
                  zz[i][k+1] -= p*q;
                  zz[i][k] -= p;
                }
              }
            }
          }
        }
      } while (l+1 < nn);
    }
    if (anorm != 0.0) {
      for (nn=n-1;nn>=0;nn--) {
        p=wri[nn].re();
        q=wri[nn].im();
        na=nn-1;
        if (q == 0.0) {
          m=nn;
          a[nn][nn]=1.0;
          for (i=nn-1;i>=0;i--) {
            w=a[i][i]-p;
            r=0.0;
            for (j=m;j<=nn;j++)
              r += a[i][j]*a[j][nn];
            if (wri[i].im() < 0.0) {
              z=w;
              s=r;
            } else {
              m=i;
             
              if (wri[i].im() == 0.0) {
                t=w;
                if (t == 0.0)
                  t=EPS*anorm;
                a[i][nn]=-r/t;
              } else {
                x=a[i][i+1];
                y=a[i+1][i];
                q=SQR(wri[i].re()-p)+SQR(wri[i].im());
                t=(x*s-z*r)/q;
                a[i][nn]=t;
                if (abs(x) > abs(z))
                  a[i+1][nn]=(-r-w*t)/x;
                else
                  a[i+1][nn]=(-s-y*t)/z;
              }
              t=abs(a[i][nn]);
              if (EPS*t*t > 1)
                for (j=i;j<=nn;j++)
                  a[j][nn] /= t;
            }
          }
        } else if (q < 0.0) {
          m=na;
          if (abs(a[nn][na]) > abs(a[na][nn])) {
            a[na][na]=q/a[nn][na];
            a[na][nn]=-(a[nn][nn]-p)/a[nn][na];
          } else {
            Complex temp=new Complex(0.0,-a[na][nn]).div(new Complex(a[na][na]-p,q));
            a[na][na]=temp.re();
            a[na][nn]=temp.im();
          }
          a[nn][na]=0.0;
          a[nn][nn]=1.0;
          for (i=nn-2;i>=0;i--) {
            w=a[i][i]-p;
            ra=sa=0.0;
            for (j=m;j<=nn;j++) {
              ra += a[i][j]*a[j][na];
              sa += a[i][j]*a[j][nn];
            }
            if (wri[i].im() < 0.0) {
              z=w;
              r=ra;
              s=sa;
            } else {
              m=i;
              if (wri[i].im() == 0.0) {
                Complex temp = new Complex(-ra,-sa).div(new Complex(w,q));
                a[i][na]=temp.re();
                a[i][nn]=temp.im();
              } else {
                x=a[i][i+1];
                y=a[i+1][i];
                vr=SQR(wri[i].re()-p)+SQR(wri[i].im())-q*q;
                vi=2.0*q*(wri[i].re()-p);
                if (vr == 0.0 && vi == 0.0)
                  vr=EPS*anorm*(abs(w)+abs(q)+abs(x)+abs(y)+abs(z));
                Complex temp= new Complex(x*r-z*ra+q*sa,x*s-z*sa-q*ra).div(new Complex(vr,vi));
                a[i][na]=temp.re();
                a[i][nn]=temp.im();
                if (abs(x) > abs(z)+abs(q)) {
                  a[i+1][na]=(-ra-w*a[i][na]+q*a[i][nn])/x;
                  a[i+1][nn]=(-sa-w*a[i][nn]-q*a[i][na])/x;
                } else {
                  temp= new Complex(-r-y*a[i][na],-s-y*a[i][nn]).div(new Complex(z,q));
                  a[i+1][na]=temp.re();
                  a[i+1][nn]=temp.im();
                }
              }
            }
            t=max(abs(a[i][na]),abs(a[i][nn]));
            if (EPS*t*t > 1)
View Full Code Here


  }
 
  private void sort() {
    int i;
    for (int j=1;j<n;j++) {
      Complex x=wri[j];
      for (i=j-1;i>=0;i--) {
        if (wri[i].re() >= x.re()) break;
        wri[i+1]=wri[i];
      }
      wri[i+1]=x;
    }
  }
View Full Code Here

 
  private void sortvecs() {
    int i;
    double[] temp = new double[n];
    for (int j=1;j<n;j++) {
      Complex x=wri[j];
      for (int k=0;k<n;k++)
        temp[k]=zz[k][j];
      for (i=j-1;i>=0;i--) {
        if (wri[i].re() >= x.re()) break;
        wri[i+1]=wri[i];
        for (int k=0;k<n;k++)
          zz[k][i+1]=zz[k][i];
      }
      wri[i+1]=x;
View Full Code Here

    boolean polish=true;
    double eps=1.e-10,err,sbeps=1.e-14;
    doubleW b = new doubleW(0);
    doubleW c = new doubleW(0);
   
    Complex pp[]={
        new Complex(2.0),
        new Complex(-2.0),
        new Complex(7.0),
        new Complex(1.0),
        new Complex(-3.0),
        new Complex(5.0)};
    double ppr[]={2.0,-2.0,7.0,1.0,-3.0,5.0};
    Complex[] p=new Complex[N];System.arraycopy(pp, 0, p, 0, N);
    double[] pr=buildVector(ppr);
    Complex[] rts=new Complex[N-1],rts2 =new Complex[2];
    boolean localflag, globalflag=false;

   

    // Test qroot
    System.out.println("Testing qroot");
    // Ran myran = new Ran(17); not use it
    zroots(p,rts,!polish)// Find actual roots
    //    for (j=0;j<N-1;j++) System.out.printf(rts[j] << " ");
    c.val=0.25;   // Constructed guess from actual roots
    b.val=-0.20;
    qroot(pr,b,c,eps);
    Complex[] a=new Complex[3];
    a[0]=new Complex(c.val); a[1]=new Complex(b.val); a[2]=new Complex(1.0);   // Now test result
    zroots(a,rts2,polish);
    err=0.0
    for (j=0;j<2;j++) {
      Complex r = p[4].add(rts2[j].mul(p[5]));
      r = p[3].add(rts2[j].mul(r));
      r = p[2].add(rts2[j].mul(r));
      r = p[1].add(rts2[j].mul(r));
      r = p[0].add(rts2[j].mul(r));
     
      err += r.abs();
    }
    System.out.printf("qroot: Discrepancy = %f\n", abs(err));
    localflag = abs(err) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** qroot: Quadratic is not a factor of the polynomial");
    }

    c.val=1.3;    // Constructed another guess from actual roots
    b.val=-1.4;
    qroot(pr,b,c,eps);
    a[0]=new Complex(c.val); a[1]=new Complex(b.val); a[2]=new Complex(1.0);   // Now test result
    zroots(a,rts2,polish);
    err=0.0
    for (j=0;j<2;j++) {
      Complex r = p[4].add(rts2[j].mul(p[5]));
      r = p[3].add(rts2[j].mul(r));
      r = p[2].add(rts2[j].mul(r));
      r = p[1].add(rts2[j].mul(r));
      r = p[0].add(rts2[j].mul(r));
      err += r.abs();
    }
    System.out.printf("qroot: Discrepancy = %f\n", abs(err));
    localflag = abs(err) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
View Full Code Here

  @Test
  public void test() {
    boolean polish=true;
    int i,ncount,NPOLES=6;
    double sbeps,dd[]={3.0,-15.0/4,5.0/2,-15.0/16,3.0/16,63.0/64};
    Complex z1=new Complex(),z2=new Complex();
    double[] d =buildVector(dd);
    Complex[] zcoef=new Complex[NPOLES+1],zeros=new Complex[NPOLES];
    boolean localflag, globalflag=false;

   

    // Test fixrts
    System.out.println("Testing fixrts");

    // finding roots of (z-1/2)^6=1.0
    // first write roots
    zcoef[NPOLES]=new Complex(1.0,0);
    for (i=0;i<NPOLES;i++)
      zcoef[i] = new Complex(-d[NPOLES-1-i],0.0);
    zroots(zcoef,zeros,polish);

    sbeps=1.e-13;
    ncount=0;   // Count roots outside unit circle
    for (i=0;i<NPOLES;i++) {
      z1=zeros[i].sub(new Complex(0.5,0))// compute (z-1/2)^6=1.0
      z2=z1.mul(z1).mul(z1);
      z1=z2.mul(z2);
      //System.out.printf(setprecision(18) << abs(z1-Complex(1.0,0.0)));
      localflag = z1.sub(new Complex(1.0,0.0)).abs() > sbeps;
      //System.out.printf(abs(zeros[i]));
      if (zeros[i].abs() > 1.0) ncount++;
      globalflag = globalflag || localflag;
      if (localflag) {
        fail("*** fixrts: zroots incorrectly identified a root");
       
      }
    }
    System.out.printf("   Original roots outside unit circle: %d\n", ncount);

    // now fix roots to lie within unit circle
    fixrts(d);

    // check results
    zcoef[NPOLES]=new Complex(1.0,0);
    for (i=0;i<NPOLES;i++)
      zcoef[i] = new Complex(-d[NPOLES-1-i],0.0);
    zroots(zcoef,zeros,polish);

    ncount=0;
    for (i=0;i<NPOLES;i++)
      if (zeros[i].abs() > 1.0) ncount++;
View Full Code Here

public class FFT_Ex {

 
  public static void main(String[] args) {
    Complex[] vc = new Complex[8];
    vc[0] = new Complex(1,10.1);
    vc[1] = new Complex(11,1.1);
    vc[2] = new Complex(12,12.1);
    vc[3] = new Complex(13,17.15);
    vc[4] = new Complex(1.3,1.13);
    vc[5] = new Complex(1.4,1.21);
    vc[6] = new Complex(1.7,11.1);
    vc[7] = new Complex(2.0,12.1);
    FFT.four1(vc, 1);
    System.out.println(vc[0]);
    System.out.println(vc[1]);
    System.out.println(vc[4]);
    System.out.println(vc[7]);
View Full Code Here

public class Zroots_Ex {
 
  public static void main(final String[]args) {
   
    Complex c0 = new Complex(0.000193,0);
    Complex c1 = new Complex(0.098243,0);
    Complex c2 = new Complex(25.024523,0);
    Complex c3 = new Complex(6.238928,0);
    Complex c4 = new Complex(1,0);
    Complex[] c = new Complex[]{c0, c1,c2,c3,c4};
    Complex[] roots = new Complex[4];
    zroots(c, roots,true);
    for(int i=0;i<roots.length;i++)
      System.out.println(roots[i]);
View Full Code Here

   * @param x
   * @param its
   * @return
   */
  public static Complex laguer(final Complex[] a, final Complex xx, final intW its) {
    Complex x = xx;
    final int MR=8,MT=10,MAXIT=MT*MR;
    final double EPS=DBL_EPSILON;
    Complex dx=new Complex(0,0),x1=new Complex(0,0),b=new Complex(0,0),
        d=new Complex(0,0),f=new Complex(0,0),g=new Complex(0,0),h=new Complex(0,0),
        sq=new Complex(0,0),gp=new Complex(0,0),gm=new Complex(0,0),g2=new Complex(0,0);
    int m=a.length-1;
    for (int iter=1;iter<=MAXIT;iter++) {
      its.val=iter;
      b=a[m];
      double err=b.mod();
      d=new Complex(0,0);
      f=new Complex(0,0);
      double abx=x.mod();
      for (int j=m-1;j>=0;j--) {
        f=x.mul(f).add(d);
        d=x.mul(d).add(b);
        b=x.mul(b).add(a[j]);
        err=b.mod()+abx*err;
      }
      err *= EPS;
      if (b.mod() <= err)
        return x;
      g=d.div(b);
      g2=g.mul(g);
      h=g2.sub(f.div(b).mul(2.0));
      sq = (h.mul(m).sub(g2).mul(m-1)).sqrt();
      //sq=sqrt((m-1)*(m*h-g2));
      gp=g.add(sq);
      gm=g.sub(sq);
      double abp=gp.mod();
      double abm=gm.mod();
      if (abp < abm) gp=gm;
      dx=max(abp,abm) > 0.0 ? new Complex(m,0).div(gp) : Complex.polar(1+abx,iter); // polar
      x1=x.sub(dx);
      if (x.equals(x1)) return x; // XXX "==" must to be replace to equals
      if (iter % MT != 0)
        x=x1;
      else {
        x = x.sub( new Complex(frac[iter/MT],0).div(dx));
      }
    }
    throw new IllegalArgumentException("too many iterations in laguer");
  }
View Full Code Here

   */
  public static void zroots(final Complex[] a, final Complex[] roots, final boolean polish) {
    final double EPS=1.0e-14;
    int i;
    intW its = new intW(0);
    Complex x=new Complex(0,0),b=new Complex(0,0),c=new Complex(0,0);
    int m=a.length-1;
    Complex[] ad = new Complex[m+1];
    for(i=0;i<ad.length;i++)ad[i] = new Complex(0,0);
    for (int j=0;j<=m;j++) ad[j]=a[j];
    for (int j=m-1;j>=0;j--) {
      x= new Complex(0.0,0);
      Complex[] ad_v = new Complex[j+2];
      for (int jj=0;jj<j+2;jj++) ad_v[jj]=ad[jj];
      x = laguer(ad_v,x,its);
      if (abs(x.im()) <= 2.0*EPS*abs(x.re()))
        x=new Complex(x.re(),0.0);
      roots[j]=x;
      b=ad[j+1];
      for (int jj=j;jj>=0;jj--) {
        c=ad[jj];
        ad[jj]=b;
View Full Code Here

  @Test
  public void test() {
    int i,j,N=5;
    double sbeps=5.e-14;
    double a[]={2.0,-2.0,7.0,1.0,-3.0,5.0};
    Complex b = new Complex();
    double[] aa= buildVector(a),dy = new double[N];
    Complex[] rts = new Complex[N];
    boolean localflag, globalflag=false;

    // Test zrhqr
    System.out.println("Testing zrhqr");
    // Roots of polynomial 
    zrhqr(aa,rts);
    for (i=0;i<N;i++) {
      b=new Complex(a[5]);
      for (j=0;j<5;j++) b=b.mul(rts[i]).add(new Complex(a[4-j]));
      dy[i]=b.abs();
//      System.out.printf(b << "  %f\n", dy[i]);
    }
    System.out.printf("zrhqr: Maximum discrepancy = %f\n", maxel(dy));
    localflag = maxel(dy) > sbeps;
    globalflag = globalflag || localflag;
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.