1 /*
2  * examples/smithvalence.C
3  * Copyright (c) Linbox
4  * ========LICENCE========
5  * This file is part of the library LinBox.
6  *
7  * LinBox is free software: you can redistribute it and/or modify
8  * it under the terms of the  GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  * ========LICENCE========
21  */
22 
23 /**\file examples/smithvalence.C
24  * @example  examples/smithvalence.C
25  \brief Valence of sparse matrix over Z or Zp.
26  \ingroup examples
27 */
28 #ifndef DISABLE_COMMENTATOR
29 #define DISABLE_COMMENTATOR
30 #endif
31 
32 #define __VALENCE_REPORTING__ 1
33 
34 #include <linbox/linbox-config.h>
35 
36 #include <iostream>
37 #include <givaro/givintfactor.h>
38 #include <fflas-ffpack/paladin/parallel.h>
39 #include <linbox/solutions/smith-form.h>
40 #include <linbox/algorithms/smith-form-valence.h>
41 #include <vector>
42 
43 using namespace LinBox;
44 
45 
46 
47 
main(int argc,char ** argv)48 int main (int argc, char **argv)
49 {
50 
51 	if (argc < 2 || argc > 4) {
52 		std::cerr << "Usage: smithvalence <matrix-file-in-supported-format> [-ata|-aat|valence] [coprime]" << std::endl;
53         std::cerr << "       Optional parameters valence and coprime are integers." << std::endl;
54         std::cerr << "       Prime factors of valence will be used for local computation." << std::endl;
55         std::cerr << "       coprime will be used for overall Z rank computation." << std::endl;
56 		return -1;
57 	}
58 
59     const std::string filename(argv[1]);
60 
61 	std::ifstream input (filename);
62 	if (!input) { std::cerr << "Error opening matrix file " << filename << std::endl; return -1; }
63 
64 	Givaro::ZRing<Integer> ZZ;
65 	MatrixStream< Givaro::ZRing<Integer> > ms( ZZ, input );
66 	typedef SparseMatrix<Givaro::ZRing<Integer>>  Blackbox;
67 	Blackbox A (ms);
68 	input.close();
69 
70 	std::clog << "A is " << A.rowdim() << " by " << A.coldim() << std::endl;
71 
72     size_t method=0;
73 	Givaro::Integer val_A(0);
74 
75 	if (argc >= 3) {
76 		if (strcmp(argv[2],"-ata") == 0) {
77             method=2;
78 		}
79 		else if (strcmp(argv[2],"-aat") == 0) {
80             method=1;
81 		}
82 		else {
83 			std::clog << "Suppose primes are contained in " << argv[2] << std::endl;
84 			val_A = LinBox::Integer(argv[2]);
85 		}
86 	}
87 
88     Givaro::Integer coprimeV=1;
89 	if (argc >= 4) {
90 		std::clog << "Suppose " << argv[3] << " is coprime with Smith form" << std::endl;
91 		coprimeV =Givaro::Integer(argv[3]);
92 	}
93 
94     std::vector<Givaro::Integer> SmithDiagonal;
95 
96 #ifdef  __LINBOX_USE_OPENMP
97 	std::clog << "num procs: " << omp_get_num_procs() << std::endl;
98     std::clog << "max threads: " << MAX_THREADS << std::endl;
99 #endif
100         // Returns the Smith form as a diagonal,
101         // the valence val_A,
102         // the coprimeV used to compute the integral rank
103 	LinBox::Timer chrono; chrono.start();
104     PAR_BLOCK {
105         smithValence(SmithDiagonal, val_A, A, filename, coprimeV, method);
106     }
107 
108 	chrono.stop();
109 
110 	std::clog << "Integer Smith Form :" << std::endl;
111     writeCompressedSmith(std::cout, SmithDiagonal, ZZ, A.rowdim(), A.coldim()) << std::endl;
112 
113 	std::clog << chrono << std::endl;
114 
115 
116 	return 0;
117 }
118 
119 // Local Variables:
120 // mode: C++
121 // tab-width: 4
122 // indent-tabs-mode: nil
123 // c-basic-offset: 4
124 // End:
125 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
126