<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>linbox: examples/charpoly.C</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.7.4 --> <script type="text/javascript"> function hasClass(ele,cls) { return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')); } function addClass(ele,cls) { if (!this.hasClass(ele,cls)) ele.className += " "+cls; } function removeClass(ele,cls) { if (hasClass(ele,cls)) { var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)'); ele.className=ele.className.replace(reg,' '); } } function toggleVisibility(linkObj) { var base = linkObj.getAttribute('id'); var summary = document.getElementById(base + '-summary'); var content = document.getElementById(base + '-content'); var trigger = document.getElementById(base + '-trigger'); if ( hasClass(linkObj,'closed') ) { summary.style.display = 'none'; content.style.display = 'block'; trigger.src = 'open.png'; removeClass(linkObj,'closed'); addClass(linkObj,'opened'); } else if ( hasClass(linkObj,'opened') ) { summary.style.display = 'block'; content.style.display = 'none'; trigger.src = 'closed.png'; removeClass(linkObj,'opened'); addClass(linkObj,'closed'); } return false; } </script> <div id="top"> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">linbox</div> </td> </tr> </tbody> </table> </div> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div class="header"> <div class="headertitle"> <div class="title">examples/charpoly.C</div> </div> </div> <div class="contents"> <p>Characteristic polynomial of matrix over Z or Zp.</p> <div class="fragment"><pre class="fragment"><span class="comment">/* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */</span> <span class="comment">// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s</span> <span class="comment">/*</span> <span class="comment"> * examples/charpoly.C</span> <span class="comment"> *</span> <span class="comment"> * Copyright (C) 2005, 2010 D. Saunders, C. Pernet, J-G. Dumas</span> <span class="comment"> *</span> <span class="comment"> * This file is part of LinBox.</span> <span class="comment"> *</span> <span class="comment"> * LinBox is free software: you can redistribute it and/or modify</span> <span class="comment"> * it under the terms of the GNU Lesser General Public License as</span> <span class="comment"> * published by the Free Software Foundation, either version 2 of</span> <span class="comment"> * the License, or (at your option) any later version.</span> <span class="comment"> *</span> <span class="comment"> * LinBox is distributed in the hope that it will be useful,</span> <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment"> * GNU Lesser General Public License for more details.</span> <span class="comment"> *</span> <span class="comment"> * You should have received a copy of the GNU Lesser General Public</span> <span class="comment"> * License along with LinBox. If not, see</span> <span class="comment"> * <http://www.gnu.org/licenses/>.</span> <span class="comment"> */</span> <span class="preprocessor">#include <iostream></span> <span class="preprocessor">#include <iomanip></span> <span class="preprocessor">#include "<a class="code" href="timer_8h.html" title="LinBox timer is Givaro's.">linbox/util/timer.h</a>"</span> <span class="preprocessor">#include "<a class="code" href="modular-double_8h.html" title="Standard representation of Z/mZ over double .">linbox/field/modular-double.h</a>"</span> <span class="preprocessor">#include "linbox/field/unparametric.h"</span> <span class="preprocessor">#include "linbox/blackbox/sparse.h"</span> <span class="preprocessor">#include "linbox/blackbox/blas-blackbox.h"</span> <span class="keyword">using namespace </span>std; <span class="preprocessor">#include "linbox/solutions/charpoly.h"</span> <span class="preprocessor">#include "<a class="code" href="givaro-polynomial_8h.html" title="NO DOC.">linbox/ring/givaro-polynomial.h</a>"</span> <span class="keyword">using namespace </span>LinBox; <span class="keyword">template</span> <<span class="keyword">class</span> Field, <span class="keyword">class</span> Polynomial> std::ostream& printPolynomial (std::ostream& out, <span class="keyword">const</span> <a name="_a0"></a><a class="code" href="class_lin_box_1_1_modular_3_01uint32__t_01_4.html" title="Specialization of class Modular for uint32_t element type.">Field</a> &F, <span class="keyword">const</span> Polynomial &v) { <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = v.size () - 1; i >= 0; i--) { F.write (out, v[i]); <span class="keywordflow">if</span> (i > 0) out << <span class="stringliteral">" X^"</span> << i << <span class="stringliteral">" + "</span>; } <span class="keywordflow">return</span> out; } <span class="keyword">template</span> <<span class="keyword">class</span> Field, <span class="keyword">class</span> Polynomial> std::ostream& prettyprintIntegerPolynomial (std::ostream& out, <span class="keyword">const</span> <a class="code" href="class_lin_box_1_1_modular_3_01uint32__t_01_4.html" title="Specialization of class Modular for uint32_t element type.">Field</a> &F, <span class="keyword">const</span> Polynomial &v) { <span class="keywordtype">size_t</span> n = v.size()-1; <span class="keywordflow">if</span> (n == 0) { F.write(out, v[0]); } <span class="keywordflow">else</span> { <span class="keywordflow">if</span>(v[n] != 0) { <span class="keywordflow">if</span> (v[n] != 1) F.write(out, v[n]) << <span class="charliteral">'*'</span>; out << <span class="charliteral">'X'</span>; <span class="keywordflow">if</span> (n > 1) out << <span class="charliteral">'^'</span> << n; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = (<span class="keywordtype">int</span>)n - 1; i > 0; i--) { <span class="keywordflow">if</span> (v[i] != 0) { <span class="keywordflow">if</span> (v[i] >0) out << <span class="stringliteral">" + "</span>; <span class="keywordflow">if</span> (v[i] != 1) F.write (out, v[i]) << <span class="charliteral">'*'</span>; out << <span class="charliteral">'X'</span>; <span class="keywordflow">if</span> (i > 1) out << <span class="charliteral">'^'</span> << i; } } <span class="keywordflow">if</span> (v[0] != 0) { <span class="keywordflow">if</span> (v[0] >0) out << <span class="stringliteral">" + "</span>; F.write(out, v[0]); } } } <span class="keywordflow">return</span> out; } <span class="keyword">template</span> <<span class="keyword">class</span> Field, <span class="keyword">class</span> Factors, <span class="keyword">class</span> Exponents> std::ostream& printFactorization (std::ostream& out, <span class="keyword">const</span> <a class="code" href="class_lin_box_1_1_modular_3_01uint32__t_01_4.html" title="Specialization of class Modular for uint32_t element type.">Field</a> &F, <span class="keyword">const</span> Factors &f, <span class="keyword">const</span> Exponents& exp) { <span class="keyword">typename</span> Factors::const_iterator itf = f.begin(); <span class="keyword">typename</span> Exponents::const_iterator ite = exp.begin(); <span class="keywordflow">for</span> ( ; itf != f.end(); ++itf, ++ite) { prettyprintIntegerPolynomial(out << <span class="charliteral">'('</span>, F, *(*itf)) << <span class="charliteral">')'</span>; <span class="keywordflow">if</span> (*ite > 1) out << <span class="charliteral">'^'</span> << *ite; out << endl; } <span class="keywordflow">return</span> out; } <span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) { commentator.<a name="a1"></a>setMaxDetailLevel (2); commentator.<a name="a2"></a>setMaxDepth (2); commentator.<a name="a3"></a>setReportStream (std::cerr); cout<<setprecision(8); cerr<<setprecision(8); <span class="keywordflow">if</span> (argc < 2 || argc > 3) { cerr << <span class="stringliteral">"Usage: charpoly <matrix-file-in-SMS-format> [<p>]"</span> << endl; <span class="keywordflow">return</span> -1; } ifstream input (argv[1]); <span class="keywordflow">if</span> (!input) { cerr << <span class="stringliteral">"Error opening matrix file "</span> << argv[1] << endl; <span class="keywordflow">return</span> -1; } <span class="keywordflow">if</span> (argc == 2) { <a name="_a4"></a><a class="code" href="class_lin_box_1_1_p_i_d__integer.html" title="Domain for integer operations.">PID_integer</a> ZZ; <a name="_a5"></a><a class="code" href="class_lin_box_1_1_dense_matrix.html" title="Blackbox interface to dense matrix representation.">DenseMatrix<PID_integer ></a> A (ZZ); A.read (input); <span class="keyword">typedef</span> <a name="_a6"></a><a class="code" href="class_lin_box_1_1_giv_polynomial_ring.html" title="Polynomials.">GivPolynomialRing<PID_integer,::Givaro::Dense></a> <a class="code" href="class_lin_box_1_1_giv_polynomial_ring.html" title="Polynomials.">IntPolRing</a>; IntPolRing::Element c_A; Timer tim; tim.clear();tim.start(); charpoly (c_A, A, <a name="_a7"></a><a class="code" href="struct_lin_box_1_1_blackbox_specifier.html" title="BlackboxSpecifier.">Method::Blackbox</a>()); tim.stop(); cout << <span class="stringliteral">"Characteristic Polynomial is "</span>; <span class="comment">//printPolynomial (cout, ZZ, c_A) << endl;</span> cout << tim << endl; <span class="preprocessor">#ifdef __LINBOX_HAVE_NTL</span> <span class="preprocessor"></span> cout << <span class="stringliteral">"Do you want a factorization (y/n) ? "</span>; <span class="keywordtype">char</span> tmp; cin >> tmp; <span class="keywordflow">if</span> (tmp == <span class="charliteral">'y'</span> || tmp == <span class="charliteral">'Y'</span>) { commentator.<a name="a8"></a><a class="code" href="class_lin_box_1_1_commentator.html#a9b3772c898bc20f270af8d75ddc62090" title="Start an activity.">start</a>(<span class="stringliteral">"Integer Polynomial factorization by NTL"</span>, <span class="stringliteral">"NTLfac"</span>); vector<IntPolRing::Element*> intFactors; vector<unsigned long> exp; IntPolRing IPD(ZZ); tim.start(); IPD.factor (intFactors, exp, c_A); tim.stop(); commentator.<a name="a9"></a><a class="code" href="class_lin_box_1_1_commentator.html#a3a418a21b882e15baa44fe8bef7aca84" title="Stop an activity.">stop</a>(<span class="stringliteral">"done"</span>, NULL, <span class="stringliteral">"NTLfac"</span>); printFactorization(cout << intFactors.size() << <span class="stringliteral">" integer polynomial factors:"</span> << endl, ZZ, intFactors, exp) << endl; vector<IntPolRing::Element*>::const_iterator itf = intFactors.begin(); <span class="keywordflow">for</span> ( ; itf != intFactors.end(); ++itf) <span class="keyword">delete</span> *itf; cout << tim << endl; } <span class="preprocessor">#endif</span> <span class="preprocessor"></span> } <span class="keywordflow">if</span> (argc == 3) { <span class="keyword">typedef</span> <a name="_a10"></a><a class="code" href="class_lin_box_1_1_modular_3_01double_01_4.html" title="Standard representation of .">Modular<double></a> <a class="code" href="class_lin_box_1_1_modular_3_01uint32__t_01_4.html" title="Specialization of class Modular for uint32_t element type.">Field</a>; <span class="keywordtype">double</span> q = atof(argv[2]); Field F(q); <a name="_a11"></a><a class="code" href="class_lin_box_1_1_dense_matrix.html">DenseMatrix<Field></a> B (F); B.read (input); cout << <span class="stringliteral">"B is "</span> << B.rowdim() << <span class="stringliteral">" by "</span> << B.coldim() << endl; GivPolynomialRing<Field,::Givaro::Dense>::Element c_B; Timer tim; tim.clear();tim.start(); charpoly (c_B, B); tim.stop(); cout << <span class="stringliteral">"Characteristic Polynomial is "</span>; <span class="comment">//printPolynomial (cout, F, c_B) << endl;</span> cout << tim << endl; } <span class="keywordflow">return</span> 0; } </pre></div> </div> </div> <hr class="footer"/><address class="footer"><small>Generated on Tue Aug 30 2011 for linbox by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address> </body> </html>