File: Polynomial.cxx

package info (click to toggle)
clam 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 20,981
  • sloc: cpp: 92,504; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (360 lines) | stat: -rw-r--r-- 8,744 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*   Clam Copyright (C)  */


#include "Polynomial.hxx"
#include <CLAM/ProcessingData.hxx>
#include <CLAM/Array.hxx>
#include <CLAM/CLAM_Math.hxx>    


namespace CLAM
{

	//I'm not sure what this does, just copying from similar classes
	void Polynomial::DefaultInit()
	{
		//AddAll();
		UpdateData();
	}

	//solves the roots of the polynomial
	//based on an algorithm implemented in CPAN Polynomial::Solve by John M. Gamble
	//which was based on a perl binding of GNU Scientific library by Nick Ing-Simmons
	//which was based on a a fortran implementation of the QR Hessenberg algorithm by Hiroshi Murakami
	//References:
	//R. S. Martin, G. Peters and J. H. Wilkinson, "The QR Algorithm for Real Hessenberg Matrices", Numer. Math. 14, 219-231(1970).
	//B. N. Parlett and C. Reinsch, "Balancing a Matrix for Calculation of Eigenvalues and Eigenvectors", Numer. Math. 13, 293-304(1969).
	//Alan Edelman and H. Murakami, "Polynomial Roots from Companion Matrix Eigenvalues", Math. Comp., v64,#210, pp.763-776(1995).
	//Numerical Recipes in C, by William Press, Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling, Cambridge University Press (general reference).
	//Forsythe, George E., Michael A. Malcolm, and Cleve B. Moler (1977), Computer Methods for Mathematical Computations, Prentice-Hall (general reference).
	CLAM::Array <Complex> Polynomial::PolyRoots(DataArray coefficients) {
    
		Array<Complex> roots;  //an array to store the roots
    
		CLAM_ASSERT(coefficients.Size() > 0, "You gave PolyRoots() a polynomial with all zero coefficients.  ");

		//check for zero valued coefficients in the higher power terms
		//(not be needed for use w/ LPC formants, but just in case of future uses)
		while(Abs(coefficients[0]) < epsilon){ 
			coefficients.DeleteElem(0);
		}
    
		//check for zero valued coefficients in the lower powered terms
		while(Abs(coefficients[coefficients.Size()-1]) < epsilon ) {
			coefficients.DeleteElem(coefficients.Size()-1);
			roots.AddElem(0);
		}
    

		//build companion matrix
		BuildCompanion(coefficients);  //I made m, the companion matrix, a member variable instead of passing it

		//find eigenvalues of real upper hessenberg matrix
		roots = EigenHessenberg();  //this is removing a root if there was one from the while loop above

		return roots;
	}
  
  
	//cf, http://en.wikipedia.org/wiki/Companion_matrix
	//cf http://search.cpan.org/src/JGAMBLE/Math-Polynomial-Solve-2.11/lib/Math/Polynomial/Solve.pm
	void Polynomial::BuildCompanion(DataArray coefficients) 
	{
		//divide by leading coefficient:
		int co1 = coefficients[0];
		for ( int j = 0; j < coefficients.Size() ; j++ ) {
			coefficients[j]/=co1;
		}
		coefficients.DeleteElem(0);//if this is uncommented, it will be more like the example perl algorithms

		//resize matrix
		M.resize(coefficients.Size());
		for (int i=0; i<coefficients.Size(); i++)
			M[i].resize(coefficients.Size());

		//zero out matrix
		for(int i=0; i<M.size();i++)
			for(int j=0; j<M[0].size();j++)
				M[i][j] = 0;
	
		//setup sub diagonal matrix
		for (int i = 1; i < M.size(); i++) {
			M[i][i-1] =  1;
		}

		//put the coefficients into the last row:
		for (int i = 0; i < M.size(); i++) {
			M[i][M[i].size()-1] = - coefficients[M.size()-i-1]; 
		}
    
		//balancing the unsymmetric matrix:  //possible improvement: use LAPACK interface
		int notConverged=1;
		while (notConverged==1) {
			notConverged = 0;
			for(int i=0;i<M.size();i++) //go down the subdiagonal
			{
				double c;
				if(i != M.size()-1)  // if the index is not in the last row
				{
					c =  Abs(M[i+1][i]);     // (m at (i+1,i) is the subdiagonal)
				}
				else      // at the last row
				{
					c = 0;
					for(int j=0; j < M.size()-1; j++){   //add up the last column  
						c += Abs(M[j][M.size()-1]);
					}
				}
				double r;
				if(i==0)                                       // if at first row
				{
					r = Abs(M[0][M.size()-1]);   
				}
				else if(i!=M.size()-1)              // if at last row
				{
					r = Abs(M[i][i-1]) + Abs(M[i][M.size()-1]);
				}
				else                                           // if in the middle rows
				{
					r = Abs(M[i][i-1]);
				}
				if (c == 0 || r == 0) 
					continue;
	  
				double g = r/2;  //2 is given as the base of floating point represetnations.  Not sure why...
				double f = 1;
				double s = c+r;
				while(c<g)
				{
					f*=2;       //2 as "base"
					c*=4;       //4 as "base"^2
				}
				g = r*2;
				while(c>=g)
				{
					f/=2;
					c/=4;
				}
	  
				if( (c+r) < 0.95 * s * f) 
				{
					g = 1/f;
					notConverged = 1;

					//what the following code does:
					//for j=0:end m[i,j] *= g
					//for j=0:end m[j,i] *= f
					if(i==0) 
					{
						M[0][M[0].size()-1] =  M[0][M[0].size()-1] *g ;
					}
					else
					{
						M[i][i-1] =  M[i][i-1] *g ;
						M[i][M[i].size()-1] =  M[i][M[i].size()-1]*g ;
					}
	      
					if (i!=M.size()-1)
					{
						M[i+1][i] =  M[i+1][i] * f ;		  
					}
					else
					{
						for(int j=0; j<M.size(); j++)
							M[j][i] =  M[j][i]*f;
					}
	      
				} //if (c+r) < ...
			} //for(int i....

		} //while(notConverged)
	}

	Array<Complex> Polynomial::EigenHessenberg() 
	{
		double p,q,r;
		double w,x,y;
		double s,z;
		double t = 0.0;
    
		int n = M.size()-1;

		Array<Complex> roots;
    
	ROOT:
		while (n>=0) 
		{
			unsigned int iterations = 0;
			int na = n-1;
			const unsigned int maxIter = 60; //max # of iterations
			while( iterations < maxIter ) 
			{
	    
				//look for small subdiagonal element:
				unsigned int l;  // note l is lowercase L
				for (l = n; l >= 1; l--) 
				{
					if (Abs( M[l][l-1] ) <= epsilon * ( Abs( M[l-1][l-1]) + Abs(M[l][l]) ) )
						break;
				}
				x = M[n][n];
	    
				if(l==n) //one real root found
				{
					roots.AddElem(x+t);
					n--;
					goto ROOT;
				}
	    
				y = M[na][na];
				w = M[n][na] * M[na][n];
	    
				if(l == na)
				{
					p = (y-x) / 2;
					q = p*p + w;
					y = sqrt( Abs(q) );
					x += t;
	    
					if(q>0) //real pair 
					{
						if (p < 0) y = -y;
						y += p;
						roots.AddElem( x - w/y );
						roots.AddElem( x + y );
					}
					else //complex or twin pair
					{
						roots.AddElem( Complex(x + p, - y)   );
						roots.AddElem(  Complex(x + p,  y ) );
					}

					n -= 2;
					goto ROOT;
				}
	    
				if (iterations == maxIter) std::cerr<<"Too many iterations: "<<iterations<<" iterations @ n="<<n<<std::endl;

				//exceptional shift
				if((iterations>1) && (iterations % 10  == 0) ) 
				{
					t+=x;
					for(unsigned int i =0; i<=n; i++) 
					{
						M[i][i] -= x;
					}		
					s = Abs(M[n][na]) + Abs(M[na][n-2]);
					y = 0.75*x;
					x = y;
					w = -0.4375 * s*s;
				}
				iterations++;
	    
				//look for 2 consecutive small subdiagonal units
				unsigned int m;
				for(m=n-2; m >=l; m--) 
				{
					z = M[m][m];
					r = x - z;
					s = y - z;
					p = (r*s - w) / M[m+1][m] + M[m][m+1];
					q = M[m+1][m+1] - z - r - s;
					r = M[m+2][m+1];
		
					s = Abs(p) + Abs(q) + Abs(r);
					p /= s;
					q /= s;
					r /= s;
 
					if(m == l) 
						break;  //from for(m=n-1; ...
					if( Abs(M[m][m-1]) * ( Abs(q)+Abs(r) ) <=
					    epsilon  * Abs(p)  * ( Abs(M[m-1][m-1]) + Abs(z) + Abs(M[m+1][m+1]) )  )
						break; //from for(m=n-1; ...
				} //for(m=n-1; ...
	    
				for (unsigned int i = m + 2; i <= n; i++)
				{
					M[i][i-2] = 0.0;
				}
				for (unsigned int i = m+3; i <= n; i++)
				{
					M[i][i-3] =  0.0;
				}
	    
				//Double QR step for rows L-n and cols m-n 
				for(unsigned int k = m; k <= na; k++) 
				{
					bool notLast = (k!=na);
					if(k!=m)
					{
						p = M[k][k-1];
						q = M[k+1][k-1];
						r = notLast ? M[k+2][k-1] : 0;
		    
						x = Abs(p) + Abs(q) + Abs(r);
						if (x==0)
							break;

						p /= x;
						q /= x;
						r /= x;
					}
		
					s = sqrt(p*p + q*q + r*r);
					if(p<0)
						s = -s;

					if(k != m) 
						M[k][k-1] =  -s*x ;
					else if (l != m)
						M[k][k-1] =  M[k][k-1]*-1 ;
		
					p += s;
					x = p/s;
					y = q/s;
					z = r/s;
					q /= p;
					r /= p;

					//Row modification
					for (unsigned int j = k; j <= n; j++)
					{
						p = M[k][j] +  q * M[k+1][j];
		    
						if(notLast)
						{
							p += r * M[k+2][j];
							M[k+2][j] =  M[k+2][j] - p*z;
						}
		    
						M[k+1][j] =  M[k+1][j] - p*y;
						M[k][j] =  M[k][j] - p*x;
					}
		
					int j = k+3;
					if(j>n)
						j=n;
		
					//Column modification
					for(int i = l; i <= j; i++) 
					{
						p = x * M[i][k] + y * M[i][k+1];
						if(notLast)
						{
							p+= z * M[i][k+2];
							M[i][k+2] -= p*r;
						}
		    
						M[i][k+1] -=  p*q;
						M[i][k] -= p;
					}
		
				} //for k...
			} //while iterations...
		} //while n ...
		return roots;
	}
 
}