예제 #1
0
BandMatrix  createSymmetricPositiveDefiniteBandMatrix( const dim_t matDim, const dim_t bandWidth ) {
    assert( bandWidth < matDim );

    /*-----------------------------------------------------------------------------
     *  initialize the band matrix
     *-----------------------------------------------------------------------------*/
    BandMatrix  bandM;
    bandM._matDim = matDim;
    bandM._lowerBand = bandWidth;
    bandM._upperBand = bandWidth;
    bandM._vals.assign( matDim * (bandWidth*2+1), 0 );


    /*-----------------------------------------------------------------------------
     *  generate rand values for matrix entries
     *-----------------------------------------------------------------------------*/
    //std::cout << "dim = " << bandM._matDim << std::endl;
    srand( static_cast<unsigned>(time(0)) );    /* seed */
    for( dim_t i = 0; i < matDim; i++ ) {
        for( dim_t j = 0; j <= bandWidth; j++ ) { /* let diagnoal entires MUCH GREATER than the others */
            const data_t HI = (j==0) ? 20000 : 10;
            const data_t LO = (j==0) ? 10000 : -10;
            const data_t r = LO + static_cast<data_t>(rand()) / (static_cast<data_t>( RAND_MAX/(HI-LO) ) );
            bandM.writeEntry( i, i+j, r );
            if( j>0 )
                bandM.writeEntry( i+j, i, r );
        }
    }

    return bandM;
}
예제 #2
0
bool    checkBandMatrixEqual( BandMatrix & m1, BandMatrix & m2 ) {
    if( m1._matDim != m2._matDim || m1._lowerBand != m2._lowerBand || m1._upperBand != m2._upperBand || m1._vals.size() != m2._vals.size() )
        return false;
    for( dim_t i = 0; i < m1.getNumNonZeroEntries(); i++ ) {
        if( std::fabs( m1._vals[i]-m2._vals[i] ) > EPSILON )
            return false;
    }
    return true;
}
예제 #3
0
ReturnMatrix Returner6(const GenericMatrix& GM)
{ BandMatrix M = GM*6; M.Release(); return M; }
예제 #4
0
void trymatd()
{
   Tracer et("Thirteenth test of Matrix package");
   Tracer::PrintTrace();
   Matrix X(5,20);
   int i,j;
   for (j=1;j<=20;j++) X(1,j) = j+1;
   for (i=2;i<=5;i++) for (j=1;j<=20; j++) X(i,j) = (long)X(i-1,j) * j % 1001;
   SymmetricMatrix S; S << X * X.t();
   Matrix SM = X * X.t() - S;
   Print(SM);
   LowerTriangularMatrix L = Cholesky(S);
   Matrix Diff = L*L.t()-S; Clean(Diff, 0.000000001);
   Print(Diff);
   {
      Tracer et1("Stage 1");
      LowerTriangularMatrix L1(5);
      Matrix Xt = X.t(); Matrix Xt2 = Xt;
      QRZT(X,L1);
      Diff = L - L1; Clean(Diff,0.000000001); Print(Diff);
      UpperTriangularMatrix Ut(5);
      QRZ(Xt,Ut);
      Diff = L - Ut.t(); Clean(Diff,0.000000001); Print(Diff);
      Matrix Y(3,20);
      for (j=1;j<=20;j++) Y(1,j) = 22-j;
      for (i=2;i<=3;i++) for (j=1;j<=20; j++)
         Y(i,j) = (long)Y(i-1,j) * j % 101;
      Matrix Yt = Y.t(); Matrix M,Mt; Matrix Y2=Y;
      QRZT(X,Y,M); QRZ(Xt,Yt,Mt);
      Diff = Xt - X.t(); Clean(Diff,0.000000001); Print(Diff);
      Diff = Yt - Y.t(); Clean(Diff,0.000000001); Print(Diff);
      Diff = Mt - M.t(); Clean(Diff,0.000000001); Print(Diff);
      Diff = Y2 * Xt2 * S.i() - M * L.i();
      Clean(Diff,0.000000001); Print(Diff);
   }

   ColumnVector C1(5);
   {
      Tracer et1("Stage 2");
      X.ReSize(5,5);
      for (j=1;j<=5;j++) X(1,j) = j+1;
      for (i=2;i<=5;i++) for (j=1;j<=5; j++)
         X(i,j) = (long)X(i-1,j) * j % 1001;
      for (i=1;i<=5;i++) C1(i) = i*i;
      CroutMatrix A = X;
      ColumnVector C2 = A.i() * C1; C1 = X.i()  * C1;
      X = C1 - C2; Clean(X,0.000000001); Print(X);
   }

   {
      Tracer et1("Stage 3");
      X.ReSize(7,7);
      for (j=1;j<=7;j++) X(1,j) = j+1;
      for (i=2;i<=7;i++) for (j=1;j<=7; j++)
         X(i,j) = (long)X(i-1,j) * j % 1001;
      C1.ReSize(7);
      for (i=1;i<=7;i++) C1(i) = i*i;
      RowVector R1 = C1.t();
      Diff = R1 * X.i() - ( X.t().i() * R1.t() ).t(); Clean(Diff,0.000000001);
      Print(Diff);
   }

   {
      Tracer et1("Stage 4");
      X.ReSize(5,5);
      for (j=1;j<=5;j++) X(1,j) = j+1;
      for (i=2;i<=5;i++) for (j=1;j<=5; j++)
         X(i,j) = (long)X(i-1,j) * j % 1001;
      C1.ReSize(5);
      for (i=1;i<=5;i++) C1(i) = i*i;
      CroutMatrix A1 = X*X;
      ColumnVector C2 = A1.i() * C1; C1 = X.i()  * C1; C1 = X.i()  * C1;
      X = C1 - C2; Clean(X,0.000000001); Print(X);
   }


   {
      Tracer et1("Stage 5");
      int n = 40;
      SymmetricBandMatrix B(n,2); B = 0.0;
      for (i=1; i<=n; i++)
      {
         B(i,i) = 6;
         if (i<=n-1) B(i,i+1) = -4;
         if (i<=n-2) B(i,i+2) = 1;
      }
      B(1,1) = 5; B(n,n) = 5;
      SymmetricMatrix A = B;
      ColumnVector X(n);
      X(1) = 429;
      for (i=2;i<=n;i++) X(i) = (long)X(i-1) * 31 % 1001;
      X = X / 100000L;
      // the matrix B is rather ill-conditioned so the difficulty is getting
      // good agreement (we have chosen X very small) may not be surprising;
      // maximum element size in B.i() is around 1400
      ColumnVector Y1 = A.i() * X;
      LowerTriangularMatrix C1 = Cholesky(A);
      ColumnVector Y2 = C1.t().i() * (C1.i() * X) - Y1;
      Clean(Y2, 0.000000001); Print(Y2);
      UpperTriangularMatrix CU = C1.t().i();
      LowerTriangularMatrix CL = C1.i();
      Y2 = CU * (CL * X) - Y1;
      Clean(Y2, 0.000000001); Print(Y2);
      Y2 = B.i() * X - Y1; Clean(Y2, 0.000000001); Print(Y2);

      LowerBandMatrix C2 = Cholesky(B);
      Matrix M = C2 - C1; Clean(M, 0.000000001); Print(M);
      ColumnVector Y3 = C2.t().i() * (C2.i() * X) - Y1;
      Clean(Y3, 0.000000001); Print(Y3);
      CU = C1.t().i();
      CL = C1.i();
      Y3 = CU * (CL * X) - Y1;
      Clean(Y3, 0.000000001); Print(Y3);

      Y3 = B.i() * X - Y1; Clean(Y3, 0.000000001); Print(Y3);

      SymmetricMatrix AI = A.i();
      Y2 = AI*X - Y1; Clean(Y2, 0.000000001); Print(Y2);
      SymmetricMatrix BI = B.i();
      BandMatrix C = B; Matrix CI = C.i();
      M = A.i() - CI; Clean(M, 0.000000001); Print(M);
      M = B.i() - CI; Clean(M, 0.000000001); Print(M);
      M = AI-BI; Clean(M, 0.000000001); Print(M);
      M = AI-CI; Clean(M, 0.000000001); Print(M);

      M = A; AI << M; M = AI-A; Clean(M, 0.000000001); Print(M);
      C = B; BI << C; M = BI-B; Clean(M, 0.000000001); Print(M);
   }

   {
      Tracer et1("Stage 5");
      SymmetricMatrix A(4), B(4);
      A << 5
        << 1 << 4
        << 2 << 1 << 6
        << 1 << 0 << 1 << 7;
      B << 8
        << 1 << 5
        << 1 << 0 << 9
        << 2 << 1 << 0 << 6;
      LowerTriangularMatrix AB = Cholesky(A) * Cholesky(B);
      Matrix M = Cholesky(A) * B * Cholesky(A).t() - AB*AB.t();
      Clean(M, 0.000000001); Print(M);
      M = A * Cholesky(B); M = M * M.t() - A * B * A;
      Clean(M, 0.000000001); Print(M);
   }
   
   {
      Tracer et1("Stage 6");
      int N=49;
      int i;
      SymmetricBandMatrix S(N,1);
      Matrix B(N,N+1); B=0;
      for (i=1;i<=N;i++) { S(i,i)=1; B(i,i)=1; B(i,i+1)=-1; }
      for (i=1;i<N; i++) S(i,i+1)=-.5;
      DiagonalMatrix D(N+1); D = 1;
      B = B.t()*S.i()*B - (D-1.0/(N+1))*2.0;
      Clean(B, 0.000000001); Print(B);
   }

   {
      Tracer et1("Stage 7");
      // Copying and moving CroutMatrix
      Matrix A(7,7);
      A.Row(1) <<  3 <<  2 << -1 <<  4 << -3 <<  5 <<  9;
      A.Row(2) << -8 <<  7 <<  2 <<  0 <<  7 <<  0 << -1;
      A.Row(3) <<  2 << -2 <<  3 <<  1 <<  9 <<  0 <<  3;
      A.Row(4) << -1 <<  5 <<  2 <<  2 <<  5 << -1 <<  2;
      A.Row(5) <<  4 << -4 <<  1 <<  9 << -8 <<  7 <<  5;
      A.Row(6) <<  1 << -2 <<  5 << -1 << -2 <<  5 <<  1;
      A.Row(7) << -6 <<  3 << -1 <<  8 << -1 <<  2 <<  2;
      RowVector D(30); D = 0;
      Real x = determinant(A);
      CroutMatrix B = A;
      D(1) = determinant(B) / x - 1;
      Matrix C = A * Inverter1(B) - IdentityMatrix(7);
      Clean(C, 0.000000001); Print(C);
      // Test copy constructor (in Inverter2 and ordinary copy)
      CroutMatrix B1; B1 = B;
      D(2) = determinant(B1) / x - 1;
      C = A * Inverter2(B1) - IdentityMatrix(7);
      Clean(C, 0.000000001); Print(C);
      // Do it again with release
      B.release(); B1 = B;
      D(2) = B.nrows(); D(3) = B.ncols(); D(4) = B.size();
      D(5) = determinant(B1) / x - 1;
      B1.release();
      C = A * Inverter2(B1) - IdentityMatrix(7);
      D(6) = B1.nrows(); D(7) = B1.ncols(); D(8) = B1.size();
      Clean(C, 0.000000001); Print(C);
      // see if we get an implicit invert
      B1 = -A; 
      D(9) = determinant(B1) / x + 1; // odd number of rows - sign will change 
      C = -A * Inverter2(B1) - IdentityMatrix(7);
      Clean(C, 0.000000001); Print(C);
      // check for_return
      B = LU1(A); B1 = LU2(A); CroutMatrix B2 = LU3(A);
      C = A * B.i() - IdentityMatrix(7); Clean(C, 0.000000001); Print(C);
      D(10) = (B == B1 ? 0 : 1) + (B == B2 ? 0 : 1);
      // check lengths
      D(13) = B.size()-49;
      // check release(2)
      B1.release(2);
      B2 = B1; D(15) = B == B2 ? 0 : 1;
      CroutMatrix B3 = B1; D(16) = B == B3 ? 0 : 1;
      D(17) = B1.size();
      // some oddments
      B1 = B; B1 = B1.i(); C = A - B1.i(); Clean(C, 0.000000001); Print(C);
      B1 = B; B1.release(); B1 = B1; B2 = B1;
      D(19) = B == B1 ? 0 : 1; D(20) = B == B2 ? 0 : 1;
      B1.cleanup(); B2 = B1; D(21) = B1.size(); D(22) = B2.size();
      GenericMatrix GM = B; C = A.i() - GM.i(); Clean(C, 0.000000001); Print(C);
      B1 = GM; D(23) = B == B1 ? 0 : 1;
      B1 = A * 0; B2 = B1; D(24) = B2.is_singular() ? 0 : 1;
      // check release again - see if memory moves
      const Real* d = B.const_data();
      const int* i = B.const_data_indx();
      B1 = B;
      const Real* d1 = B1.const_data();
      const int* i1 = B1.const_data_indx();
      B1.release(); B2 = B1;
      const Real* d2 = B2.const_data();
      const int* i2 = B2.const_data_indx();
      D(25) = (d != d1 ? 0 : 1) + (d1 == d2 ? 0 : 1)
         + (i != i1 ? 0 : 1) + (i1 == i2 ? 0 : 1);
  
      Clean(D, 0.000000001); Print(D);
   }

   {
      Tracer et1("Stage 8");
      // Same for BandLUMatrix
      BandMatrix A(7,3,2);
      A.Row(1) <<  3 <<  2 << -1;
      A.Row(2) << -8 <<  7 <<  2 <<  0;
      A.Row(3) <<  2 << -2 <<  3 <<  1 <<  9;
      A.Row(4) << -1 <<  5 <<  2 <<  2 <<  5 << -1;
      A.Row(5)       << -4 <<  1 <<  9 << -8 <<  7 <<  5;
      A.Row(6)             <<  5 << -1 << -2 <<  5 <<  1;
      A.Row(7)                   <<  8 << -1 <<  2 <<  2;
      RowVector D(30); D = 0;
      Real x = determinant(A);
      BandLUMatrix B = A;
      D(1) = determinant(B) / x - 1;
      Matrix C = A * Inverter1(B) - IdentityMatrix(7);
      Clean(C, 0.000000001); Print(C);
      // Test copy constructor (in Inverter2 and ordinary copy)
      BandLUMatrix B1; B1 = B;
      D(2) = determinant(B1) / x - 1;
      C = A * Inverter2(B1) - IdentityMatrix(7);
      Clean(C, 0.000000001); Print(C);
      // Do it again with release
      B.release(); B1 = B;
      D(2) = B.nrows(); D(3) = B.ncols(); D(4) = B.size();
      D(5) = determinant(B1) / x - 1;
      B1.release();
      C = A * Inverter2(B1) - IdentityMatrix(7);
      D(6) = B1.nrows(); D(7) = B1.ncols(); D(8) = B1.size();
      Clean(C, 0.000000001); Print(C);
      // see if we get an implicit invert
      B1 = -A; 
      D(9) = determinant(B1) / x + 1; // odd number of rows - sign will change 
      C = -A * Inverter2(B1) - IdentityMatrix(7);
      Clean(C, 0.000000001); Print(C);
      // check for_return
      B = LU1(A); B1 = LU2(A); BandLUMatrix B2 = LU3(A);
      C = A * B.i() - IdentityMatrix(7); Clean(C, 0.000000001); Print(C);
      D(10) = (B == B1 ? 0 : 1) + (B == B2 ? 0 : 1);
      // check lengths
      D(11) = B.bandwidth().lower()-3;
      D(12) = B.bandwidth().upper()-2;
      D(13) = B.size()-42;
      D(14) = B.size2()-21;
      // check release(2)
      B1.release(2);
      B2 = B1; D(15) = B == B2 ? 0 : 1;
      BandLUMatrix B3 = B1; D(16) = B == B3 ? 0 : 1;
      D(17) = B1.size();
      // Compare with CroutMatrix
      CroutMatrix CM = A;
      C = CM.i() - B.i(); Clean(C, 0.000000001); Print(C);
      D(18) = determinant(CM) / x - 1;
      // some oddments
      B1 = B; CM = B1.i(); C = A - CM.i(); Clean(C, 0.000000001); Print(C);
      B1 = B; B1.release(); B1 = B1; B2 = B1;
      D(19) = B == B1 ? 0 : 1; D(20) = B == B2 ? 0 : 1;
      B1.cleanup(); B2 = B1; D(21) = B1.size(); D(22) = B2.size();
      GenericMatrix GM = B; C = A.i() - GM.i(); Clean(C, 0.000000001); Print(C);
      B1 = GM; D(23) = B == B1 ? 0 : 1;
      B1 = A * 0; B2 = B1; D(24) = B2.is_singular() ? 0 : 1;
      // check release again - see if memory moves
      const Real* d = B.const_data(); const Real* dd = B.const_data();
      const int* i = B.const_data_indx();
      B1 = B;
      const Real* d1 = B1.const_data(); const Real* dd1 = B1.const_data();
      const int* i1 = B1.const_data_indx();
      B1.release(); B2 = B1;
      const Real* d2 = B2.const_data(); const Real* dd2 = B2.const_data();
      const int* i2 = B2.const_data_indx();
      D(25) = (d != d1 ? 0 : 1) + (d1 == d2 ? 0 : 1)
         + (dd != dd1 ? 0 : 1) + (dd1 == dd2 ? 0 : 1)
         + (i != i1 ? 0 : 1) + (i1 == i2 ? 0 : 1);

      Clean(D, 0.000000001); Print(D);
   }

   {
      Tracer et1("Stage 9");
      // Modification of Cholesky decomposition

      int i, j;

      // Build test matrix
      Matrix X(100, 10);
      MultWithCarry mwc;   // Uniform random number generator
      for (i = 1; i <= 100; ++i) for (j = 1; j <= 10; ++j)
         X(i, j) = 2.0 * (mwc.Next() - 0.5);
      Matrix X1 = X;     // save copy

      // Form sums of squares and products matrix and Cholesky decompose
      SymmetricMatrix A; A << X.t() * X;
      UpperTriangularMatrix U1 = Cholesky(A).t();

      // Do QR decomposition of X and check we get same triangular matrix
      UpperTriangularMatrix U2;
      QRZ(X, U2);
      Matrix Diff = U1 - U2; Clean(Diff, 0.000000001); Print(Diff);

      // Try adding new row to X and updating triangular matrix 
      RowVector NewRow(10);
      for (j = 1; j <= 10; ++j) NewRow(j) = 2.0 * (mwc.Next() - 0.5);
      UpdateCholesky(U2, NewRow);
      X = X1 & NewRow; QRZ(X, U1);
      Diff = U1 - U2; Clean(Diff, 0.000000001); Print(Diff);

      // Try removing two rows and updating triangular matrix
      DowndateCholesky(U2, X1.Row(20));
      DowndateCholesky(U2, X1.Row(35));
      X = X1.Rows(1,19) & X1.Rows(21,34) & X1.Rows(36,100) & NewRow; QRZ(X, U1);
      Diff = U1 - U2; Clean(Diff, 0.000000001); Print(Diff);

      // Circular shifts

      CircularShift(X, 3,6);
      CircularShift(X, 5,5);
      CircularShift(X, 4,5);
      CircularShift(X, 1,6);
      CircularShift(X, 6,10);
   }
   
   {
      Tracer et1("Stage 10");
      // Try updating QRZ, QRZT decomposition
      TestUpdateQRZ tuqrz1(10, 100, 50, 25); tuqrz1.DoTest();
      tuqrz1.Reset(); tuqrz1.ClearRow(1); tuqrz1.DoTest();
      tuqrz1.Reset(); tuqrz1.ClearRow(1); tuqrz1.ClearRow(2); tuqrz1.DoTest();
      tuqrz1.Reset(); tuqrz1.ClearRow(5); tuqrz1.ClearRow(6); tuqrz1.DoTest();
      tuqrz1.Reset(); tuqrz1.ClearRow(10); tuqrz1.DoTest();
      TestUpdateQRZ tuqrz2(15, 100, 0, 0); tuqrz2.DoTest();
      tuqrz2.Reset(); tuqrz2.ClearRow(1); tuqrz2.DoTest();
      tuqrz2.Reset(); tuqrz2.ClearRow(1); tuqrz2.ClearRow(2); tuqrz2.DoTest();
      tuqrz2.Reset(); tuqrz2.ClearRow(5); tuqrz2.ClearRow(6); tuqrz2.DoTest();
      tuqrz2.Reset(); tuqrz2.ClearRow(15); tuqrz2.DoTest();
      TestUpdateQRZ tuqrz3(5, 0, 10, 0); tuqrz3.DoTest();
      
   }
   
//   cout << "\nEnd of Thirteenth test\n";
}
예제 #5
0
void trymat8()
{
//   cout << "\nEighth test of Matrix package\n";
   Tracer et("Eighth test of Matrix package");
   Tracer::PrintTrace();

   int i;


   DiagonalMatrix D(6);
   for (i=1;i<=6;i++)  D(i,i)=i*i+i-10;
   DiagonalMatrix D2=D;
   Matrix MD=D;

   DiagonalMatrix D1(6); for (i=1;i<=6;i++) D1(i,i)=-100+i*i*i;
   Matrix MD1=D1;
   Print(Matrix(D*D1-MD*MD1));
   Print(Matrix((-D)*D1+MD*MD1));
   Print(Matrix(D*(-D1)+MD*MD1));
   DiagonalMatrix DX=D;
   {
      Tracer et1("Stage 1");
      DX=(DX+D1)*DX; Print(Matrix(DX-(MD+MD1)*MD));
      DX=D;
      DX=-DX*DX+(DX-(-D1))*((-D1)+DX);
      // Matrix MX = Matrix(MD1);
      // MD1=DX+(MX.t())*(MX.t()); Print(MD1);
      MD1=DX+(Matrix(MD1).t())*(Matrix(MD1).t()); Print(MD1);
      DX=D; DX=DX; DX=D2-DX; Print(DiagonalMatrix(DX));
      DX=D;
   }
   {
      Tracer et1("Stage 2");
      D.Release(2);
      D1=D; D2=D;
      Print(DiagonalMatrix(D1-DX));
      Print(DiagonalMatrix(D2-DX));
      MD1=1.0;
      Print(Matrix(MD1-1.0));
   }
   {
      Tracer et1("Stage 3");
      //GenericMatrix
      LowerTriangularMatrix LT(4);
      LT << 1 << 2 << 3 << 4 << 5 << 6  << 7 << 8 << 9 << 10;
      UpperTriangularMatrix UT = LT.t() * 2.0;
      GenericMatrix GM1 = LT;
      LowerTriangularMatrix LT1 = GM1-LT; Print(LT1);
      GenericMatrix GM2 = GM1; LT1 = GM2; LT1 = LT1-LT; Print(LT1);
      GM2 = GM1; LT1 = GM2; LT1 = LT1-LT; Print(LT1);
      GM2 = GM1*2; LT1 = GM2; LT1 = LT1-LT*2; Print(LT1);
      GM1.Release();
      GM1=GM1; LT1=GM1-LT; Print(LT1); LT1=GM1-LT; Print(LT1);
      GM1.Release();
      GM1=GM1*4; LT1=GM1-LT*4; Print(LT1);
      LT1=GM1-LT*4; Print(LT1); GM1.CleanUp();
      GM1=LT; GM2=UT; GM1=GM1*GM2; Matrix M=GM1; M=M-LT*UT; Print(M);
      Transposer(LT,GM2); LT1 = LT - GM2.t(); Print(LT1);
      GM1=LT; Transposer(GM1,GM2); LT1 = LT - GM2.t(); Print(LT1);
      GM1 = LT; GM1 = GM1 + GM1; LT1 = LT*2-GM1; Print(LT1);
      DiagonalMatrix D; D << LT; GM1 = D; LT1 = GM1; LT1 -= D; Print(LT1);
      UpperTriangularMatrix UT1 = GM1; UT1 -= D; Print(UT1);
   }
   {
      Tracer et1("Stage 4");
      // Another test of SVD
      Matrix M(12,12); M = 0;
      M(1,1) = M(2,2) = M(4,4) = M(6,6) =
         M(7,7) = M(8,8) = M(10,10) = M(12,12) = -1;
      M(1,6) = M(1,12) = -5.601594;
      M(3,6) = M(3,12) = -0.000165;
      M(7,6) = M(7,12) = -0.008294;
      DiagonalMatrix D;
      SVD(M,D);
      SortDescending(D);
      // answer given by matlab
      DiagonalMatrix DX(12);
      DX(1) = 8.0461;
      DX(2) = DX(3) = DX(4) = DX(5) = DX(6) = DX(7) = 1;
      DX(8) = 0.1243;
      DX(9) = DX(10) = DX(11) = DX(12) = 0;
      D -= DX; Clean(D,0.0001); Print(D);
   }
#ifndef DONT_DO_NRIC
   {
      Tracer et1("Stage 5");
      // test numerical recipes in C interface
      DiagonalMatrix D(10);
      D << 1 << 4 << 6 << 2 << 1 << 6 << 4 << 7 << 3 << 1;
      ColumnVector C(10);
      C << 3 << 7 << 5 << 1 << 4 << 2 << 3 << 9 << 1 << 3;
      RowVector R(6);
      R << 2 << 3 << 5 << 7 << 11 << 13;
      nricMatrix M(10, 6);
      DCR( D.nric(), C.nric(), 10, R.nric(), 6, M.nric() );
      M -= D * C * R;  Print(M);

      D.ReSize(5);
      D << 1.25 << 4.75 << 9.5 << 1.25 << 3.75;
      C.ReSize(5);
      C << 1.5 << 7.5 << 4.25 << 0.0 << 7.25;
      R.ReSize(9);
      R << 2.5 << 3.25 << 5.5 << 7 << 11.25 << 13.5 << 0.0 << 1.5 << 3.5;
      Matrix MX = D * C * R;
      M.ReSize(MX);
      DCR( D.nric(), C.nric(), 5, R.nric(), 9, M.nric() );
      M -= MX;  Print(M);
      
      // test swap
      nricMatrix A(3,4); nricMatrix B(4,5);
      A.Row(1) << 2 << 7 << 3 << 6;
      A.Row(2) << 6 << 2 << 5 << 9;
      A.Row(3) << 1 << 0 << 1 << 6;
      B.Row(1) << 2 << 8 << 4 << 5 << 3;
      B.Row(2) << 1 << 7 << 5 << 3 << 9;
      B.Row(3) << 7 << 8 << 2 << 1 << 6;
      B.Row(4) << 5 << 2 << 9 << 0 << 9;
      nricMatrix A1(1,2); nricMatrix B1;
      nricMatrix X(3,5); Matrix X1 = A * B;
      swap(A, A1); swap(B1, B);
      for (int i = 1; i <= 3; ++i) for (int j = 1; j <= 5; ++j)
      {
         X.nric()[i][j] = 0.0;
         for (int k = 1; k <= 4; ++k)
            X.nric()[i][j] += A1.nric()[i][k] * B1.nric()[k][j];
      }
      X1 -= X; Print(X1); 
   }
#endif
   {
      Tracer et1("Stage 6");
      // test dotproduct
      DiagonalMatrix test(5); test = 1;
      ColumnVector C(10);
      C << 3 << 7 << 5 << 1 << 4 << 2 << 3 << 9 << 1 << 3;
      RowVector R(10);
      R << 2 << 3 << 5 << 7 << 11 << 13 << -3 << -4 << 2 << 4;
      test(1) = (R * C).AsScalar() - DotProduct(C, R);
      test(2) = C.SumSquare() - DotProduct(C, C);
      test(3) = 6.0 * (C.t() * R.t()).AsScalar() - DotProduct(2.0 * C, 3.0 * R);
      Matrix MC = C.AsMatrix(2,5), MR = R.AsMatrix(5,2);
      test(4) = DotProduct(MC, MR) - (R * C).AsScalar();
      UpperTriangularMatrix UT(5);
      UT << 3 << 5 << 2 << 1 << 7
              << 1 << 1 << 8 << 2
                   << 7 << 0 << 1
                        << 3 << 5
                             << 6;
      LowerTriangularMatrix LT(5);
      LT << 5
         << 2 << 3
         << 1 << 0 << 7
         << 9 << 8 << 1 << 2
         << 0 << 2 << 1 << 9 << 2;
      test(5) = DotProduct(UT, LT) - Sum(SP(UT, LT));
      Print(test);
      // check row-wise load;
      LowerTriangularMatrix LT1(5);
      LT1.Row(1) << 5;
      LT1.Row(2) << 2   << 3;
      LT1.Row(3) << 1   << 0   << 7;
      LT1.Row(4) << 9   << 8   << 1   << 2;
      LT1.Row(5) << 0   << 2   << 1   << 9   << 2;
      Matrix M = LT1 - LT; Print(M);
      // check solution with identity matrix
      IdentityMatrix IM(5); IM *= 2;
      LinearEquationSolver LES1(IM);
      LowerTriangularMatrix LTX = LES1.i() * LT;
      M = LTX * 2 - LT; Print(M);
      DiagonalMatrix D = IM;
      LinearEquationSolver LES2(IM);
      LTX = LES2.i() * LT;
      M = LTX * 2 - LT; Print(M);
      UpperTriangularMatrix UTX = LES1.i() * UT;
      M = UTX * 2 - UT; Print(M);
      UTX = LES2.i() * UT;
      M = UTX * 2 - UT; Print(M);
   }

   {
      Tracer et1("Stage 7");
      // Some more GenericMatrix stuff with *= |= &=
      // but don't any additional checks
      BandMatrix BM1(6,2,3);
      BM1.Row(1) << 3 << 8 << 4 << 1;
      BM1.Row(2) << 5 << 1 << 9 << 7 << 2;
      BM1.Row(3) << 1 << 0 << 6 << 3 << 1 << 3;
      BM1.Row(4)      << 4 << 2 << 5 << 2 << 4;
      BM1.Row(5)           << 3 << 3 << 9 << 1;
      BM1.Row(6)                << 4 << 2 << 9;
      BandMatrix BM2(6,1,1);
      BM2.Row(1) << 2.5 << 7.5;
      BM2.Row(2) << 1.5 << 3.0 << 8.5;
      BM2.Row(3)        << 6.0 << 6.5 << 7.0;
      BM2.Row(4)               << 2.5 << 2.0 << 8.0;
      BM2.Row(5)                      << 0.5 << 4.5 << 3.5;
      BM2.Row(6)                             << 9.5 << 7.5;
      Matrix RM1 = BM1, RM2 = BM2;
      Matrix X;
      GenericMatrix GRM1 = RM1, GBM1 = BM1, GRM2 = RM2, GBM2 = BM2;
      Matrix Z(6,0); Z = 5; Print(Z);
      GRM1 |= Z; GBM1 |= Z; GRM2 &= Z.t(); GBM2 &= Z.t();
      X = GRM1 - BM1; Print(X); X = GBM1 - BM1; Print(X);
      X = GRM2 - BM2; Print(X); X = GBM2 - BM2; Print(X);

      GRM1 = RM1; GBM1 = BM1; GRM2 = RM2; GBM2 = BM2;
      GRM1 *= GRM2; GBM1 *= GBM2;
      X = GRM1 - BM1 * BM2; Print(X);
      X = RM1 * RM2 - GBM1; Print(X);

      GRM1 = RM1; GBM1 = BM1; GRM2 = RM2; GBM2 = BM2;
      GRM1 *= GBM2; GBM1 *= GRM2;          // Bs and Rs swapped on LHS
      X = GRM1 - BM1 * BM2; Print(X);
      X = RM1 * RM2 - GBM1; Print(X);

      X = BM1.t(); BandMatrix BM1X = BM1.t();
      GRM1 = RM1; X -= GRM1.t(); Print(X); X = BM1X - BM1.t(); Print(X);

      // check that linear equation solver works with Identity Matrix
      IdentityMatrix IM(6); IM *= 2;
      GBM1 = BM1; GBM1 *= 4; GRM1 = RM1; GRM1 *= 4;
      DiagonalMatrix D = IM;
      LinearEquationSolver LES1(D);
      BandMatrix BX;
      BX = LES1.i() * GBM1; BX -= BM1 * 2; X = BX; Print(X);
      LinearEquationSolver LES2(IM);
      BX = LES2.i() * GBM1; BX -= BM1 * 2; X = BX; Print(X);
      BX = D.i() * GBM1; BX -= BM1 * 2; X = BX; Print(X);
      BX = IM.i() * GBM1; BX -= BM1 * 2; X = BX; Print(X);
      BX = IM.i(); BX *= GBM1; BX -= BM1 * 2; X = BX; Print(X);

      // try symmetric band matrices
      SymmetricBandMatrix SBM; SBM << SP(BM1, BM1.t());
      SBM << IM.i() * SBM;
      X = 2 * SBM - SP(RM1, RM1.t()); Print(X);

      // Do this again with more general D
      D << 2.5 << 7.5 << 2 << 5 << 4.5 << 7.5;
      BX = D.i() * BM1; X = BX - D.i() * RM1;
      Clean(X,0.00000001); Print(X);
      BX = D.i(); BX *= BM1; X = BX - D.i() * RM1;
      Clean(X,0.00000001); Print(X);
      SBM << SP(BM1, BM1.t());
      BX = D.i() * SBM; X = BX - D.i() * SP(RM1, RM1.t());
      Clean(X,0.00000001); Print(X);

      // test return
      BX = TestReturn(BM1); X = BX - BM1;
      if (BX.BandWidth() != BM1.BandWidth()) X = 5;
      Print(X);
   }

//   cout << "\nEnd of eighth test\n";
}
예제 #6
0
파일: tmtm.cpp 프로젝트: 151706061/Slicer3
void trymatm()
{
   Tracer et("Twenty second test of Matrix package");
   Tracer::PrintTrace();

   {
      Tracer et1("Stage 1");


      Matrix A(2,3);
      A << 3 << 5 << 2
        << 4 << 1 << 6;

      Matrix B(4,3);
      B <<  7 <<  2 <<  9
        <<  1 <<  3 <<  6
        <<  4 << 10 <<  5
        << 11 <<  8 << 12;

      Matrix C(8, 9);

      C.Row(1) << 21 <<  6 << 27  << 35 << 10 << 45  << 14 <<  4 << 18;
      C.Row(2) <<  3 <<  9 << 18  <<  5 << 15 << 30  <<  2 <<  6 << 12;
      C.Row(3) << 12 << 30 << 15  << 20 << 50 << 25  <<  8 << 20 << 10;
      C.Row(4) << 33 << 24 << 36  << 55 << 40 << 60  << 22 << 16 << 24;

      C.Row(5) << 28 <<  8 << 36  <<  7 <<  2 <<  9  << 42 << 12 << 54;
      C.Row(6) <<  4 << 12 << 24  <<  1 <<  3 <<  6  <<  6 << 18 << 36;
      C.Row(7) << 16 << 40 << 20  <<  4 << 10 <<  5  << 24 << 60 << 30;
      C.Row(8) << 44 << 32 << 48  << 11 <<  8 << 12  << 66 << 48 << 72;

      Matrix AB = KP(A,B) - C; Print(AB);

      IdentityMatrix I1(10); IdentityMatrix I2(15); I2 *= 2;
      DiagonalMatrix D = KP(I1, I2) - IdentityMatrix(150) * 2;
      Print(D);
   }

   {
      Tracer et1("Stage 2");

      UpperTriangularMatrix A(3);
      A << 3 << 8 << 5
             << 7 << 2
                  << 4;
      UpperTriangularMatrix B(4);
      B << 4 << 1 << 7 << 2
             << 3 << 9 << 8
                  << 1 << 5
                       << 6;

      UpperTriangularMatrix C(12);

      C.Row(1) <<12<< 3<<21<< 6 <<32<< 8<<56<<16 <<20<< 5<<35<<10;
      C.Row(2)     << 9<<27<<24 << 0<<24<<72<<64 << 0<<15<<45<<40;
      C.Row(3)         << 3<<15 << 0<< 0<< 8<<40 << 0<< 0<< 5<<25;
      C.Row(4)             <<18 << 0<< 0<< 0<<48 << 0<< 0<< 0<<30;

      C.Row(5)                  <<28<< 7<<49<<14 << 8<< 2<<14<< 4;
      C.Row(6)                      <<21<<63<<56 << 0<< 6<<18<<16;
      C.Row(7)                          << 7<<35 << 0<< 0<< 2<<10;
      C.Row(8)                              <<42 << 0<< 0<< 0<<12;

      C.Row(9)                                   <<16<< 4<<28<< 8;
      C.Row(10)                                      <<12<<36<<32;
      C.Row(11)                                          << 4<<20;
      C.Row(12)                                              <<24;


      UpperTriangularMatrix AB = KP(A,B) - C; Print(AB);

      LowerTriangularMatrix BT = B.t(); Matrix N(12,12);

      N.Row(1) <<12 << 0<< 0<< 0 <<32<< 0<< 0<< 0 <<20<< 0<< 0<< 0;
      N.Row(2) << 3 << 9<< 0<< 0 << 8<<24<< 0<< 0 << 5<<15<< 0<< 0;
      N.Row(3) <<21 <<27<< 3<< 0 <<56<<72<< 8<< 0 <<35<<45<< 5<< 0;
      N.Row(4) << 6 <<24<<15<<18 <<16<<64<<40<<48 <<10<<40<<25<<30;

      N.Row(5) << 0 << 0<< 0<< 0 <<28<< 0<< 0<< 0 << 8<< 0<< 0<< 0;
      N.Row(6) << 0 << 0<< 0<< 0 << 7<<21<< 0<< 0 << 2<< 6<< 0<< 0;
      N.Row(7) << 0 << 0<< 0<< 0 <<49<<63<< 7<< 0 <<14<<18<< 2<< 0;
      N.Row(8) << 0 << 0<< 0<< 0 <<14<<56<<35<<42 << 4<<16<<10<<12;

      N.Row(9) << 0 << 0<< 0<< 0 << 0<< 0<< 0<< 0 <<16<< 0<< 0<< 0;
      N.Row(10)<< 0 << 0<< 0<< 0 << 0<< 0<< 0<< 0 << 4<<12<< 0<< 0;
      N.Row(11)<< 0 << 0<< 0<< 0 << 0<< 0<< 0<< 0 <<28<<36<< 4<< 0;
      N.Row(12)<< 0 << 0<< 0<< 0 << 0<< 0<< 0<< 0 << 8<<32<<20<<24;

      Matrix N1 = KP(A, BT); N1 -= N; Print(N1);
      AB << KP(A, BT); AB << (AB - N); Print(AB);
      BT << KP(A, BT); BT << (BT - N); Print(BT);

      LowerTriangularMatrix AT = A.t();
      N1 = KP(AT, B); N1 -= N.t(); Print(N1);
      AB << KP(AT, B); AB << (AB - N.t()); Print(AB);
      BT << KP(AT, B); BT << (BT - N.t()); Print(BT);
   }

   {
      Tracer et1("Stage 3");

      BandMatrix BMA(6,2,3);
      BMA.Row(1) << 5.25 << 4.75 << 2.25 << 1.75;
      BMA.Row(2) << 1.25 << 9.75 << 4.50 << 0.25 << 1.50;
      BMA.Row(3) << 7.75 << 1.50 << 3.00 << 4.25 << 0.50 << 5.50;
      BMA.Row(4) << 2.75 << 9.00 << 8.00 << 3.25 << 3.50;
      BMA.Row(5) << 8.75 << 6.25 << 5.00 << 5.75;
      BMA.Row(6) << 3.75 << 6.75 << 6.00;

      Matrix A = BMA;

      BandMatrix BMB(4,2,1);
      BMB.Row(1) << 4.5 << 9.5;
      BMB.Row(2) << 1.5 << 6.0 << 2.0;
      BMB.Row(3) << 0.5 << 2.5 << 8.5 << 7.5;
      BMB.Row(4) << 3.0 << 4.0 << 6.5;

      SquareMatrix B = BMB;

      BandMatrix BMC = KP(BMA, BMB);
      BandMatrix BMC1 = KP(BMA, B);
      Matrix C2 = KP(A, BMB);
      Matrix C = KP(A, B);

      Matrix M = C - BMC; Print(M);
      M = C - BMC1; Print(M);
      M = C - C2; Print(M);

      RowVector X(4);
      X(1) = BMC.BandWidth().Lower() - 10;
      X(2) = BMC.BandWidth().Upper() - 13;
      X(3) = BMC1.BandWidth().Lower() - 11;
      X(4) = BMC1.BandWidth().Upper() - 15;
      Print(X);

      UpperTriangularMatrix UT;  UT << KP(BMA, BMB);
      UpperTriangularMatrix UT1; UT1 << (C - UT); Print(UT1);
      LowerTriangularMatrix LT;  LT << KP(BMA, BMB);
      LowerTriangularMatrix LT1; LT1 << (C - LT); Print(LT1);
   }

   {
      Tracer et1("Stage 4");

      SymmetricMatrix SM1(4);
      SM1.Row(1) << 2;
      SM1.Row(2) << 4 << 5;
      SM1.Row(3) << 9 << 2 << 1;
      SM1.Row(4) << 3 << 6 << 8 << 2;

      SymmetricMatrix SM2(3);
      SM2.Row(1) <<  3;
      SM2.Row(2) << -7 << -6;
      SM2.Row(3) <<  4 << -2 << -1;

      SymmetricMatrix SM = KP(SM1, SM2);
      Matrix M1 = SM1; Matrix M2 = SM2;
      Matrix M = KP(SM1, SM2); M -= SM; Print(M);
      M = KP(SM1, SM2) - SM; Print(M);
      M = KP(M1, SM2) - SM; Print(M);
      M = KP(SM1, M2) - SM; Print(M);
      M = KP(M1, M2); M -= SM; Print(M);
   }

   {
      Tracer et1("Stage 5");

      Matrix A(2,3);
      A << 3 << 5 << 2
        << 4 << 1 << 6;

      Matrix B(3,4);
      B <<  7 <<  2 <<  9 << 11
        <<  1 <<  3 <<  6 <<  8
        <<  4 << 10 <<  5 << 12;

      RowVector C(2); C << 3 << 7;
      ColumnVector D(4); D << 0 << 5 << 13 << 11;

      Matrix M = KP(C * A, B * D) - KP(C, B) * KP(A, D); Print(M);
   }

   {
      Tracer et1("Stage 6");

      RowVector A(3), B(5), C(15);
      A << 5 << 2 << 4;
      B << 3 << 2 << 0 << 1 << 6;
      C << 15 << 10 << 0 << 5 << 30
        <<  6 <<  4 << 0 << 2 << 12
        << 12 <<  8 << 0 << 4 << 24;
      Matrix N = KP(A, B) - C;    Print(N);
      N = KP(A.t(), B.t()) - C.t();    Print(N);
      N = KP(A.AsDiagonal(), B.AsDiagonal()) - C.AsDiagonal();    Print(N);
   }

   {
      Tracer et1("Stage 7");
      IdentityMatrix I(3);
      ColumnVector CV(4); CV << 4 << 3 << 1 << 7;
      Matrix A = KP(I, CV) + 5;
      Matrix B(3,12);
      B.Row(1) << 9 << 8 << 6 << 12 << 5 << 5 << 5 << 5 << 5 << 5 << 5 << 5;
      B.Row(2) << 5 << 5 << 5 << 5 << 9 << 8 << 6 << 12 << 5 << 5 << 5 << 5;
      B.Row(3) << 5 << 5 << 5 << 5 << 5 << 5 << 5 << 5 << 9 << 8 << 6 << 12;
      B -= A.t(); Print(B);

   }

   {
      Tracer et1("Stage 8");          // SquareMatrix
      Matrix A(2,3), B(3,2);
      A << 2 << 6 << 7
        << 4 << 3 << 9;
      B << 1 << 3
        << 4 << 8
        << 0 << 6;
      SquareMatrix AB = A * B;
      Matrix M = (B.t() * A.t()).t(); M -= AB; Print(M);
      AB = B * A;
      M = (A.t() * B.t()).t(); M -= AB; Print(M);
      AB.ReSize(5,5); AB = 0;
      AB.SubMatrix(1,2,1,3) = A; AB.SubMatrix(4,5,3,5) = A;
      AB.SubMatrix(1,3,4,5) = B; AB.SubMatrix(3,5,1,2) = B;
      SquareMatrix C(5);
      C.Row(1) << 2 << 6 << 7 << 1 << 3;
      C.Row(2) << 4 << 3 << 9 << 4 << 8;
      C.Row(3) << 1 << 3 << 0 << 0 << 6;
      C.Row(4) << 4 << 8 << 2 << 6 << 7;
      C.Row(5) << 0 << 6 << 4 << 3 << 9;
      C -= AB; Print(C);
      AB = A.SymSubMatrix(1,2);
      AB = (AB | AB) & (AB | AB);
      C.ReSize(4);
      C.Row(1) << 2 << 6 << 2 << 6;
      C.Row(2) << 4 << 3 << 4 << 3;
      C.Row(3) << 2 << 6 << 2 << 6;
      C.Row(4) << 4 << 3 << 4 << 3;
      M = AB;
      C -= M; Print(C);
      C << M; C += -M; Print(C);
      
   }


}
예제 #7
0
 void UpperBandMatrix<TYPE>::Swap(BandMatrix<TYPE>& gm)
 {
     assert(gm.BandWidth().Lower() == 0);
     BandMatrix<TYPE>::Swap(gm);
 }
예제 #8
0
void trymat7()
{
//   cout << "\nSeventh test of Matrix package\n";
    Tracer et("Seventh test of Matrix package");
    Tracer::PrintTrace();

    int i,j;


    DiagonalMatrix D(6);
    UpperTriangularMatrix U(6);
    for (i=1; i<=6; i++) {
        for (j=i; j<=6; j++) U(i,j)=i*i*j-50;
        D(i,i)=i*i+i-10;
    }
    LowerTriangularMatrix L=(U*3.0).t();
    SymmetricMatrix S(6);
    for (i=1; i<=6; i++) for (j=i; j<=6; j++) S(i,j)=i*i+2.0+j;
    Matrix MD=D;
    Matrix ML=L;
    Matrix MU=U;
    Matrix MS=S;
    Matrix M(6,6);
    for (i=1; i<=6; i++) for (j=1; j<=6; j++) M(i,j)=i*j+i*i-10.0;
    {
        Tracer et1("Stage 1");
        Print(Matrix((S-M)-(MS-M)));
        Print(Matrix((-M-S)+(MS+M)));
        Print(Matrix((U-M)-(MU-M)));
    }
    {
        Tracer et1("Stage 2");
        Print(Matrix((L-M)+(M-ML)));
        Print(Matrix((D-M)+(M-MD)));
        Print(Matrix((D-S)+(MS-MD)));
        Print(Matrix((D-L)+(ML-MD)));
    }

    {
        M=MU.t();
    }
    LowerTriangularMatrix LY=D.i()*U.t();
    {
        Tracer et1("Stage 3");
        MS=D*LY-M;
        Clean(MS,0.00000001);
        Print(MS);
        L=U.t();
        LY=D.i()*L;
        MS=D*LY-M;
        Clean(MS,0.00000001);
        Print(MS);
    }
    {
        Tracer et1("Stage 4");
        UpperTriangularMatrix UT(11);
        int i, j;
        for (i=1; i<=11; i++) for (j=i; j<=11; j++) UT(i,j)=i*i+j*3;
        GenericMatrix GM;
        Matrix X;
        UpperBandMatrix UB(11,3);
        UB.Inject(UT);
        UT = UB;
        UpperBandMatrix UB2 = UB / 8;
        GM = UB2-UT/8;
        X = GM;
        Print(X);
        SymmetricBandMatrix SB(11,4);
        SB << (UB + UB.t());
        X = SB - UT - UT.t();
        Print(X);
        BandMatrix B = UB + UB.t()*2;
        DiagonalMatrix D;
        D << B;
        X.ReSize(1,1);
        X(1,1) = Trace(B)-Sum(D);
        Print(X);
        X = SB + 5;
        Matrix X1=X;
        X = SP(UB,X);
        Matrix X2 =UB;
        X1 = (X1.AsDiagonal() * X2.AsDiagonal()).AsRow()-X.AsColumn().t();
        Print(X1);
        X1=SB.t();
        X2 = B.t();
        X = SB.i() * B - X1.i() * X2.t();
        Clean(X,0.00000001);
        Print(X);
        X = SB.i();
        X = X * B - X1.i() * X2.t();
        Clean(X,0.00000001);
        Print(X);
        D = 1;
        X = SB.i() * SB - D;
        Clean(X,0.00000001);
        Print(X);
        ColumnVector CV(11);
        CV << 2 << 6 <<3 << 8 << -4 << 17.5 << 2 << 1 << -2 << 5 << 3.75;
        D << 2 << 6 <<3 << 8 << -4 << 17.5 << 2 << 1 << -2 << 5 << 3.75;
        X = CV.AsDiagonal();
        X = X-D;
        Print(X);
        SymmetricBandMatrix SB1(11,7);
        SB1 = 5;
        SymmetricBandMatrix SB2 = SB1 + D;
        X.ReSize(11,11);
        X=0;
        for (i=1; i<=11; i++) for (j=1; j<=11; j++)
            {
                if (abs(i-j)<=7) X(i,j)=5;
                if (i==j) X(i,j)+=CV(i);
            }
        SymmetricMatrix SM;
        SM.ReSize(11);
        SM=SB;
        SB = SB+SB2;
        X1 = SM+X-SB;
        Print(X1);
        SB2=0;
        X2=SB2;
        X1=SB;
        Print(X2);
        for (i=1; i<=11; i++) SB2.Column(i)<<SB.Column(i);
        X1=X1-SB2;
        Print(X1);
        X = SB;
        SB2.ReSize(11,4);
        SB2 = SB*5;
        SB2 = SB + SB2;
        X1 = X*6 - SB2;
        Print(X1);
        X1 = SP(SB,SB2/3);
        X1=X1-SP(X,X*2);
        Print(X1);
        X1 = SP(SB2/6,X*2);
        X1=X1-SP(X*2,X);
        Print(X1);
    }

    {
        // test the simple integer array class
        Tracer et("Stage 5");
        ColumnVector Test(10);
        Test = 0.0;
        int i;
        SimpleIntArray A(100);
        for (i = 0; i < 100; i++) A[i] = i*i+1;
        SimpleIntArray B(100), C(50), D;
        B = A;
        A.ReSize(50, true);
        C = A;
        A.ReSize(150, true);
        D = A;
        for (i = 0; i < 100; i++) if (B[i] != i*i+1) Test(1)=1;
        for (i = 0; i < 50; i++) if (C[i] != i*i+1) Test(2)=1;
        for (i = 0; i < 50; i++) if (D[i] != i*i+1) Test(3)=1;
        for (i = 50; i < 150; i++) if (D[i] != 0) Test(3)=1;
        A.resize(75);
        A = A.size();
        for (i = 0; i < 75; i++) if (A[i] != 75) Test(4)=1;
        A.resize(25);
        A = A.size();
        for (i = 0; i < 25; i++) if (A[i] != 25) Test(5)=1;
        A.ReSize(25);
        A = 23;
        for (i = 0; i < 25; i++) if (A[i] != 23) Test(6)=1;
        A.ReSize(0);
        A.ReSize(15);
        A = A.Size();
        for (i = 0; i < 15; i++) if (A[i] != 15) Test(7)=1;
        const SimpleIntArray E = B;
        for (i = 0; i < 100; i++) if (E[i] != i*i+1) Test(8)=1;
        SimpleIntArray F;
        F.resize_keep(5);
        for (i = 0; i < 5; i++) if (F[i] != 0) Test(9)=1;
        Print(Test);
    }

    {
        // testing RealStarStar
        Tracer et("Stage 6");
        MultWithCarry MWC;

        Matrix A(10, 12), B(12, 15), C(10, 15);
        FillWithValues(MWC, A);
        FillWithValues(MWC, B);
        ConstRealStarStar a(A);
        ConstRealStarStar b(B);
        RealStarStar c(C);
        c_matrix_multiply(10,12,15,a,b,c);
        Matrix X = C - A * B;
        Clean(X,0.00000001);
        Print(X);
        A.ReSize(11, 10);
        B.ReSize(10,8);
        C.ReSize(11,8);
        FillWithValues(MWC, A);
        FillWithValues(MWC, B);
        C = -1;
        c_matrix_multiply(11,10,8,
                          ConstRealStarStar(A),ConstRealStarStar(B),RealStarStar(C));
        X = C - A * B;
        Clean(X,0.00000001);
        Print(X);
    }

    {
        // testing resize_keep
        Tracer et("Stage 7");
        Matrix X, Y;
        MultWithCarry MWC;

        X.resize(20,35);
        FillWithValues(MWC, X);
        Matrix M(20,35);
        M = X;
        X = M.submatrix(1,15,1,25);
        M.resize_keep(15,25);
        Y  = X - M;
        Print(Y);
        M.resize_keep(15,25);
        Y  = X - M;
        Print(Y);
        Y.resize(29,27);
        Y = 0;
        Y.submatrix(1,15,1,25) = X;
        M.resize_keep(29,27);
        Y -= M;
        Print(Y);
        M.resize_keep(0,5);
        M.resize_keep(10,10);
        Print(M);
        M.resize_keep(15,0);
        M.resize_keep(10,10);
        Print(M);

        X.resize(20,35);
        FillWithValues(MWC, X);
        M = X;
        M.resize_keep(38,17);
        Y.resize(38,17);
        Y = 0;
        Y.submatrix(1,20,1,17) = X.submatrix(1,20,1,17);
        Y -= M;
        Print(Y);

        X.resize(40,12);
        FillWithValues(MWC, X);
        M = X;
        M.resize_keep(38,17);
        Y.resize(38,17);
        Y = 0;
        Y.submatrix(1,38,1,12) = X.submatrix(1,38,1,12);
        Y -= M;
        Print(Y);

#ifndef DONT_DO_NRIC

        X.resize(20,35);
        FillWithValues(MWC, X);
        nricMatrix nM(20,35);
        nM = X;
        X = nM.submatrix(1,15,1,25);
        nM.resize_keep(15,25);
        Y  = X - nM;
        Print(Y);
        nM.resize_keep(15,25);
        Y  = X - nM;
        Print(Y);
        Y.resize(29,27);
        Y = 0;
        Y.submatrix(1,15,1,25) = X;
        nM.resize_keep(29,27);
        Y -= nM;
        Print(Y);
        nM.resize_keep(0,5);
        nM.resize_keep(10,10);
        Print(nM);
        nM.resize_keep(15,0);
        nM.resize_keep(10,10);
        Print(nM);

        X.resize(20,35);
        FillWithValues(MWC, X);
        nM = X;
        nM.resize_keep(38,17);
        Y.resize(38,17);
        Y = 0;
        Y.submatrix(1,20,1,17) = X.submatrix(1,20,1,17);
        Y -= nM;
        Print(Y);

        X.resize(40,12);
        FillWithValues(MWC, X);
        nM = X;
        nM.resize_keep(38,17);
        Y.resize(38,17);
        Y = 0;
        Y.submatrix(1,38,1,12) = X.submatrix(1,38,1,12);
        Y -= nM;
        Print(Y);

#endif

        X.resize(20,20);
        FillWithValues(MWC, X);
        SquareMatrix SQM(20);
        SQM << X;
        X = SQM.sym_submatrix(1,13);
        SQM.resize_keep(13);
        Y  = X - SQM;
        Print(Y);
        SQM.resize_keep(13);
        Y  = X - SQM;
        Print(Y);
        Y.resize(23,23);
        Y = 0;
        Y.sym_submatrix(1,13) = X;
        SQM.resize_keep(23,23);
        Y -= SQM;
        Print(Y);
        SQM.resize_keep(0);
        SQM.resize_keep(50);
        Print(SQM);

        X.resize(20,20);
        FillWithValues(MWC, X);
        SymmetricMatrix SM(20);
        SM << X;
        X = SM.sym_submatrix(1,13);
        SM.resize_keep(13);
        Y  = X - SM;
        Print(Y);
        SM.resize_keep(13);
        Y  = X - SM;
        Print(Y);
        Y.resize(23,23);
        Y = 0;
        Y.sym_submatrix(1,13) = X;
        SM.resize_keep(23);
        Y -= SM;
        Print(Y);
        SM.resize_keep(0);
        SM.resize_keep(50);
        Print(SM);

        X.resize(20,20);
        FillWithValues(MWC, X);
        LowerTriangularMatrix LT(20);
        LT << X;
        X = LT.sym_submatrix(1,13);
        LT.resize_keep(13);
        Y  = X - LT;
        Print(Y);
        LT.resize_keep(13);
        Y  = X - LT;
        Print(Y);
        Y.resize(23,23);
        Y = 0;
        Y.sym_submatrix(1,13) = X;
        LT.resize_keep(23);
        Y -= LT;
        Print(Y);
        LT.resize_keep(0);
        LT.resize_keep(50);
        Print(LT);

        X.resize(20,20);
        FillWithValues(MWC, X);
        UpperTriangularMatrix UT(20);
        UT << X;
        X = UT.sym_submatrix(1,13);
        UT.resize_keep(13);
        Y  = X - UT;
        Print(Y);
        UT.resize_keep(13);
        Y  = X - UT;
        Print(Y);
        Y.resize(23,23);
        Y = 0;
        Y.sym_submatrix(1,13) = X;
        UT.resize_keep(23);
        Y -= UT;
        Print(Y);
        UT.resize_keep(0);
        UT.resize_keep(50);
        Print(UT);

        X.resize(20,20);
        FillWithValues(MWC, X);
        DiagonalMatrix DM(20);
        DM << X;
        X = DM.sym_submatrix(1,13);
        DM.resize_keep(13);
        Y  = X - DM;
        Print(Y);
        DM.resize_keep(13);
        Y  = X - DM;
        Print(Y);
        Y.resize(23,23);
        Y = 0;
        Y.sym_submatrix(1,13) = X;
        DM.resize_keep(23);
        Y -= DM;
        Print(Y);
        DM.resize_keep(0);
        DM.resize_keep(50);
        Print(DM);

        X.resize(1,20);
        FillWithValues(MWC, X);
        RowVector RV(20);
        RV << X;
        X = RV.columns(1,13);
        RV.resize_keep(13);
        Y  = X - RV;
        Print(Y);
        RV.resize_keep(13);
        Y  = X - RV;
        Print(Y);
        Y.resize(1,23);
        Y = 0;
        Y.columns(1,13) = X;
        RV.resize_keep(1,23);
        Y -= RV;
        Print(Y);
        RV.resize_keep(0);
        RV.resize_keep(50);
        Print(RV);

        X.resize(20,1);
        FillWithValues(MWC, X);
        ColumnVector CV(20);
        CV << X;
        X = CV.rows(1,13);
        CV.resize_keep(13);
        Y  = X - CV;
        Print(Y);
        CV.resize_keep(13);
        Y  = X - CV;
        Print(Y);
        Y.resize(23,1);
        Y = 0;
        Y.rows(1,13) = X;
        CV.resize_keep(23,1);
        Y -= CV;
        Print(Y);
        CV.resize_keep(0);
        CV.resize_keep(50);
        Print(CV);


    }


//   cout << "\nEnd of seventh test\n";
}
예제 #9
0
파일: tmtd.cpp 프로젝트: 99731/GoTools
void trymatd()
{
   Tracer et("Thirteenth test of Matrix package");
   Tracer::PrintTrace();
   Matrix X(5,20);
   int i,j;
   for (j=1;j<=20;j++) X(1,j) = j+1;
   for (i=2;i<=5;i++) for (j=1;j<=20; j++) X(i,j) = (long)X(i-1,j) * j % 1001;
   SymmetricMatrix S; S << X * X.t();
   Matrix SM = X * X.t() - S;
   Print(SM);
   LowerTriangularMatrix L = Cholesky(S);
   Matrix Diff = L*L.t()-S; Clean(Diff, 0.000000001);
   Print(Diff);
   {
      Tracer et1("Stage 1");
      LowerTriangularMatrix L1(5);
      Matrix Xt = X.t(); Matrix Xt2 = Xt;
      QRZT(X,L1);
      Diff = L - L1; Clean(Diff,0.000000001); Print(Diff);
      UpperTriangularMatrix Ut(5);
      QRZ(Xt,Ut);
      Diff = L - Ut.t(); Clean(Diff,0.000000001); Print(Diff);
      Matrix Y(3,20);
      for (j=1;j<=20;j++) Y(1,j) = 22-j;
      for (i=2;i<=3;i++) for (j=1;j<=20; j++)
         Y(i,j) = (long)Y(i-1,j) * j % 101;
      Matrix Yt = Y.t(); Matrix M,Mt; Matrix Y2=Y;
      QRZT(X,Y,M); QRZ(Xt,Yt,Mt);
      Diff = Xt - X.t(); Clean(Diff,0.000000001); Print(Diff);
      Diff = Yt - Y.t(); Clean(Diff,0.000000001); Print(Diff);
      Diff = Mt - M.t(); Clean(Diff,0.000000001); Print(Diff);
      Diff = Y2 * Xt2 * S.i() - M * L.i();
      Clean(Diff,0.000000001); Print(Diff);
   }

   ColumnVector C1(5);
   {
      Tracer et1("Stage 2");
      X.ReSize(5,5);
      for (j=1;j<=5;j++) X(1,j) = j+1;
      for (i=2;i<=5;i++) for (j=1;j<=5; j++)
         X(i,j) = (long)X(i-1,j) * j % 1001;
      for (i=1;i<=5;i++) C1(i) = i*i;
      CroutMatrix A = X;
      ColumnVector C2 = A.i() * C1; C1 = X.i()  * C1;
      X = C1 - C2; Clean(X,0.000000001); Print(X);
   }

   {
      Tracer et1("Stage 3");
      X.ReSize(7,7);
      for (j=1;j<=7;j++) X(1,j) = j+1;
      for (i=2;i<=7;i++) for (j=1;j<=7; j++)
         X(i,j) = (long)X(i-1,j) * j % 1001;
      C1.ReSize(7);
      for (i=1;i<=7;i++) C1(i) = i*i;
      RowVector R1 = C1.t();
      Diff = R1 * X.i() - ( X.t().i() * R1.t() ).t(); Clean(Diff,0.000000001);
      Print(Diff);
   }

   {
      Tracer et1("Stage 4");
      X.ReSize(5,5);
      for (j=1;j<=5;j++) X(1,j) = j+1;
      for (i=2;i<=5;i++) for (j=1;j<=5; j++)
         X(i,j) = (long)X(i-1,j) * j % 1001;
      C1.ReSize(5);
      for (i=1;i<=5;i++) C1(i) = i*i;
      CroutMatrix A1 = X*X;
      ColumnVector C2 = A1.i() * C1; C1 = X.i()  * C1; C1 = X.i()  * C1;
      X = C1 - C2; Clean(X,0.000000001); Print(X);
   }


   {
      Tracer et1("Stage 5");
      int n = 40;
      SymmetricBandMatrix B(n,2); B = 0.0;
      for (i=1; i<=n; i++)
      {
         B(i,i) = 6;
         if (i<=n-1) B(i,i+1) = -4;
         if (i<=n-2) B(i,i+2) = 1;
      }
      B(1,1) = 5; B(n,n) = 5;
      SymmetricMatrix A = B;
      ColumnVector X(n);
      X(1) = 429;
      for (i=2;i<=n;i++) X(i) = (long)X(i-1) * 31 % 1001;
      X = X / 100000L;
      // the matrix B is rather ill-conditioned so the difficulty is getting
      // good agreement (we have chosen X very small) may not be surprising;
      // maximum element size in B.i() is around 1400
      ColumnVector Y1 = A.i() * X;
      LowerTriangularMatrix C1 = Cholesky(A);
      ColumnVector Y2 = C1.t().i() * (C1.i() * X) - Y1;
      Clean(Y2, 0.000000001); Print(Y2);
      UpperTriangularMatrix CU = C1.t().i();
      LowerTriangularMatrix CL = C1.i();
      Y2 = CU * (CL * X) - Y1;
      Clean(Y2, 0.000000001); Print(Y2);
      Y2 = B.i() * X - Y1; Clean(Y2, 0.000000001); Print(Y2);

      LowerBandMatrix C2 = Cholesky(B);
      Matrix M = C2 - C1; Clean(M, 0.000000001); Print(M);
      ColumnVector Y3 = C2.t().i() * (C2.i() * X) - Y1;
      Clean(Y3, 0.000000001); Print(Y3);
      CU = C1.t().i();
      CL = C1.i();
      Y3 = CU * (CL * X) - Y1;
      Clean(Y3, 0.000000001); Print(Y3);

      Y3 = B.i() * X - Y1; Clean(Y3, 0.000000001); Print(Y3);

      SymmetricMatrix AI = A.i();
      Y2 = AI*X - Y1; Clean(Y2, 0.000000001); Print(Y2);
      SymmetricMatrix BI = B.i();
      BandMatrix C = B; Matrix CI = C.i();
      M = A.i() - CI; Clean(M, 0.000000001); Print(M);
      M = B.i() - CI; Clean(M, 0.000000001); Print(M);
      M = AI-BI; Clean(M, 0.000000001); Print(M);
      M = AI-CI; Clean(M, 0.000000001); Print(M);

      M = A; AI << M; M = AI-A; Clean(M, 0.000000001); Print(M);
      C = B; BI << C; M = BI-B; Clean(M, 0.000000001); Print(M);


   }

   {
      Tracer et1("Stage 5");
      SymmetricMatrix A(4), B(4);
      A << 5
        << 1 << 4
        << 2 << 1 << 6
        << 1 << 0 << 1 << 7;
      B << 8
        << 1 << 5
        << 1 << 0 << 9
        << 2 << 1 << 0 << 6;
      LowerTriangularMatrix AB = Cholesky(A) * Cholesky(B);
      Matrix M = Cholesky(A) * B * Cholesky(A).t() - AB*AB.t();
      Clean(M, 0.000000001); Print(M);
      M = A * Cholesky(B); M = M * M.t() - A * B * A;
      Clean(M, 0.000000001); Print(M);
   }
   {
      Tracer et1("Stage 6");
      int N=49;
      int i;
      SymmetricBandMatrix S(N,1);
      Matrix B(N,N+1); B=0;
      for (i=1;i<=N;i++) { S(i,i)=1; B(i,i)=1; B(i,i+1)=-1; }
      for (i=1;i<N; i++) S(i,i+1)=-.5;
      DiagonalMatrix D(N+1); D = 1;
      B = B.t()*S.i()*B - (D-1.0/(N+1))*2.0;
      Clean(B, 0.000000001); Print(B);
   }
   {
      Tracer et1("Stage 7");
      // See if you can pass a CroutMatrix to a function
      Matrix A(4,4);
      A.Row(1) <<  3 <<  2 << -1 <<  4;
      A.Row(2) << -8 <<  7 <<  2 <<  0;
      A.Row(3) <<  2 << -2 <<  3 <<  1;
      A.Row(4) << -1 <<  5 <<  2 <<  2;
      CroutMatrix B = A;
      Matrix C = A * Inverter(B) - IdentityMatrix(4);
      Clean(C, 0.000000001); Print(C);
   }


//   cout << "\nEnd of Thirteenth test\n";
}
예제 #10
0
    static void DoNonLapCH_Decompose(SymBandMatrixView<T> A)
    {
        // Cholesky decompostion for a banded Hermitian matrix follows the 
        // same structure as a regular Cholesky decomposition, but we 
        // take advantage of the fact that the column or row lengths are 
        // shorter.
        //
        TMVAssert(A.uplo() == Lower);
        TMVAssert(A.ct() == NonConj);
        TMVAssert(isReal(T()) || A.isherm());
        TMVAssert(A.iscm() || A.isrm());
        TMVAssert(cm == A.iscm());
        const ptrdiff_t N = A.size();
#ifdef XDEBUG
        Matrix<T> A0(A);
        //cout<<"CHDecompose:\n";
        //cout<<"A = "<<TMV_Text(A)<<"  "<<A<<endl;
#endif

        VectorView<TMV_RealType(T)> Adiag = A.diag().realPart();
        const ptrdiff_t nlo = A.nlo();

        if (nlo == 0) {
            TMV_RealType(T)* Ajj= Adiag.ptr();
            const ptrdiff_t ds = Adiag.step();
            for(ptrdiff_t j=0;j<N;++j,Ajj+=ds) {
#ifdef TMVFLDEBUG
                TMVAssert(Ajj >= A.realPart()._first);
                TMVAssert(Ajj < A.realPart()._last);
#endif
                if (*Ajj <= TMV_RealType(T)(0))  {
#ifdef NOTHROW
                    std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                    exit(1); 
#else
                    throw NonPosDefHermBandMatrix<T>(A);
#endif
                }
                *Ajj = TMV_SQRT(*Ajj);
            }
        } else if (cm) {
            TMV_RealType(T)* Ajj= Adiag.ptr();
            const ptrdiff_t ds = Adiag.step();
            ptrdiff_t endcol = nlo+1;
            for(ptrdiff_t j=0;j<N-1;++j,Ajj+=ds) {
#ifdef TMVFLDEBUG
                TMVAssert(Ajj >= A.realPart()._first);
                TMVAssert(Ajj < A.realPart()._last);
#endif
                if (*Ajj <= TMV_RealType(T)(0))  {
#ifdef NOTHROW
                    std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                    exit(1); 
#else
                    throw NonPosDefHermBandMatrix<T>(A);
#endif
                }
                *Ajj = TMV_SQRT(*Ajj);
                A.col(j,j+1,endcol) /= *Ajj;
                A.subSymMatrix(j+1,endcol) -= 
                    A.col(j,j+1,endcol) ^ A.col(j,j+1,endcol).conjugate();
                if (endcol < N) ++endcol;
            }
#ifdef TMVFLDEBUG
            TMVAssert(Ajj >= A.realPart()._first);
            TMVAssert(Ajj < A.realPart()._last);
#endif
            if (*Ajj <= TMV_RealType(T)(0))  {
#ifdef NOTHROW
                std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                exit(1); 
#else
                throw NonPosDefHermBandMatrix<T>(A);
#endif
            }
            *Ajj = TMV_SQRT(*Ajj);
        } else {
            TMV_RealType(T)* Aii = Adiag.ptr();
            const ptrdiff_t ds = Adiag.step();
            ptrdiff_t startrow = 0;
#ifdef TMVFLDEBUG
            TMVAssert(Aii >= A.realPart()._first);
            TMVAssert(Aii < A.realPart()._last);
#endif
            if (*Aii <= TMV_RealType(T)(0)) {
#ifdef NOTHROW
                std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                exit(1); 
#else
                throw NonPosDefHermBandMatrix<T>(A);
#endif
            }
            *Aii = TMV_SQRT(*Aii);
            for(ptrdiff_t i=1;i<N;++i) {
                if (i > nlo) ++startrow;
                Aii+=ds;
                A.row(i,startrow,i) %= 
                    A.subSymBandMatrix(startrow,i).lowerBand().adjoint();
#ifdef TMVFLDEBUG
                TMVAssert(Aii >= A.realPart()._first);
                TMVAssert(Aii < A.realPart()._last);
#endif
                *Aii -= NormSq(A.row(i,startrow,i));
                if (*Aii <= TMV_RealType(T)(0))  {
#ifdef NOTHROW
                    std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                    exit(1); 
#else
                    throw NonPosDefHermBandMatrix<T>(A);
#endif
                }
                *Aii = TMV_SQRT(*Aii);
            }
        }

#ifdef XDEBUG
        //cout<<"Done CHDecompose\n";
        BandMatrix<T> L = A.lowerBand();
        BandMatrix<T> A2 = L * L.adjoint();
        TMV_RealType(T) normll = TMV_SQR(Norm(L));
        if (!(Norm(A2-A0) < 0.001*normll)) {
            cerr<<"CH_Decompose: A = "<<TMV_Text(A)<<"  "<<A0<<endl;
            cerr<<"Done: A = "<<A<<endl;
            cerr<<"L = "<<L<<endl;
            cerr<<"Lt = "<<L.adjoint()<<endl;
            cerr<<"L*Lt = "<<A2<<endl;
            cerr<<"Norm(diff) = "<<Norm(A2-A0);
            cerr<<"  Norm(L)^2 = "<<normll<<endl;
            abort();
        }
#endif
    }
예제 #11
0
    static void SymLDL_Decompose(SymBandMatrixView<T> A)
    {
        TMVAssert(A.uplo() == Lower);
        TMVAssert(A.ct() == NonConj);
        TMVAssert(A.issym());
        TMVAssert(A.nlo() == 1);
        const ptrdiff_t N = A.size();
#ifdef XDEBUG
        Matrix<T> A0(A);
        //cout<<"SymLDLDecompose:\n";
        //cout<<"A = "<<TMV_Text(A)<<"  "<<A<<endl;
#endif

        T* Dj = A.ptr();
        T* Lj = A.diag(-1).ptr();

        if (A.isdm()) {
            T Ax0 = *Lj;
            if (*Dj == T(0))  {
#ifdef NOTHROW
                std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                exit(1); 
#else
                throw NonPosDefSymBandLDL<T>(A);
#endif
            }
            *Lj /= *Dj;
            ++Dj;
            *Dj -= *Lj * Ax0;
            ++Lj;
        } else {
            ptrdiff_t step = A.diagstep();
            for(ptrdiff_t j=0;j<N-1;++j) {
                T Ax0 = *Lj;
                if (*Dj == T(0))  {
#ifdef NOTHROW
                    std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                    exit(1); 
#else
                    throw NonPosDefSymBandLDL<T>(A);
#endif
                }
                *Lj /= *Dj;
                Dj += step;
                *Dj -= *Lj * Ax0;
                Lj += step;
            }
        }
        if (*Dj == T(0))  {
#ifdef NOTHROW
            std::cerr<<"Non Posdef HermBandMatrix found\n"; 
            exit(1); 
#else
            throw NonPosDefSymBandLDL<T>(A);
#endif
        }

#ifdef XDEBUG
        //cout<<"Done SymLDLDecom\n";
        BandMatrix<T> L = A.lowerBand();
        L.diag().SetAllTo(T(1));
        DiagMatrix<T> D = DiagMatrixViewOf(A.diag());
        BandMatrix<T> A2 = L * D * L.transpose();
        TMV_RealType(T) normldl = TMV_SQR(Norm(L))*Norm(D);
        //cout<<"L = "<<L<<endl;
        //cout<<"D = "<<D<<endl;
        //cout<<"A2 = "<<A2<<endl;
        //cout<<"cf. A0 = "<<A0<<endl;
        if (!(Norm(A2-A0) < 0.001*normldl)) {
            cerr<<"SymLDLDecomp: A = "<<TMV_Text(A)<<"  "<<A0<<endl;
            cerr<<"Done: A = "<<A<<endl;
            cerr<<"L = "<<L<<endl;
            cerr<<"D = "<<D<<endl;
            cerr<<"LT = "<<L.transpose()<<endl;
            cerr<<"L*D*LT = "<<A2<<endl;
            cerr<<"Norm(diff) = "<<Norm(A2-A0);
            cerr<<"  Norm(L)^2*Norm(D) = "<<normldl<<endl;
            abort();
        }
#endif
    }
예제 #12
0
    static void NonLapLDL_Decompose(SymBandMatrixView<T> A)
    {
        // For tridiagonal Hermitian band matrices, the sqrt can 
        // become a significant fraction of the calculation.
        // This is unnecessary if we instead decompose A into L D Lt
        // where L is unit diagonal.
        //
        // In this case, the equations become:
        //
        // A = L0 D L0t
        //
        // ( A00 Ax0t ) = (  1  0 ) ( D0  0 ) ( 1 Lx0t )
        // ( Ax0 Axx  ) = ( Lx0 1 ) (  0 Dx ) ( 0   1  )
        //              = (  1  0 ) ( D0  D0 Lx0t )
        //              = ( Lx0 1 ) ( 0   Dx      )
        //
        // The equations from this are:
        //
        // A00 = D0
        // Ax0 = Lx0 D0
        // Axx = Lx0 D0 Lx0t + Axx'
        //
        // where Axx' = Dx is the sub-matrix for the next step.
        //
        TMVAssert(A.uplo() == Lower);
        TMVAssert(A.ct() == NonConj);
        TMVAssert(A.isherm());
        TMVAssert(A.nlo() == 1);
        const ptrdiff_t N = A.size();
#ifdef XDEBUG
        Matrix<T> A0(A);
        //cout<<"LDLDecompose:\n";
        //cout<<"A = "<<TMV_Text(A)<<"  "<<A<<endl;
#endif

        TMV_RealType(T)* Dj = A.realPart().ptr();
        T* Lj = A.diag(-1).ptr();

        if (A.isdm()) {
            for(ptrdiff_t j=0;j<N-1;++j) {
                T Ax0 = *Lj;
                if (*Dj == TMV_RealType(T)(0))  {
#ifdef NOTHROW
                    std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                    exit(1); 
#else
                    throw NonPosDefHermBandLDL<T>(A);
#endif
                }
                *Lj /= *Dj;
                if (isReal(T())) ++Dj; else Dj+=2;
                *Dj -= TMV_REAL(TMV_CONJ(*Lj) * Ax0);
                ++Lj;
            }
        } else {
            const ptrdiff_t Dstep = A.diag().realPart().step();
            const ptrdiff_t Lstep = A.diag().step();
            for(ptrdiff_t j=0;j<N-1;++j) {
                T Ax0 = *Lj;
                if (*Dj == TMV_RealType(T)(0))  {
#ifdef NOTHROW
                    std::cerr<<"Non Posdef HermBandMatrix found\n"; 
                    exit(1); 
#else
                    throw NonPosDefHermBandLDL<T>(A);
#endif
                }
                *Lj /= *Dj;
                Dj += Dstep;
                *Dj -= TMV_REAL(TMV_CONJ(*Lj) * Ax0);
                Lj += Lstep;
            }
        }
        if (*Dj == TMV_RealType(T)(0))  {
#ifdef NOTHROW
            std::cerr<<"Non Posdef HermBandMatrix found\n"; 
            exit(1); 
#else
            throw NonPosDefHermBandLDL<T>(A);
#endif
        }

#ifdef XDEBUG
        //cout<<"Done LDLDecompose\n";
        BandMatrix<T> L = A.lowerBand();
        L.diag().SetAllTo(T(1));
        DiagMatrix<T> D = DiagMatrixViewOf(A.diag());
        BandMatrix<T> A2 = L * D * L.adjoint();
        TMV_RealType(T) normldl = TMV_SQR(Norm(L))*Norm(D);
        //cout<<"Done: A = "<<A<<endl;
        //cout<<"L = "<<L<<endl;
        //cout<<"D = "<<D<<endl;
        //cout<<"A2 = "<<A2<<endl;
        //cout<<"cf A0 = "<<A0<<endl;
        if (!(Norm(A2-A0) < 0.001*normldl)) {
            cerr<<"LDL_Decompose: A = "<<TMV_Text(A)<<"  "<<A0<<endl;
            cerr<<"Done: A = "<<A<<endl;
            cerr<<"L = "<<L<<endl;
            cerr<<"D = "<<D<<endl;
            cerr<<"Lt = "<<L.adjoint()<<endl;
            cerr<<"L*D*Lt = "<<A2<<endl;
            cerr<<"Norm(diff) = "<<Norm(A2-A0);
            cerr<<"  Norm(L)^2*Norm(D) = "<<normldl<<endl;
            abort();
        }
#endif
    }