Characteristic polynomial of matrix over Z or Zp.
#define _LB_CRATIMING 1
#include <iostream>
#include <iomanip>
#include <givaro/zring.h>
using namespace std;
#include <linbox/solutions/charpoly.h>
#include <linbox/ring/polynomial-ring.h>
template <class Field, class Polynomial>
std::ostream&
printPolynomial (std::ostream& out,
const Field &F,
const Polynomial &v)
{
for (int i = (int)(v.size () - 1); i >= 0; i--) {
F.write (out, v[(size_t)i]);
if (i > 0)
out << " X^" << i << " + ";
}
return out;
}
template <class Field, class Polynomial>
std::ostream& prettyprintIntegerPolynomial (std::ostream& out, const Field &F, const Polynomial &v)
{
size_t n = v.size()-1;
if (n == 0) {
F.write(out, v[0]);
}
else {
if(v[n] != 0) {
if (v[n] != 1) F.write(out, v[n]) << '*';
out << 'X';
if (n > 1) out << '^' << n;
for (int i = (int)n - 1; i > 0; i--) {
if (v[(size_t)i] != 0) {
if (v[(size_t)i] >0) out << " + ";
if (v[(size_t)i] != 1) F.write (out, v[(size_t)i]) << '*';
out << 'X';
if (i > 1) out << '^' << i;
}
}
if (v[0] != 0) {
if (v[0] >0) out << " + ";
F.write(out, v[0]);
}
}
}
return out;
}
template <class Field, class Factors, class Exponents>
std::ostream& printFactorization (std::ostream& out, const Field &F, const Factors &f, const Exponents& exp)
{
typename Factors::const_iterator itf = f.begin();
typename Exponents::const_iterator ite = exp.begin();
for ( ; itf != f.end(); ++itf, ++ite) {
prettyprintIntegerPolynomial(out << '(', F, *itf) << ')';
if (*ite > 1) out << '^' << *ite;
out << endl;
}
return out;
}
typedef Givaro::ZRing<Givaro::Integer> IntDom;
int main (int argc, char **argv)
{
commentator().
setMaxDetailLevel (2);
commentator().
setMaxDepth (2);
commentator().
setReportStream (std::cerr);
cout<<setprecision(8);
cerr<<setprecision(8);
if (argc < 2 || argc > 3) {
cerr << "Usage: charpoly <matrix-file-in-SMS-format> [<p>]" << endl;
return -1;
}
ifstream input (argv[1]);
if (!input) { cerr << "Error opening matrix file " << argv[1] << endl; return -1; }
if (argc == 2) {
IntDom ZZ;
DenseMatrix<IntDom > A (ZZ);
A.read (input);
DensePolynomial<IntDom> c_A(ZZ);
Timer tim; tim.clear();tim.start();
charpoly (c_A, A);
tim.stop();
clog << "Characteristic Polynomial is ";
cout << tim << endl;
#ifdef __LINBOX_HAVE_NTL
clog << "Do you want a factorization (y/n) ? ";
char tmp;
cin >> tmp;
if (tmp == 'y' || tmp == 'Y') {
commentator().
start(
"Integer Polynomial factorization by NTL",
"NTLfac");
vector<DensePolynomial<IntDom> > intFactors;
vector<uint64_t> exp;
PolynomialRing<IntDom> IPD(ZZ);
tim.start();
IPD.factor (intFactors, exp, c_A);
tim.stop();
commentator().
stop(
"done", NULL,
"NTLfac");
printFactorization(clog << intFactors.size() << " integer polynomial factors:" << endl, ZZ, intFactors, exp) << endl;
cout << tim << endl;
}
#endif
}
if (argc == 3) {
typedef Givaro::Modular<double> Field;
double q = atof(argv[2]);
Field F(q);
DenseMatrix<Field> B (F);
B.read (input);
clog << "B is " << B.rowdim() << " by " << B.coldim() << endl;
DensePolynomial<Field> c_B(F);
Timer tim; tim.clear();tim.start();
charpoly (c_B, B);
tim.stop();
clog << "Characteristic Polynomial is ";
cout << tim << endl;
}
return 0;
}
linbox base configuration file
void printPolynomial(const Field &F, const Polynomial &v)
Definition: minpoly.C:33
A Givaro::Modular ring is a representations of Z/mZ.
Namespace in which all linbox code resides.
Definition: alt-blackbox-block-container.h:4
A SparseMatrix<_Field, _Storage> ....
LinBox timer is Givaro's.