This file is indexed.

/usr/include/linbox/solutions/rank.h is in liblinbox-dev 1.1.6~rc0-4.1.

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
/* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* linbox/solutions/rank.h
 * ------------------------------------
 * See COPYING for license information.
 */

#ifndef __RANK_H
#define __RANK_H

//#include "linbox-config.h"
#include "linbox/field/modular.h"
#include "linbox/randiter/random-prime.h"
#include "linbox/algorithms/matrix-hom.h"
#include "linbox/blackbox/sparse.h"
#include "linbox/blackbox/diagonal.h"
#include "linbox/blackbox/compose.h"
#include "linbox/blackbox/permutation.h"
#include "linbox/blackbox/transpose.h"
#include "linbox/blackbox/blas-blackbox.h"
#include "linbox/algorithms/blackbox-container-symmetrize.h"
#include "linbox/algorithms/blackbox-container-symmetric.h"
#include "linbox/algorithms/blackbox-container.h"
#include "linbox/algorithms/massey-domain.h"
#include "linbox/algorithms/gauss.h"
#include "linbox/algorithms/blas-domain.h"
#include "linbox/matrix/blas-matrix.h"
#include "linbox/switch/cekstv.h"
#include "linbox/blackbox/butterfly.h"


#include "linbox/vector/vector-traits.h"
#include "linbox/solutions/trace.h"
#include "linbox/solutions/methods.h"


#include "linbox/util/debug.h"

// Namespace in which all LinBox library code resides
namespace LinBox {


	/** 
            Compute the rank of a linear transform A over a field by selected method. 
            For very large and/or very sparse matrices the Wiedemann method will be faster
            (and is memory efficient).
            For some sparse matrices SparseElimination may outperform Wiedemann.
            For small or dense matrices BlasElimination will be faster.
	    \param r - output rank of A.
            \param - input A linear transform, member of any blackbox class.
            \param - input M may be a Method::Wiedemann (the default), a Method::BlasElimination, or a Method::SparseElimination..
            \returns  reference to r.
            \ingroup solutions
	*/
	
 	template <class Blackbox, class Method, class DomainCategory>
 	unsigned long &rank (unsigned long                   &r,
 			     const Blackbox                  &A,
			     const DomainCategory          &tag,
 			     const Method                   &M);

	// error hanlder for rational domain
	template <class Blackbox, class Method>
 	unsigned long &rank (unsigned long                           &r,
 			     const Blackbox                          &A,
			     const RingCategories::RationalTag     &tag,
 			     const Method                           &M){
		throw LinboxError("LinBox ERROR: rank is not yet defined over a rational domain");
	}


	/** 
            Compute the rank of a linear transform A over a field. 

            The default method is Wiedemann(), using diagonal preconditioning and 
            the minpoly.  For small or dense matrices BlasElimination will be faster.
            \returns <em>r</em> rank of A.
            \param A linear transform, member of any blackbox class.
            \ingroup solutions
	*/
    template <class Blackbox>
    unsigned long &rank (unsigned long                   &r,
                         const Blackbox                  &A)
    {  return rank(r, A, typename FieldTraits<typename Blackbox::Field>::categoryTag(), Method::Hybrid()); 
    }

	/** A may be modified
	*/
    template <class Matrix>
    unsigned long &rankin (unsigned long                   &r,
                         Matrix                  &A)
    {  return rankin(r, A, typename FieldTraits<typename Matrix::Field>::categoryTag(), Method::Elimination()); 
    }

    template <class Blackbox>
    unsigned long &rank (unsigned long                   &r,
                         const Blackbox                  &A,
                         const RingCategories::ModularTag     &tag,
                         const Method::Hybrid         &m)
    { // this should become a BB/Blas hybrid in the style of Duran/Saunders/Wan.  
        if (useBB(A)) return rank(r, A, tag, Method::Blackbox(m )); 
        else return rank(r, A, tag, Method::Elimination( m ));
    }

    template <class Blackbox>
    unsigned long &rank (unsigned long                   &r,
                         const Blackbox                  &A,
                         const RingCategories::ModularTag                  &tag,
                         const Method::Elimination    &m)
    {  
        typedef typename Blackbox::Field Field;
        const Field& F = A.field();
        integer a, b; F.characteristic(a); F.cardinality(b);
        if (a == b && a < LinBox::BlasBound)
		return rank(r, A, tag, Method::BlasElimination(m));
	else
		return rank(r, A, tag, Method::NonBlasElimination( m ));
    }


    template <class Field, class Vector>
    unsigned long &rank (unsigned long                   &r,
                         const SparseMatrix<Field, Vector>         &A,
                         const RingCategories::ModularTag                  &tag,
                         const Method::Elimination    &m)
    {  
        return rank(r, A, tag, Method::SparseElimination(m));
    }


	// specialization of NonBlas for SparseMatrix
    template <class Blackbox>
    unsigned long &rank (unsigned long                   &r,
                         const Blackbox                  &A,
                         const   RingCategories::ModularTag           &tag,
                         const Method::NonBlasElimination& m)
    {
        return rank(r, A, tag, Method::SparseElimination(m));
    }


    template <class Blackbox>
    unsigned long &rank (unsigned long                   &r,
                         const Blackbox                  &A,
                         const  RingCategories::ModularTag                   &tag,
                         const Method::Blackbox& m);
    

	/** 
            Compute the rank of a linear transform A over a field. 

            The default method is Wiedemann(), using diagonal preconditioning and 
            the minpoly.  For small or dense matrices BlasElimination will be faster.
            \returns <em>r</em> rank of A.
            \param A linear transform, member of any blackbox class.
            \ingroup solutions
	*/
    template <class Blackbox, class Method>
    unsigned long &rank (unsigned long                   &r,
                         const Blackbox                  &A,
                         const Method    &M)
    {  return rank(r, A, typename FieldTraits<typename Blackbox::Field>::categoryTag(), M); 
    }

	/// M may be <code>Method::Wiedemann()</code>.
    template <class Blackbox>
    unsigned long &rank (unsigned long                   &res,
                         const Blackbox                  &A,
                         const RingCategories::ModularTag          &tag,
                         const Method::Wiedemann    &M) 
// This is too much for solutions.  It belongs in algorithms
    {
            
        typedef typename Blackbox::Field Field;
        const Field F = A.field();
        typename Field::RandIter iter (F);
            
        if (M.symmetric()) {
            commentator.start ("Symmetric Rank", "srank");
		
                
            std::vector<typename Field::Element> d1;
            size_t i;
                
            VectorWrapper::ensureDim (d1, A.coldim ());

            for (i = 0; i < A.coldim (); i++)
                do iter.random (d1[i]); while (F.isZero (d1[i]));

                
            Diagonal<Field> D1 (F, d1);
                
                
            Compose<Diagonal<Field>,Blackbox > B1 (&D1, &A);
            typedef Compose<Compose<Diagonal<Field>,Blackbox >, Diagonal<Field> > BlackBox1;
            BlackBox1 B (&B1, &D1);
                
            BlackboxContainerSymmetric<Field, BlackBox1> TF (&B, F, iter);
            MasseyDomain<Field, BlackboxContainerSymmetric<Field, BlackBox1> > WD (&TF, M.earlyTermThreshold ());
            std::vector<typename Field::Element> phi;
            WD.pseudo_minpoly (phi, res);
            commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "Pseudo Minpoly degree: " << res << std::endl;
               
            commentator.start ("Monte Carlo certification", "trace");
            typename Field::Element t, p2; F.init(p2, 0UL);
            trace(t, B);            
            if (phi.size() >= 2) F.neg(p2, phi[ phi.size()-2]);
                
            int nbperm = 0; unsigned long rk;
            int logn = 2*(unsigned long)floor( log( (double)A.rowdim() ) );
            bool tryagain = (! F.areEqual( t, p2 ));
            while( tryagain ) {
                commentator.stop ("fail", NULL, "trace");

//                 Permutation<Field> P(A.rowdim(), F);          
//                 for (i = 0; i < A.rowdim (); ++i)
//                     P.permute( rand() % A.rowdim() , rand() % A.rowdim() );
//                 for (i = 0; i < A.rowdim (); ++i)
//                     P.permute( rand() % A.rowdim() , rand() % A.rowdim() );

//                 Transpose< Permutation<Field> > TP(&P);
//                 typedef Compose< Permutation<Field>, Blackbox > BlackboxP;
//                 typedef Compose< Compose< Permutation<Field>, Blackbox >, Transpose< Permutation<Field> > > BlackboxPAP;
//                 BlackboxP PA(&P, &A);
//                 BlackboxPAP BP( &PA , &TP );

//                 for (i = 0; i < A.coldim (); i++)
//                     do iter.random (d1[i]); while (F.isZero (d1[i]));
//                 Diagonal<Field> D1 (F, d1);
//                 Compose<Diagonal<Field>,BlackboxPAP > B1 (&D1, &BP);
//                 typedef Compose<Compose<Diagonal<Field>,BlackboxPAP >, Diagonal<Field> > BlackBox2;
//                 BlackBox2 B (&B1, &D1);

                for (i = 0; i < A.coldim (); i++)
                    do iter.random (d1[i]); while (F.isZero (d1[i]));
                Diagonal<Field> D1 (F, d1);
                Compose<Diagonal<Field>,Blackbox > B1 (&D1, &A);
                BlackBox1 B (&B1, &D1);
                
                BlackboxContainerSymmetric<Field, BlackBox1> TF (&B, F, iter);
                MasseyDomain<Field, BlackboxContainerSymmetric<Field, BlackBox1> > WD (&TF, M.earlyTermThreshold ());
                
                WD.pseudo_minpoly (phi, rk);
                commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "Permuted pseudo Minpoly degree: " << res << std::endl;
                commentator.start ("Monte Carlo certification", "trace");
                if (phi.size() >= 2) F.neg(p2, phi[ phi.size()-2]);
                
                trace(t, B);

                tryagain = (! F.areEqual( t, p2 ));
                if (res > rk) 
                    tryagain = true;
                else
                    res = rk;
                if( ++nbperm > logn) break;
            }
            commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "symm permutations : " << nbperm << std::endl;
            nbperm = 0;
            while(tryagain) {
                commentator.stop ("fail", NULL, "trace");
//             F.write( F.write( commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION)
//                               << "end trace: ", t) << ", p2: ", p2) << std::endl;
                typename Field::RandIter r (F);
                typename CekstvSwitch<Field>::Factory factory (r);
                typedef Butterfly<Field, CekstvSwitch<Field> > ButterflyP;
                ButterflyP P (F, A.rowdim(), factory);
                for (i = 0; i < A.coldim (); i++)
                    do iter.random (d1[i]); while (F.isZero (d1[i]));
                Diagonal<Field> D1 (F, d1);
                typedef Compose< ButterflyP, Diagonal<Field> > ButD;
                ButD PD(&P, &D1);

                Transpose< ButD > TP (&PD);
                
                Compose< ButD, Blackbox > B1( &PD, &A);

                typedef Compose< Compose< ButD, Blackbox > , Transpose< ButD > > BlackBoxBAB;
                BlackBoxBAB PAP(&B1, &TP);
                
                BlackboxContainerSymmetric<Field, BlackBoxBAB> TF (&PAP, F, iter);
                MasseyDomain<Field, BlackboxContainerSymmetric<Field, BlackBoxBAB> > WD (&TF, M.earlyTermThreshold ());
                
                WD.pseudo_minpoly (phi, rk);
                commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "Butterfly pseudo Minpoly degree: " << res << std::endl;
                commentator.start ("Monte Carlo certification", "trace");
                if (phi.size() >= 2) F.neg(p2, phi[ phi.size()-2]);
                
                trace(t, PAP);

                tryagain = (! F.areEqual( t, p2 ));
                if (res > rk) 
                    tryagain = true;
                else
                    res = rk;
                ++nbperm;
            }
            
                // F.write( F.write( commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION)
                //              << "end trace: ", t) << ", p2: ", p2) << std::endl;
            
            commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "butterflies : " << nbperm << std::endl;

            commentator.stop ("success", NULL, "trace");
            commentator.stop ("done", NULL, "srank");
            
            return res;
        } else {
                
            commentator.start ("Rank", "wrank");

            std::vector<typename Field::Element> d1, d2;
            size_t i;
                
            VectorWrapper::ensureDim (d1, A.coldim ());
            VectorWrapper::ensureDim (d2, A.rowdim ());
                
            for (i = 0; i < A.coldim (); i++)
                do iter.random (d1[i]); while (F.isZero (d1[i]));
                
            for (i = 0; i < A.rowdim (); i++)
                do iter.random (d2[i]); while (F.isZero (d2[i]));
                
            Diagonal<Field> D1 (F, d1), D2 (F, d2);
            Transpose<Blackbox> AT (&A);
                
            Compose<Diagonal<Field>,Transpose<Blackbox> > B1 (&D1, &AT);
            Compose<Compose<Diagonal<Field>,Transpose<Blackbox> >, Diagonal<Field> > B2 (&B1, &D2);
            Compose<Compose<Compose<Diagonal<Field>,Transpose<Blackbox> >, Diagonal<Field> >, Blackbox> B3 (&B2, &A);
                // Here there is an extra diagonal computation
                // The probability of success is also divided by two, as 
                // D2^2 contains only squares and squares are half the total elements                
            typedef Compose<Compose<Compose<Compose<Diagonal<Field>,Transpose<Blackbox> >, Diagonal<Field> >, Blackbox>, Diagonal<Field> > Blackbox1;
            Blackbox1 B (&B3, &D1);

            BlackboxContainerSymmetric<Field, Blackbox1> TF (&B, F, iter);
            MasseyDomain<Field, BlackboxContainerSymmetric<Field, Blackbox1> > WD (&TF, M.earlyTermThreshold ());
                
            std::vector<typename Field::Element> phi;
            WD.pseudo_minpoly (phi, res);
            commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "Pseudo Minpoly degree: " << res << std::endl;
            commentator.start ("Monte Carlo certification", "trace");

            typename Field::Element t, p2; F.init(p2, 0UL);
            trace(t, B);            
            if (phi.size() >= 2) F.neg(p2, phi[ phi.size()-2]);
                
            int nbperm = 0; unsigned long rk;
            int logn = 2*(unsigned long)floor( log( (double)A.rowdim() ) );
            bool tryagain = (! F.areEqual( t, p2 ));
            while( tryagain ) {
                commentator.stop ("fail", NULL, "trace");
                Permutation<Field> P(A.rowdim(), F);          
                for (i = 0; i < A.rowdim (); ++i)
                    P.permute( rand() % A.rowdim() , rand() % A.rowdim() );
                for (i = 0; i < A.rowdim (); ++i)
                    P.permute( rand() % A.rowdim() , rand() % A.rowdim() );

                typedef Compose< Permutation<Field>, Blackbox > BlackboxP;
                BlackboxP BP(&P, &A);

                for (i = 0; i < A.coldim (); i++)
                    do iter.random (d1[i]); while (F.isZero (d1[i]));
                
                for (i = 0; i < A.rowdim (); i++)
                    do iter.random (d2[i]); while (F.isZero (d2[i]));
                
                Diagonal<Field> D1 (F, d1), D2 (F, d2);
                Transpose<BlackboxP> AT (&BP);
                
                Compose<Diagonal<Field>,Transpose<BlackboxP> > B1 (&D1, &AT);
                Compose<Compose<Diagonal<Field>,Transpose<BlackboxP> >, Diagonal<Field> > B2 (&B1, &D2);
                Compose<Compose<Compose<Diagonal<Field>,Transpose<BlackboxP> >, Diagonal<Field> >, BlackboxP> B3 (&B2, &BP);
                typedef Compose<Compose<Compose<Compose<Diagonal<Field>,Transpose<BlackboxP> >, Diagonal<Field> >, BlackboxP>, Diagonal<Field> > Blackbox1;
                Blackbox1 B (&B3, &D1);

                BlackboxContainerSymmetric<Field, Blackbox1> TF (&B, F, iter);
                MasseyDomain<Field, BlackboxContainerSymmetric<Field, Blackbox1> > MD (&TF, M.earlyTermThreshold ());
                
                MD.pseudo_minpoly (phi, rk);
                commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "Permuted pseudo Minpoly degree: " << rk << std::endl;
                commentator.start ("Monte Carlo certification", "trace");
                if (phi.size() >= 2) F.neg(p2, phi[ phi.size()-2]);
                
                trace(t, B);
                tryagain = (! F.areEqual( t, p2 ));
                if (res > rk) 
                    tryagain = true;
                else
                    res = rk;
                if( ++nbperm > logn) break;
            }
            commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "permutations : " << nbperm << std::endl;
            nbperm = 0;
            while(tryagain) {
                commentator.stop ("fail", NULL, "trace");
                typename Field::RandIter r (F);
                typename CekstvSwitch<Field>::Factory factory (r);
                typedef Butterfly<Field, CekstvSwitch<Field> > ButterflyP;
                ButterflyP P (F, A.rowdim(), factory);
                
                typedef Compose< ButterflyP, Blackbox > BlackboxP;
                BlackboxP BP(&P, &A);

                for (i = 0; i < A.coldim (); i++)
                    do iter.random (d1[i]); while (F.isZero (d1[i]));
                
                for (i = 0; i < A.rowdim (); i++)
                    do iter.random (d2[i]); while (F.isZero (d2[i]));
                
                Diagonal<Field> D1 (F, d1), D2 (F, d2);
                Transpose<BlackboxP> AT (&BP);
                
                Compose<Diagonal<Field>,Transpose<BlackboxP> > B1 (&D1, &AT);
                Compose<Compose<Diagonal<Field>,Transpose<BlackboxP> >, Diagonal<Field> > B2 (&B1, &D2);
                Compose<Compose<Compose<Diagonal<Field>,Transpose<BlackboxP> >, Diagonal<Field> >, BlackboxP> B3 (&B2, &BP);
                typedef Compose<Compose<Compose<Compose<Diagonal<Field>,Transpose<BlackboxP> >, Diagonal<Field> >, BlackboxP>, Diagonal<Field> > Blackbox1;
                Blackbox1 B (&B3, &D1);

                BlackboxContainerSymmetric<Field, Blackbox1> TF (&B, F, iter);
                MasseyDomain<Field, BlackboxContainerSymmetric<Field, Blackbox1> > MD (&TF, M.earlyTermThreshold ());
                
                MD.pseudo_minpoly (phi, rk);
                commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "Butterfly pseudo Minpoly degree: " << rk << std::endl;
                commentator.start ("Monte Carlo certification", "trace");
                if (phi.size() >= 2) F.neg(p2, phi[ phi.size()-2]);
                
                trace(t, B);
                tryagain = (! F.areEqual( t, p2 ));
                if (res > rk) 
                    tryagain = true;
                else
                    res = rk;
                ++nbperm;
            }
            commentator.report(Commentator::LEVEL_ALWAYS,INTERNAL_DESCRIPTION) << "butterflies : " << nbperm << std::endl;
            commentator.stop ("success", NULL, "trace");
            commentator.stop ("done", NULL, "wrank");
            
            return res;
        }

    }

	/// M may be <code>Method::SparseElimination()</code>.
    template <class Field>
    unsigned long &rank (unsigned long                       &r,
                         const SparseMatrix<Field, typename LinBox::Vector<Field>::SparseSeq>  &A,
                         const RingCategories::ModularTag    &tag,
                         const Method::SparseElimination     &M) 
    {
         // We make a copy as these data will be destroyed
        SparseMatrix<Field, typename LinBox::Vector<Field>::SparseSeq> A1 (A);  
        return rankin(r, A1, tag, M);
    }
    
    template <class Field, class Method>
    unsigned long &rankin (unsigned long                   &r,
                           SparseMatrix<Field, typename LinBox::Vector<Field>::SparseSeq>  &A,
                         const Method    &M)
    {  return rankin(r, A, typename FieldTraits<Field>::categoryTag(), M); 
    }


   template <class Blackbox, class Ring>
    unsigned long &rankin (unsigned long                     &r,
                           Blackbox &A,
                           const RingCategories::IntegerTag    &tag,
                           const Method::SparseElimination     &M)
    {
        commentator.start ("Integer Rank", "irank");
        typedef Modular<double> Field;
        integer mmodulus; 
        FieldTraits<Field>::maxModulus(mmodulus);
        RandomPrimeIterator genprime( (long) floor (log((double)mmodulus) ) );
        ++genprime;
        typedef typename Blackbox::template rebind< Field >::other FBlackbox;
        FBlackbox * Ap;
        MatrixHom::map(Ap, A, Field(*genprime) );
        commentator.report (Commentator::LEVEL_ALWAYS,INTERNAL_WARNING) << "Integer Rank is done modulo " << mmodulus << std::endl;
        rankin(r, *Ap, RingCategories::ModularTag(), M);
        delete Ap;
        commentator.stop ("done", NULL, "irank");
        return r;
    }
    
    template <class Field>
    unsigned long &rankin (unsigned long                       &r,
                           SparseMatrix<Field, typename LinBox::Vector<Field>::SparseSeq>  &A,
                           const RingCategories::ModularTag    &tag,
                           const Method::SparseElimination     &M) 
    {
        commentator.start ("Sparse Elimination Rank", "serank");
        GaussDomain<Field> GD ( A.field() );
        GD.rankin (r, A, M.strategy ());
        commentator.stop ("done", NULL, "serank");
        return r;
    }

	// Change of representation to be able to call the sparse elimination
    template <class Blackbox>
    unsigned long &rank (unsigned long                       &r,
                         const Blackbox  &A,
                         const RingCategories::ModularTag    &tag,
                         const Method::SparseElimination     &M) 
    {
        typedef typename Blackbox::Field Field;
        typedef SparseMatrix<Field, typename LinBox::Vector<Field>::SparseSeq> SparseBB;
        SparseBB * SpA;
	MatrixHom::map(SpA, A, A.field());
        rankin(r, *SpA, tag, M);
        delete SpA;
        return r;
    }
    
	// M may be <code>Method::BlasElimination()</code>.
    template <class Blackbox>
    unsigned long &rank (unsigned long                     &r,
                         const Blackbox                      &A,
                         const RingCategories::ModularTag          &tag,
                         const Method::BlasElimination  &M) 
    {
        
        commentator.start ("Blas Rank", "blasrank");
        typedef typename Blackbox::Field Field;
        const Field F = A.field();
        integer a, b; F.characteristic(a); F.cardinality(b);
        linbox_check( a == b );
        linbox_check( a < LinBox::BlasBound);
        BlasMatrix<typename Field::Element> B(A);
	BlasMatrixDomain<Field> D(F);
        r = D.rank(B);
	commentator.stop ("done", NULL, "blasrank");
        return r;
    }


// is this used?
	// A is modified.
    template <class Matrix>
    unsigned long &rankin (unsigned long                      &r,
                           Matrix                               &A,
                           const RingCategories::ModularTag          &tag,
                           const Method::SparseElimination &M) 
    {
        typedef typename Matrix::Field Field;
        const Field F = A.field();
        GaussDomain<Field> GD (F);
        GD.rankin( r, A, M.strategy ());
        return r;
    }

	/// A is modified.
    template <class Field>
    unsigned long &rankin (unsigned long                     &r,
                           BlasBlackbox<Field>                 &A,
                           const RingCategories::ModularTag          &tag,
                           const Method::BlasElimination  &M) 
    {

        commentator.start ("BlasBB Rank", "blasbbrank");
        const Field F = A.field();
        BlasMatrixDomain<Field> D(F);
        r = D.rankin(static_cast< BlasMatrix<typename Field::Element>& >(A));
        commentator.stop ("done", NULL, "blasbbrank");
        return r;
    }

    template <class Blackbox, class MyMethod>
    unsigned long &rank (unsigned long                     &r,
                         const Blackbox                      &A,
                         const RingCategories::IntegerTag          &tag,
                         const MyMethod                           &M)
    {
        commentator.start ("Integer Rank", "iirank");
        typedef Modular<double> Field;
        integer mmodulus; 
        FieldTraits<Field>::maxModulus(mmodulus);
        RandomPrimeIterator genprime( (long) floor (log((double)mmodulus) ) );
        ++genprime;
        typedef typename Blackbox::template rebind< Field >::other FBlackbox;
        FBlackbox * Ap;
        MatrixHom::map(Ap, A, Field(*genprime) );
        commentator.report (Commentator::LEVEL_ALWAYS,INTERNAL_WARNING) << "Integer Rank is done modulo " << mmodulus << std::endl;
        rank(r, *Ap, RingCategories::ModularTag(), M);
        delete Ap;
        commentator.stop ("done", NULL, "iirank");
        return r;
    }
} // LinBox


#ifdef __LINBOX_HAVE_GIVARO
#define LINBOX_EXTENSION_DEGREE_MAX 20

#include "linbox/field/givaro-extension.h"
namespace LinBox {
    template <class Blackbox>
    unsigned long &rank (unsigned long                   &r,
                         const Blackbox                  &A,
                         const  RingCategories::ModularTag                   &tag,
                         const Method::Blackbox& m)
    {  
        commentator.start ("BB Rank", "extend");
        if (m.certificate()) {
            typedef typename Blackbox::Field Field;
            const Field& F = A.field();
            integer a,c; F.cardinality(a); F.characteristic(c);
            if (a != c) {
                unsigned long extend = (unsigned long)FF_EXPONENT_MAX(a,(integer)LINBOX_EXTENSION_DEGREE_MAX);
                if (extend > 1) {
                    commentator.report (Commentator::LEVEL_ALWAYS,INTERNAL_WARNING) << "Extension of degree " << extend << std::endl;
                    GivaroExtension<Field> EF( F, extend);
                    typedef typename Blackbox::template rebind< GivaroExtension<Field>  >::other FBlackbox;
                    FBlackbox * Ap;
                    MatrixHom::map(Ap, A, EF );
                    rank(r, *Ap, tag, Method::Wiedemann(m));
                }
            } else {
                unsigned long extend = (unsigned long)FF_EXPONENT_MAX(c,(integer)LINBOX_EXTENSION_DEGREE_MAX);
                if (extend > 1) {
                    commentator.report (Commentator::LEVEL_ALWAYS,INTERNAL_WARNING) << "Word size extension : " << extend << std::endl;
                    GivaroGfq EF( (unsigned long)c, extend);                    
                    typedef typename Blackbox::template rebind< GivaroGfq >::other FBlackbox;
                    FBlackbox * Ap;
                    MatrixHom::map(Ap, A, EF );
                    rank(r, *Ap, tag, Method::Wiedemann(m));
                }
            }
        } else
            rank(r, A, tag, Method::Wiedemann(m)); 
        commentator.stop ("done", NULL, "extend");
        return r; 
    }
}
#else
namespace LinBox {
    template <class Blackbox>
    unsigned long &rank (unsigned long                   &r,
                         const Blackbox                  &A,
                         const  RingCategories::ModularTag                   &tag,
                         const Method::Blackbox& m)
    {  
            return rank(r, A, tag, Method::Wiedemann(m)); 
    }
}
#endif



#endif // __RANK_H