gsl_eigen__symm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* eigen/symm.c
  2. *
  3. * Copyright (C) 2001, 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. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include "gsl_math.h"
  22. #include "gsl_vector.h"
  23. #include "gsl_matrix.h"
  24. #include "gsl_linalg.h"
  25. #include "gsl_eigen.h"
  26. /* Compute eigenvalues/eigenvectors of real symmetric matrix using
  27. reduction to tridiagonal form, followed by QR iteration with
  28. implicit shifts.
  29. See Golub & Van Loan, "Matrix Computations" (3rd ed), Section 8.3
  30. */
  31. #include "gsl_eigen__qrstep.c"
  32. gsl_eigen_symm_workspace *
  33. gsl_eigen_symm_alloc (const size_t n)
  34. {
  35. gsl_eigen_symm_workspace *w;
  36. if (n == 0)
  37. {
  38. GSL_ERROR_NULL ("matrix dimension must be positive integer",
  39. GSL_EINVAL);
  40. }
  41. w = ((gsl_eigen_symm_workspace *)
  42. malloc (sizeof (gsl_eigen_symm_workspace)));
  43. if (w == 0)
  44. {
  45. GSL_ERROR_NULL ("failed to allocate space for workspace", GSL_ENOMEM);
  46. }
  47. w->d = (double *) malloc (n * sizeof (double));
  48. if (w->d == 0)
  49. {
  50. GSL_ERROR_NULL ("failed to allocate space for diagonal", GSL_ENOMEM);
  51. }
  52. w->sd = (double *) malloc (n * sizeof (double));
  53. if (w->sd == 0)
  54. {
  55. GSL_ERROR_NULL ("failed to allocate space for subdiagonal", GSL_ENOMEM);
  56. }
  57. w->size = n;
  58. return w;
  59. }
  60. void
  61. gsl_eigen_symm_free (gsl_eigen_symm_workspace * w)
  62. {
  63. free (w->sd);
  64. free (w->d);
  65. free (w);
  66. }
  67. int
  68. gsl_eigen_symm (gsl_matrix * A, gsl_vector * eval,
  69. gsl_eigen_symm_workspace * w)
  70. {
  71. if (A->size1 != A->size2)
  72. {
  73. GSL_ERROR ("matrix must be square to compute eigenvalues", GSL_ENOTSQR);
  74. }
  75. else if (eval->size != A->size1)
  76. {
  77. GSL_ERROR ("eigenvalue vector must match matrix size", GSL_EBADLEN);
  78. }
  79. else
  80. {
  81. const size_t N = A->size1;
  82. double *const d = w->d;
  83. double *const sd = w->sd;
  84. size_t a, b;
  85. /* handle special case */
  86. if (N == 1)
  87. {
  88. double A00 = gsl_matrix_get (A, 0, 0);
  89. gsl_vector_set (eval, 0, A00);
  90. return GSL_SUCCESS;
  91. }
  92. /* use sd as the temporary workspace for the decomposition,
  93. since we can discard the tau result immediately if we are not
  94. computing eigenvectors */
  95. {
  96. gsl_vector_view d_vec = gsl_vector_view_array (d, N);
  97. gsl_vector_view sd_vec = gsl_vector_view_array (sd, N - 1);
  98. gsl_vector_view tau = gsl_vector_view_array (sd, N - 1);
  99. gsl_linalg_symmtd_decomp (A, &tau.vector);
  100. gsl_linalg_symmtd_unpack_T (A, &d_vec.vector, &sd_vec.vector);
  101. }
  102. /* Make an initial pass through the tridiagonal decomposition
  103. to remove off-diagonal elements which are effectively zero */
  104. chop_small_elements (N, d, sd);
  105. /* Progressively reduce the matrix until it is diagonal */
  106. b = N - 1;
  107. while (b > 0)
  108. {
  109. if (sd[b - 1] == 0.0 || isnan(sd[b - 1]))
  110. {
  111. b--;
  112. continue;
  113. }
  114. /* Find the largest unreduced block (a,b) starting from b
  115. and working backwards */
  116. a = b - 1;
  117. while (a > 0)
  118. {
  119. if (sd[a - 1] == 0.0)
  120. {
  121. break;
  122. }
  123. a--;
  124. }
  125. {
  126. const size_t n_block = b - a + 1;
  127. double *d_block = d + a;
  128. double *sd_block = sd + a;
  129. /* apply QR reduction with implicit deflation to the
  130. unreduced block */
  131. qrstep (n_block, d_block, sd_block, NULL, NULL);
  132. /* remove any small off-diagonal elements */
  133. chop_small_elements (n_block, d_block, sd_block);
  134. }
  135. }
  136. {
  137. gsl_vector_view d_vec = gsl_vector_view_array (d, N);
  138. gsl_vector_memcpy (eval, &d_vec.vector);
  139. }
  140. return GSL_SUCCESS;
  141. }
  142. }