tmt4.cc
Go to the documentation of this file.
00001 
00002 //#define WANT_STREAM
00003 
00004 
00005 #include "include.h"
00006 
00007 #include "newmat.h"
00008 
00009 #include "tmt.h"
00010 
00011 #ifdef use_namespace
00012 using namespace NEWMAT;
00013 #endif
00014 
00015 
00016 /**************************** test program ******************************/
00017 
00018 
00019 void trymat4()
00020 {
00021 //   cout << "\nFourth test of Matrix package\n";
00022    Tracer et("Fourth test of Matrix package");
00023    Tracer::PrintTrace();
00024 
00025    int i,j;
00026 
00027    {
00028       Tracer et1("Stage 1");
00029       Matrix M(10,10);
00030       UpperTriangularMatrix U(10);
00031       for (i=1;i<=10;i++) for (j=1;j<=10;j++) M(i,j) = 100*i+j;
00032       U << -M;
00033       Matrix X1 = M.Rows(2,4);
00034       Matrix Y1 = U.t().Rows(2,4);
00035       Matrix X = U; { Print(Matrix(X.Columns(2,4).t()-Y1)); }
00036       RowVector RV = M.Row(5);
00037       {
00038          X.ReSize(3,10);
00039          X.Row(1) << M.Row(2); X.Row(2) << M.Row(3); X.Row(3) << M.Row(4);
00040          Print(Matrix(X-X1));
00041       }
00042       {
00043          UpperTriangularMatrix V = U.SymSubMatrix(3,5);
00044          Matrix MV = U.SubMatrix(3,5,3,5); { Print(Matrix(MV-V)); }
00045          Matrix X2 = M.t().Columns(2,4); { Print(Matrix(X2-X1.t())); }
00046          Matrix Y2 = U.Columns(2,4); { Print(Matrix(Y2-Y1.t())); }
00047          ColumnVector CV = M.t().Column(5); { Print(ColumnVector(CV-RV.t())); }
00048          X.ReSize(10,3); M = M.t();
00049          X.Column(1) << M.Column(2); X.Column(2) << M.Column(3);
00050          X.Column(3) << M.Column(4);
00051          Print(Matrix(X-X2));
00052       }
00053    }
00054 
00055    {
00056       Tracer et1("Stage 2");
00057       Matrix M; Matrix X; M.ReSize(5,8);
00058       for (i=1;i<=5;i++) for (j=1;j<=8;j++) M(i,j) = 100*i+j;
00059       {
00060          X = M.Columns(5,8); M.Columns(5,8) << M.Columns(1,4);
00061              M.Columns(1,4) << X;
00062          X = M.Columns(3,4); M.Columns(3,4) << M.Columns(1,2);
00063              M.Columns(1,2) << X;
00064          X = M.Columns(7,8); M.Columns(7,8) << M.Columns(5,6);
00065              M.Columns(5,6) << X;
00066       }
00067       {
00068          X = M.Column(2); M.Column(2) = M.Column(1); M.Column(1) = X;
00069          X = M.Column(4); M.Column(4) = M.Column(3); M.Column(3) = X;
00070          X = M.Column(6); M.Column(6) = M.Column(5); M.Column(5) = X;
00071          X = M.Column(8); M.Column(8) = M.Column(7); M.Column(7) = X;
00072          X.ReSize(5,8);
00073       }
00074       for (i=1;i<=5;i++) for (j=1;j<=8;j++) X(i,9-j) = 100*i+j;
00075       Print(Matrix(X-M));
00076    }
00077    {
00078       Tracer et1("Stage 3");
00079       // try submatrices of zero dimension
00080       Matrix A(4,5); Matrix B, C;
00081       for (i=1; i<=4; i++) for (j=1; j<=5; j++)
00082          A(i,j) = 100+i*10+j;
00083       B = A + 100;
00084       C = A | B.Columns(4,3); Print(Matrix(A - C));
00085       C = A | B.Columns(1,0); Print(Matrix(A - C));
00086       C = A | B.Columns(6,5); Print(Matrix(A - C));
00087       C = A & B.Rows(2,1); Print(Matrix(A - C));
00088    }
00089    {
00090       Tracer et1("Stage 4");
00091       BandMatrix BM(5,3,2);
00092       BM(1,1) = 1; BM(1,2) = 2; BM(1,3) = 3;
00093       BM(2,1) = 4; BM(2,2) = 5; BM(2,3) = 6; BM(2,4) = 7;
00094       BM(3,1) = 8; BM(3,2) = 9; BM(3,3) =10; BM(3,4) =11; BM(3,5) =12;
00095       BM(4,1) =13; BM(4,2) =14; BM(4,3) =15; BM(4,4) =16; BM(4,5) =17;
00096                    BM(5,2) =18; BM(5,3) =19; BM(5,4) =20; BM(5,5) =21;
00097       SymmetricBandMatrix SM(5,3);
00098       SM.Inject(BandMatrix(BM + BM.t()));
00099       Matrix A = BM + 1;
00100       Matrix M = A + A.t() - 2;
00101       Matrix C = A.i() * BM;
00102       C = A * C - BM; Clean(C, 0.000000001); Print(C);
00103       C = A.i() * SM;
00104       C = A * C - M; Clean(C, 0.000000001); Print(C);
00105 
00106       // check row-wise load
00107       BandMatrix BM1(5,3,2);
00108       BM1.Row(1) <<  1 <<  2 <<  3;
00109       BM1.Row(2) <<  4 <<  5 <<  6 <<  7;
00110       BM1.Row(3) <<  8 <<  9 << 10 << 11 << 12;
00111       BM1.Row(4) << 13 << 14 << 15 << 16 << 17;
00112       BM1.Row(5)       << 18 << 19 << 20 << 21;
00113       Matrix M1 = BM1 - BM; Print(M1);
00114    }
00115    {
00116       Tracer et1("Stage 5");
00117       Matrix X(4,4);
00118       X << 1 << 2 << 3 << 4
00119         << 5 << 6 << 7 << 8
00120         << 9 <<10 <<11 <<12
00121         <<13 <<14 <<15 <<16;
00122       Matrix Y(4,0);
00123       Y = X | Y;
00124       X -= Y; Print(X);
00125 
00126       DiagonalMatrix D(1);
00127       D << 23;                       // matrix input with just one value
00128       D(1) -= 23; Print(D);
00129 
00130    }
00131    {
00132       Tracer et1("Stage 6");
00133       Matrix h (2,2);
00134       h << 1.0 << 2.0 << 0.0 << 1.0 ;
00135       RowVector c(2);
00136       c << 0.0 << 1.0;
00137       h -= c & c;
00138       h -= c.t().Reverse() | c.Reverse().t();
00139       Print(h);
00140    }
00141    {
00142       Tracer et1("Stage 7");
00143       // Check row-wise input for diagonal matrix
00144       DiagonalMatrix D(4);
00145       D << 18 << 23 << 31 << 17;
00146       DiagonalMatrix D1(4);
00147       D1.Row(1) << 18; D1.Row(2) << 23; D1.Row(3) << 31; D1.Row(4) << 17;
00148       D1 -= D; Print(D1);
00149       D1(1) = 18; D1(2) = 23; D1(3) = 31; D1(4) = 17;
00150       D1 -= D; Print(D1);
00151    }
00152 
00153 //   cout << "\nEnd of fourth test\n";
00154 }
00155 


rl_agent
Author(s): Todd Hester
autogenerated on Thu Jun 6 2019 22:00:13