Package com.nr.la

Examples of com.nr.la.NRsparseMat


    for (i=0;i<nvals;i++) a[myran.int32p() % N][myran.int32p() % M] = (double)(i);
    nvals=0;    // There may be fewer than nvals non-zero elements
    for (i=0;i<N;i++)
      for(j=0;j<M;j++)
        if (a[i][j] != 0.0) nvals++;
    NRsparseMat as = new NRsparseMat(N,M,nvals);
    k=0;
    for (i=0;i<M;i++) {     // Columns
      as.col_ptr[i]=k;
      for(j=0;j<N;j++)    // Rows
        if (a[j][i] != 0.0) {      
          as.row_ind[k]=j;
          as.val[k++]=a[j][i];
        }
    }
    as.col_ptr[M]=k;

    double[][] b = new double[N][M];
    for (i=0;i<M;i++)
      for (j=as.col_ptr[i];j<as.col_ptr[i+1];j++)
        b[as.row_ind[j]][i]=as.val[j];
    sbeps = 5.e-14;
    localflag = maxel(matsub(a,b)) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** NRsparseMat: Reconstructed matrix is incorrect");
     
    }

    // Test multiplication of sparse matrix by vector
    double[] c = new double[as.ncols];
    ranvec(c);
    double[] d=as.ax(c);    // Sparse multiplication by vector
    // Find same result by brute force
    double[] e = new double[as.nrows];
    for (i=0;i<as.nrows;i++) {
      val=0.0;
      for (j=0;j<as.ncols;j++) val += a[i][j]*c[j];
      e[i]=val;
    }

//    System.out.println("Discrepancy in A*x: " << maxel(vecsub(d,e)) << endl;
    localflag = (maxel(vecsub(d,e)) > sbeps);
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** NRsparseMat: Multiplication of sparse matrix by vector failed.");
     
    }
   
    // Test multiplication of sparse transpose by vector
    double[] f = new double[as.nrows];
    ranvec(f);
    double[] g=as.atx(f); // Sparse transpose multiplied by vector
    // Find same result by brute force
    double[] h= new double[as.ncols];
    for (i=0;i<as.ncols;i++) {
      val=0.0;
      for (j=0;j<as.nrows;j++) val += a[j][i]*f[j];
 
View Full Code Here


    nvals=0;    // There may be fewer than nvals non-zero elements
    for (i=0;i<N;i++)
      for(j=0;j<M;j++)
        if (a[i][j] != 0) nvals++;

    NRsparseMat as = new NRsparseMat(N,M,nvals);
    k=0;
    for (i=0;i<M;i++) {     // Columns
      as.col_ptr[i]=k;
      for(j=0;j<N;j++)    // Rows
        if (a[j][i] != 0.0) {      
          as.row_ind[k]=j;
          as.val[k++]=a[j][i];
        }
    }
    as.col_ptr[M]=k;

    NRsparseMat bs=as.transpose();
    ADAT c = new ADAT(as,bs);
    double[] d = new double[M];    // Random diagonal matrix
    ranvec(d);
    c.updateD(d);   // Update ADAT object
    NRsparseMat cs=c.ref()// cs represents N x N matrix

    // Next, compute the N x N matrix by brute force
    double[][] e = new double[M][N];
    for (i=0;i<M;i++)
      for (j=0;j<N;j++)
        e[i][j]=d[i]*a[j][i];
    double[][] f=matmul(a,e); // f is N x N

    // Find sparse representation of F
    nvals=0;    // Count non-zero elements
    for (i=0;i<N;i++)
      for(j=0;j<N;j++)
        if (f[i][j] != 0.0) nvals++;
    NRsparseMat fs = new NRsparseMat(N,N,nvals);
    k=0;
    for (i=0;i<N;i++) {     // Columns
      fs.col_ptr[i]=k;
      for(j=0;j<N;j++)    // Rows
        if (f[j][i] != 0.0) {      
View Full Code Here

    // Test Linbcg (and NRsparseMat)
    System.out.println("Testing Linbcg (and NRsparseMat)");
    final int NP=20;
    final int NSIZE=58;
    NRsparseMat sa = new NRsparseMat(NP,NP,NSIZE);
    sa.col_ptr[1]=2;
    for (i=1;i<NP-1;i++)
      sa.col_ptr[i+1]=sa.col_ptr[i]+3;
    sa.col_ptr[NP]=sa.col_ptr[NP-1]+2;
    sa.row_ind[0]=0;
    sa.row_ind[1]=1;
    sa.val[0]=3.0;
    sa.val[1]=-2.0;
    int k=1;
    for (j=1;j<NP-1;j++) {
      i=j-1;
      sa.row_ind[++k]=i;
      sa.val[k]=2.0;
      sa.row_ind[++k]=++i;
      sa.val[k]=3.0;
      sa.row_ind[++k]=++i;
      sa.val[k]=-2.0;
    }
    sa.row_ind[++k]=NP-2;
    sa.val[k]=2.0;
    sa.row_ind[++k]=NP-1;
    sa.val[k]=3.0;
    double[] bbb = buildVector(NP,1.0),xx = new double[NP];
    bbb[0]=3.0;
    bbb[NP-1] = -1.0;
    final int ITOL=1,ITMAX=75;
    final double TOL=1.0e-9;
    doubleW err = new doubleW(0);
    intW iter = new intW(0);

    TestLinbcg test = new TestLinbcg(sa);
    test.solve(bbb,xx,ITOL,TOL,ITMAX,iter,err);

    sbeps = 5.e-15;
    localflag = maxel(vecsub(sa.ax(xx),bbb)) > sbeps;
    globalflag = globalflag || localflag;
    if (localflag) {
      fail("*** Linbcg: Derived class gives inconsistent solution vector");
     
    }
View Full Code Here

TOP

Related Classes of com.nr.la.NRsparseMat

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.