This file is indexed.

/usr/include/linbox/algorithms/rational-solver.h is in liblinbox-dev 1.4.2-5build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
/* linbox/algorithms/rational-solver.h
 * Copyright (C) 2004 Zhendong Wan, Pascal Giorgi
 *
 * Written by Zhendong Wan  <wan@mail.eecis.udel.edu>
 *         and Pascal Giorgi <pascal.giorgi@ens-lyon.fr>
 * Modified by David Pritchard  <daveagp@mit.edu>
 *
 * ========LICENCE========
 * This file is part of the library LinBox.
 *
  * LinBox is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 */

/*! @file algorithms/rational-solver.h
 * @ingroup algorithms
 * @brief Rational solving (Dixon, Wiedemann,...)
 */

#ifndef __LINBOX_rational_solver_H
#define __LINBOX_rational_solver_H

#include <iostream>

#include "linbox/linbox-config.h"
#include "linbox/util/debug.h"

// #include "linbox/field/multimod-field.h"
#include "linbox/solutions/methods.h"
#include "linbox/blackbox/archetype.h"
#include "linbox/matrix/dense-matrix.h"
#include "linbox/blackbox/lambda-sparse.h"
#include "linbox/blackbox/compose.h"
#include "linbox/algorithms/vector-fraction.h"
#include "linbox/util/timer.h"

//#define RSTIMING
#define DEFAULT_PRIMESIZE 20

namespace LinBox
{// LinBox


#define SINGULARITY_THRESHOLD 5
#define BAD_PRECONTITIONER_THRESHOLD 5
#define DEFAULT_MAXPRIMES 5
#define SL_DEFAULT SL_LASVEGAS



	/** @defgroup padic p-adic lifting for linear system solutions.
	 *  @brief interface for solving linear system by p-adic lifting technique over the quotient field of a ring.
	 *  i.e. solution over the rational for an integer linear system.
	 *
	 * \par Headers
	 *  <code>\#include "linbox/algorithms/rational-solver.h></code>
	 *
	 *  See the following reference for details on this algorithm:
	 * \bib
	 *    - Robert T. Moenck and John H. Carter <i>Approximate algorithms to
	 *  derive exact solutions to system of linear equations.</i> In Proc.
	 *  EUROSAM'79, volume 72 of Lectures Note in Computer Science, pages
	 *  65-72, Berlin-Heidelberger-New York, 1979. Springer-Verlag.
	 *    - John D. Dixon <i>Exact Solution of linear equations using p-adic
	 *  expansions.</i> Numerische Mathematik, volume 40, pages 137-141,
	 *  1982.
	 *  .
	 * \ingroup algorithms
	 *
	 */


	/** \brief define the different return status of the p-adic based solver's computation.
	 *
	 * \ingroup padic
	 */
	enum SolverReturnStatus {
		SS_OK,
		SS_FAILED,
		SS_SINGULAR,
		SS_INCONSISTENT,
		SS_BAD_PRECONDITIONER
	};

	/** Define the different strategy which can be used in the p-adic based solver.
	 *
	 * Used to determine what level of solving should be done:
	 * - Monte Carlo: Try to solve if possible, but result is not guaranteed.
	 *   In any case a 0 denominator should not be returned.
	 * - Las Vegas  : Result should be guaranteed correct.
	 * - Certified  : Additionally, provide certificates that the result returned is correct.
	 *              - if the return value is \p SS_INCONSISTENT, this means
	 *                   \p lastCertificate satisfies \f$lC \cdot A = 0\f$ and \f$lC \cdot b \neq 0 \f$
	 *              - if diophantine solving was called and the return value is \p SS_OK, this means
	 *                   \p lastCertificate satisfies \f$ \mathrm{den}(lC \cdot A) = 1, \mathrm{den}(lC \cdot b) = \mathrm{den}(answer) \f$
	 *              .
	 * .
	 * \ingroup padic
	 */
	enum SolverLevel {
		SL_MONTECARLO,
		SL_LASVEGAS,
		SL_CERTIFIED
	};    // note: code may assume that each level is 'stronger' than the previous one

	/*****************/
	/*** Interface ***/
	/*****************/

	/** \brief Interface for the different specialization of p-adic lifting based solvers.
	 *
	 * The following type are abstract in the implementation and can be
	 * change during the instanciation of the class:
	 * -  Ring: ring over which entries are defined
	 * -  Field: finite field for p-adic lifting
	 * -  RandomPrime: generator of random primes
	 * -  MethodTraits: type of subalgorithm to use in p-adic lifting (default is DixonTraits)
	 * .
	 *
	 * \ingroup padic
	 */
	template<class Ring, class Field, class RandomPrime, class MethodTraits = DixonTraits>
	class RationalSolver {

	public:
		/** Solve a linear system \c Ax=b over quotient field of a ring
		 *         giving a random solution if the system is singular and consistent,
		 *         giving the unique solution if the system is non-singular.
		 *
		 * @param num  Vector of numerators of the solution
		 * @param den  The common denominator. 1/den * num is the rational solution of \c Ax=b.
		 * @param A    Matrix of linear system
		 * @param b    Right-hand side of system
		 * @param maxPrimes maximum number of moduli to try
		 * @param side
		 *
		 * @return status of solution
		 */
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solve(Vector1& num, Integer& den,
					 const IMatrix& A, const Vector2& b,
					 const bool side, int maxPrimes = DEFAULT_MAXPRIMES) const;


		/** Solve a nonsingular linear system \c Ax=b over quotient field of a ring,
		 *          giving the unique solution of the system.
		 *
		 * @param num  Vector of numerators of the solution
		 * @param den  The common denominator. 1/den * num is the rational solution of \c Ax=b.
		 * @param A   Matrix of linear system
		 * @param b   Right-hand side of system
		 * @param maxPrimes maximum number of moduli to try
		 *
		 * @return status of solution
		 */
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveNonsingular(Vector1& num, Integer& den,
						    const IMatrix& A, const Vector2& b,
						    // bool toto,
						    int maxPrimes = DEFAULT_MAXPRIMES) const;

		/** brief Solve a singular linear system \c Ax=b over quotient field of a ring,
		 *         giving a random solution if the system is singular and consistent.
		 *
		 * @param num  Vector of numerators of the solution
		 * @param den  The common denominator. 1/den * num is the rational solution of \c Ax=b.
		 * @param A   Matrix of linear system
		 * @param b   Right-hand side of system
		 * @param maxPrimes maximum number of moduli to try
		 *
		 * @return status of solution
		 */
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveSingular(Vector1& num, Integer& den,
						 const IMatrix& A, const Vector2& b,
						 int maxPrimes = DEFAULT_MAXPRIMES) const;


	};

	/***********************/
	/*** Specialisations ***/
	/***********************/

	/*-----------*/
	/* WIEDEMANN */
	/*-----------*/

#ifdef RSTIMING
	class WiedemannTimer {
	public:
		mutable Timer ttSetup, ttRecon, ttGetDigit, ttGetDigitConvert, ttRingApply, ttRingOther;
		void clear() const {
			ttSetup.clear();
			ttRecon.clear();
			ttGetDigit.clear();
			ttGetDigitConvert.clear();
			ttRingOther.clear();
			ttRingApply.clear();
		}

		template<class RR, class LC>
		void update(RR& rr, LC& lc) const {
			ttSetup += lc.ttSetup;
			ttRecon += rr.ttRecon;
			ttGetDigit += lc.ttGetDigit;
			ttGetDigitConvert += lc.ttGetDigitConvert;
			ttRingOther += lc.ttRingOther;
			ttRingApply += lc.ttRingApply;
		}
	};
#endif

	/** Partial specialization of p-adic based solver with Wiedemann algorithm.
	 *
	 *   See the following reference for details on this algorithm:
	 * @bib
	 *   - Douglas H. Wiedemann <i>Solving sparse linear equations over
	 *   finite fields</i>.  IEEE Transaction on Information Theory, 32(1),
	 *   pages 54-62, 1986.
	 *   - Erich Kaltofen and B. David Saunders <i>On Wiedemann's method of
	 *   solving sparse linear systems</i>.  In Applied Algebra, Algebraic
	 *   Algorithms and Error Correcting Codes - AAECC'91, volume 539 of
	 *   Lecture Notes in Computer Sciences, pages 29-38, 1991.
	 *
	 */
	template<class Ring, class Field,class RandomPrime>
	class RationalSolver<Ring, Field, RandomPrime, WiedemannTraits> {

	public:
		typedef Ring                                 RingType;
		typedef typename Ring::Element                Integer;
		typedef typename Field::Element               Element;
		typedef typename RandomPrime::Prime_Type        Prime;
		typedef std::vector<Element>              FPolynomial;

	protected:
		Ring                       _ring;
		mutable RandomPrime _genprime;
		mutable Prime          _prime;
		WiedemannTraits       _traits;

#ifdef RSTIMING
		mutable Timer  tNonsingularSetup,   ttNonsingularSetup,
			tNonsingularMinPoly, ttNonsingularMinPoly,
			totalTimer;

		mutable WiedemannTimer   ttNonsingularSolve;
#endif
	public:

		/** Constructor
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 * @param traits
		 */
		RationalSolver (const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE),
				const WiedemannTraits& traits=WiedemannTraits()) :
			_ring(r), _genprime(rp), _traits(traits)
		{

			_genprime.template setBitsField<Field>();
			++_genprime; _prime=*_genprime;
#ifdef RSTIMING
			clearTimers();
#endif
		}

		/**  Constructor with a prime.
		 * @param p   a Prime
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 * @param traits
		 */
		RationalSolver (const Prime& p, const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE),
				const WiedemannTraits& traits=WiedemannTraits()) :
			_ring(r), _genprime(rp), _prime(p), _traits(traits)
		{

#ifdef RSTIMING
			clearTimers();
#endif
		}


		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solve(Vector1& num, Integer& den,
					 const IMatrix& A, const Vector2& b,
					 const bool s=false, int maxPrimes = DEFAULT_MAXPRIMES) const;


		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveNonsingular(Vector1& num, Integer& den,
						    const IMatrix& A, const Vector2& b,
						    int maxPrimes = DEFAULT_MAXPRIMES) const;


		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveSingular(Vector1& num, Integer& den,
						 const IMatrix& A, const Vector2& b,
						 int maxPrimes = DEFAULT_MAXPRIMES) const;


		template <class IMatrix, class FMatrix, class IVector>
		void sparseprecondition (const Field&, const IMatrix* ,
					 Compose< LambdaSparseMatrix<Ring>,Compose<IMatrix, LambdaSparseMatrix<Ring> > > *&,
					 const FMatrix*,
					 Compose<LambdaSparseMatrix<Field>,Compose<FMatrix,LambdaSparseMatrix<Field> > > *&,
					 const IVector&, IVector&, LambdaSparseMatrix<Ring> *&,
					 LambdaSparseMatrix<Ring> *&,
					 LambdaSparseMatrix<Field> *&,
					 LambdaSparseMatrix<Field> *&) const;

#if 0
		   template <class IMatrix, class FMatrix, class IVector, class FVector>
		   void precondition (const Field&,
				      const IMatrix&,
				      BlackboxArchetype<IVector>*&,
				      const FMatrix*,
				      BlackboxArchetype<FVector>*&,
				      const IVector&,
				      IVector&,
				      BlackboxArchetype<IVector>*&,
				      BlackboxArchetype<IVector>*&) const;
#endif

#ifdef RSTIMING
		void clearTimers() const
		{
			ttNonsingularSetup.clear();
			ttNonsingularMinPoly.clear();

			ttNonsingularSolve.clear();
		}

	public:

		inline std::ostream& printTime(const Timer& timer, const char* title, std::ostream& os, const char* pref = "") const
		{
			if (&timer != &totalTimer)
				totalTimer += timer;
			if (timer.count() > 0) {
				os << pref << title;
				for (int i=strlen(title)+strlen(pref); i<28; i++)
					os << ' ';
				return os << timer << std::endl;
			}
			else
				return os;
		}

		inline std::ostream& printWiedemannTime(const WiedemannTimer& timer, const char* title, std::ostream& os) const
		{
			if (timer.ttSetup.count() > 0) {
				printTime(timer.ttSetup, "Setup", os, title);
				printTime(timer.ttGetDigit, "Field Apply", os, title);
				printTime(timer.ttGetDigitConvert, "Ring-Field-Ring Convert", os, title);
				printTime(timer.ttRingApply, "Ring Apply", os, title);
				printTime(timer.ttRingOther, "Ring Other", os, title);
				printTime(timer.ttRecon, "Reconstruction", os, title);
			}
			return os;
		}

		std::ostream& reportTimes(std::ostream& os) const
		{
			totalTimer.clear();
			printTime(ttNonsingularSetup, "NonsingularSetup", os);
			printTime(ttNonsingularMinPoly, "NonsingularMinPoly", os);
			printWiedemannTime(ttNonsingularSolve, "NS ", os);
			printTime(totalTimer , "TOTAL", os);
			return os;
		}
#endif

		void chooseNewPrime() const {
			_genprime.template setBitsField<Field>();
			++_genprime; _prime = *_genprime;
		}

	}; // end of specialization for the class RationalSover with Wiedemann traits

	/*-----------------*/
	/* BLOCK WIEDEMANN */
	/*-----------------*/

#ifdef RSTIMING
	class BlockWiedemannTimer {
	public:
		mutable Timer ttSetup, ttRecon, ttGetDigit, ttGetDigitConvert, ttRingApply, ttRingOther, ttMinPoly;
		void clear() const {
			ttSetup.clear();
			ttRecon.clear();
			ttGetDigit.clear();
			ttGetDigitConvert.clear();
			ttRingOther.clear();
			ttRingApply.clear();
			ttMinPoly.clear();
		}

		template<class RR, class LC>
		void update(RR& rr, LC& lc) const {
			ttSetup += lc.ttSetup;
			ttRecon += rr.ttRecon;
			ttGetDigit += lc.ttGetDigit;
			ttGetDigitConvert += lc.ttGetDigitConvert;
			ttRingOther += lc.ttRingOther;
			ttRingApply += lc.ttRingApply;
			ttMinPoly += lc.ttMinPoly;
		}
	};
#endif

	/** \brief partial specialization of p-adic based solver with block Wiedemann algorithm.
	 *
	 *   See the following reference for details on this algorithm:
	 *   @bib
	 *   - Douglas H. Wiedemann <i>Solving sparse linear equations over
	 *   finite fields</i>.  IEEE Transaction on Information Theory, 32(1),
	 *   pages 54-62, 1986.
	 *   - Don Coppersmith  <i>Solving homogeneous linear equations over
	 *   GF(2) via block Wiedemann algorithm.</i> Mathematic of
	 *   computation, 62(205), pages 335-350, 1994.
	 *
	 *   - Erich Kaltofen and B. David Saunders <i>On Wiedemann's method of
	 *   solving sparse linear systems</i>.  In Applied Algebra, Algebraic
	 *   Algorithms and Error Correcting Codes, AAECC'91, volume 539 of
	 *   Lecture Notes in Computer Sciences, pages 29-38, 1991.
	 *
	 */
	template<class Ring, class Field,class RandomPrime>
	class RationalSolver<Ring, Field, RandomPrime, BlockWiedemannTraits> {

	public:
		typedef Ring                                 RingType;
		typedef typename Ring::Element                Integer;
		typedef typename Field::Element               Element;
		typedef typename RandomPrime::Prime_Type        Prime;
		typedef BlasMatrix<Field>               Coefficient;
		typedef std::vector<Element>              FPolynomial;
		typedef std::vector<Coefficient>     FBlockPolynomial;

	protected:
		Ring                         _ring;
		RandomPrime           _genprime;
		mutable Prime            _prime;
		BlockWiedemannTraits    _traits;

#ifdef RSTIMING
		mutable Timer  tNonsingularSetup,   ttNonsingularSetup,
			tNonsingularBlockMinPoly, ttNonsingularBlockMinPoly,
			totalTimer;

		mutable BlockWiedemannTimer   ttNonsingularSolve;
#endif
	public:

		/*! Constructor.
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 * @param traits
		 */
		RationalSolver (const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE),
				const BlockWiedemannTraits& traits=BlockWiedemannTraits()) :
			_ring(r), _genprime(rp), _traits(traits)
		{

			_genprime.template setBitsField<Field>();
			++_genprime; _prime=*_genprime;
#ifdef RSTIMING
			clearTimers();
#endif
		}

		/*! Constructor with a prime.
		 * @param p   a Prime
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 * @param traits
		 */
		RationalSolver (const Prime& p, const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE),
				const BlockWiedemannTraits& traits=BlockWiedemannTraits()) :
			_ring(r), _genprime(rp), _prime(p), _traits(traits)
		{

#ifdef RSTIMING
			clearTimers();
#endif
		}

		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solve(Vector1& num, Integer& den,
					 const IMatrix& A, const Vector2& b,
					 const bool s=false, int maxPrimes = DEFAULT_MAXPRIMES) const;

		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveNonsingular(Vector1& num, Integer& den,
						    const IMatrix& A, const Vector2& b,
						    int maxPrimes = DEFAULT_MAXPRIMES) const;


		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveSingular(Vector1& num, Integer& den,
						 const IMatrix& A, const Vector2& b,
						 int maxPrimes = DEFAULT_MAXPRIMES) const;



#ifdef RSTIMING
		void clearTimers() const
		{
			ttNonsingularSetup.clear();
			ttNonsingularBlockMinPoly.clear();

			ttNonsingularSolve.clear();
		}

	public:

		inline std::ostream& printTime(const Timer& timer, const char* title, std::ostream& os, const char* pref = "") const
		{
			if (&timer != &totalTimer)
				totalTimer += timer;
			if (timer.count() > 0) {
				os << pref << title;
				for (int i=strlen(title)+strlen(pref); i<28; i++)
					os << ' ';
				return os << timer << std::endl;
			}
			else
				return os;
		}

		inline std::ostream& printBlockWiedemannTime(const BlockWiedemannTimer& timer, const char* title, std::ostream& os) const
		{
			if (timer.ttSetup.count() > 0) {
				printTime(timer.ttSetup, "Setup", os, title);
				printTime(timer.ttGetDigit, "Field Apply", os, title);
				printTime(timer.ttGetDigitConvert, "Ring-Field-Ring Convert", os, title);
				printTime(timer.ttRingApply, "Ring Apply", os, title);
				printTime(timer.ttRingOther, "Ring Other", os, title);
				printTime(timer.ttRecon, "Reconstruction", os, title);
			}
			return os;
		}

		std::ostream& reportTimes(std::ostream& os) const
		{
			totalTimer.clear();
			printTime(ttNonsingularSetup, "NonsingularSetup", os);
			printTime(ttNonsingularBlockMinPoly, "NonsingularMinPoly", os);
			printBlockWiedemannTime(ttNonsingularSolve, "NS ", os);
			printTime(totalTimer , "TOTAL", os);
			std::cout<<"MinPoly computation        :"<<ttNonsingularSolve.ttMinPoly<<std::endl;
			return os;
		}
#endif
	}; // end of specialization for the class RationalSover with BlockWiedemann traits

	/*-------*/
	/* DIXON */
	/*-------*/

#ifdef RSTIMING
	class DixonTimer {
	public:
		mutable Timer ttSetup, ttRecon, ttGetDigit, ttGetDigitConvert, ttRingApply, ttRingOther;
		mutable int rec_elt;
		void clear() const
		{
			ttSetup.clear();
			ttRecon.clear();
			ttGetDigit.clear();
			ttGetDigitConvert.clear();
			ttRingOther.clear();
			ttRingApply.clear();
			rec_elt=0;
		}

		template<class RR, class LC>
		void update(RR& rr, LC& lc) const
		{
			ttSetup += lc.ttSetup;
			ttRecon += rr.ttRecon;
			rec_elt += rr._num_rec;
			ttGetDigit += lc.ttGetDigit;
			ttGetDigitConvert += lc.ttGetDigitConvert;
			ttRingOther += lc.ttRingOther;
			ttRingApply += lc.ttRingApply;
		}
	};
#endif

	/** \brief partial specialization of p-adic based solver with Dixon algorithm.
	 *
	 *   See the following reference for details on this algorithm:
	 *   @bib
	 *  - John D. Dixon <i>Exact Solution of linear equations using p-adic
	 *  expansions</i>. Numerische Mathematik, volume 40, pages 137-141,
	 *  1982.
	 *
	 */

	template<class Ring, class Field,class RandomPrime>
	class RationalSolver<Ring, Field, RandomPrime, DixonTraits> {

	public:

		typedef Ring                                 RingType;
		typedef typename Ring::Element               Integer;
		typedef typename Field::Element              Element;
		typedef typename RandomPrime::Prime_Type     Prime;

		// polymorphic 'certificate' generated when level >= SL_CERTIFIED
		// certificate of inconsistency when any solving routine returns SS_INCONSISTENT
		// certificate of minimal denominator when findRandomSolutionAndCertificate is called & return is SS_OK
		mutable VectorFraction<Ring>                 lastCertificate;

		//next 2 fields generated only by findRandomSolutionAndCertificate, when return is SS_OK
		mutable Integer                              lastZBNumer;               //filled in if level >= SL_CERTIFIED
		mutable Integer                              lastCertifiedDenFactor;    //filled in if level >= SL_LASVEGAS
		//note: lastCertificate * b = lastZBNumer / lastCertifiedDenFactor, in lowest form

	protected:

		mutable RandomPrime             _genprime;
		mutable Prime                   _prime;
		Ring                            _ring;
#ifdef RSTIMING
		mutable Timer
		tSetup,           ttSetup,
		tLQUP,            ttLQUP,
		tFastInvert,      ttFastInvert,        //only done in deterministic or inconsistent
		tCheckConsistency,ttCheckConsistency,        //includes lifting the certificate
		tMakeConditioner, ttMakeConditioner,
		tInvertBP,        ttInvertBP,              //only done in random
		tCheckAnswer,     ttCheckAnswer,
		tCertSetup,       ttCertSetup,        //remaining 3 only done when makeMinDenomCert = true
		tCertMaking,      ttCertMaking,

		tNonsingularSetup,ttNonsingularSetup,
		tNonsingularInv,  ttNonsingularInv,

		totalTimer;

		mutable DixonTimer
		ttConsistencySolve, ttSystemSolve, ttCertSolve, ttNonsingularSolve;
#endif

	public:

		/** Constructor
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 */
		RationalSolver (const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE)) :
			lastCertificate(r, 0), _genprime(rp), _ring(r)
		{
			_genprime.template setBitsField<Field>();
			++_genprime; _prime=*_genprime;
#ifdef RSTIMING
			clearTimers();
#endif
		}


		/** Constructor, trying the prime p first
		 * @param p a Prime
		 * @param r a Ring, set by default
		 * @param rp a RandomPrime generator, set by default
		 */
		RationalSolver (const Prime& p, const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE)) :
			lastCertificate(r, 0), _genprime(rp), _prime(p), _ring(r)
		{
#ifdef RSTIMING
			clearTimers();
#endif
		}


		/** Solve a linear system \c Ax=b over quotient field of a ring.
		 *
		 * @param num Vector of numerators of the solution
		 * @param den  The common denominator. 1/den * num is the rational solution of \c Ax=b.
		 * @param A        Matrix of linear system
		 * @param b        Right-hand side of system
		 * @param s
		 * @param maxPrimes maximum number of moduli to try
		 * @param level    level of certification to be used
		 *
		 * @return status of solution. if \c (return != SS_FAILED), and \c (level >= SL_LASVEGAS), solution is guaranteed correct.
		 *  \c  SS_FAILED - all primes used were bad
		 *  \c SS_OK - solution found.
		 *  \c  SS_INCONSISTENT - system appreared inconsistent. certificate is in \p  lastCertificate if \c (level >= SL_CERTIFIED)
		 */
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solve(Vector1& num, Integer& den, const IMatrix& A,
					 const Vector2& b, const bool s = false,
					 const int maxPrimes = DEFAULT_MAXPRIMES,
					 const SolverLevel level = SL_DEFAULT) const;

		/** overload so that the bool 'oldMatrix' argument is not accidentally set to true */
		template <class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solve(Vector1& num, Integer& den,
					 const IMatrix& A, const Vector2& b, const int maxPrimes,
					 const SolverLevel level = SL_DEFAULT) const
		{
			return solve (num, den, A, b, false, maxPrimes, level);
		}

		/** Solve a nonsingular, square linear system \c Ax=b over quotient field of a ring.
		 *
		 * @param num       Vector of numerators of the solution
		 * @param den       The common denominator. <code>1/den * num</code> is the rational solution of <code>Ax = b</code>
		 * @param A         Matrix of linear system (it must be square)
		 * @param b         Right-hand side of system
		 * @param s         unused
		 * @param maxPrimes maximum number of moduli to try
		 *
		 * @return status of solution :
		 *   - \c SS_FAILED   all primes used were bad;
		 *   - \c SS_OK       solution found, guaranteed correct;
		 *   - \c SS_SINGULAR system appreared singular mod all primes.
		 *   .
		 */
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveNonsingular(Vector1& num, Integer& den, const IMatrix& A,
						    const Vector2& b, bool s = false,
						    int maxPrimes = DEFAULT_MAXPRIMES) const;

		/** Solve a general rectangular linear system \c Ax=b over quotient field of a ring.
		 *  If A is known to be square and nonsingular, calling solveNonsingular is more efficient.
		 *
		 * @param num       Vector of numerators of the solution
		 * @param den       The common denominator. <code>1/den * num</code> is the rational solution of <code>Ax = b</code>
		 * @param A         Matrix of linear system
		 * @param b         Right-hand side of system
		 * @param maxPrimes maximum number of moduli to try
		 * @param level     level of certification to be used
		 *
		 * @return status of solution. if <code>(return != SS_FAILED)</code>, and <code>(level >= SL_LASVEGAS)</code>, solution is guaranteed correct.
		 *   - \c SS_FAILED        all primes used were bad
		 *   - \c SS_OK            solution found.
		 *   - \c SS_INCONSISTENT  system appreared inconsistent. certificate is in \p lastCertificate if <code>(level >= SL_CERTIFIED)</code>
		 */
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveSingular(Vector1& num, Integer& den, const IMatrix& A,
						 const Vector2& b,
						 int maxPrimes = DEFAULT_MAXPRIMES, const SolverLevel level = SL_DEFAULT) const;

		/** Find a random solution of the general linear system  \c Ax=b over quotient field of a ring.
		 *
		 * @param num   Vector of numerators of the solution
		 * @param den   The common denominator. <code>1/den * num</code> is the rational solution of <code>Ax = b</code>.
		 * @param A         Matrix of linear system
		 * @param b         Right-hand side of system
		 * @param maxPrimes maximum number of moduli to try
		 * @param level     level of certification to be used
		 *
		 * @return status of solution. if <code>(return != SS_FAILED)</code>, and <code>(level >= SL_LASVEGAS)</code>, solution is guaranteed correct.
		 *  - \c SS_FAILED  all primes used were bad
		 *  - \c SS_OK  solution found.
		 *  - \c SS_INCONSISTENT  system appreared inconsistent. certificate is in lastCertificate if <code>(level >= SL_CERTIFIED)</code>
		 */
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus findRandomSolution(Vector1& num, Integer& den, const IMatrix& A,
						      const Vector2& b,
						      int maxPrimes = DEFAULT_MAXPRIMES,
						      const SolverLevel level = SL_DEFAULT) const;

		/** Big solving routine to perform random solving and certificate generation.
		 * Same arguments and return as findRandomSolution, except
		 *
		 * @param num  Vector of numerators of the solution
		 * @param den  The common denominator. <code>1/den * num</code> is the rational solution of <code>Ax = b</code>
		 * @param A
		 * @param b
		 * @param randomSolution  parameter to determine whether to randomize or not (since solveSingular calls this function as well)
		 * @param makeMinDenomCert  determines whether a partial certificate for the minimal denominator of a rational solution is made
		 * @param maxPrimes
		 * @param level
		 *
		 * When <code>(randomSolution == true && makeMinDenomCert == true)</code>,
		 *  - If <code>(level == SL_MONTECARLO)</code> this function has the same effect as calling findRandomSolution.
		 *  - If <code>(level >= SL_LASVEGAS && return == SS_OK)</code>, \c lastCertifiedDenFactor contains a certified factor of the min-solution's denominator.
		 *  - If <code>(level >= SL_CERTIFIED && return == SS_OK)</code>, \c lastZBNumer and \c lastCertificate are updated as well.
		 *
		 */
		template <class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus monolithicSolve (Vector1& num, Integer& den, const IMatrix& A,
						    const Vector2& b,
						    bool makeMinDenomCert, bool randomSolution,
						    int maxPrimes = DEFAULT_MAXPRIMES,
						    const SolverLevel level = SL_DEFAULT) const;

		Ring getRing() const
		{
			return _ring;
		}

		void chooseNewPrime() const
		{
			_genprime.template setBitsField<Field>();
			++_genprime;
			_prime = *_genprime;
		}

#ifdef RSTIMING
		void clearTimers() const
		{
			ttSetup.clear();
			ttLQUP.clear();
			ttFastInvert.clear();
			ttCheckConsistency.clear();
			ttMakeConditioner.clear();
			ttInvertBP.clear();
			ttCheckAnswer.clear();
			ttCertSetup.clear();
			ttCertMaking.clear();
			ttNonsingularSetup.clear();
			ttNonsingularInv.clear();

			ttConsistencySolve.clear();
			ttSystemSolve.clear();
			ttCertSolve.clear();
			ttNonsingularSolve.clear();
		}

	public:

		inline std::ostream& printTime(const Timer& timer, const char* title,
					       std::ostream& os, const char* pref = "") const
		{
			if (&timer != &totalTimer)
				totalTimer += timer;
			if (timer.count() > 0) {
				os << pref << title;
				for (int i=strlen(title)+strlen(pref); i<28; i++)
					os << ' ';
				return os << timer << std::endl;
			}
			else
				return os;
		}

		inline std::ostream& printDixonTime(const DixonTimer& timer, const char* title,
						    std::ostream& os) const
		{
			if (timer.ttSetup.count() > 0) {
				printTime(timer.ttSetup, "Setup", os, title);
				printTime(timer.ttGetDigit, "Field Apply", os, title);
				printTime(timer.ttGetDigitConvert, "Ring-Field-Ring Convert", os, title);
				printTime(timer.ttRingApply, "Ring Apply", os, title);
				printTime(timer.ttRingOther, "Ring Other", os, title);
				printTime(timer.ttRecon, "Reconstruction", os, title);
				os<<" number of elt recontructed: "<<timer.rec_elt<<std::endl;
			}
			return os;
		}

		std::ostream& reportTimes(std::ostream& os) const
		{
			totalTimer.clear();
			printTime(ttNonsingularSetup, "NonsingularSetup", os);
			printTime(ttNonsingularInv, "NonsingularInv", os);
			printDixonTime(ttNonsingularSolve, "NS ", os);
			printTime(ttSetup , "Setup", os);
			printTime(ttLQUP , "LQUP", os);
			printTime(ttFastInvert , "FastInvert", os);
			printTime(ttCheckConsistency , "CheckConsistency", os);
			printDixonTime(ttConsistencySolve, "INC ", os);
			printTime(ttMakeConditioner , "MakeConditioner", os);
			printTime(ttInvertBP , "InvertBP", os);
			printDixonTime(ttSystemSolve, "SYS ", os);
			printTime(ttCheckAnswer , "CheckAnswer", os);
			printTime(ttCertSetup , "CertSetup", os);
			printDixonTime(ttCertSolve, "CER ", os);
			printTime(ttCertMaking , "CertMaking", os);
			printTime(totalTimer , "TOTAL", os);
			return os;
		}
#endif

	}; // end of specialization for the class RationalSolver with Dixon traits


	/*----------------*/
	/* HYBRID Num/Sym */
	/*----------------*/

	/** \brief solver using a hybrid Numeric/Symbolic computation.
		template argument Field and RandomPrime are not used.
		Keep it just for interface consistency.
	 */
	template <class Ring, class Field, class RandomPrime>
	class RationalSolver<Ring, Field, RandomPrime, NumSymOverlapTraits>;

	/** \brief solver using a hybrid Numeric/Symbolic computation.
	 *
	 *   This is the original numerix/symbolic solver, now replaced by an enhanced version.
	 *
	 *   See the following reference for details on this implementation:
	 *   @bib
	 *   - Zhendong Wan <i>Exactly solve integer linear systems using numerical methods.</i>
	 *   Submitted to Journal of Symbolic Computation, 2004.
	 *   @warning entries in Matrix must be smaller than \f$2^50\f$.
	 *
	 */
	//template argument Field and RandomPrime are not used.
	//Keep it just for interface consistency.
	template <class Ring, class Field, class RandomPrime>
	class RationalSolver<Ring, Field, RandomPrime, NumSymNormTraits> ;

	/*--------------*/
	/* BLOCK HANKEL */
	/*--------------*/

	/*! Block Hankel.
	 * NO DOC
	 */
	template<class Ring, class Field,class RandomPrime>
	class RationalSolver<Ring, Field, RandomPrime, BlockHankelTraits> {
	public:
		typedef Ring                                 RingType;
		typedef typename Ring::Element               Integer;
		typedef typename Field::Element              Element;
		typedef typename RandomPrime::Prime_Type     Prime;

	protected:
		RandomPrime                     _genprime;
		mutable Prime                   _prime;
		Ring                            _ring;

	public:


		/** Constructor.
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 */
		RationalSolver (const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE)) :
			_genprime(rp), _ring(r)
		{
			_genprime.template setBitsField<Field>();
			_prime=_genprime.randomPrime();
		}


		/** Constructor, trying the prime p first.
		 * @param p   a Prime
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 */
		RationalSolver (const Prime& p, const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE)) :
			_genprime(rp), _prime(p), _ring(r)
		{}


		// solve non singular system
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveNonsingular(Vector1& num, Integer& den,
						    const IMatrix& A, const Vector2& b,
						    size_t blocksize, int maxPrimes = DEFAULT_MAXPRIMES) const;
	};



	/*-----------*/
	/* SPARSE LU */
	/*-----------*/

	/*! Sparse LU.
	 * NO DOC
	 */
	template<class Ring, class Field,class RandomPrime>
	class RationalSolver<Ring, Field, RandomPrime, SparseEliminationTraits> {
	public:
		typedef Ring                                 RingType;
		typedef typename Ring::Element               Integer;
		typedef typename Field::Element              Element;
		typedef typename RandomPrime::Prime_Type     Prime;

	protected:
		RandomPrime                     _genprime;
		mutable Prime                   _prime;
		Ring                            _ring;

	public:


		/** Constructor.
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 */
		RationalSolver (const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE)) :
			_genprime(rp), _ring(r)
		{
			_genprime.template setBitsField<Field>();
			_prime=_genprime.randomPrime();
		}


		/** Constructor, trying the prime p first.
		 * @param p   a Prime
		 * @param r   a Ring, set by default
		 * @param rp  a RandomPrime generator, set by default
		 */
		RationalSolver (const Prime& p, const Ring& r = Ring(),
				const RandomPrime& rp = RandomPrime(DEFAULT_PRIMESIZE)) :
			_genprime(rp), _prime(p), _ring(r)
		{}


		// solve non singular system
		template<class IMatrix, class Vector1, class Vector2>
		SolverReturnStatus solveNonsingular(Vector1& num, Integer& den,
						    const IMatrix& A, const Vector2& b,
						    int maxPrimes = DEFAULT_MAXPRIMES) const;
	};

}


#include "linbox/algorithms/rational-solver.inl"

#endif //__LINBOX_rational_solver_H



// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End:
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s