Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
bool testQLUP(const Field &F, size_t n, unsigned int iterations, int rseed, double sparsity = 0.05)
{
	bool res = true;

	commentator().start ("Testing Sparse elimination qlup", "testQLUP", iterations);

	size_t Ni = n;
	size_t Nj = n;
	integer card; F.cardinality(card);
	typename Field::RandIter generator (F,card,rseed);
	RandStream stream (F, generator, sparsity, n, n);

	for (size_t i = 0; i < iterations; ++i) {
		commentator().startIteration ((unsigned)i);


		stream.reset();

		Blackbox A (F, stream);

		std::ostream & report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION);

		F.write( report ) << endl;
		A.write( report,Tag::FileFormat::Maple ) << endl;

		DenseVector<Field> u(F,Nj), v(F,Ni), w1(F,Nj), w2(F,Ni), w3(F,Ni), w(F,Ni);
		for(auto it=u.begin();it!=u.end();++it)
			generator.random (*it);


		A.apply(v,u);


		unsigned long rank;

		Method::SparseElimination SE;
		SE.strategy(Specifier::PIVOT_LINEAR);
		GaussDomain<Field> GD ( F );
		typename Field::Element determinant;
		Blackbox L(F, A.rowdim(), A.coldim());
		Permutation<Field> Q((int)A.rowdim(),F);
		Permutation<Field> P((int)A.coldim(),F);

		GD.QLUPin(rank, determinant,
			  Q, L, A, P,
			  A.rowdim(), A.coldim() );

		Q.apply(w, L.apply(w3, A.apply(w2, P.apply(w1,u) ) ) );

		bool error = false;
		auto itv=v.begin();
		auto itw=w.begin();
		for( ; itw!=w.end();++itw,++itv) {
			if (! F.areEqual(*itw,*itv) ) {
				error = true;
			}
		}

		if (error) {
			res = false;

			report << "ERROR : matrix(" << u.size() << ",1,[";
			for(auto itu=u.begin(); itu!=u.end();++itu)
				report << *itu << ',';
			report << "]);\n[";
			for(auto itv2=v.begin(); itv2!=v.end();++itv2)
				report << *itv2 << ' ';
			report << "]  !=  [";
			for(auto itw2=w.begin(); itw2!=w.end();++itw2)
				report << *itw2 << ' ';
			report << "]" << std::endl;


			report << "w1: [";
			for(auto itw2=w1.begin(); itw2!=w1.end();++itw2)
				report << *itw2 << ' ';
			report << "]" << std::endl;
			report << "w2: [";
			for(auto itw2=w2.begin(); itw2!=w2.end();++itw2)
				report << *itw2 << ' ';
			report << "]" << std::endl;
			report << "w3: [";
			for(auto itw2=w3.begin(); itw2!=w3.end();++itw2)
				report << *itw2 << ' ';
			report << "]" << std::endl;
		}

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

	commentator().stop (MSG_STATUS (res), (const char *) 0, "testQLUP");

	return res;
}
Ejemplo n.º 5
0
bool testQLUPnullspace(const Field &F, size_t n, unsigned int iterations, int rseed, double sparsity = 0.05)
{
	bool res = true;

	commentator().start ("Testing Sparse elimination qlup nullspacebasis", "testQLUPnullspace", iterations);

	size_t Ni = n;
	size_t Nj = n;
	integer card; F.cardinality(card);
	typename Field::RandIter generator (F,card,rseed);
	RandStream stream (F, generator, sparsity, n, n, rseed);

	for (size_t i = 0; i < iterations; ++i) {
		commentator().startIteration ((unsigned)i);

		stream.reset();
		Blackbox A (F, stream);

		std::ostream & report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION);

		F.write( report ) << endl;
		A.write( report, Tag::FileFormat::Maple ) << endl;


		Method::SparseElimination SE;
		SE.strategy(Specifier::PIVOT_LINEAR);
		GaussDomain<Field> GD ( F );

		Blackbox CopyA ( A );
		Blackbox X(F, A.coldim(), A.coldim() );

		GD.nullspacebasisin(X, CopyA );

		size_t nullity = X.coldim();

		DenseVector<Field> u(F,nullity);
		for(auto it=u.begin();it!=u.end();++it)
			generator.random (*it);
		DenseVector<Field> v(F,Nj);
		X.apply(v,u);
		report << "Random combination of the rows of the NullSpace basis" << std::endl;

		DenseVector<Field> w(F,Ni);
		A.apply(w, v);

		VectorDomain<Field> VD(F);

		if (! VD.isZero(w)) {
			res=false;
			A.write( report, Tag::FileFormat::Maple ) << endl;

			report << "ERROR u: matrix(" << u.size() << ",1,[";
			for(auto itu=u.begin(); itu!=u.end();++itu)
				report << *itu << ',';
			report << "]);\n[";
			report << "ERROR v: matrix(" << v.size() << ",1,[";
			for(auto itu=v.begin(); itu!=v.end();++itu)
				report << *itu << ',';
			report << "]);\n[";
			for(auto itv=w.begin(); itv!=w.end();++itv)
				report << *itv << ' ';
			report << "]  !=  0" << std::endl;

		}

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

	commentator().stop (MSG_STATUS (res), (const char *) 0, "testQLUPnullspace");

	return res;
}
Ejemplo n.º 6
0
bool testQLUPsolve(const Field &F, size_t n, unsigned int iterations, int rseed, double sparsity = 0.05)
{
	bool res = true;

	commentator().start ("Testing Sparse elimination qlup solve", "testQLUPsolve", iterations);

	size_t Ni = n;
	size_t Nj = n;
	integer card; F.cardinality(card);
	typename Field::RandIter generator (F,card,rseed);
	RandStream stream (F, generator, sparsity, n, n);

	GF2 F2;
	GF2::RandIter bitgenerator(F2,2,rseed);
	// GF2::Element randomsolve;

	for (size_t i = 0; i < iterations; ++i) {
		commentator().startIteration ((unsigned)i);

		stream.reset();
		Blackbox A (F, stream);

		std::ostream & report = commentator().report (Commentator::LEVEL_UNIMPORTANT, INTERNAL_DESCRIPTION);

		F.write( report ) << endl;
		A.write( report, Tag::FileFormat::Maple ) << endl;

		DenseVector<Field> u(F,Nj), v(F,Ni), x(F,Nj), y(F,Ni);
		for(auto it=u.begin();it!=u.end();++it)
			generator.random (*it);

		A.apply(v,u);


		Method::SparseElimination SE;
		SE.strategy(Specifier::PIVOT_LINEAR);
		GaussDomain<Field> GD ( F );

		Blackbox CopyA ( A );

		GD.solvein(x, A, v /*, bitgenerator .random(randomsolve) */ );
		// report << "Random solving: " << randomsolve << std::endl;

		CopyA.apply(y, x);

		VectorDomain<Field> VD(F);


		if (! VD.areEqual(v,y)) {
			res=false;
			A.write( report, Tag::FileFormat::Maple ) << endl;

			report << "ERROR v: matrix(" << v.size() << ",1,[";
			for(auto itu=v.begin(); itu!=v.end();++itu)
				report << *itu << ',';
			report << "]);\n[";
			report << "ERROR y: matrix(" << y.size() << ",1,[";
			for(auto itu=y.begin(); itu!=y.end();++itu)
				report << *itu << ',';
			report << "]);\n[";
			for(auto itv=x.begin(); itv!=x.end();++itv)
				report << *itv << ' ';
			report << "]  !=  [";
			for(auto itw=y.begin(); itw!=y.end();++itw)
				report << *itw << ' ';
			report << "]" << std::endl;

		}

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

	commentator().stop (MSG_STATUS (res), (const char *) 0, "testQLUPsolve");

	return res;
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
0
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;
}