gsl_eigen__herm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* eigen/herm.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 of complex hermitian matrix using reduction to
  27. real symmetric tridiagonal form, followed by QR iteration with
  28. implicit shifts.
  29. See Golub & Van Loan, "Matrix Computations" (3rd ed), Section 8.3 */
  30. #include "gsl_eigen__qrstep.c"
  31. gsl_eigen_herm_workspace *
  32. gsl_eigen_herm_alloc (const size_t n)
  33. {
  34. gsl_eigen_herm_workspace * w ;
  35. if (n == 0)
  36. {
  37. GSL_ERROR_NULL ("matrix dimension must be positive integer", GSL_EINVAL);
  38. }
  39. w = (gsl_eigen_herm_workspace *) malloc (sizeof(gsl_eigen_herm_workspace));
  40. if (w == 0)
  41. {
  42. GSL_ERROR_NULL ("failed to allocate space for workspace", GSL_ENOMEM);
  43. }
  44. w->d = (double *) malloc (n * sizeof (double));
  45. if (w->d == 0)
  46. {
  47. GSL_ERROR_NULL ("failed to allocate space for diagonal", GSL_ENOMEM);
  48. }
  49. w->sd = (double *) malloc (n * sizeof (double));
  50. if (w->sd == 0)
  51. {
  52. GSL_ERROR_NULL ("failed to allocate space for subdiagonal", GSL_ENOMEM);
  53. }
  54. w->tau = (double *) malloc (2 * n * sizeof (double));
  55. if (w->tau == 0)
  56. {
  57. GSL_ERROR_NULL ("failed to allocate space for tau", GSL_ENOMEM);
  58. }
  59. w->size = n;
  60. return w;
  61. }
  62. void
  63. gsl_eigen_herm_free (gsl_eigen_herm_workspace * w)
  64. {
  65. free (w->tau);
  66. free (w->sd);
  67. free (w->d);
  68. free(w);
  69. }
  70. int
  71. gsl_eigen_herm (gsl_matrix_complex * A, gsl_vector * eval,
  72. gsl_eigen_herm_workspace * w)
  73. {
  74. if (A->size1 != A->size2)
  75. {
  76. GSL_ERROR ("matrix must be square to compute eigenvalues", GSL_ENOTSQR);
  77. }
  78. else if (eval->size != A->size1)
  79. {
  80. GSL_ERROR ("eigenvalue vector must match matrix size", GSL_EBADLEN);
  81. }
  82. else
  83. {
  84. const size_t N = A->size1;
  85. double *const d = w->d;
  86. double *const sd = w->sd;
  87. size_t a, b;
  88. /* handle special case */
  89. if (N == 1)
  90. {
  91. gsl_complex A00 = gsl_matrix_complex_get (A, 0, 0);
  92. gsl_vector_set (eval, 0, GSL_REAL(A00));
  93. return GSL_SUCCESS;
  94. }
  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_complex_view tau_vec = gsl_vector_complex_view_array (w->tau, N-1);
  99. gsl_linalg_hermtd_decomp (A, &tau_vec.vector);
  100. gsl_linalg_hermtd_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. }