gsl_eigen__qrstep.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* eigen/qrstep.c
  2. *
  3. * Copyright (C) 2007 Brian Gough
  4. *
  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.
  9. *
  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.
  14. *
  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.
  18. */
  19. /* remove off-diagonal elements which are neglegible compared with the
  20. neighboring diagonal elements */
  21. static void
  22. chop_small_elements (const size_t N, const double d[], double sd[])
  23. {
  24. double d_i = d[0];
  25. size_t i;
  26. for (i = 0; i < N - 1; i++)
  27. {
  28. double sd_i = sd[i];
  29. double d_ip1 = d[i + 1];
  30. if (fabs (sd_i) < GSL_DBL_EPSILON * (fabs (d_i) + fabs (d_ip1)))
  31. {
  32. sd[i] = 0.0;
  33. }
  34. d_i = d_ip1;
  35. }
  36. }
  37. /* Generate a Givens rotation (cos,sin) which takes v=(x,y) to (|v|,0)
  38. From Golub and Van Loan, "Matrix Computations", Section 5.1.8 */
  39. inline static void
  40. create_givens (const double a, const double b, double *c, double *s)
  41. {
  42. if (b == 0)
  43. {
  44. *c = 1;
  45. *s = 0;
  46. }
  47. else if (fabs (b) > fabs (a))
  48. {
  49. double t = -a / b;
  50. double s1 = 1.0 / sqrt (1 + t * t);
  51. *s = s1;
  52. *c = s1 * t;
  53. }
  54. else
  55. {
  56. double t = -b / a;
  57. double c1 = 1.0 / sqrt (1 + t * t);
  58. *c = c1;
  59. *s = c1 * t;
  60. }
  61. }
  62. inline static double
  63. trailing_eigenvalue (const size_t n, const double d[], const double sd[])
  64. {
  65. double ta = d[n - 2];
  66. double tb = d[n - 1];
  67. double tab = sd[n - 2];
  68. double dt = (ta - tb) / 2.0;
  69. double mu;
  70. if (dt > 0)
  71. {
  72. mu = tb - tab * (tab / (dt + hypot (dt, tab)));
  73. }
  74. else if (dt == 0)
  75. {
  76. mu = tb - fabs(tab);
  77. }
  78. else
  79. {
  80. mu = tb + tab * (tab / ((-dt) + hypot (dt, tab)));
  81. }
  82. return mu;
  83. }
  84. static void
  85. qrstep (const size_t n, double d[], double sd[], double gc[], double gs[])
  86. {
  87. double x, z;
  88. double ak, bk, zk, ap, bp, aq, bq;
  89. size_t k;
  90. double mu = trailing_eigenvalue (n, d, sd);
  91. x = d[0] - mu;
  92. z = sd[0];
  93. ak = 0;
  94. bk = 0;
  95. zk = 0;
  96. ap = d[0];
  97. bp = sd[0];
  98. aq = d[1];
  99. if (n == 2)
  100. {
  101. double c, s;
  102. create_givens (x, z, &c, &s);
  103. if (gc != NULL)
  104. gc[0] = c;
  105. if (gs != NULL)
  106. gs[0] = s;
  107. {
  108. double ap1 = c * (c * ap - s * bp) + s * (s * aq - c * bp);
  109. double bp1 = c * (s * ap + c * bp) - s * (s * bp + c * aq);
  110. double aq1 = s * (s * ap + c * bp) + c * (s * bp + c * aq);
  111. ak = ap1;
  112. bk = bp1;
  113. ap = aq1;
  114. }
  115. d[0] = ak;
  116. sd[0] = bk;
  117. d[1] = ap;
  118. return;
  119. }
  120. bq = sd[1];
  121. for (k = 0; k < n - 1; k++)
  122. {
  123. double c, s;
  124. create_givens (x, z, &c, &s);
  125. /* store Givens rotation */
  126. if (gc != NULL)
  127. gc[k] = c;
  128. if (gs != NULL)
  129. gs[k] = s;
  130. /* compute G' T G */
  131. {
  132. double bk1 = c * bk - s * zk;
  133. double ap1 = c * (c * ap - s * bp) + s * (s * aq - c * bp);
  134. double bp1 = c * (s * ap + c * bp) - s * (s * bp + c * aq);
  135. double zp1 = -s * bq;
  136. double aq1 = s * (s * ap + c * bp) + c * (s * bp + c * aq);
  137. double bq1 = c * bq;
  138. ak = ap1;
  139. bk = bp1;
  140. zk = zp1;
  141. ap = aq1;
  142. bp = bq1;
  143. if (k < n - 2)
  144. aq = d[k + 2];
  145. if (k < n - 3)
  146. bq = sd[k + 2];
  147. d[k] = ak;
  148. if (k > 0)
  149. sd[k - 1] = bk1;
  150. if (k < n - 2)
  151. sd[k + 1] = bp;
  152. x = bk;
  153. z = zk;
  154. }
  155. }
  156. /* k = n - 1 */
  157. d[k] = ap;
  158. sd[k - 1] = bk;
  159. }