Skip to content

Commit 6883198

Browse files
author
Romain Lebreton
committed
* dixondenseLU.C:
* add arguments parser for matrix file, vector file, invertible matrix & pretty print * create consistant RHS if matrix is not told invertible * Error messages & exit for singular system when matrix was told invertible * Error messages & exit when the solve algorithm failed * dixonsparseelim.C: * create consistant RHS * dixon-solver-dense.h: * typo * /linbox/vector/blas-vector.h: * FIX seg fault in BlasVector::write * TODO : merge dixondenseLU & dixonsparseelim
1 parent ee50b44 commit 6883198

File tree

5 files changed

+91
-37
lines changed

5 files changed

+91
-37
lines changed

examples/dixondenseLU.C

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
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

3436
using namespace LinBox;
3537
typedef Givaro::ZRing<Givaro::Integer> Ints;
@@ -49,28 +51,38 @@ struct FixPrime {
4951

5052

5153
int 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;

examples/dixonsparseelim.C

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "linbox/solutions/solve.h"
3232
#include "linbox/util/matrix-stream.h"
3333
#include "linbox/solutions/methods.h"
34+
#include "linbox/util/error.h"
3435

3536
using namespace LinBox;
3637
typedef Givaro::ZRing<Givaro::Integer> Ints;
@@ -95,7 +96,9 @@ int main (int argc, char **argv) {
9596

9697
// Vectors
9798
ZVector X(ZZ, A.coldim()),B(ZZ, A.rowdim());
99+
ZVector U(ZZ, A.coldim());
98100

101+
// B = A U
99102
if (createB) {
100103
std::cerr << "Creating a random {-1,1} vector " << std::endl;
101104
srand48( BaseTimer::seed() );
@@ -105,6 +108,8 @@ int main (int argc, char **argv) {
105108
*it = -1;
106109
else
107110
*it = 1;
111+
112+
A.apply(B, U);
108113
} else {
109114
for(ZVector::iterator it=B.begin();
110115
it != B.end(); ++it)
@@ -141,7 +146,16 @@ int main (int argc, char **argv) {
141146

142147

143148
chrono.start();
144-
rsolve.solve(X, d, A, B);
149+
try
150+
{
151+
rsolve.solve(X, d, A, B);
152+
}
153+
catch(LinboxError& e)
154+
{
155+
std::cerr << e << '\n';
156+
return -1;
157+
}
158+
145159

146160
// END Replacement solve with fixed prime
147161
//====================================================

linbox/algorithms/dixon-solver/dixon-solver-dense.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ namespace LinBox {
206206
* SL_LASVEGAS)</code>, solution is guaranteed correct.
207207
* - \c SS_FAILED all primes used were bad
208208
* - \c SS_OK solution found.
209-
* - \c SS_INCONSISTENT system appreared inconsistent. certificate is in \p
209+
* - \c SS_INCONSISTENT system appeared inconsistent. certificate is in \p
210210
* lastCertificate if <code>(level >= SL_CERTIFIED)</code>
211211
*/
212212
template <class IMatrix, class Vector1, class Vector2>

linbox/algorithms/rational-solver.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ namespace LinBox
567567
/*
568568
* Specialization for Sparse Elimination method
569569
*/
570-
// solve non singular system using Sparse LU
570+
// solve any system using Sparse LU
571571
// max prime is not use. only check with one prime
572572
template <class Ring, class Field, class RandomPrime>
573573
template <class IMatrix, class Vector1, class Vector2>

linbox/vector/blas-vector.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ namespace LinBox {
298298
os << '<' ;
299299
for(auto it= _rep.begin();it != _rep.end(); ++it) {
300300
field().write(os, *it);
301-
++it ;
302-
if (it != this->end())
301+
if (it != this->end()-1)
303302
os << ',' ;
304303
}
305304
return os << '>' ;

0 commit comments

Comments
 (0)