Various Smith form algorithms may be used for matrices over the integers or over Z_m. Moduli greater than 2^32 are not supported here. Several types of example matrices may be constructed or the matrix be read from a file. Run the program with no arguments for a synopsis of the command line parameters.
For the "adaptive" method, the matrix must be over the integers. This is expected to work best for large matrices.
For the "2local" method, the computation is done mod 2^32.
For the "local" method, the modulus must be a prime power.
For the "ilio" method, the modulus may be arbitrary composite. If the modulus is a multiple of the integer determinant, the integer Smith form is obtained. Determinant plus ilio may be best for smaller matrices.
This example was used during the design process of the adaptive algorithm. The matrix example generation code that was here is now in mats.C.
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <linbox/ring/local2_32.h>
#include <linbox/ring/pir-ntl-zz_p.h>
#include <linbox/algorithms/smith-form-local.h>
#include <linbox/algorithms/smith-form-iliopoulos.h>
template<
class I1,
class Lp>
void distinct (I1 a, I1 b, Lp& c);
template <class I> void display(I b, I e);
int main(int argc, char* argv[])
{
if (argc < 3 or argc > 4) {
cout << "\nUsage: " << argv[0] << " alg file [m]\n" << endl;
cout << " alg = `adaptive', `ilio', `local', or `2local'," << endl
<< " Modulus m is needed for `local' and `ilio'" << endl
<< " m must be a prime power for `local', arbitrary composite for `ilio'." << endl
<< " Integer smith form is obtained by `ilio' if m is a multiple of the "
<< " largest invariant, eg. det." << endl
<< " The matrix is read from file (from cin if file is `-').\n" << endl;
cout << " Regardless of file format, internal matrix rep is dense." << endl
<< " For algoritms using sparse matrix rep, see the examples"
<< " smithvalence.C, power_rank.C, and poweroftwo_ranks.C." << endl;
return 0;
}
string algo = argv[1];
string src = argv[2];
unsigned long m = 1; if (argc == 4) m = atoi(argv[3]);
UserTimer T;
if (algo == "adaptive")
{
typedef Givaro::ZRing<Integer> Ints;
Ints Z;
DenseVector<Givaro::ZRing<Integer> > v(Z,M.coldim());
T.start();
T.stop();
list<pair<integer, size_t> > p;
cout << "Integer Smith Form using adaptive alg :\n";
display(p.begin(), p.end());
cout << "T" << M.coldim() << "adaptive(Ints)" << m << " := ";
}
else if (algo == "ilio") {
PIR R( (int32_t)m);
T.start();
SmithFormIliopoulos::smithFormIn (M);
T.stop();
typedef list< PIR::Element > List;
List L;
for (size_t i = 0; i < M.rowdim(); ++i)
L.push_back(M[(size_t)i][(size_t)i]);
list<pair<PIR::Element, size_t> > p;
cout << "Modular Smith Form using ilio alg :\n";
display(p.begin(), p.end());
cout << "T" << M.coldim() << "ilio(PIR-Modular-int32_t)" << m << " := ";
}
else if (algo == "local") {
PIR R( (int32_t)m);
typedef list< PIR::Element > List;
List L;
T.start();
SmithForm( L, M, R );
T.stop();
list<pair<PIR::Element, size_t> > p;
PIR::Element x = p.back().first, y;
R.neg(y,x);
R.gcdin(x,y);
if (not R.areEqual(p.back().first, x))
R.write(R.write (cerr << "x ", x) << ", back ", p.back().first) << endl;;
p.back().first = x;
cout << "Local Smith Form :\n";
display(p.begin(), p.end());
cout << "T" << M.coldim() << "local(PIR-Modular-int32_t)" << m << " := ";
}
else if (algo == "2local") {
typedef list< Local2_32::Element > List;
List L;
T.start();
SmithForm( L, M, R );
T.stop();
list<pair<Local2_32::Element, size_t> > p;
cout << "2-Local Smith Form :\n";
display(p.begin(), p.end());
cout << "T" << M.coldim() << "local2_32 := ";
}
else
printf ("Unknown algorithm ");
T.print(cout); cout << endl;
return 0 ;
}
if (src[0]==
'-') M.
read(cin);
else {
ifstream in(src);
}
}
template<class I1, class Lp>
{
typename iterator_traits<I1>::value_type e;
size_t count = 0;
if (a != b) {e = *a; ++a; count = 1;}
else return;
while (a != b)
{ if (*a == e) ++count;
else
{ c.push_back(typename Lp::value_type(e, count));
e = *a; count = 1;
}
++a;
}
c.push_back(typename Lp::value_type(e, count));
return;
}
template <class I>
void display(I b, I e)
{ cout << "(";
for (I p = b; p != e; ++p) cout << "[" << p->first << "," << p->second << "] ";
cout << ")" << endl;
}