bool testRandomApply1 (Field &F, const char *text, unsigned int iterations, VectorStream<Row> &A_stream) { typedef SparseMatrix <Field, Row> Blackbox; ostringstream str; str << "Testing sparse random apply (1, " << text << ")" << ends; commentator().start (str.str ().c_str (), "testRandomApply1", iterations); bool ret = true; bool iter_passed; size_t i, k; VectorDomain<Field> VD (F); StandardBasisStream<Field, Vector> stream (F, A_stream.n ()); Vector e_j, w; VectorWrapper::ensureDim (e_j, A_stream.n ()); VectorWrapper::ensureDim (w, A_stream.m ()); for (i = 0; i < iterations; i++) { commentator().startIteration ((unsigned)i); iter_passed = true; Blackbox A (F, A_stream); A_stream.reset (); ostream &report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION); report << "Matrix:" << endl; A.write (report, FORMAT_PRETTY); stream.reset (); while (stream) { stream.next (e_j); A.apply (w, e_j); for (k = 0; k < A_stream.m (); k++) if (!F.areEqual (A.getEntry (k, stream.j () - 1), VectorWrapper::constRef<Field> (w, k))) ret = iter_passed = false; report << "Output vector " << stream.j () << ": "; VD.write (report, w) << endl; } if (!iter_passed) commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Output vectors were incorrect" << endl; commentator().stop ("done"); commentator().progress (); } commentator().stop (MSG_STATUS (ret), (const char *) 0, "testRandomApply1"); return ret; }
bool runSparseMatrixTests (const Field &F, const char *desc, int iterations, VectorStream<Row> &A_stream) { typedef std::vector <typename Field::Element> DenseVector; typedef std::vector <pair <size_t, typename Field::Element> > SparseSeqVector; typedef std::map <size_t, typename Field::Element> SparseMapVector; typedef std::pair <std::vector<size_t>, std::vector<typename Field::Element> > SparseParVector; bool pass = true; ostringstream str1, str2, str3, str4, str5; str1 << "Testing sparse matrix with " << desc << " row type" << ends; commentator().start (str1.str ().c_str (), "runSparseMatrixTests", 4); str2 << desc << "/dense" << ends; str3 << desc << "/sparse sequence" << ends; str4 << desc << "/sparse associative" << ends; str5 << desc << "/sparse parallel" << ends; RandomDenseStream<Field, DenseVector> dense_stream1 (F, A_stream.n (), iterations); RandomDenseStream<Field, DenseVector> dense_stream2 (F, A_stream.m (), iterations); #if 0 RandomSparseStream<Field, SparseSeqVector> sparse_seq_stream1 (F, 0.1, A_stream.n (), iterations); RandomSparseStream<Field, SparseSeqVector> sparse_seq_stream2 (F, 0.1, A_stream.m (), iterations); RandomSparseStream<Field, SparseMapVector> sparse_map_stream1 (F, 0.1, A_stream.n (), iterations); RandomSparseStream<Field, SparseMapVector> sparse_map_stream2 (F, 0.1, A_stream.m (), iterations); RandomSparseStream<Field, SparseParVector> sparse_par_stream1 (F, 0.1, A_stream.n (), iterations); RandomSparseStream<Field, SparseParVector> sparse_par_stream2 (F, 0.1, A_stream.m (), iterations); #endif if (!runSparseMatrixTestsByVector (F, str2.str ().c_str (), iterations, dense_stream1, dense_stream2, A_stream)) pass = false; #if 0 commentator().progress (); if (!runSparseMatrixTestsByVector (F, str2.str ().c_str (), iterations, sparse_seq_stream1, sparse_seq_stream2, A_stream)) pass = false; commentator().progress (); if (!runSparseMatrixTestsByVector (F, str2.str ().c_str (), iterations, sparse_map_stream1, sparse_map_stream2, A_stream)) pass = false; commentator().progress (); if (!runSparseMatrixTestsByVector (F, str2.str ().c_str (), iterations, sparse_par_stream1, sparse_par_stream2, A_stream)) pass = false; commentator().progress (); #endif commentator().stop (MSG_STATUS (pass), (const char *) 0, "runSparseMatrixTests"); return pass; }
static bool testRandomLinearity (Field &F, const char *text, VectorStream<Row> &A_stream, VectorStream<Vector> &stream1, VectorStream<Vector> &stream2) { typedef SparseMatrix <Field, Row> Blackbox; ostringstream str; str << "Testing linearity (" << text << ")" << ends; commentator().start (str.str ().c_str (), "testRandomLinearity", stream1.m ()); Blackbox A (F, A_stream); A_stream.reset (); ostream &report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION); report << "Input matrix:" << endl; A.write (report, FORMAT_PRETTY); bool ret = testLinearity (F, A, stream1, stream2); stream1.reset (); stream2.reset (); commentator().stop (MSG_STATUS (ret), (const char *) 0, "testRandomLinearity"); return ret; }
static bool testIdentityApply (Field &F, const char *text, VectorStream<Vector> &stream) { typedef SparseMatrix <Field, Row> Blackbox; ostringstream str; str << "Testing identity apply (" << text << ")" << ends; commentator().start (str.str ().c_str (), "testIdentityApply", stream.m ()); bool ret = true; bool iter_passed = true; VectorDomain<Field> VD (F); StandardBasisStream<Field, Row> f1 (F, stream.n ()); Blackbox A (F, f1); ostream &report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION); report << "Matrix:" << endl; A.write (report, FORMAT_PRETTY); Vector v, w; VectorWrapper::ensureDim (v, stream.n ()); VectorWrapper::ensureDim (w, stream.n ()); while (stream) { commentator().startIteration ((unsigned)stream.j ()); iter_passed = true; stream.next (v); ostream &Report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION); Report << "Input vector: "; VD.write (Report, v); Report << endl; A.apply (w, v); Report << "Output vector: "; VD.write (Report, w); Report << endl; if (!VD.areEqual (v, w)) ret = iter_passed = false; if (!iter_passed) commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Vectors are not equal" << endl; commentator().stop ("done"); commentator().progress (); } stream.reset (); commentator().stop (MSG_STATUS (ret), (const char *) 0, "testIdentityApply"); return ret; }
static bool testIdentitySolve (const Field &F, VectorStream<Vector> &stream, const char *text, MethodTraits method) { typedef ScalarMatrix <Field> Blackbox; ostringstream str; str << "Testing identity solve (" << text << ")"; commentator().start (str.str ().c_str (), "testIdentitySolve", stream.m ()); bool ret = true; VectorDomain<Field> VD (F); typename Field::Element s; F.assign (s, F.one); Blackbox I (F, stream.n (), stream.n(), s); size_t n = stream.n(); Vector v(F,n), w(F,n); // VectorWrapper::ensureDim (v, stream.n ()); // VectorWrapper::ensureDim (w, stream.n ()); MethodTraits traits (method); while (stream) { commentator().startIteration ((unsigned)stream.j ()); bool iter_passed = true; stream.next (v); ostream &report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); report << "Input vector: "; VD.write (report, v); report << endl; try { solve (w, I, v, method); //solve (I, w, v, F, traits); } catch (SolveFailed) { commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Solve failed to solve system" << endl; ret = iter_passed = false; } catch (InconsistentSystem<Vector> e) { commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Solve reported an inconsistent system" << endl; ret = iter_passed = false; } if (iter_passed) { report << "Output vector: "; VD.write (report, w); report << endl; if (!VD.areEqual (w, v)) ret = iter_passed = false; if (!iter_passed) commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Vectors are not equal" << endl; } commentator().stop ("done"); commentator().progress (); } stream.reset (); commentator().stop (MSG_STATUS (ret), (const char *) 0, "testIdentitySolve"); return ret; }
static bool testSingularPreconditionedSolve (const Field &F, VectorStream<SparseVector> &stream1, VectorStream<Vector> &stream2, const char *text, Method::Wiedemann::Preconditioner preconditioner) { ostringstream str; str << "Testing singular preconditioned solve (" << text << ")"; commentator().start (str.str ().c_str (), "testSingularPreconditionedSolve", stream1.m ()); VectorDomain<Field> VD (F); typename WiedemannSolver<Field>::ReturnStatus status; bool ret = true; SparseVector d1; typename Field::Element uTb; typename LinBox::Vector<Field>::Dense d; Vector b, x, y, u; VectorWrapper::ensureDim (d, stream2.dim ()); VectorWrapper::ensureDim (b, stream2.dim ()); VectorWrapper::ensureDim (x, stream2.dim ()); VectorWrapper::ensureDim (y, stream2.dim ()); Method::Wiedemann traits; traits.preconditioner (preconditioner); while (stream1 && stream2) { commentator().startIteration ((unsigned)stream1.j ()); stream1.next (d1); stream2.next (b); VD.copy (d, d1); ostream &report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION); report << "Diagonal entries: "; VD.write (report, d); report << endl; report << "Right-hand side: "; VD.write (report, b); report << endl; BlasVector<Field> dd(F,d); Diagonal<Field> A (dd); //Diagonal<Field> A (F, d); status = solve (A, x, b, u, F, traits); if (status == WiedemannSolver<Field>::INCONSISTENT) { A.applyTranspose (y, u); report << "Certificate of inconsistency found." << endl; report << "Certificate is: "; VD.write (report, u) << endl; report << "u^T A = "; VD.write (report, y) << endl; VD.dot (uTb, u, b); report << "u^T b = "; F.write (report, uTb) << endl; if (!VD.isZero (y)) { commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: u is not in the right nullspace of D" << endl; ret = false; } if (F.isZero (uTb)) { commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: u^T b = 0" << endl; ret = false; } } else if (status == WiedemannSolver<Field>::FAILED) { commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Solver refused to certify inconsistency" << endl; ret = false; } else { commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Solver gave solution even though system is inconsistent" << endl; ret = false; } commentator().stop ("done"); commentator().progress (); } stream1.reset (); stream2.reset (); commentator().stop (MSG_STATUS (ret), (const char *) 0, "testSingularPreconditionedSolve"); return ret; }
static bool testSingularConsistentSolve (const Field &F, unsigned int n, VectorStream<Vector> &stream1, VectorStream<Vector> &stream2, const char *text, MethodTraits method) { typedef Diagonal <Field, Vector> Blackbox; ostringstream str; str << "Testing singular consistent solve (" << text << ")"; commentator().start (str.str ().c_str (), "testSingularConsistentSolve", stream1.m ()); VectorDomain<Field> VD (F); bool ret = true; Vector d1, b1, d, b, x, y; VectorWrapper::ensureDim (d, n); VectorWrapper::ensureDim (b, n); VectorWrapper::ensureDim (x, n); VectorWrapper::ensureDim (y, n); VectorWrapper::ensureDim (d1, n); VectorWrapper::ensureDim (b1, n); MethodTraits traits (method); traits.preconditioner (MethodTraits::NO_PRECONDITIONER); while (stream1 && stream2) { commentator().startIteration ((unsigned)stream1.j ()); ActivityState state = commentator().saveActivityState (); stream1.next (d1); stream2.next (b1); VD.copy (d, d1); VD.copy (b, b1); ostream &report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); report << "Diagonal entries: "; VD.write (report, d); report << endl; report << "Right-hand side: "; VD.write (report, b); report << endl; BlasVector<Field> dd(F,d); Blackbox D (dd); //Blackbox D (d); try { bool iter_passed = true; solve (D, x, b, F, traits); report << "System solution: "; VD.write (report, x); report << endl; D.apply (y, x); report << "Output: "; VD.write (report, y); report << endl; if (!VD.areEqual (y, b)) ret = iter_passed = false; if (!iter_passed) commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Computed solution is incorrect" << endl; } catch (SolveFailed) { commentator().restoreActivityState (state); commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: System solution failed" << endl; ret = false; } catch (InconsistentSystem<Vector> e) { commentator().restoreActivityState (state); ostream &Report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR); Report << "ERROR: Inconsistent system exception" << endl; Report << "Certificate is: "; VD.write (Report, e.u ()) << endl; ret = false; commentator().restoreActivityState (state); } commentator().stop ("done"); commentator().progress (); } stream1.reset (); stream2.reset (); commentator().stop (MSG_STATUS (ret), (const char *) 0, "testSingularConsistentSolve"); return ret; }
static bool testNonsingularSolve (const Field &F, VectorStream<Vector> &stream1, VectorStream<Vector> &stream2, const char *text, MethodTraits method) { typedef Diagonal <Field> Blackbox; ostringstream str; str << "Testing nonsingular solve (" << text << ")"; commentator().start (str.str ().c_str (), "testNonsingularSolve", stream1.m ()); VectorDomain<Field> VD (F); bool ret = true; size_t n= stream1.n (); Vector d(F,n), b(F,n), x(F,n), y(F,n); // VectorWrapper::ensureDim (d, stream1.n ()); // VectorWrapper::ensureDim (b, stream1.n ()); // VectorWrapper::ensureDim (x, stream1.n ()); // VectorWrapper::ensureDim (y, stream1.n ()); MethodTraits traits (method); while (stream1 && stream2) { commentator().startIteration ((unsigned)stream1.j ()); ActivityState state = commentator().saveActivityState (); bool iter_passed = true; stream1.next (d); stream2.next (b); ostream &report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); report << "Diagonal entries: "; VD.write (report, d); report << endl; report << "Right-hand side: "; VD.write (report, b); report << endl; BlasVector<Field> dd(F,d); Blackbox D (dd); try { solve (x, D, b, method); } catch (SolveFailed) { commentator().restoreActivityState (state); commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: System solution failed" << endl; ret = iter_passed = false; } catch (InconsistentSystem<Vector> e) { commentator().restoreActivityState (state); commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: solve reported inconsistent system" << endl; ret = iter_passed = false; } if (iter_passed) { report << "System solution: "; VD.write (report, x); report << endl; D.apply (y, x); report << "Output: "; VD.write (report, y); report << endl; if (!VD.areEqual (y, b)) ret = iter_passed = false; if (!iter_passed) commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Computed solution is incorrect" << endl; } commentator().stop ("done"); commentator().progress (); } stream1.reset (); stream2.reset (); commentator().stop (MSG_STATUS (ret), (const char *) 0, "testNonsingularSolve"); return ret; }
bool testRandomApply2 (Field &F, const char *text, unsigned int iterations, VectorStream<Row> &A_stream) { typedef SparseMatrix <Field, Row> Blackbox; ostringstream str; str << "Testing sparse random apply (2, " << text << ")" << ends; commentator().start (str.str ().c_str (), "testRandomApply2", iterations); bool ret = true; bool iter_passed; size_t i, j, k; VectorDomain<Field> VD (F); typename Field::RandIter r (F); typename Field::Element sum; integer c; // long width; F.characteristic (c); // width = logp (c, 10) + 1; Vector v, w; VectorWrapper::ensureDim (v, A_stream.n ()); VectorWrapper::ensureDim (w, A_stream.m ()); for (k = 0; k < A_stream.n (); k++) F.init (VectorWrapper::ref<Field> (v, k), 1); for (i = 0; i < iterations; i++) { commentator().startIteration ((unsigned)i); iter_passed = true; Blackbox A (F, A_stream); A_stream.reset (); ostream &report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION); report << "Matrix:" << endl; A.write (report, FORMAT_PRETTY); A.apply (w, v); for (j = 0; j < A_stream.m (); j++) { F.init (sum, 0); for (k = 0; k < A_stream.n (); k++) F.addin (sum, A.getEntry (j, k)); if (!F.areEqual (sum, VectorWrapper::constRef<Field> (w, j))) ret = iter_passed = false; } report << "Output vector: "; VD.write (report, w) << endl; if (!iter_passed) commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Output vector was incorrect" << endl; commentator().stop ("done"); commentator().progress (); } commentator().stop (MSG_STATUS (ret), (const char *) 0, "testRandomApply2"); return ret; }
static bool testNilpotentApply (Field &F, const char *text, VectorStream<Vector> &stream) { typedef SparseMatrix <Field, Row> Blackbox; ostringstream str; str << "Testing nilpotent apply (" << text << ")" << ends; commentator().start (str.str ().c_str (), "testNilpotentApply", stream.m ()); bool ret = true; bool even, iter_passed; StandardBasisStream<Field, Row> f1 (F, stream.n ()); Row tmp; f1.next (tmp); // Small trick: pull the first vector out to shift elements up one row Blackbox A (F, f1); ostream &report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION); report << "Matrix:" << endl; A.write (report, FORMAT_PRETTY); size_t j; NonzeroRandIter<Field> r (F, typename Field::RandIter (F)); VectorDomain<Field> VD (F); Vector v, w; VectorWrapper::ensureDim (v, stream.n ()); VectorWrapper::ensureDim (w, stream.n ()); while (stream) { commentator().startIteration ((unsigned)stream.j ()); iter_passed = true; even = false; stream.next (v); // Make sure last element is nonzero r.random (VectorWrapper::ref<Field> (v, stream.n () - 1)); ostream &Report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION); Report << "Input vector: "; VD.write (Report, v); Report << endl; commentator().start ("Applying vectors"); for (j = 0; j < stream.n () - 1; j++, even = !even) if (even) A.apply (v, w); else A.apply (w, v); commentator().stop ("Done"); Report << "A^(n-1) v: "; VD.write (Report, even ? w : v); Report << endl; if (VD.isZero (even ? w : v)) { ret = false; commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: A^(n-1) v is prematurely zero" << endl; } if (even) A.apply (v, w); else A.apply (w, v); Report << "A^n v: "; VD.write (Report, even ? v : w); Report << endl; if (!VD.isZero (even ? v : w)) ret = iter_passed = false; if (!iter_passed) commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: A^n v is non-zero" << endl; commentator().stop ("done"); commentator().progress (); } stream.reset (); commentator().stop (MSG_STATUS (ret), (const char *) 0, "testNilpotentApply"); return ret; }
static bool testZeroApply (Field1 &F1, Field2 &F2, VectorStream<Vector> &stream1, VectorStream<Vector> &stream2) { commentator().start ("Testing zero apply", "testZeroApply", stream1.m ()); bool ret = true; bool iter_passed = true; Vector d1, d2, v, w, zero; VectorDomain<Field1> VD (F1); typename Field1::Element neg_one; VectorWrapper::ensureDim (zero, stream1.dim ()); VectorWrapper::ensureDim (d1, stream1.dim ()); VectorWrapper::ensureDim (d2, stream1.dim ()); VectorWrapper::ensureDim (v, stream1.dim ()); VectorWrapper::ensureDim (w, stream2.dim ()); // F.init (neg_one, 1); // F.negin (neg_one); F1.init (neg_one, -1); while (stream1) { commentator().startIteration ((unsigned)stream1.j ()); iter_passed = true; stream1.next (d1); VD.mul (d2, d1, neg_one); Diagonal <Field1> D1 (F1, d1), D2 (F1, d2); Sum <Diagonal<Field1>,Diagonal <Field1> > A (&D1, &D2); ostream &report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); report << "Diagonal matrix: "; VD.write (report, d1); report << endl; report << "Negative diagonal matrix: "; VD.write (report, d2); report << endl; stream2.reset (); while (stream2) { stream2.next (w); report << "Input vector: "; VD.write (report, w); report << endl; A.apply (v, w); report << "Output vector: "; VD.write (report, v); report << endl; if (!VD.isZero (v)) ret = iter_passed = false; } if (!iter_passed) commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) << "ERROR: Vector is not zero" << endl; commentator().stop ("done"); commentator().progress (); ret = ret && testBBrebind(F2, A); } commentator().stop (MSG_STATUS (ret), (const char *) 0, "testZeroApply"); return ret; }