tmt2.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 trymat2()
00020 {
00021 //   cout << "\nSecond test of Matrix package\n\n";
00022    Tracer et("Second test of Matrix package");
00023    Tracer::PrintTrace();
00024 
00025    int i,j;
00026 
00027    Matrix M(3,5);
00028    for (i=1; i<=3; i++) for (j=1; j<=5; j++) M(i,j) = 100*i + j;
00029    Matrix X(8,10);
00030    for (i=1; i<=8; i++) for (j=1; j<=10; j++) X(i,j) = 1000*i + 10*j;
00031    Matrix Y = X; Matrix Z = X;
00032    { X.SubMatrix(2,4,3,7) << M; }
00033    for (i=1; i<=3; i++) for (j=1; j<=5; j++) Y(i+1,j+2) = 100*i + j;
00034    Print(Matrix(X-Y));
00035 
00036 
00037    Real a[15]; Real* r = a;
00038    for (i=1; i<=3; i++) for (j=1; j<=5; j++) *r++ = 100*i + j;
00039    { Z.SubMatrix(2,4,3,7) << a; }
00040    Print(Matrix(Z-Y));
00041 
00042    { M=33; X.SubMatrix(2,4,3,7) << M; }
00043    { Z.SubMatrix(2,4,3,7) = 33; }
00044    Print(Matrix(Z-X));
00045 
00046    for (i=1; i<=8; i++) for (j=1; j<=10; j++) X(i,j) = 1000*i + 10*j;
00047    Y = X;
00048    UpperTriangularMatrix U(5);
00049    for (i=1; i<=5; i++) for (j=i; j<=5; j++) U(i,j) = 100*i + j;
00050    { X.SubMatrix(3,7,5,9) << U; }
00051    for (i=1; i<=5; i++) for (j=i; j<=5; j++) Y(i+2,j+4) = 100*i + j;
00052    for (i=1; i<=5; i++) for (j=1; j<i; j++) Y(i+2,j+4) = 0.0;
00053    Print(Matrix(X-Y));
00054    for (i=1; i<=8; i++) for (j=1; j<=10; j++) X(i,j) = 1000*i + 10*j;
00055    Y = X;
00056    for (i=1; i<=5; i++) for (j=i; j<=5; j++) U(i,j) = 100*i + j;
00057    { X.SubMatrix(3,7,5,9).Inject(U); }
00058    for (i=1; i<=5; i++) for (j=i; j<=5; j++) Y(i+2,j+4) = 100*i + j;
00059    Print(Matrix(X-Y));
00060 
00061 
00062    // test growing and shrinking a vector
00063    {
00064       ColumnVector V(100);
00065       for (i=1;i<=100;i++) V(i) = i*i+i;
00066       V = V.Rows(1,50);               // to get first 50 vlaues.
00067 
00068       {
00069          V.Release(); ColumnVector VX=V;
00070          V.ReSize(100); V = 0.0; V.Rows(1,50)=VX;
00071       }                               // V now length 100
00072 
00073       M=V; M=100;                     // to make sure V will hold its values
00074       for (i=1;i<=50;i++) V(i) -= i*i+i;
00075       Print(V);
00076 
00077 
00078            // test redimensioning vectors with two dimensions given
00079       ColumnVector CV1(10); CV1 = 10;
00080       ColumnVector CV2(5); CV2.ReSize(10,1); CV2 = 10;
00081       V = CV1-CV2; Print(V);
00082 
00083       RowVector RV1(20); RV1 = 100;
00084       RowVector RV2; RV2.ReSize(1,20); RV2 = 100;
00085       V = (RV1-RV2).t(); Print(V);
00086 
00087       X.ReSize(4,7);
00088       for (i=1; i<=4; i++) for (j=1; j<=7; j++) X(i,j) = 1000*i + 10*j;
00089       Y = 10.5 * X;
00090       Z = 7.25 - Y;
00091       M = Z + X * 10.5 - 7.25;
00092       Print(M);
00093       Y = 2.5 * X;
00094       Z = 9.25 + Y;
00095       M = Z - X * 2.5 - 9.25;
00096       Print(M);
00097       U.ReSize(8);
00098       for (i=1; i<=8; i++) for (j=i; j<=8; j++) U(i,j) = 100*i + j;
00099       Y = 100 - U;
00100       M = Y + U - 100;
00101       Print(M);
00102    }
00103 
00104    {
00105       SymmetricMatrix S,T;
00106 
00107       S << (U + U.t());
00108       T = 100 - S; M = T + S - 100; Print(M);
00109       T = 100 - 2 * S; M = T + S * 2 - 100; Print(M);
00110       X = 100 - 2 * S; M = X + S * 2 - 100; Print(M);
00111       T = S; T = 100 - T; M = T + S - 100; Print(M);
00112    }
00113 
00114    // test new
00115    {
00116       ColumnVector CV1; RowVector RV1;
00117       Matrix* MX; MX = new Matrix; if (!MX) Throw(Bad_alloc("New fails "));
00118       MX->ReSize(10,20);
00119       for (i = 1; i <= 10; i++) for (j = 1; j <= 20; j++)
00120          (*MX)(i,j) = 100 * i + j;
00121       ColumnVector* CV = new ColumnVector(10);
00122       if (!CV) Throw(Bad_alloc("New fails "));
00123       *CV << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
00124       RowVector* RV =  new RowVector(CV->t() | (*CV + 10).t());
00125       if (!RV) Throw(Bad_alloc("New fails "));
00126       CV1 = ColumnVector(10); CV1 = 1; RV1 = RowVector(20); RV1 = 1;
00127       *MX -= 100 * *CV * RV1 + CV1 * *RV;
00128       Print(*MX);
00129       delete MX; delete CV; delete RV;
00130    }
00131 
00132 
00133    // test copying of vectors and matrices with no elements
00134    {
00135       ColumnVector dims(16);
00136       Matrix M1; Matrix M2 = M1; Print(M2);
00137       dims(1) = M2.Nrows(); dims(2) = M2.Ncols();
00138       dims(3) = (Real)(unsigned long)M2.Store(); dims(4) = M2.Storage();
00139       M2 = M1;
00140       dims(5) = M2.Nrows(); dims(6) = M2.Ncols();
00141       dims(7) = (Real)(unsigned long)M2.Store(); dims(8) = M2.Storage();
00142       M2.ReSize(10,20); M2.CleanUp();
00143       dims(9) = M2.Nrows(); dims(10) = M2.Ncols();
00144       dims(11) = (Real)(unsigned long)M2.Store(); dims(12) = M2.Storage();
00145       M2.ReSize(20,10); M2.ReSize(0,0);
00146       dims(13) = M2.Nrows(); dims(14) = M2.Ncols();
00147       dims(15) = (Real)(unsigned long)M2.Store(); dims(16) = M2.Storage();
00148       Print(dims);
00149    }
00150 
00151    {
00152       ColumnVector dims(16);
00153       ColumnVector M1; ColumnVector M2 = M1; Print(M2);
00154       dims(1) = M2.Nrows(); dims(2) = M2.Ncols()-1;
00155       dims(3) = (Real)(unsigned long)M2.Store(); dims(4) = M2.Storage();
00156       M2 = M1;
00157       dims(5) = M2.Nrows(); dims(6) = M2.Ncols()-1;
00158       dims(7) = (Real)(unsigned long)M2.Store(); dims(8) = M2.Storage();
00159       M2.ReSize(10); M2.CleanUp();
00160       dims(9) = M2.Nrows(); dims(10) = M2.Ncols()-1;
00161       dims(11) = (Real)(unsigned long)M2.Store(); dims(12) = M2.Storage();
00162       M2.ReSize(10); M2.ReSize(0);
00163       dims(13) = M2.Nrows(); dims(14) = M2.Ncols()-1;
00164       dims(15) = (Real)(unsigned long)M2.Store(); dims(16) = M2.Storage();
00165       Print(dims);
00166    }
00167 
00168    {
00169       ColumnVector dims(16);
00170       RowVector M1; RowVector M2 = M1; Print(M2);
00171       dims(1) = M2.Nrows()-1; dims(2) = M2.Ncols();
00172       dims(3) = (Real)(unsigned long)M2.Store(); dims(4) = M2.Storage();
00173       M2 = M1;
00174       dims(5) = M2.Nrows()-1; dims(6) = M2.Ncols();
00175       dims(7) = (Real)(unsigned long)M2.Store(); dims(8) = M2.Storage();
00176       M2.ReSize(10); M2.CleanUp();
00177       dims(9) = M2.Nrows()-1; dims(10) = M2.Ncols();
00178       dims(11) = (Real)(unsigned long)M2.Store(); dims(12) = M2.Storage();
00179       M2.ReSize(10); M2.ReSize(0);
00180       dims(13) = M2.Nrows()-1; dims(14) = M2.Ncols();
00181       dims(15) = (Real)(unsigned long)M2.Store(); dims(16) = M2.Storage();
00182       Print(dims);
00183    }
00184 
00185    // test identity matrix
00186    {
00187       Matrix M;
00188       IdentityMatrix I(10); DiagonalMatrix D(10); D = 1;
00189       M = I; M -= D; Print(M);
00190       D -= I; Print(D);
00191       ColumnVector X(8);
00192       D = 1;
00193       X(1) = Sum(D) - Sum(I);
00194       X(2) = SumAbsoluteValue(D) - SumAbsoluteValue(I);
00195       X(3) = SumSquare(D) - SumSquare(I);
00196       X(4) = Trace(D) - Trace(I);
00197       X(5) = Maximum(D) - Maximum(I);
00198       X(6) = Minimum(D) - Minimum(I);
00199       X(7) = LogDeterminant(D).LogValue() - LogDeterminant(I).LogValue();
00200       X(8) = LogDeterminant(D).Sign() - LogDeterminant(I).Sign();
00201       Clean(X,0.00000001); Print(X);
00202 
00203       for (i = 1; i <= 10; i++) for (j = 1; j <= 10; j++)
00204          M(i,j) = 100 * i + j;
00205       Matrix N;
00206       N = M * I - M; Print(N);
00207       N = I * M - M; Print(N);
00208       N = M * I.i() - M; Print(N);
00209       N = I.i() * M - M; Print(N);
00210       N = I.i(); N -= I; Print(N);
00211       N = I.t(); N -= I; Print(N);
00212       N = I.t(); N += (-I); Print(N); // <----------------
00213       D = I; N = D; D = 1; N -= D; Print(N);
00214       N = I; D = 1; N -= D; Print(N);
00215       N = M + 2 * IdentityMatrix(10); N -= (M + 2 * D); Print(N);
00216 
00217       I *= 4;
00218 
00219       D = 4;
00220 
00221       X.ReSize(14);
00222       X(1) = Sum(D) - Sum(I);
00223       X(2) = SumAbsoluteValue(D) - SumAbsoluteValue(I);
00224       X(3) = SumSquare(D) - SumSquare(I);
00225       X(4) = Trace(D) - Trace(I);
00226       X(5) = Maximum(D) - Maximum(I);
00227       X(6) = Minimum(D) - Minimum(I);
00228       X(7) = LogDeterminant(D).LogValue() - LogDeterminant(I).LogValue();  // <--
00229       X(8) = LogDeterminant(D).Sign() - LogDeterminant(I).Sign();
00230       int i,j;
00231       X(9) = I.Maximum1(i) - 4; X(10) = i-1;
00232       X(11) = I.Maximum2(i,j) - 4; X(12) = i-10; X(13) = j-10;
00233       X(14) = I.Nrows() - 10;
00234       Clean(X,0.00000001); Print(X);
00235 
00236 
00237       N = D.i();
00238       N += I / (-16);
00239       Print(N);
00240       N = M * I - 4 * M; Print(N);
00241       N = I * M - 4 * M; Print(N);
00242       N = M * I.i() - 0.25 * M; Print(N);
00243       N = I.i() * M - 0.25 * M; Print(N);
00244       N = I.i(); N -= I * 0.0625; Print(N);
00245       N = I.i(); N = N - 0.0625 * I; Print(N);
00246       N = I.t(); N -= I; Print(N);
00247       D = I * 2; N = D; D = 1; N -= 8 * D; Print(N);
00248       N = I * 2; N -= 8 * D; Print(N);
00249       N = 0.5 * I + M; N -= M; N -= 2.0 * D; Print(N);
00250 
00251       IdentityMatrix J(10); J = 8;
00252       D = 4;
00253       DiagonalMatrix E(10); E = 8;
00254       N = (I + J) - (D + E); Print(N);
00255       N = (5*I + 3*J) - (5*D + 3*E); Print(N);
00256       N = (-I + J) - (-D + E); Print(N);
00257       N = (I - J) - (D - E); Print(N);
00258       N = (I | J) - (D | E); Print(N);
00259       N = (I & J) - (D & E); Print(N);
00260       N = SP(I,J) - SP(D,E); Print(N);
00261       N = D.SubMatrix(2,5,3,8) - I.SubMatrix(2,5,3,8); Print(N);
00262 
00263       N = M; N.Inject(I); D << M; N -= (M + I); N += D; Print(N);
00264       D = 4;
00265 
00266       IdentityMatrix K = I.i()*7 - J.t()/4;
00267       N = D.i() * 7 - E / 4 - K; Print(N);
00268       K = I * J; N = K - D * E; Print(N);
00269       N = I * J; N -= D * E; Print(N);
00270       K = 5*I - 3*J;
00271       N = K - (5*D - 3*E); Print(N);
00272       K = I.i(); N = K - 0.0625 * I; Print(N);
00273       K = I.t(); N = K - I; Print(N);
00274 
00275 
00276       K.ReSize(20); D.ReSize(20); D = 1;
00277       D -= K; Print(D);
00278 
00279       I.ReSize(3); J.ReSize(3); K = I * J; N = K - I; Print(N);
00280       K << D; N = K - D; Print(N);
00281 
00282 
00283    }
00284 
00285 
00286 //   cout << "\nEnd of second test\n";
00287 }


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