3030#include "linbox/solutions/solve.h"
3131#include "linbox/util/matrix-stream.h"
3232#include "linbox/solutions/methods.h"
33+ #include "linbox/util/args-parser.h"
34+ #include "linbox/algorithms/rational-solver.h"
3335
3436using namespace LinBox ;
3537typedef Givaro ::ZRing < Givaro ::Integer > Ints ;
@@ -49,28 +51,38 @@ struct FixPrime {
4951
5052
5153int main (int argc , char * * argv ) {
52- // Usage
53- if (argc < 2 || argc > 4 ) {
54- std ::cerr << "Usage: solve <matrix-file-in-supported-format> [<dense-vector-file>]" << std ::endl ;
55- return 0 ;
56- }
5754
58- // File
59- std ::ifstream input (argv [1 ]);
55+ // TODO : seed ?
56+ std ::string matrix_file = "" ;
57+ std ::string vector_file = "" ;
58+ bool inv = false;
59+ bool pp = false;
60+
61+ Argument as [] = {
62+ { 'm' , "-m FILE" , "Set the input file for the matrix." , TYPE_STR , & matrix_file },
63+ { 'v' , "-v FILE" , "Set the input file for the vector." , TYPE_STR , & vector_file },
64+ { 'i' , "-i" , "whether the matrix is invertible." , TYPE_BOOL , & inv },
65+ { 'p' , "-p" , "whether you want to pretty print the matrix." , TYPE_BOOL , & pp },
66+ END_OF_ARGUMENTS
67+ };
68+
69+ FFLAS ::parseArguments (argc ,argv ,as );
70+
71+ // Matrix File
72+ if (matrix_file .empty ()) {
73+ std ::cerr << "You must specify an input file for the matrix with -m" << std ::endl ;
74+ exit (-1 );
75+ }
76+ std ::ifstream input (matrix_file );
6077 if (!input ) { std ::cerr << "Error opening matrix file " << argv [1 ] << std ::endl ; return -1 ; }
6178
62-
79+ // Vector File
6380 std ::ifstream invect ;
64- bool createB = false;
65- if (argc == 2 ) {
66- createB = true;
67- }
68- if (argc == 3 ) {
69- invect .open (argv [2 ], std ::ifstream ::in );
81+ bool createB = vector_file .empty ();
82+ if (!createB ) {
83+ invect .open (vector_file , std ::ifstream ::in );
7084 if (!invect ) {
7185 createB = true;
72- } else {
73- createB = false;
7486 }
7587 }
7688
@@ -81,43 +93,53 @@ int main (int argc, char **argv) {
8193 Ints ::Element d ;
8294 std ::cout << "A is " << A .rowdim () << " by " << A .coldim () << std ::endl ;
8395
96+ if (pp )
8497 {
8598 // Print Matrix
8699
87100 // Matrix Market
88101 // std::cout << "A is " << A << std::endl;
89102
90103 // Maple
91- if ( (A .rowdim () < 100 ) && (A .coldim () < 100 ) )
92- A .write (std ::cout << "Pretty A is " , Tag ::FileFormat ::Maple ) << std ::endl ;
104+ A .write (std ::cout << "A:=" , Tag ::FileFormat ::Maple ) << ';' << std ::endl ;
93105 }
94106
95107 // Vectors
96108 ZVector X (ZZ , A .coldim ()),U (ZZ , A .coldim ()),B (ZZ , A .rowdim ());
97109
98110 if (createB ) {
99- std ::cerr << "Creating a random consistant {-1,1} vector " << std ::endl ;
100- srand48 ( BaseTimer ::seed () );
101- for (auto& it :U )
102- if (drand48 () < 0.5 )
103- it = -1 ;
104- else
105- it = 1 ;
106- A .apply (B ,U );
111+ if (inv ) {
112+ std ::cerr << "Creating a random {-1,1} vector " << std ::endl ;
113+ srand48 ( BaseTimer ::seed () );
114+ for (auto& it :B )
115+ if (drand48 () < 0.5 )
116+ it = -1 ;
117+ else
118+ it = 1 ;
119+ } else {
120+ std ::cerr << "Creating a random consistant {-1,1} vector " << std ::endl ;
121+ srand48 ( BaseTimer ::seed () );
122+ for (auto& it :U )
123+ if (drand48 () < 0.5 )
124+ it = -1 ;
125+ else
126+ it = 1 ;
127+
128+ // B = A U
129+ A .apply (B ,U );
130+ }
107131 } else {
108132 for (ZVector ::iterator it = B .begin ();
109133 it != B .end (); ++ it )
110134 invect >> * it ;
111135 }
112136
137+ if (pp )
113138 {
114139 // Print RHS
115-
116- std ::cout << "B is [" ;
117- for (auto it :B ) ZZ .write (std ::cout , it ) << " ";
118- std ::cout << "]" << std ::endl ;
140+ B .write (std ::cout << "B:=" , Tag ::FileFormat ::Maple ) << ';' << std ::endl ;
119141 }
120-
142+
121143 std ::cout << "B is " << B .size () << "x1" << std ::endl ;
122144
123145 Timer chrono ;
@@ -138,7 +160,26 @@ int main (int argc, char **argv) {
138160 std ::cout << "Using: " << * fixedprime << " as the fixed p-adic." << std ::endl ;
139161
140162 chrono .start ();
141- rsolve .solve (X , d , A , B , false,(int )m .trialsBeforeFailure );
163+ if (inv )
164+ {
165+ SolverReturnStatus ss = rsolve .solveNonsingular (X , d , A , B , false,(int )m .trialsBeforeFailure );
166+ if (ss != SS_OK ) {
167+ std ::cerr << "Error during solveNonsingular (possibly singular matrix or p-adic precision too small)" << std ::endl ;
168+ exit (-1 );
169+ }
170+ }
171+ else
172+ {
173+ SolverReturnStatus ss = rsolve .solve (X , d , A , B , false,(int )m .trialsBeforeFailure );
174+ if (ss == SS_FAILED ){
175+ std ::cerr << "Error during solve (all primes used were bad)" << std ::endl ;
176+ exit (-1 );
177+ }
178+ if (ss == SS_INCONSISTENT ){
179+ std ::cerr << "Error: system appeared inconsistent" << std ::endl ;
180+ exit (-1 );
181+ }
182+ }
142183 chrono .stop ();
143184
144185 std ::cout << "CPU time (seconds): " << chrono .usertime () << std ::endl ;
0 commit comments