Changeset 3005
- Timestamp:
- 08/01/08 13:00:29 (4 months ago)
- Location:
- trunk/linbox
- Files:
-
- 4 modified
-
linbox/solutions/getentry.h (modified) (2 diffs)
-
linbox/solutions/trace.h (modified) (1 diff)
-
tests/test-getentry.C (modified) (8 diffs)
-
tests/test-trace.C (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/linbox/linbox/solutions/getentry.h
r2904 r3005 25 25 { 26 26 27 namespace GetEntryTags 28 { struct GenericBB{}; struct Local{}; 29 struct SpecialCDB{}; struct SpecialCBD{}; struct SpecialCDD{}; 30 }; // namespace GetEntryTags 31 32 template<class BB> struct GetEntryCategory { typedef GetEntryTags::GenericBB Tag; }; 33 27 34 /** \brief 28 35 * Getting the i,j entry of the blackbox. … … 30 37 template <class BB> 31 38 typename 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 } 33 43 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 45 template <class BB, class Method> 46 typename 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); } 36 48 37 Hybrid method will choose based on matrix size and type 38 */ 49 // Generic BBs require use of apply. 50 template <class BB> 51 typename 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. 69 template<class Field> struct GetEntryCategory<DenseMatrix<Field> > { typedef GetEntryTags::Local Tag; }; 70 template<class Field> struct GetEntryCategory<SparseMatrix<Field> > { typedef GetEntryTags::Local Tag; }; 71 template<class Field, class Trait> struct GetEntryCategory<Diagonal<Field, Trait> > { typedef GetEntryTags::Local Tag; }; 72 template<class Field> struct GetEntryCategory<ScalarMatrix<Field> > { typedef GetEntryTags::Local Tag; }; 39 73 40 74 template <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) 75 typename BB::Field::Element& getEntry(typename BB::Field::Element& x, const BB& A, const size_t i, const size_t j, typename GetEntryTags::Local t ) 64 76 { return A.getEntry(x, i, j); } 65 77 78 // Compose< Diagonal, BB > specialization 79 template <class Field, class Trait, class BB> 80 struct GetEntryCategory<Compose<Diagonal<Field, Trait>,BB> > { typedef GetEntryTags::SpecialCDB Tag; }; 66 81 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) 82 template <class Field, class Trait, class BB> 83 typename 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) 117 84 { 118 85 typename Field::Element y; 119 86 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); 122 89 } 123 90 124 91 // 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) 92 template <class BB, class Field, class Trait> 93 struct GetEntryCategory<Compose<BB, Diagonal<Field, Trait> > > { typedef GetEntryTags::SpecialCBD Tag; }; 94 95 template <class BB, class Field, class Trait> 96 typename 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) 127 97 { 128 98 typename Field::Element y; 129 99 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); 132 102 } 133 103 134 104 // Compose< Diagonal, Diagonal > specialization 135 105 template <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) 106 struct GetEntryCategory<Compose<Diagonal<Field, T1>,Diagonal<Field, T2> > > { typedef GetEntryTags::SpecialCDD Tag; }; 107 108 template <class Field, class T1, class T2> 109 typename 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) 137 110 { 138 111 if (i != j) 139 return A.field().init( t, 0UL);112 return A.field().init(x, 0UL); 140 113 else { 141 114 typename Field::Element y; 142 115 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); 145 118 } 146 119 } -
trunk/linbox/linbox/solutions/trace.h
r2204 r3005 12 12 13 13 #include <vector> 14 //#include <algorithm>15 14 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"24 15 #include "linbox/blackbox/scalar-matrix.h" 25 #include "linbox/solutions/methods.h"16 //#include "linbox/blackbox/toeplitz.h" 26 17 #include "linbox/solutions/getentry.h" 27 #include "linbox/blackbox/diagonal.h"28 #include "linbox/blackbox/compose.h"29 18 30 19 namespace LinBox 31 20 { 32 21 22 // Trait to show whether or not the BB class has a local trace function. 23 template<class BB> struct TraceCategory; 33 24 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 25 namespace TraceTags { struct Generic{}; struct Local{}; }; 26 27 template<class BB> struct TraceCategory { typedef typename TraceTags::Generic Tag; }; 28 29 template<class Field> 30 struct 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. 38 37 39 Also sum of diagonal entries. 40 This is the generic one. 38 Also it is the sum of the diagonal entries. 41 39 42 testing multiple doc comments. 40 Runtime on n by n matrix is n times the cost of getEntry(). 41 This is linear in n for those classes where getEntry is constant time 42 (eg DenseMatrix and SparseMatrix). 43 Trace is constant time when the diagonal is necessarily constant, eg. for ScalarMatrix and Toeplitz. 44 Worst case time is cost of n blackbox applies (matrix vector products), and apply cost typically ranges between O(n) and O(n^2). 45 43 46 */ 44 47 template <class BB> 45 48 typename 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 } 47 52 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. 54 If getEntry is constant time on n by n BB's, trace will be in O(n). 52 55 */ 53 54 56 template <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; 57 typename 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); 64 60 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)); 69 63 return t; 70 64 } 71 65 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. 67 Allows constant time trace for, eg., ScalarMatrix, Toeplitz. 68 */ 69 template <class BB> 70 typename BB::Field::Element & trace(typename BB::Field::Element & t, const BB& A, TraceTags::Local tt) 101 71 { return A.trace(t); } 102 72 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 196 74 #endif // __TRACE_H -
trunk/linbox/tests/test-getentry.C
r2814 r3005 23 23 #include "linbox/field/modular-int32.h" 24 24 #include "linbox/solutions/getentry.h" 25 #include "linbox/blackbox/compose.h" 25 26 #include "linbox/blackbox/diagonal.h" 26 27 #include "linbox/blackbox/scalar-matrix.h" … … 31 32 using namespace LinBox; 32 33 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 */ 35 template <class Field> 36 bool 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 */ 44 53 template <class Field> 45 54 bool testScalarMatrixgetEntry (const Field &F, size_t n) … … 49 58 ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); 50 59 report << "scalarmatrix getEntry test (using specialization)" << endl; 51 typename Field::Element s, t, r, th;60 typename Field::Element s, t, r, th; 52 61 F.init(r, 0); 53 62 F.init(s, 2); … … 71 80 } 72 81 82 /* getEntry of sparse matrix */ 73 83 template <class Field> 74 84 bool testSparseMatrixgetEntry (const Field &F, size_t n) … … 117 127 } 118 128 129 /* getEntry of dense matrix */ 119 130 template <class Field> 120 131 static bool testDenseMatrixgetEntry (const Field &F, size_t n) … … 161 172 } 162 173 174 /* getEntry of diagonal matrix */ 163 175 template <class Field> 164 176 static bool testDiagonalgetEntry (const Field &F, VectorStream<vector<typename Field::Element> > &stream) … … 225 237 } 226 238 239 /* getEntry of composed blackbox with diagonal component*/ 240 template <class Field> 241 bool 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 } 227 275 int main (int argc, char **argv) 228 276 { … … 252 300 RandomDenseStream<Field, Vector> stream (F, n, iterations); 253 301 302 if (!testGenericBBgetEntry (F, n)) pass = false; 254 303 if (!testScalarMatrixgetEntry (F, n)) pass = false; 255 304 if (!testSparseMatrixgetEntry (F, n)) pass = false; 256 305 if (!testDenseMatrixgetEntry (F, n)) pass = false; 257 306 if (!testDiagonalgetEntry (F, stream)) pass = false; 307 if (!testSpecialCDgetEntry (F, n)) pass = false; 258 308 259 309 commentator.stop("getEntry solution test suite"); -
trunk/linbox/tests/test-trace.C
r2814 r3005 2 2 3 3 /* tests/test-trace.C 4 * Copyright (C) 2002 Bradford Hovinen4 * Copyright (C) -bds 5 5 * 6 * Written by Bradford Hovinen <hovinen@cis.udel.edu>6 * Earlier version by Bradford Hovinen <hovinen@cis.udel.edu> 7 7 * 8 8 * -------------------------------------------------------- … … 14 14 15 15 #include <iostream> 16 #include <fstream>17 16 #include <vector> 18 #include <cstdio>19 17 20 18 #include "test-common.h" 21 19 22 #include "linbox/util/commentator.h"23 20 #include "linbox/field/modular-int.h" 24 21 #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"31 22 32 23 using namespace LinBox; 33 34 /* Test 1: Trace of random diagonal matrix35 *36 * Construct a random diagonal matrix and check that its computed trace is the37 * same as the sum of its entries38 *39 * F - Field over which to perform computations40 * stream - Stream that comprises source of diagonal vectors41 *42 * Return true on success and false on failure43 */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
