File: charpoly.C

package info (click to toggle)
linbox 1.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,940 kB
  • sloc: cpp: 108,392; lisp: 5,469; makefile: 1,345; sh: 1,244; csh: 131; python: 74; perl: 2
file content (177 lines) | stat: -rw-r--r-- 4,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * examples/charpoly.C
 *
 * Copyright (C) 2005, 2010 D. Saunders, C. Pernet, J-G. Dumas
 * ========LICENCE========
 * This file is part of the library LinBox.
 *
 * LinBox is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 */

/** \file examples/charpoly.C
 * @example  examples/charpoly.C
 \brief Characteristic polynomial of matrix over Z or Zp.
 \ingroup examples
*/
#define __LB_CRA_TIMING__ 1
//#define __LINBOX_HEURISTIC_CRA
#include <linbox/linbox-config.h>

#include <iostream>
#include <iomanip>

#include <linbox/util/timer.h>
#include <linbox/ring/modular.h>
#include <givaro/zring.h>
#include <linbox/matrix/sparse-matrix.h>
using namespace std;

#include <linbox/solutions/charpoly.h>
#include <linbox/ring/polynomial-ring.h>
using namespace LinBox;

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 ";
		printPolynomial (clog, ZZ, c_A) << endl;
		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 ";
		printPolynomial (clog, F, c_B) << endl;
		cout << tim << endl;
	}

	return 0;
}

// Local Variables:
// mode: C++
// tab-width: 4
// indent-tabs-mode: nil
// c-basic-offset: 4
// End:
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s