Changeset 3005

Show
Ignore:
Timestamp:
08/01/08 13:00:29 (4 months ago)
Author:
saunders
Message:

cleaned up solution getentry.h (should be get-entry.h), and simplified trace.h in view of that. Their tests are a bit more thorough now too.

Location:
trunk/linbox
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/linbox/linbox/solutions/getentry.h

    r2904 r3005  
    2525{ 
    2626 
     27namespace GetEntryTags  
     28{       struct GenericBB{};     struct Local{};  
     29        struct SpecialCDB{}; struct SpecialCBD{}; struct SpecialCDD{};  
     30}; // namespace GetEntryTags 
     31 
     32template<class BB> struct GetEntryCategory { typedef GetEntryTags::GenericBB Tag; }; 
     33 
    2734/** \brief 
    2835 * Getting the i,j entry of the blackbox. 
     
    3037template <class BB>  
    3138typename BB::Field::Element& getEntry(typename BB::Field::Element& x, const BB& A, const size_t i, const size_t j) 
    32 { return getEntry(x, A, i, j, Method::Hybrid()); } 
     39{  
     40        typename GetEntryCategory<BB>::Tag t;  
     41        return getEntry(x, A, i, j, t); 
     42} 
    3343 
    34 // Any BBs that offer a local getEntry can specialize the BB class for the Hybrid method. 
    35 /** \brief our best guess 
     44/// To ignore methods 
     45template <class BB, class Method>  
     46typename BB::Field::Element& getEntry(typename BB::Field::Element& x, const BB& A, const size_t i, const size_t j, Method & m) 
     47{ return getEntry(x, A, i, j);  } 
    3648 
    37 Hybrid method will choose based on matrix size and type 
    38 */ 
     49// Generic BBs require use of apply. 
     50template <class BB>  
     51typename BB::Field::Element& getEntry(typename BB::Field::Element& x, const BB& A, const size_t i, const size_t j, typename GetEntryTags::GenericBB t) 
     52{ 
     53        typedef typename BB::Field Field; 
     54        typedef typename Field::Element Elt; 
     55        typedef std::vector<Elt> Vector; 
     56 
     57        const Field& F = A.field(); 
     58        Elt zero; F.init(zero, 0UL); 
     59        Vector v(A.coldim(), zero), w(A.rowdim(), zero); 
     60        for (typename Vector::iterator it = v.begin (); it != v.end (); ++it) 
     61                F.assign (*it, zero); 
     62        F.init(v[j],1UL); 
     63        A.apply (w, v); 
     64        F.assign (x, VectorWrapper::constRef<Field, Vector> (w, i)); 
     65        return x; 
     66} 
     67 
     68// BBs that offer a local getEntry. 
     69template<class Field> struct GetEntryCategory<DenseMatrix<Field> > { typedef GetEntryTags::Local Tag; }; 
     70template<class Field> struct GetEntryCategory<SparseMatrix<Field> > { typedef GetEntryTags::Local Tag; }; 
     71template<class Field, class Trait> struct GetEntryCategory<Diagonal<Field, Trait> > { typedef GetEntryTags::Local Tag; }; 
     72template<class Field> struct GetEntryCategory<ScalarMatrix<Field> > { typedef GetEntryTags::Local Tag; }; 
    3973 
    4074template <class BB>  
    41 typename BB::Field::Element& getEntry(typename BB::Field::Element& x, const BB& A, const size_t i, const size_t j,  
    42                 const Method::Hybrid& m) 
    43 { return getEntry(x, A, i, j, Method::Blackbox(m)); } 
    44  
    45 // DenseMatrix specialization 
    46 template <class Field>  
    47 typename Field::Element& getEntry(typename Field::Element& x, const DenseMatrix<Field>& A, const size_t i, const size_t j,  
    48                 const Method::Hybrid& m) 
    49 {        
    50         return A.getEntry(x,i,j); 
    51 } 
    52  
    53 // SparseMatrix specialization 
    54 template <class Field>  
    55 typename Field::Element& getEntry(typename Field::Element& x, const SparseMatrix<Field>& A, const size_t i, const size_t j,  
    56                                   const Method::Hybrid& m) 
    57 { 
    58         return A.getEntry(x,i,j); 
    59 } 
    60  
    61 // scalar matrix specialization  
    62 template <class Field> 
    63 typename Field::Element & getEntry(typename Field::Element & x, const ScalarMatrix<Field>& A, const size_t i, const size_t j, const Method::Hybrid& m)  
     75typename BB::Field::Element& getEntry(typename BB::Field::Element& x, const BB& A, const size_t i, const size_t j, typename GetEntryTags::Local t ) 
    6476{ return A.getEntry(x, i, j); } 
    6577 
     78// Compose< Diagonal, BB > specialization 
     79template <class Field, class Trait, class BB>  
     80struct GetEntryCategory<Compose<Diagonal<Field, Trait>,BB> > { typedef GetEntryTags::SpecialCDB Tag; }; 
    6681 
    67 // diagonal specialization  
    68 template <class Field, class Trait> 
    69 typename Field::Element & getEntry(typename Field::Element & x, const Diagonal<Field, Trait>& A, const size_t i, const size_t j, const Method::Hybrid& m)  
    70 { return A.getEntry(x, i, j); } 
    71  
    72 /** \brief our elimination (a fake in this case) 
    73  
    74 Elimination method will go to blackbox. 
    75 */ 
    76 template <class BB>  
    77 typename BB::Field::Element& getEntry(typename BB::Field::Element& x, const BB& A, const size_t i, const size_t j, const Method::Elimination& m) 
    78 { return getEntry(x, A, i, j, Method::Blackbox(m));  
    79 } 
    80  
    81  
    82         /** Compute the getEntry of a linear operator A, represented as a black 
    83          * box. This class is parameterized by the black box type so that it can 
    84          * be specialized for different black boxes. 
    85          */ 
    86  
    87         template <class Blackbox> 
    88         typename Blackbox::Field::Element &getEntry (typename Blackbox::Field::Element &res, 
    89                                         const Blackbox          &A, const size_t i, const size_t j,  
    90                                         const Method::Blackbox& m) 
    91         { 
    92  
    93                 typedef typename Blackbox::Field Field; 
    94                 typedef std::vector<typename Field::Element> Vector; 
    95                 Vector v, w; 
    96                 VectorWrapper::ensureDim (v, A.coldim ()); 
    97                 VectorWrapper::ensureDim (w, A.rowdim ()); 
    98                 const Field& F = A.field(); 
    99                 typename Field::Element zero; F.init(zero, 0UL); 
    100                 typename Vector::iterator it; 
    101                 for (it = v.begin (); it != v.end (); ++it) 
    102                         F.assign (*it, zero); 
    103                 F.init(v[j],1UL); 
    104                 F.init (res, 0); 
    105                 A.apply (w, v); 
    106                 F.assign (res, VectorWrapper::constRef<Field, Vector> (w, i)); 
    107                 return res; 
    108         } 
    109  
    110  
    111  
    112  
    113  
    114 // Compose< Diagonal, BB > specialization 
    115 template <class Field, class Trait, class BlackBox>  
    116 typename Field::Element& getEntry(typename Field::Element& t, const Compose<Diagonal<Field, Trait>, BlackBox>& A, const size_t i, const size_t j, const Method::Hybrid& m) 
     82template <class Field, class Trait, class BB>  
     83typename Field::Element& getEntry(typename Field::Element& x, const Compose<Diagonal<Field, Trait>, BB>& A, const size_t i, const size_t j, GetEntryTags::SpecialCDB t) 
    11784{ 
    11885    typename Field::Element y; 
    11986    getEntry(y, *(A.getLeftPtr()), i, i); 
    120     getEntry(t, *(A.getRightPtr()), i, j); 
    121     return A.field().mulin(t, y); 
     87    getEntry(x, *(A.getRightPtr()), i, j); 
     88    return A.field().mulin(x, y); 
    12289} 
    12390 
    12491// Compose< BB, Diagonal > specialization 
    125 template <class BlackBox, class Field, class Trait>  
    126 typename Field::Element& getEntry(typename Field::Element& t, const Compose<BlackBox, Diagonal<Field, Trait> >& A, const size_t i, const size_t j, const Method::Hybrid& m) 
     92template <class BB, class Field, class Trait>  
     93struct GetEntryCategory<Compose<BB, Diagonal<Field, Trait> > > { typedef GetEntryTags::SpecialCBD Tag; }; 
     94 
     95template <class BB, class Field, class Trait>  
     96typename Field::Element& getEntry(typename Field::Element& x, const Compose<BB, Diagonal<Field, Trait> >& A, const size_t i, const size_t j, GetEntryTags::SpecialCBD t)   
    12797{ 
    12898    typename Field::Element y; 
    12999    getEntry(y, *(A.getLeftPtr()), i, j); 
    130     getEntry(t, *(A.getRightPtr()), j, j); 
    131     return A.field().mulin(t, y); 
     100    getEntry(x, *(A.getRightPtr()), j, j); 
     101    return A.field().mulin(x, y); 
    132102} 
    133103 
    134104// Compose< Diagonal, Diagonal > specialization 
    135105template <class Field, class T1, class T2>  
    136 typename Field::Element& getEntry(typename Field::Element& t, const Compose<Diagonal<Field,T1>, Diagonal<Field, T2> >& A, const size_t i, const size_t j, const Method::Hybrid& m) 
     106struct GetEntryCategory<Compose<Diagonal<Field, T1>,Diagonal<Field, T2> > > { typedef GetEntryTags::SpecialCDD Tag; }; 
     107 
     108template <class Field, class T1, class T2>  
     109typename Field::Element& getEntry(typename Field::Element& x, const Compose<Diagonal<Field,T1>, Diagonal<Field, T2> >& A, const size_t i, const size_t j, typename GetEntryTags::SpecialCDD t) 
    137110{ 
    138111    if (i != j)  
    139         return A.field().init(t, 0UL); 
     112        return A.field().init(x, 0UL); 
    140113    else { 
    141114        typename Field::Element y; 
    142115        getEntry(y, *(A.getLeftPtr()), i, i); 
    143         getEntry(t, *(A.getRightPtr()), j, j); 
    144         return A.field().mulin(t, y); 
     116        getEntry(x, *(A.getRightPtr()), j, j); 
     117        return A.field().mulin(x, y); 
    145118    } 
    146119} 
  • trunk/linbox/linbox/solutions/trace.h

    r2204 r3005  
    1212 
    1313#include <vector> 
    14 //#include <algorithm> 
    1514 
    16 // must fix this list... 
    17 //#include "linbox/algorithms/wiedemann.h" 
    18 //#include "linbox/algorithms/lanczos.h" 
    19 //#include "linbox/algorithms/block-lanczos.h" 
    20 #include "linbox/util/debug.h" 
    21 #include "linbox/vector/vector-domain.h" 
    22 #include "linbox/blackbox/dense.h" 
    23 #include "linbox/blackbox/sparse.h" 
    2415#include "linbox/blackbox/scalar-matrix.h" 
    25 #include "linbox/solutions/methods.h" 
     16//#include "linbox/blackbox/toeplitz.h" 
    2617#include "linbox/solutions/getentry.h" 
    27 #include "linbox/blackbox/diagonal.h" 
    28 #include "linbox/blackbox/compose.h" 
    2918 
    3019namespace LinBox  
    3120{ 
    3221 
     22// Trait to show whether or not the BB class has a local trace function. 
     23template<class BB> struct TraceCategory; 
    3324 
    34 /* for trace we actually use only the blackbox method and local defs for sparse, 
    35  dense, and a few other BB types. 
    36 */ 
    37 /** \brief sum of eigenvalues 
     25namespace TraceTags { struct Generic{}; struct Local{}; }; 
     26 
     27template<class BB> struct TraceCategory         { typedef typename TraceTags::Generic Tag; }; 
     28 
     29template<class Field> 
     30struct TraceCategory<ScalarMatrix<Field> >      { typedef typename TraceTags::Local Tag; }; 
     31 
     32//template<class Field, class PD> 
     33//struct TraceCategory<Toeplitz<Field,PD> >     { typedef typename TraceTags::Local Tag; }; 
     34 
     35// trace 
     36/** \brief Sum of the eigenvalues. 
    3837  
    39 Also sum of diagonal entries. 
    40 This is the generic one. 
     38Also it is the sum of the diagonal entries. 
    4139 
    42 testing multiple doc comments. 
     40Runtime on n by n matrix is n times the cost of getEntry().   
     41This is linear in n for those classes where getEntry is constant time 
     42(eg DenseMatrix and SparseMatrix).   
     43Trace is constant time when the diagonal is necessarily constant, eg. for ScalarMatrix and Toeplitz. 
     44Worst case time is cost of n blackbox applies (matrix vector products), and apply cost typically ranges between O(n) and O(n^2). 
     45 
    4346*/ 
    4447template <class BB>  
    4548typename BB::Field::Element& trace(typename BB::Field::Element& t, const BB& A) 
    46 { return trace(t, A, Method::Hybrid()); } 
     49{       typename TraceCategory<BB>::Tag tt;  
     50        return trace(t, A, tt);  
     51} 
    4752 
    48 // Any BBs that offer a local trace can specialize the BB class for the Hybrid method. 
    49 /** \brief our best guess 
    50  
    51 Hybrid method will choose based on matrix size and type 
     53/* Generic approach.  It will be efficient for BBs with efficient getEntry. 
     54If getEntry is constant time on n by n BB's, trace will be in O(n). 
    5255*/ 
    53  
    5456template <class BB>  
    55 typename BB::Field::Element& trace(typename BB::Field::Element& t, const BB& A,  
    56                 const Method::Hybrid& m) 
    57 { return trace(t, A, Method::Blackbox(m)); } 
    58  
    59 // DenseMatrix specialization 
    60 template <class Field>  
    61 typename Field::Element& trace(typename Field::Element& t, const DenseMatrix<Field>& A,  
    62                 const Method::Hybrid& m) 
    63 {       typename Field::Element x; 
     57typename BB::Field::Element& trace(typename BB::Field::Element& t, const BB& A, TraceTags::Generic tt) 
     58{       typename BB::Field::Element x; 
     59        A.field().init(x, 0); 
    6460        A.field().init(t, 0); 
    65         for (size_t i = 0; i < A.coldim(); ++i)  { 
    66                 A.getEntry(x,i,i); 
    67                 A.field().addin(t, x); 
    68         } 
     61        for (size_t i = 0; i < A.coldim(); ++i) 
     62                A.field().addin(t, getEntry(x,A,i,i)); 
    6963        return t; 
    7064} 
    7165 
    72 // SparseMatrix specialization 
    73 template <class Field, class Row>  
    74 typename Field::Element& trace(typename Field::Element& t, const SparseMatrix<Field, Row>& A,  
    75                 const Method::Hybrid& m) 
    76 {       typename Field::Element x; 
    77         A.field().init(t, 0); 
    78         for (size_t i = 0; i < A.coldim(); ++i) {  
    79                 A.getEntry(x,i,i); 
    80                 A.field().addin(t, x); 
    81         } 
    82         return t; 
    83 } 
    84  
    85 // Diagonal specialization 
    86 template <class Field, class Trait>  
    87 typename Field::Element& trace(typename Field::Element& t, const Diagonal<Field, Trait>& A,  
    88                 const Method::Hybrid& m) 
    89 {       typename Field::Element x; 
    90         A.field().init(t, 0); 
    91         for (size_t i = 0; i < A.coldim(); ++i) {  
    92                 A.getEntry(x,i,i); 
    93                 A.field().addin(t, x); 
    94         } 
    95         return t; 
    96 } 
    97  
    98 // scalar matrix specialization  
    99 template <class Field> 
    100 typename Field::Element & trace(typename Field::Element & t, const ScalarMatrix<Field>& A, const Method::Hybrid& m)  
     66/* Specialization for BB's with local trace function.   
     67Allows constant time trace for, eg., ScalarMatrix, Toeplitz. 
     68*/ 
     69template <class BB> 
     70typename BB::Field::Element & trace(typename BB::Field::Element & t, const BB& A, TraceTags::Local tt)  
    10171{ return A.trace(t); } 
    10272 
    103 /** \brief our elimination (a fake in this case) 
    104  
    105 Elimination method will go to blackbox. 
    106 */ 
    107 template <class BB>  
    108 typename BB::Field::Element& trace(typename BB::Field::Element& t, const BB& A, const Method::Elimination& m) 
    109 { return trace(t, A, Method::Blackbox(m));  
    110 } 
    111  
    112  
    113     /** Compute the trace of a linear operator A, represented as a black 
    114      * box. This class is parameterized by the black box type so that it can 
    115      * be specialized for different black boxes. 
    116      */ 
    117  
    118 template <class Blackbox> 
    119 typename Blackbox::Field::Element &trace (typename Blackbox::Field::Element &res, 
    120                                           const Blackbox          &A,  
    121                                           const Method::Blackbox& m) 
    122 { 
    123      
    124     typedef typename Blackbox::Field Field; 
    125     typedef std::vector<typename Field::Element> Vector; 
    126     Vector v, w; 
    127     Field F = A.field(); 
    128     StandardBasisStream<Field, Vector> stream (F, A.coldim ()); 
    129      
    130     VectorWrapper::ensureDim (v, A.coldim ()); 
    131     VectorWrapper::ensureDim (w, A.rowdim ()); 
    132      
    133     F.init (res, 0); 
    134      
    135     while (stream) { 
    136         stream >> v; 
    137         A.apply (w, v); 
    138         F.addin (res, VectorWrapper::constRef<Field, Vector> (w, stream.pos () - 1)); 
    139     } 
    140      
    141     return res; 
    142 } 
    143  
    144  
    145 // Compose< Diagonal, BB > specialization 
    146 template <class Field, class Trait, class BlackBox>  
    147 typename Field::Element& trace(typename Field::Element& t, const Compose<Diagonal<Field, Trait>, BlackBox>& A, const Method::Hybrid& m) 
    148 { 
    149     typename Field::Element x, y; 
    150     A.field().init(t, 0); 
    151     size_t n = (A.coldim()<A.rowdim()?A.coldim():A.rowdim()); 
    152     for (size_t i = 0; i < n; ++i) {  
    153         getEntry(x, *(A.getRightPtr()), i, i); 
    154         getEntry(y, *(A.getLeftPtr()), i, i); 
    155         A.field().axpyin(t, x, y); 
    156     } 
    157     return t; 
    158 } 
    159  
    160 // Compose< BB, Diagonal > specialization 
    161 template <class BlackBox, class Field, class Trait>  
    162 typename Field::Element& trace(typename Field::Element& t, const Compose<BlackBox, Diagonal<Field, Trait> >& A, const Method::Hybrid& m) 
    163 { 
    164     typename Field::Element x, y; 
    165     A.field().init(t, 0); 
    166     size_t n = (A.coldim()<A.rowdim()?A.coldim():A.rowdim()); 
    167     for (size_t i = 0; i < n; ++i) {  
    168         getEntry(x, *(A.getRightPtr()), i, i); 
    169         getEntry(y, *(A.getLeftPtr()), i, i); 
    170         A.field().axpyin(t, x, y); 
    171     } 
    172     return t; 
    173 } 
    174  
    175  
    176 // Compose< Diagonal, Diagonal > specialization 
    177 template <class Field, class T1, class T2>  
    178 typename Field::Element& trace(typename Field::Element& t, const Compose<Diagonal<Field,T1>, Diagonal<Field, T2> >& A, const Method::Hybrid& m) 
    179 { 
    180     typename Field::Element x, y; 
    181     A.field().init(t, 0); 
    182     size_t n = (A.coldim()<A.rowdim()?A.coldim():A.rowdim()); 
    183     for (size_t i = 0; i < n; ++i) {  
    184         getEntry(x, *(A.getRightPtr()), i, i); 
    185         getEntry(y, *(A.getLeftPtr()), i, i); 
    186         A.field().axpyin(t, x, y); 
    187     } 
    188     return t; 
    189 } 
    190  
    191  
    192 } 
    193  
    194  
    195  
     73}; // namespace LinBox 
    19674#endif // __TRACE_H 
  • trunk/linbox/tests/test-getentry.C

    r2814 r3005  
    2323#include "linbox/field/modular-int32.h" 
    2424#include "linbox/solutions/getentry.h" 
     25#include "linbox/blackbox/compose.h" 
    2526#include "linbox/blackbox/diagonal.h" 
    2627#include "linbox/blackbox/scalar-matrix.h" 
     
    3132using namespace LinBox; 
    3233 
    33 /* Test 1: getEntry of random diagonal matrix 
    34  * 
    35  * Construct a random diagonal matrix and check that its computed getEntry is the 
    36  * correct i,j element 
    37  * 
    38  * F - Field over which to perform computations 
    39  * stream - Stream that comprises source of diagonal vectors 
    40  * 
    41  * Return true on success and false on failure 
    42  */ 
    43  
     34/* getEntry of generic blackbox matrix */ 
     35template <class Field> 
     36bool testGenericBBgetEntry (const Field &F, size_t n) 
     37{ 
     38        bool ret = true; 
     39    typename Field::Element s, x, z; 
     40        ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); 
     41        F.init(x, 0); 
     42        F.init(s, 2); 
     43        F.init(z, 0); 
     44        ScalarMatrix<Field> B(F, n, s); 
     45        getEntry(x, B, 0, n-1, typename GetEntryTags::GenericBB() );  
     46        if (n > 1 && !F.isZero(x)) ret = false; 
     47        getEntry(x, B, 0, 0, typename GetEntryTags::GenericBB() ); 
     48        if ( !F.areEqual(s, x)) ret = false; 
     49        if (!ret) report << "testGenericBBgetEntry failure" << std::endl; 
     50        return ret; 
     51} 
     52/* getEntry of scalar matrix */ 
    4453template <class Field> 
    4554bool testScalarMatrixgetEntry (const Field &F, size_t n) 
     
    4958        ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); 
    5059        report << "scalarmatrix getEntry test (using specialization)" << endl; 
    51         typename Field::Element s, t, r, th;  
     60    typename Field::Element s, t, r, th;  
    5261        F.init(r, 0); 
    5362        F.init(s, 2); 
     
    7180} 
    7281 
     82/* getEntry of sparse matrix */ 
    7383template <class Field> 
    7484bool testSparseMatrixgetEntry (const Field &F, size_t n) 
     
    117127} 
    118128 
     129/* getEntry of dense matrix */ 
    119130template <class Field> 
    120131static bool testDenseMatrixgetEntry (const Field &F, size_t n) 
     
    161172} 
    162173 
     174/* getEntry of diagonal matrix */ 
    163175template <class Field> 
    164176static bool testDiagonalgetEntry (const Field &F, VectorStream<vector<typename Field::Element> > &stream)  
     
    225237} 
    226238 
     239/* getEntry of composed blackbox with diagonal component*/ 
     240template <class Field> 
     241bool testSpecialCDgetEntry (const Field &F, size_t n) 
     242{ 
     243        bool ret = true; 
     244        typedef typename Field::Element Elt; 
     245        typedef ScalarMatrix<Field> BB; 
     246        typedef Diagonal<Field> DD; 
     247    Elt s, x, t, u; 
     248        ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); 
     249        F.init(x, 0); 
     250        F.init(s, 2); 
     251        F.init(t, 0); 
     252        F.init(u, 0); 
     253        F.mul(u, s, t); 
     254        BB B(F, n, s); 
     255        vector<Elt> d(n, t); 
     256        DD D(F, d); 
     257        Compose<DD, BB> CDB (D, B); 
     258        Compose<BB, DD> CBD (B, D); 
     259        Compose<DD, DD> CDD (D, D); 
     260        getEntry(x, CDB, 0, n-1); 
     261        if (n > 1 && !F.isZero(x)) ret = false; 
     262        getEntry(x, CDB, 0, 0); 
     263        if ( !F.areEqual(u, x)) ret = false; 
     264        getEntry(x, CBD, 0, n-1); 
     265        if (n > 1 && !F.isZero(x)) ret = false; 
     266        getEntry(x, CBD, 0, 0); 
     267        if ( !F.areEqual(u, x)) ret = false; 
     268        getEntry(x, CDD, 0, n-1); 
     269        if (n > 1 && !F.isZero(x)) ret = false; 
     270        getEntry(x, CDD, 0, 0); 
     271        if ( !F.areEqual(F.mul(u,t,t), x)) ret = false; 
     272        if (!ret) report << "testSpecialCDgetEntry failure" << std::endl; 
     273        return ret; 
     274} 
    227275int main (int argc, char **argv) 
    228276{ 
     
    252300        RandomDenseStream<Field, Vector> stream (F, n, iterations); 
    253301 
     302        if (!testGenericBBgetEntry (F, n)) pass = false; 
    254303        if (!testScalarMatrixgetEntry (F, n)) pass = false; 
    255304        if (!testSparseMatrixgetEntry (F, n)) pass = false; 
    256305        if (!testDenseMatrixgetEntry (F, n)) pass = false; 
    257306        if (!testDiagonalgetEntry (F, stream)) pass = false; 
     307        if (!testSpecialCDgetEntry (F, n)) pass = false; 
    258308 
    259309        commentator.stop("getEntry solution test suite"); 
  • trunk/linbox/tests/test-trace.C

    r2814 r3005  
    22 
    33/* tests/test-trace.C 
    4  * Copyright (C) 2002 Bradford Hovinen 
     4 * Copyright (C) -bds 
    55 * 
    6  * Written by Bradford Hovinen <hovinen@cis.udel.edu> 
     6 * Earlier version by Bradford Hovinen <hovinen@cis.udel.edu> 
    77 * 
    88 * -------------------------------------------------------- 
     
    1414 
    1515#include <iostream> 
    16 #include <fstream> 
    1716#include <vector> 
    18 #include <cstdio> 
    1917 
    2018#include "test-common.h" 
    2119 
    22 #include "linbox/util/commentator.h" 
    2320#include "linbox/field/modular-int.h" 
    2421#include "linbox/solutions/trace.h" 
    25 #include "linbox/blackbox/compose.h" 
    26 #include "linbox/blackbox/diagonal.h" 
    27 #include "linbox/blackbox/scalar-matrix.h" 
    28 #include "linbox/blackbox/sparse.h" 
    29 #include "linbox/blackbox/dense.h" 
    30 #include "linbox/vector/stream.h" 
    3122 
    3223using namespace LinBox; 
    33  
    34 /* Test 1: Trace of random diagonal matrix 
    35  * 
    36  * Construct a random diagonal matrix and check that its computed trace is the 
    37  * same as the sum of its entries 
    38  * 
    39  * F - Field over which to perform computations 
    40  * stream - Stream that comprises source of diagonal vectors 
    41  * 
    42  * Return true on success and false on failure 
    43  */ 
    44  
    45 template <class Field> 
    46 bool testScalarMatrixTrace (const Field &F, size_t n) 
    47 { 
    48         bool ret = true; 
    49         commentator.start ("Testing scalar matrix trace", "", 1); 
    50         ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); 
    51         report << "scalarmatrix trace test (using specialization)" << endl; 
    52         typename Field::Element s, t, th;  
    53         F.init(s, 2); 
    54         F.init(th, 2*n); 
    55         ScalarMatrix<Field> B(F, n, s); 
    56         trace(t, B); 
    57         commentator.stop (MSG_STATUS (ret), (const char *) 0, "testScalarMatrixTrace"); 
    58         if (!F.areEqual(t, th)) { 
    59         report << "bad scalar matrix trace " << t << ", should be " << th << endl; 
    60  
    61                 return false;  
    62         }  
    63         else return true; 
    64 } 
    65  
    66 template <class Field> 
    67 bool testSparseMatrixTrace (const Field &F, size_t n) 
    68 { 
    69         commentator.start ("Building sparse matrix", "", 1); 
    70         bool ret = true; 
    71         ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); 
    72         typename Field::Element s, t, th;  
    73         F.init(s, 2); 
    74         size_t m = (n > 10 ? 10 : n); 
    75         F.init(th, 2*m); 
    76         SparseMatrix<Field> B(F, n, n); 
    77         for (size_t i = 0; i <  m; ++i) 
    78                 for (size_t j = 0; j < m; ++j)  
    79                         B.setEntry(i,j,s); 
    80         commentator.stop ("", "done"); 
    81         commentator.start ("Testing sparse matrix trace", "", 1); 
    82         report << "sparse matrix trace test (using specialization)" << endl; 
    83         trace(t, B); 
    84         if (!F.areEqual(t, th)) { 
    85         report << "bad sparse matrix trace " << t << ", should be " << th << endl; 
    86  
    87                 ret = false;  
    88         }  
    89         else ret = true; 
    90         commentator.stop (MSG_STATUS (ret), (const char *) 0, "testSparseMatrixTrace"); 
    91         return ret; 
    92 } 
    93  
    94 template <class Field> 
    95 static bool testDenseMatrixTrace (const Field &F, size_t n) 
    96 { 
    97         bool ret = true; 
    98         typename Field::Element s, t, th;  
    99         F.init(s, 2); 
    100         size_t m = (n > 10 ? 10 : n); 
    101         F.init(th, 2*m); 
    102         DenseMatrix<Field> B(F, n, n); 
    103         for (size_t i = 0; i <  m; ++i) 
    104                 for (size_t j = 0; j < n; ++j)  
    105                         B.setEntry(i, j, s); 
    106         commentator.start ("Testing dense matrix trace", "", 1); 
    107         ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); 
    108         report << "dense matrix trace test (using specialization)" << endl; 
    109         trace(t, B); 
    110         if (!F.areEqual(t, th)) { 
    111                 report << "bad dense matrix trace " << t << ", should be " << th << endl; 
    112  
    113                 ret = false;  
    114         }  
    115         else ret = true; 
    116         commentator.stop (MSG_STATUS (ret), (const char *) 0, "testDenseMatrixTrace"); 
    117         return ret; 
    118 } 
    119  
    120 template <class Field> 
    121 static bool testDiagonalTrace (const Field &F, VectorStream<vector<typename Field::Element> > &stream)  
    122 { 
    123         typedef vector <typename Field::Element> Vector; 
    124         typedef Diagonal <Field> Blackbox; 
    125  
    126         commentator.start ("Testing diagonal trace", "testDiagonalTrace", stream.m ()); 
    127         ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); 
    128  
    129         VectorDomain<Field> VD (F); 
    130  
    131         bool ret = true; 
    132         size_t i; 
    133  
    134         Vector d; 
    135         typename Field::Element sigma, res; 
    136  
    137         VectorWrapper::ensureDim (d, stream.dim ()); 
    138  
    139         while (stream) { 
    140                 commentator.startIteration (stream.j ()); 
    141  
    142                 stream.next (d); 
    143  
    144                 report << "Input vector:  "; 
    145                 VD.write (report, d); 
    146                 report << endl; 
    147  
    148                 F.init (sigma, 0); 
    149                 for (i = 0; i < stream.n (); i++) 
    150                         F.addin (sigma, VectorWrapper::constRef<Field, Vector> (d, i)); 
    151