Changeset 467

Show
Ignore:
Timestamp:
08/03/09 16:22:41 (15 years ago)
Author:
vbarta
Message:

added a program (square_mat_prep) to generate configurably random matrices, changed square_mat_stress to use the generated agenda

Location:
library/tests
Files:
8 added
2 modified

Legend:

Unmodified
Added
Removed
  • library/tests/CMakeLists.txt

    r462 r467  
    1212add_executable(square_mat_stress square_mat_stress.cpp) 
    1313target_link_libraries(square_mat_stress bdm itpp testutil unittest) 
     14 
     15add_executable(square_mat_prep additive_generator.cpp additive_generator.h generator.cpp generator.h size_generator.cpp size_generator.h square_mat_prep.cpp) 
     16target_link_libraries(square_mat_prep bdm itpp testutil) 
    1417 
    1518EXEC(emix_test) 
  • library/tests/square_mat_stress.cpp

    r456 r467  
    11#include "../bdm/math/square_mat.h" 
    22#include "../bdm/math/chmat.h" 
     3#include "base/user_info.h" 
    34#include "UnitTest++.h" 
    45#include "TestReporterStdout.h" 
     
    1213using std::endl; 
    1314 
     15using bdm::UIFile; 
     16using bdm::UI; 
     17 
     18const char *agenda_file_name = "agenda.cfg"; 
    1419double epsilon = 0.00001; 
    15  
    1620bool fast = false; 
    1721 
     
    4145 
    4246template<typename TMatrix> 
    43 void test_until_overflow() { 
     47void test_matrix(int index, const mat &A) { 
    4448    Real_Timer tt; 
    45     int sz = 7; 
    46     while (true) { 
    47         mat A0 = randu(sz, sz); 
    48         mat A = A0 * A0.T(); 
    4949         
    50         tt.tic(); 
    51         TMatrix sqmat(A); 
    52         double elapsed = tt.toc(); 
    53         cout << "ctor(" << sz << " x " << sz << "): " << elapsed << " s" << endl; 
     50    cout << "agenda[" << index << ']' << endl; 
     51    int sz = A.rows(); 
     52    CHECK_EQUAL(A.cols(), sz); 
    5453 
    55         tt.tic(); 
    56         mat res = sqmat.to_mat(); 
    57         elapsed = tt.toc(); 
     54    tt.tic(); 
     55    TMatrix sqmat(A); 
     56    double elapsed = tt.toc(); 
     57    cout << "ctor(" << sz << " x " << sz << "): " << elapsed << " s" << endl; 
    5858 
    59         if (!fast) { 
    60             CHECK_CLOSE(A, res, epsilon); 
    61         } 
     59    tt.tic(); 
     60    mat res = sqmat.to_mat(); 
     61    elapsed = tt.toc(); 
    6262 
    63         cout << "to_mat: " << elapsed << " s" << endl; 
     63    if (!fast) { 
     64        CHECK_CLOSE(A, res, epsilon); 
     65    } 
    6466 
    65         vec v = randu(sz); 
    66         double w = randu(); 
    67         TMatrix sqmat2 = sqmat; 
     67    cout << "to_mat: " << elapsed << " s" << endl; 
     68 
     69    vec v = randu(sz); 
     70    double w = randu(); 
     71    TMatrix sqmat2 = sqmat; 
    6872         
    69         tt.tic(); 
    70         sqmat2.opupdt(v, w); 
    71         elapsed = tt.toc(); 
     73    tt.tic(); 
     74    sqmat2.opupdt(v, w); 
     75    elapsed = tt.toc(); 
    7276 
    73         if (!fast) { 
    74             mat expA = A + w * outer_product(v, v); 
    75             CHECK_CLOSE(expA, sqmat2.to_mat(), epsilon); 
    76         } 
     77    if (!fast) { 
     78        mat expA = A + w * outer_product(v, v); 
     79        CHECK_CLOSE(expA, sqmat2.to_mat(), epsilon); 
     80    } 
    7781 
    78         cout << "opupdt: " << elapsed << " s" << endl; 
     82    cout << "opupdt: " << elapsed << " s" << endl; 
    7983 
    80         TMatrix invmat(sz); 
     84    TMatrix invmat(sz); 
    8185 
    82         tt.tic(); 
    83         sqmat.inv(invmat); 
    84         elapsed = tt.toc(); 
     86    tt.tic(); 
     87    sqmat.inv(invmat); 
     88    elapsed = tt.toc(); 
    8589 
    86         if (!fast) { 
    87             mat invA = inv(A); 
    88             CHECK_CLOSE(invA, invmat.to_mat(), epsilon); 
    89         } 
     90    if (!fast) { 
     91        mat invA = inv(A); 
     92        CHECK_CLOSE(invA, invmat.to_mat(), epsilon); 
     93    } 
    9094 
    91         cout << "inv: " << elapsed << " s" << endl; 
     95    cout << "inv: " << elapsed << " s" << endl; 
    9296 
    93         sz *= 7; 
     97    tt.tic(); 
     98    double ld = sqmat.logdet(); 
     99    elapsed = tt.toc(); 
     100 
     101    if (!fast) { 
     102        double d = det(A); 
     103        CHECK_CLOSE(log(d), ld, epsilon); 
     104    } 
     105 
     106    cout << "logdet: " << elapsed << " s" << endl; 
     107} 
     108 
     109template<typename TMatrix> 
     110void test_agenda() { 
     111    UIFile fag(agenda_file_name); 
     112    Array<mat> mag; 
     113    UI::get(mag, fag, "agenda"); 
     114    int sz = mag.size(); 
     115    CHECK(sz > 0); 
     116    for (int i = 0; i < sz; ++i) { 
     117        test_matrix<TMatrix>(i, mag(i)); 
    94118    } 
    95119} 
     
    97121SUITE(ldmat) { 
    98122    TEST(cycle) { 
    99         test_until_overflow<ldmat>(); 
     123        test_agenda<ldmat>(); 
    100124    } 
    101125} 
     
    103127SUITE(fsqmat) { 
    104128    TEST(cycle) { 
    105         test_until_overflow<fsqmat>(); 
     129        test_agenda<fsqmat>(); 
    106130    } 
    107131} 
     
    109133SUITE(chmat) { 
    110134    TEST(cycle) { 
    111         test_until_overflow<chmat>(); 
     135        test_agenda<chmat>(); 
    112136    } 
    113137} 
     
    115139int main(int argc, char const *argv[]) { 
    116140    bool unknown = false; 
    117     int update_next = 0; // 1 suite, 2 epsilon 
     141    int update_next = 0; // 1 suite, 2 epsilon, 3 agenda file 
    118142    const char *suite = "ldmat"; 
    119143    const char **param = argv + 1; 
     
    122146            if (update_next == 1) { 
    123147                suite = *param; 
    124             } else { 
     148            } else if (update_next == 2) { 
    125149                double eps = atof(*param); 
    126150                if (eps > 0) { 
     
    129153                    cerr << "invalid epsilon value ignored" << endl; 
    130154                } 
     155            } else { 
     156                agenda_file_name = *param; 
    131157            } 
    132158