Dixon System Solving via Lifting using dense LU or sparse LU.
#include <iostream>
#include <givaro/givrandom.h>
typedef Givaro::ZRing<Givaro::Integer>
Ints;
template<typename _Matrix, typename _EliminationMethod>
int test(_Matrix A, std::string vector_file,
bool inv,
bool pp,
bool sparse_elim) {
std::cout << "A is " << A.rowdim() << " by " << A.coldim() << std::endl;
if (pp)
{
}
std::ifstream invect;
bool createB = vector_file.empty();
if (!createB) {
invect.open (vector_file, std::ifstream::in);
if (!invect) {
createB = true;
} else {
for(ZVector::iterator it=B.begin(); it != B.end(); ++it)
invect >> *it;
}
}
if (createB) {
Givaro::GivRandom bgen( BaseTimer::seed() );
std::cerr << "Creating a random {-1,1} vector " << std::endl;
for(auto& it:B) it = (bgen.brand()?1:-1);
} else {
std::cerr << "Creating a random consistant {-1,1} vector " << std::endl;
for(FFPACK::rns_double::integer& it:U) it = (bgen.brand()?1:-1);
A.apply(B,U);
}
}
if(pp)
{
}
std::cout << "B is " << B.size() << "x1" << std::endl;
_EliminationMethod M;
}
Ints::Element d;
typedef Givaro::Modular<double>
Field;
std::cout << "Using: " << *fixedprime << " as the fixed p-adic." << std::endl;
chrono.start();
if (!sparse_elim){
{
std::cout << "Solving using Dense Elimination for non singular system" << std::endl;
std::cerr << "Error during solveNonsingular (possibly singular matrix or p-adic precision too small)" << std::endl;
exit(-1);
}
}
else
{
std::cout << "Solving using Dense Elimination for any system" << std::endl;
std::cerr << "Error during solve (all primes used were bad)" << std::endl;
exit(-1);
}
std::cerr << "Error: system appeared inconsistent" << std::endl;
exit(-1);
}
}
} else {
try
{
std::cout << "Solving using Sparse Elimination for any system" << std::endl;
rsolve.
solve(X, d, A, B);
}
{
std::cerr << e << '\n';
exit(-1);
}
}
chrono.stop();
std::cout << "CPU time (seconds): " << chrono.usertime() << std::endl;
{
std::cout<<"Reduced solution: \n";
size_t maxbits=0;
for (size_t i=0;i<A.coldim();++i){
maxbits=(maxbits > X[i].bitsize() ? maxbits: X[i].bitsize());
}
std::cout<<" numerators of size "<<maxbits<<" bits" << std::endl
<<" denominators hold over "<<d.bitsize()<<" bits\n";
}
{
ZVector LHS(ZZ, A.rowdim()), RHS(ZZ, B);
std::cout << "Ax=d.b : Yes" << std::endl;
else
std::cout << "Ax=d.b : No" << std::endl;
}
{
std::cout << "Solution is [";
for(auto it:X) ZZ.write(std::cout, it) << " ";
std::cout << "] / ";
ZZ.write(std::cout, d)<< std::endl;
}
return 0;
}
int main (
int argc,
char **argv) {
std::string matrix_file = "";
std::string vector_file = "";
bool pp = false;
bool sparse_elim = false;
Argument as[] = {
{ 'm', "-m FILE", "Set the input file for the matrix.", TYPE_STR , &matrix_file },
{ 'v', "-v FILE", "Set the input file for the vector.", TYPE_STR , &vector_file },
{
'i',
"-i" ,
"whether the matrix is known to be invertible.", TYPE_BOOL , &
inv },
{ 'p', "-p" , "whether you want to pretty print the matrix.", TYPE_BOOL , &pp },
{ 's', "-s" , "whether to use sparse elimination.", TYPE_BOOL , &sparse_elim },
END_OF_ARGUMENTS
};
FFLAS::parseArguments(argc,argv,as);
if (matrix_file.empty()) {
std::cerr << "You must specify an input file for the matrix with -m" << std::endl;
exit(-1);
}
std::ifstream input (matrix_file);
if (!input) { std::cerr << "Error opening matrix file " << argv[1] << std::endl; exit(-1); }
if (sparse_elim){
(A, vector_file,
inv , pp, sparse_elim);
} else {
(A, vector_file,
inv , pp, sparse_elim);
}
}
Givaro::ZRing< Givaro::Integer > Ints
Definition blassolve.C:31
DenseVector< Ints > ZVector
Definition blassolve.C:32
Interface for the different specialization of p-adic lifting based solvers.
Definition rational-solver.h:134
SolverReturnStatus solve(Vector1 &num, Integer &den, const IMatrix &A, const Vector2 &b, const bool side, int maxPrimes=5) const
Solve a linear system Ax=b over quotient field of a ring giving a random solution if the system is si...
SolverReturnStatus solveNonsingular(Vector1 &num, Integer &den, const IMatrix &A, const Vector2 &b, int maxPrimes=5) const
Solve a nonsingular linear system Ax=b over quotient field of a ring, giving the unique solution of t...
Adaptor class to make a single prime number behave like a PrimeIterator.
Definition random-prime.h:322
base class for execption handling in LinBox
Definition error.h:37
Class of matrix arithmetic functions.
Definition matrixdomain/matrix-domain.h:82
Vector1 & vectorMul(Vector1 &w, const Matrix_ &A, const Vector2 &v) const
Matrix-vector multiply w <- A * v.
Definition matrixdomain/matrix-domain.h:502
MatrixStream.
Definition matrix-stream.h:200
Prime Iterator.
Definition random-prime.h:76
Definition sparse-matrix.h:47
Definition vector-domain.h:187
bool areEqual(const Vector1 &v1, const Vector2 &v2) const
Vector equality.
Definition vector-domain.h:324
Vector & mulin(Vector &x, const Element &a) const
In-place scalar-vector multiplication.
Definition vector-domain.h:463
int test(_Matrix A, std::string vector_file, bool inv, bool pp, bool sparse_elim)
Definition dixonsolve.C:51
Givaro::Modular< uint32_t > Field
Definition dot-product.C:49
SolverReturnStatus
define the different return status of the p-adic based solver's computation.
Definition rational-solver.h:88
@ SS_INCONSISTENT
Definition rational-solver.h:92
@ SS_OK
Definition rational-solver.h:89
@ SS_FAILED
Definition rational-solver.h:90
@ Maple
Definition linbox-tags.h:87
Namespace in which all linbox code resides.
Definition alt-blackbox-block-container.h:4
Givaro::Timer Timer
Definition timer.h:55
typename DenseVectorChooser< _Field >::type DenseVector
Definition vector.h:50
@ NonSingular
The matrix is invertible.
Definition methods.h:99
BlasMatrix< _Field > DenseMatrix
Definition dense-matrix.h:46
Generates random positive prime Integers.
Rational solving (Dixon, Wiedemann,...)
A SparseMatrix<_Field, _Storage> ....
static uint64_t bestBitSize()
Definition field-traits.h:188
int test()
Definition t-rdisolve.C:129
Matrix1 & inv(MatrixDomain< Field > &MD, Matrix1 &res, const Matrix2 &A)
Definition test-matrix-domain.C:100
int main()
Definition test-tutorial.C:54