| | 36 | /* Test 0: Minimal polynomial of the zero matrix |
| | 37 | */ |
| | 38 | template <class Field, class Meth> |
| | 39 | static bool testZeroMinpoly (Field &F, size_t n, bool symmetrizing, const Meth& M) |
| | 40 | { |
| | 41 | commentator.start ("Testing zero minpoly", "testZeroMinpoly"); |
| | 42 | typedef vector <typename Field::Element> Polynomial; |
| | 43 | Polynomial phi; |
| | 44 | SparseMatrix<Field> A(F, n, n); |
| | 45 | minpoly(phi, A, M); |
| | 46 | |
| | 47 | ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); |
| | 48 | report << "Minimal polynomial is: "; |
| | 49 | printPolynomial<Field, Polynomial> (F, report, phi); |
| | 50 | |
| | 51 | bool ret; |
| | 52 | if (phi.size () == 2 && F.isZero (phi[0]) && F.isOne(phi[1]) ) |
| | 53 | ret = true; |
| | 54 | else |
| | 55 | { |
| | 56 | commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) |
| | 57 | << "ERROR: A = 0, should get x, got "; |
| | 58 | printPolynomial<Field, Polynomial> (F, report, phi); |
| | 59 | report << endl; |
| | 60 | ret = false; |
| | 61 | } |
| | 62 | commentator.stop (MSG_STATUS (ret), (const char *) 0, "testZeroMinpoly"); |
| | 63 | return ret; |
| | 64 | } |
| | 65 | template <class Field> |
| | 66 | static bool testZeroMinpoly (Field &F, size_t n) |
| | 67 | { |
| | 68 | return testZeroMinpoly(F, n, false, Method::Wiedemann()); |
| | 69 | } |
| | 70 | |
| | 290 | } |
| | 291 | |
| | 292 | /* Test 4: test gram matrix. |
| | 293 | A test of behaviour with self-orthogonal rows and cols. |
| | 294 | |
| | 295 | Gram matrix is 1's offdiagonal and 0's on diagonal. Self orthogonality when characteristic | n-1. |
| | 296 | |
| | 297 | Arg m is ignored. |
| | 298 | if p := characteristic is small then size is p+1 and minpoly is x^2 + x |
| | 299 | (because A^2 = (p-1)A). |
| | 300 | if p is large, size is 2 and minpoly is x^2 -1. |
| | 301 | */ |
| | 302 | template <class Field, class Meth> |
| | 303 | static bool testGramMinpoly (Field &F, size_t m, bool symmetrizing, const Meth& M) |
| | 304 | { |
| | 305 | commentator.start ("Testing gram minpoly", "testGramMinpoly"); |
| | 306 | typedef vector <typename Field::Element> Polynomial; |
| | 307 | integer n; |
| | 308 | F.characteristic(n); n += 1; |
| | 309 | if (n > 30) n = 2; |
| | 310 | Polynomial phi; |
| | 311 | typename Field::Element one, zero, neg1; F.init(one, 1); F.init(zero, 0); F.init(neg1); F.neg(neg1, one); |
| | 312 | DenseMatrix<Field> A(F, n, n); |
| | 313 | for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) A.setEntry(i, j, one); |
| | 314 | for (int i = 0; i < n; ++i) A.setEntry(i, i, zero); |
| | 315 | minpoly(phi, A, M); |
| | 316 | |
| | 317 | ostream &report = commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_DESCRIPTION); |
| | 318 | report << "Minimal polynomial is: "; |
| | 319 | printPolynomial<Field, Polynomial> (F, report, phi); |
| | 320 | |
| | 321 | bool ret; |
| | 322 | if (n == 2) |
| | 323 | if ( phi.size() == 3 && F.areEqual(phi[0], neg1) && F.isZero(phi[1]) && F.isOne(phi[2])) |
| | 324 | ret = true; |
| | 325 | else |
| | 326 | { |
| | 327 | commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) |
| | 328 | << "ERROR: A = gram, should get x^2 - x, got "; |
| | 329 | printPolynomial<Field, Polynomial> (F, report, phi); |
| | 330 | ret = false; |
| | 331 | } |
| | 332 | else |
| | 333 | if (phi.size() == 3 && F.isZero(phi[0]) && F.isOne(phi[1]) && F.isOne(phi[2])) |
| | 334 | ret = true; |
| | 335 | else |
| | 336 | { |
| | 337 | commentator.report (Commentator::LEVEL_IMPORTANT, INTERNAL_ERROR) |
| | 338 | << "ERROR: A = gram, should get x^2 + x, got "; |
| | 339 | printPolynomial<Field, Polynomial> (F, report, phi); |
| | 340 | ret = false; |
| | 341 | } |
| | 342 | commentator.stop (MSG_STATUS (ret), (const char *) 0, "testGramMinpoly"); |
| | 343 | return ret; |
| | 344 | } |
| | 345 | |
| | 346 | template <class Field> |
| | 347 | static bool testGramMinpoly (Field &F, size_t n) |
| | 348 | { |
| | 349 | return testGramMinpoly(F, n, false, Method::Wiedemann()); |
| 288 | | RandomDenseStream<Field, DenseVector, NonzeroRandIter<Field> > |
| 289 | | v_stream (F, NonzeroRandIter<Field> (F, Field::RandIter (F)), n, numVectors); |
| 290 | | RandomSparseStream<Field, SparseVector, NonzeroRandIter<Field> > |
| 291 | | A_stream (F, NonzeroRandIter<Field> (F, Field::RandIter (F)), (double) k / (double) n, n, n); |
| 303 | | |
| | 403 | }else{ |
| | 404 | |
| | 405 | int p; |
| | 406 | if (q % 2 == 0) p = 2; |
| | 407 | if (q % 3 == 0) p = 3; |
| | 408 | if (q % 5 == 0) p = 5; |
| | 409 | int e = 0; do {++e; q = q/p; } while (q > 1); |
| | 410 | typedef GivaroGfq Field; |
| | 411 | Field F (p, e); |
| | 412 | srand (time (NULL)); |
| | 413 | |
| | 414 | commentator.start("Wiedemann non-prime field minpoly test suite", "Wminpoly"); |
| | 415 | |
| | 416 | //no symmetrizing |
| | 417 | if (!testZeroMinpoly (F, n)) pass = false; |
| | 418 | if (!testIdentityMinpoly (F, n)) pass = false; |
| | 419 | if (!testNilpotentMinpoly (F, n)) pass = false; |
| | 420 | //if (!testRandomMinpoly (F, n)) pass = false; |
| | 421 | if (!testGramMinpoly (F, n)) pass = false; |
| | 422 | |
| | 423 | // symmetrizing |
| | 424 | //if (!testZeroMinpoly (F, n, true)) pass = false; |
| | 425 | if (!testIdentityMinpoly (F, n, true)) pass = false; |
| | 426 | //if (!testNilpotentMinpoly (F, n, true)) pass = false; |
| | 427 | //if (!testRandomMinpoly (F, n, true)) pass = false; |
| | 428 | //if (!testGramMinpoly (F, n, true)) pass = false; |
| | 429 | //need other tests... |
| | 430 | |
| | 431 | commentator.stop("Wiedemann non-prime field minpoly test suite"); |
| | 432 | } |
| | 433 | |
| | 434 | #if 0 |