Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
static bool testTransposeBlackbox(Blackbox & A)
{
	typedef typename Blackbox::Field Field;
	commentator().start ("Testing Transpose", "testTranspose", 1);

	Transpose<Blackbox> B(A);

	bool ret = true, ret1;

	size_t m = A.rowdim(), n = A.coldim();
	const Field & F = A.field();
	VectorDomain<Field> VD (F);
	BlasVector<Field> x(F,n), y(F,m), z(F,n), w(F,m);

	VD.random(x);
	A.apply(y, x);
	B.applyTranspose(w, x);
	ret1 = VD.areEqual(y, w);
	if (not ret1) commentator().report() << "A and B^T disagree, FAIL" << std::endl;
	ret = ret and ret1;

	VD.random(y);
	A.applyTranspose(x, y);
	B.apply(z, y);
	ret1 = VD.areEqual(x, z);
	if (not ret1) commentator().report() << "A^T and B disagree, FAIL" << std::endl;
	ret = ret and ret1;

	ret1 = testBlackboxNoRW(B);
	if (not ret1) commentator().report() << "testBlackbox A^T FAIL" << std::endl;
	ret = ret and ret1;

	commentator().stop (MSG_STATUS (ret), (const char *) 0, "testTranspose");

	return ret;
}
bool testRandomSolve (const Ring& R,
		      const Field& f,
		      LinBox::VectorStream<Vector>& stream1,
		      LinBox::VectorStream<Vector>& stream2)
{


	std::ostringstream str;



	commentator().start ("Testing Nonsingular Random Diagonal solve ", "testNonsingularRandomDiagonalSolve");

	bool ret = true;

        bool iter_passed = true;

	VectorDomain<Ring> VD (R);

	Vector d, b, x, y;

	VectorWrapper::ensureDim (d, stream1.n ());
        VectorWrapper::ensureDim (b, stream1.n ());
        VectorWrapper::ensureDim (x, stream1.n ());
        VectorWrapper::ensureDim (y, stream1.n ());

	int n = (int)d. size();

	while (stream1 && stream2) {

		commentator().startIteration ((unsigned)stream1.j ());

                //ActivityState state = commentator().saveActivityState ();

                iter_passed = true;

		bool zeroEntry;
		do {
		  stream1.next (d);
		  zeroEntry = false;
		  for (size_t i=0; i<stream1.n(); i++)
		    zeroEntry |= R.isZero(d[i]);
		} while (zeroEntry);

                stream2.next (b);

                std::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;

                //Diagonal<Ring> D(R, d);

		BlasMatrix<Ring> D(R, n, n);

		for(int i = 0; i < n; ++i) R.init (D[i][i],  d[i]);

		typedef RationalSolverAdaptive RSolver;
		RSolver rsolver;

		//std::vector<std::pair<typename Ring::Element, typename Ring::Element> > answer(n);
		std::vector<typename Ring::Element> num(n);
		typename Ring::Element den;

		SolverReturnStatus solveResult = rsolver.solveNonsingular(num, den, D, b); //often 5 primes are not enough

#if 0
		typename Ring::Element lden;

		R. init (lden, 1);

		typename std::vector<std::pair<typename Ring::Element, typename Ring::Element> >::iterator p;

		for (p = answer.begin(); p != answer.end(); ++ p)
			R. lcm (lden, lden, p->second);
		typename Vector::iterator p_x;
		//typename Vector::iterator p_y;
#endif

		if (solveResult == SS_OK) {
#if 0
		  for (p = answer.begin(), p_x = x. begin();
		       p != answer.end();
		       ++ p, ++ p_x) {

		    R. mul (*p_x, p->first, lden);

		    R. divin (*p_x, p->second);

		  }

		  D. apply (y, x);
#endif
		  D. apply (y, num);

		  VD. mulin(b, den);

		  if (!VD.areEqual (y, b)) {
		    ret = iter_passed = false;
		    commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR)
		      << "ERROR: Computed solution is incorrect" << endl;
		  }
		}
		else {
		    ret = iter_passed = false;
		    commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR)
		      << "ERROR: Did not return OK solving status" << endl;
		}

		commentator().stop ("done");
                commentator().progress ();

	}


	stream1.reset ();
        stream2.reset ();

        commentator().stop (MSG_STATUS (ret), (const char *) 0, "testNonsingularRandomDiagonalSolve");

	return ret;
}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
0
static bool testRandomSolve (const Field                  &F,
			     VectorStream<Vector1>        &A_stream,
			     VectorStream<Vector2>        &b_stream,
			     const char                   *text,
			     MethodTraits                  method)
{
	ostringstream str;
	str << "Testing random solve (" << text << ")";

	commentator().start (str.str ().c_str (), "testRandomSolve", b_stream.size ());

	bool ret = true;

	VectorDomain<Field> VD (F);
	MatrixDomain<Field> MD (F);

	Vector2 b, x, ATAx, ATb;

	VectorWrapper::ensureDim (b, b_stream.dim ());
	VectorWrapper::ensureDim (x, A_stream.dim ());
	VectorWrapper::ensureDim (ATAx, A_stream.dim ());
	VectorWrapper::ensureDim (ATb, A_stream.dim ());

	SparseMatrix<Field> A (F, A_stream);
	SparseMatrix<Field> AT (F,A.coldim (), A.rowdim ());
	BlasMatrix<Field> ATA (A.coldim (), A.coldim ());

	A.transpose (AT);

	MD.mul (ATA, AT, A);

	ostream &report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION);
	report << "Input matrix A:" << endl;
	A.write (report);

	report << "Matrix A^T A:" << endl;
	MD.write (report, ATA);

	MethodTraits traits (method);

	while (b_stream) {
		commentator().startIteration ((unsigned)b_stream.pos ());

		bool iter_passed = true;

		b_stream >> b;

		ostream &Report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION);
		Report << "Right-hand side b:";
		VD.write (Report, b) << endl;

		try {
			solve (A, x, b, F, traits);
		}
		catch (SolveFailed) {
			commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR)
				<< "ERROR: Solve failed to solve system" << endl;
			ret = iter_passed = false;
		}
		catch (InconsistentSystem<Vector2> 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 x: ";
			VD.write (Report, x) << endl;

			MD.vectorMul (ATAx, ATA, x);
			MD.vectorMul (ATb, AT, b);

			if (!VD.areEqual (ATAx, ATb)) {
				commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR)
					<< "ERROR: A^T Ax != A^T b" << endl;
				ret = iter_passed = false;
			}
		}

		commentator().stop ("done");
		commentator().progress ();
	}

	A_stream.reset ();
	b_stream.reset ();

	commentator().stop (MSG_STATUS (ret), (const char *) 0, "testRandomSolve");

	return ret;
}
Exemplo n.º 6
0
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;
}
Exemplo n.º 7
0
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;
}