1 // Copyright (c) 2017  John Abbott
2 // This file is part of the CoCoALib suite of examples.
3 // You are free to use any part of this example in your own programs.
4 
5 #include "CoCoA/library.H"
6 
7 using namespace std;
8 
9 //----------------------------------------------------------------------
10 const string ShortDescription =
11   "This program illustrates use of the RootBound function for      \n"
12   "obtaining an upper bound on the abs value of the complex roots. \n";
13 
14 const string LongDescription =
15   "This program illustrates use of the RootBound function for      \n"
16   "obtaining an upper bound on the abs value of the complex roots. \n"
17   "The function has an optional second arg.  The second arg says   \n"
18   "how many Graeffe iterations to perform; these lead to a better  \n"
19   "bound, but can be slow for large polynomials (as the example    \n"
20   "makes clear).                                                   \n";
21 //----------------------------------------------------------------------
22 
23 namespace CoCoA
24 {
25 
program()26   void program()
27   {
28     GlobalManager CoCoAFoundations;
29 
30     cout << ShortDescription << endl;
31     cout << boolalpha; // so that bools print out as true/false
32 
33     ring P = NewPolyRing(RingQQ(), symbols("x,y"));
34     RingElem f= RingElem(P, "x^2-2");
35     BigRat B = RootBound(f);
36 
37     //-------------------------------------------------------
38     // Now we try a larger polynomial g = product((x+k*k*k), k=1,...,200)
39     // For such a big poly "RootBound" does just a few Graeffe steps
40     // but we can use the second arg to force it to make more Graeffe steps
41     // (though this will take extra time).
42     RingElem g = one(P);
43     const RingElem& x = indet(P,0);
44     for (int k=0; k <= 250; ++k)
45       g *= (x+k*k*k);
46 
47     cout << "Let g = product((x+k*k*k), k=1,...,200)" << endl
48          << "We know the true root bound for g is 15625000." << endl << endl;
49 
50     double start = CpuTime();
51     BigRat Bg = RootBound(g);
52     const double t = CpuTime() - start;
53     cout << "RootBound uses a heuristic to decide how many Graffe iterations to use." << endl
54          << "RootBound(g) = " << Bg << "   time to compute: " << t << endl;
55 
56     cout << endl << "We can specify how many Graeffe iterations to perform as a second arg." << endl
57          << "We obtain a trade-off between speed and tightness:" << endl;
58     for (int NumGraeffe = 0; NumGraeffe < 5; ++NumGraeffe)
59     {
60       start = CpuTime();
61       Bg = RootBound(g,NumGraeffe);
62       const double t1 = CpuTime() - start;
63       cout << "RootBound(g," << NumGraeffe << ") = " << Bg << "  time to compute: " << t1 << endl;
64     }
65 
66   }
67 
68 } // end of namespace CoCoA
69 
70 //----------------------------------------------------------------------
71 // Use main() to handle any uncaught exceptions and warn the user about them.
main()72 int main()
73 {
74   try
75   {
76     CoCoA::program();
77     return 0;
78   }
79   // catch (const CoCoA::InterruptReceived& intr)
80   // {
81   //   cerr << endl
82   //        << "------------------------------" << endl
83   //        << ">>>  CoCoALib interrupted  <<<" << endl
84   //        << "------------------------------" << endl
85   //        << "-->>  " << intr << "  <<--" << endl;
86   //   return 2;
87   // }
88   catch (const CoCoA::ErrorInfo& err)
89   {
90     cerr << "***ERROR***  UNCAUGHT CoCoA error";
91     ANNOUNCE(cerr, err);
92   }
93   catch (const std::exception& exc)
94   {
95     cerr << "***ERROR***  UNCAUGHT std::exception: " << exc.what() << endl;
96   }
97   catch(...)
98   {
99     cerr << "***ERROR***  UNCAUGHT UNKNOWN EXCEPTION" << endl;
100   }
101 
102   CoCoA::BuildInfo::PrintAll(cerr);
103   return 1;
104 }
105 
106 //----------------------------------------------------------------------
107 // RCS header/log in the next few lines
108 // $Header: /Volumes/Home_1/cocoa/cvs-repository/CoCoALib-0.99/examples/ex-RootBound1.C,v 1.3 2019/11/14 17:45:18 abbott Exp $
109 // $Log: ex-RootBound1.C,v $
110 // Revision 1.3  2019/11/14 17:45:18  abbott
111 // Summary: Reduced num Graeffe iters to make it faster
112 //
113 // Revision 1.2  2018/01/17 10:24:15  abbott
114 // Summary: Changed so that it works even when SmallExponent_t is unsigned char
115 //
116 // Revision 1.1  2017/09/14 15:54:54  abbott
117 // Summary: Added RootBound
118 //
119 // Revision 1.12  2017/07/08 19:07:02  abbott
120 // Summary: Removed comment out (dodgy) code for reporting unhandled interrupts
121 //
122 // Revision 1.11  2016/11/18 18:05:15  abbott
123 // Summary: Added commented out code to catch InterruptReceived
124 //
125 // Revision 1.10  2015/06/29 14:23:19  abbott
126 // Summary: added missing CoCoA:: prefix
127 // Author: JAA
128 //
129 // Revision 1.9  2015/06/29 13:25:54  bigatti
130 // -- code in namespace CoCoA
131 //
132 // Revision 1.8  2015/06/25 14:19:02  abbott
133 // Summary: Added call to CoCoA::BuildInfo::Printall
134 // Author: JAA
135 //
136 // Revision 1.7  2013/05/28 07:07:04  bigatti
137 // -- added "cout << boolalpha": useful for testing
138 //
139 // Revision 1.6  2012/11/30 14:04:55  abbott
140 // Increased visibility of comment saying "put your code here".
141 //
142 // Revision 1.5  2010/12/17 16:07:54  abbott
143 // Ensured that all i/o in examples is on standard C++ streams
144 // (rather than GlobalInput(), etc).
145 //
146 // Revision 1.4  2008/10/07 12:12:54  abbott
147 // Removed useless commented out #include.
148 //
149 // Revision 1.3  2007/05/31 16:06:16  bigatti
150 // -- removed previous unwanted checked-in version
151 //
152 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
153 // Imported files
154 //
155 // Revision 1.9  2007/03/07 11:51:40  bigatti
156 // -- improved test alignment
157 //
158 // Revision 1.8  2007/03/03 14:15:45  bigatti
159 // -- "foundations" renamed into "GlobalManager"
160 //
161 // Revision 1.7  2007/03/02 17:46:40  bigatti
162 // -- unique RingZ and RingQ
163 // -- requires foundations.H ;  foundations blah;  (thik of a better name)
164 //
165 // Revision 1.6  2007/03/02 10:47:53  cocoa
166 // First stage of RingZ modifications -- tests do not compile currently, Anna will fix this.
167 //
168 // Revision 1.5  2007/03/01 13:52:59  bigatti
169 // -- minor: fixed typo
170 //
171 // Revision 1.4  2007/02/28 15:15:56  bigatti
172 // -- minor: removed quotes in description
173 //
174 // Revision 1.3  2007/02/12 16:27:43  bigatti
175 // -- added strings ShortDescription and LongDescription for indexing
176 //
177 // Revision 1.2  2007/02/10 18:44:03  cocoa
178 // Added "const" twice to each test and example.
179 // Eliminated dependency on io.H in several files.
180 // Improved BuildInfo, and added an example about how to use it.
181 // Some other minor cleaning.
182 //
183 // Revision 1.1.1.1  2006/05/30 11:39:36  cocoa
184 // Imported files
185 //
186 // Revision 1.1  2006/03/12 21:28:34  cocoa
187 // Major check in after many changes
188 //
189