sympd_helper.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup sympd_helper
  16. //! @{
  17. namespace sympd_helper
  18. {
  19. // computationally inexpensive algorithm to guess whether a matrix is positive definite:
  20. // (1) ensure the matrix is symmetric/hermitian (within a tolerance)
  21. // (2) ensure the diagonal entries are real and greater than zero
  22. // (3) ensure that the value with largest modulus is on the main diagonal
  23. // (4) ensure rudimentary diagonal dominance: (real(A_ii) + real(A_jj)) > 2*abs(real(A_ij))
  24. // the above conditions are necessary, but not sufficient;
  25. // doing it properly would be too computationally expensive for our purposes
  26. // more info:
  27. // http://mathworld.wolfram.com/PositiveDefiniteMatrix.html
  28. // http://mathworld.wolfram.com/DiagonallyDominantMatrix.html
  29. template<uword threshold, typename eT>
  30. inline
  31. typename enable_if2<is_cx<eT>::no, bool>::result
  32. guess_sympd_worker(const Mat<eT>& A)
  33. {
  34. arma_extra_debug_sigprint();
  35. if((A.n_rows != A.n_cols) || (A.n_rows < threshold)) { return false; }
  36. const eT tol = eT(100) * std::numeric_limits<eT>::epsilon(); // allow some leeway
  37. const uword N = A.n_rows;
  38. const eT* A_mem = A.memptr();
  39. const eT* A_col = A_mem;
  40. eT max_diag = eT(0);
  41. for(uword j=0; j < N; ++j)
  42. {
  43. const eT A_jj = A_col[j];
  44. if(A_jj <= eT(0)) { return false; }
  45. max_diag = (A_jj > max_diag) ? A_jj : max_diag;
  46. A_col += N;
  47. }
  48. A_col = A_mem;
  49. const uword Nm1 = N-1;
  50. const uword Np1 = N+1;
  51. for(uword j=0; j < Nm1; ++j)
  52. {
  53. const eT A_jj = A_col[j];
  54. const uword jp1 = j+1;
  55. const eT* A_ji_ptr = &(A_mem[j + jp1*N]); // &(A.at(j,jp1));
  56. const eT* A_ii_ptr = &(A_mem[jp1 + jp1*N]);
  57. for(uword i=jp1; i < N; ++i)
  58. {
  59. const eT A_ij = A_col[i];
  60. const eT A_ji = (*A_ji_ptr);
  61. const eT A_ij_abs = (std::abs)(A_ij);
  62. const eT A_ji_abs = (std::abs)(A_ji);
  63. // if( (A_ij_abs >= max_diag) || (A_ji_abs >= max_diag) ) { return false; }
  64. if(A_ij_abs >= max_diag) { return false; }
  65. const eT A_delta = (std::abs)(A_ij - A_ji);
  66. const eT A_abs_max = (std::max)(A_ij_abs, A_ji_abs);
  67. if( (A_delta > tol) && (A_delta > (A_abs_max*tol)) ) { return false; }
  68. const eT A_ii = (*A_ii_ptr);
  69. if( (A_ij_abs + A_ij_abs) >= (A_ii + A_jj) ) { return false; }
  70. A_ji_ptr += N;
  71. A_ii_ptr += Np1;
  72. }
  73. A_col += N;
  74. }
  75. return true;
  76. }
  77. template<uword threshold, typename eT>
  78. inline
  79. typename enable_if2<is_cx<eT>::yes, bool>::result
  80. guess_sympd_worker(const Mat<eT>& A)
  81. {
  82. arma_extra_debug_sigprint();
  83. typedef typename get_pod_type<eT>::result T;
  84. if((A.n_rows != A.n_cols) || (A.n_rows < threshold)) { return false; }
  85. const T tol = T(100) * std::numeric_limits<T>::epsilon(); // allow some leeway
  86. const uword N = A.n_rows;
  87. const eT* A_mem = A.memptr();
  88. const eT* A_col = A_mem;
  89. T max_diag = T(0);
  90. for(uword j=0; j < N; ++j)
  91. {
  92. const eT& A_jj = A_col[j];
  93. const T A_jj_real = std::real(A_jj);
  94. const T A_jj_imag = std::imag(A_jj);
  95. if( (A_jj_real <= T(0)) || (std::abs(A_jj_imag) > tol) ) { return false; }
  96. max_diag = (A_jj_real > max_diag) ? A_jj_real : max_diag;
  97. A_col += N;
  98. }
  99. const T square_max_diag = max_diag * max_diag;
  100. if(arma_isfinite(square_max_diag) == false) { return false; }
  101. A_col = A_mem;
  102. const uword Nm1 = N-1;
  103. const uword Np1 = N+1;
  104. for(uword j=0; j < Nm1; ++j)
  105. {
  106. const uword jp1 = j+1;
  107. const eT* A_ji_ptr = &(A_mem[j + jp1*N]); // &(A.at(j,jp1));
  108. const eT* A_ii_ptr = &(A_mem[jp1 + jp1*N]);
  109. const T A_jj_real = std::real(A_col[j]);
  110. for(uword i=jp1; i < N; ++i)
  111. {
  112. const eT& A_ij = A_col[i];
  113. const T A_ij_real = std::real(A_ij);
  114. const T A_ij_imag = std::imag(A_ij);
  115. // avoid using std::abs(), as that is time consuming due to division and std::sqrt()
  116. const T square_A_ij_abs = (A_ij_real * A_ij_real) + (A_ij_imag * A_ij_imag);
  117. if(arma_isfinite(square_A_ij_abs) == false) { return false; }
  118. if(square_A_ij_abs >= square_max_diag) { return false; }
  119. const T A_ij_real_abs = (std::abs)(A_ij_real);
  120. const T A_ij_imag_abs = (std::abs)(A_ij_imag);
  121. const eT& A_ji = (*A_ji_ptr);
  122. const T A_ji_real = std::real(A_ji);
  123. const T A_ji_imag = std::imag(A_ji);
  124. const T A_ji_real_abs = (std::abs)(A_ji_real);
  125. const T A_ji_imag_abs = (std::abs)(A_ji_imag);
  126. const T A_real_delta = (std::abs)(A_ij_real - A_ji_real);
  127. const T A_real_abs_max = (std::max)(A_ij_real_abs, A_ji_real_abs);
  128. if( (A_real_delta > tol) && (A_real_delta > (A_real_abs_max*tol)) ) { return false; }
  129. const T A_imag_delta = (std::abs)(A_ij_imag + A_ji_imag); // take into account complex conjugate
  130. const T A_imag_abs_max = (std::max)(A_ij_imag_abs, A_ji_imag_abs);
  131. if( (A_imag_delta > tol) && (A_imag_delta > (A_imag_abs_max*tol)) ) { return false; }
  132. const T A_ii_real = std::real(*A_ii_ptr);
  133. if( (A_ij_real_abs + A_ij_real_abs) >= (A_ii_real + A_jj_real) ) { return false; }
  134. A_ji_ptr += N;
  135. A_ii_ptr += Np1;
  136. }
  137. A_col += N;
  138. }
  139. return true;
  140. }
  141. template<typename eT>
  142. inline
  143. bool
  144. guess_sympd(const Mat<eT>& A)
  145. {
  146. // analyse matrices with size >= 16x16
  147. return guess_sympd_worker<16u>(A);
  148. }
  149. template<typename eT>
  150. inline
  151. bool
  152. guess_sympd_anysize(const Mat<eT>& A)
  153. {
  154. // analyse matrices with size >= 2x2
  155. return guess_sympd_worker<2u>(A);
  156. }
  157. } // end of namespace sympd_helper
  158. //! @}