3 * Copyright (C) 2007 Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* remove off-diagonal elements which are neglegible compared with the
21 neighboring diagonal elements */
24 chop_small_elements (const size_t N, const double d[], double sd[])
30 for (i = 0; i < N - 1; i++)
33 double d_ip1 = d[i + 1];
35 if (fabs (sd_i) < GSL_DBL_EPSILON * (fabs (d_i) + fabs (d_ip1)))
43 /* Generate a Givens rotation (cos,sin) which takes v=(x,y) to (|v|,0)
45 From Golub and Van Loan, "Matrix Computations", Section 5.1.8 */
48 create_givens (const double a, const double b, double *c, double *s)
55 else if (fabs (b) > fabs (a))
58 double s1 = 1.0 / sqrt (1 + t * t);
65 double c1 = 1.0 / sqrt (1 + t * t);
72 trailing_eigenvalue (const size_t n, const double d[], const double sd[])
76 double tab = sd[n - 2];
78 double dt = (ta - tb) / 2.0;
84 mu = tb - tab * (tab / (dt + hypot (dt, tab)));
92 mu = tb + tab * (tab / ((-dt) + hypot (dt, tab)));
99 qrstep (const size_t n, double d[], double sd[], double gc[], double gs[])
102 double ak, bk, zk, ap, bp, aq, bq;
105 double mu = trailing_eigenvalue (n, d, sd);
122 create_givens (x, z, &c, &s);
130 double ap1 = c * (c * ap - s * bp) + s * (s * aq - c * bp);
131 double bp1 = c * (s * ap + c * bp) - s * (s * bp + c * aq);
133 double aq1 = s * (s * ap + c * bp) + c * (s * bp + c * aq);
150 for (k = 0; k < n - 1; k++)
153 create_givens (x, z, &c, &s);
155 /* store Givens rotation */
164 double bk1 = c * bk - s * zk;
166 double ap1 = c * (c * ap - s * bp) + s * (s * aq - c * bp);
167 double bp1 = c * (s * ap + c * bp) - s * (s * bp + c * aq);
168 double zp1 = -s * bq;
170 double aq1 = s * (s * ap + c * bp) + c * (s * bp + c * aq);