Beispiel #1
0
static bool testNonsingularRatRatSolve (size_t n, int iterations)
{
	commentator().start ("Testing nonsingular solve with rational vector", "testNonsingularRatRatSolve", iterations);

	bool ret = true;
	int i;
	size_t j;

	GMPRationalField Q;
	SparseMatrix<GMPRationalField > A(Q,n,n);

	PID_integer Z;
	std::vector<GMPRationalField::Element> b(n);
	std::vector<GMPRationalField::Element> true_x(n),x(n);

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

		for (j=0; j < n; ++j) {
			integer tmp_n, tmp_d, tmp_bn, tmp_bd;
			GMPRationalField::Element tmp,tmpb;
			tmp_n = (integer) rand() % (2*(i + 1)) + 1;
			tmp_d = (integer) rand() % (2*(i + 1)) + 1;
			tmp_bn = (integer) rand() % (2*(i + 1)) ;
			tmp_bd = (integer) rand() % (2*(i + 1)) + 1;
			//integer::nonzerorandom(tmp_n, 2*(i + 1) );
			//integer::nonzerorandom(tmp_d, 2*(i + 1) );
			//integer::random(tmp_bn, 2*(i + 1));
			//integer::nonzerorandom(tmp_bd, 2*(i +1) );
			if ( ( i%2) && (j % 2)) integer::negin(tmp_bn);
			Q.init(tmp, tmp_n,tmp_d);
			A.setEntry(j,j,tmp);
			Q.init(tmpb,tmp_bn,tmp_bd);
			b[j]= tmpb;

			Q.init(true_x[j] , tmp_bn * tmp_d, tmp_bd * tmp_n);
		}

		// ostream &report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION);
		solve (x, A, b);

		for (j=0; j < n; ++j) {
			if (!Q.areEqual(x[j] ,true_x[j])) {
				commentator().report() << "ERROR: System solution failed" << endl;
				ret = false;
			}
		}

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

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

	return ret;
}
Beispiel #2
0
static bool testDiagRatCharpoly (size_t n, unsigned int iterations)
{
	commentator().start ("Testing rational charpoly of diagonal matrix ", "testNonsingularRatIntSolve", iterations);

	bool ret = true;
	int i;
	size_t j;

	GMPRationalField Q;
	SparseMatrix<GMPRationalField > A(Q,n,n);
	BlasMatrix <GMPRationalField > B(Q,n,n);
	BlasVector<GMPRationalField> c(Q);

	for (i=0; i < (int)iterations; i++) {
        	GMPRationalField::Element c0,cn;
	        Q.init(c0,1,1);
	        Q.init(cn,0,1);

		commentator().startIteration ((unsigned int)i);

		size_t k = (size_t)ceil((double)n/2);
		for (j=0; j < k; ++j) {
			integer tmp_n, tmp_d;
			GMPRationalField::Element tmp, abstmp;
			tmp_n = (integer) rand() % (5*(i +1)) + 1;
			tmp_d = (integer) rand() % (5*(i +1)) + 1;
			if ( ( i%2) && (j % 2)) integer::negin(tmp_n);

			Q.init(tmp, tmp_n,tmp_d);

			A.setEntry(2*j,2*j,tmp);
			B.setEntry(2*j,2*j,tmp);

			if (2*j +1 < n) {
				A.setEntry(2*j+1,2*j+1,tmp);
				B.setEntry(2*j+1,2*j+1,tmp);
			}

			Q.mulin(c0, tmp);
			Q.addin(cn, tmp);
		}
		if (k%2==0) Q.negin(cn);


		// ostream &report = commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION);
		charpoly (c, A);

		if ( (c.size() == k+1) && ((!Q.areEqual(c[0] , c0)) || (!Q.areEqual(c[k-1] , cn) ) ) ) {
			commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR)
				<< "ERROR: Sparse charpoly failed" << endl;
			ret = false;
		}
		c.clear();

		charpoly (c, B);
		if ( (c.size() == k+1) && ((!Q.areEqual(c[0] , c0)) || (!Q.areEqual(c[n-1] , cn) ) ) ) {
                        commentator().report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR)
                                << "ERROR: Dense charpoly failed" << endl;
                        ret = false;
                }

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

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

	return ret;
}