fn_eigs_sym.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2011-2017 Ryan Curtin (http://www.ratml.org/)
  2. // Copyright 2017 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. #include <armadillo>
  16. #include "catch.hpp"
  17. using namespace arma;
  18. TEST_CASE("fn_eigs_test")
  19. {
  20. for (size_t trial = 0; trial < 10; ++trial)
  21. {
  22. // Test ARPACK decomposition of sparse matrices.
  23. sp_mat m(1000, 1000);
  24. sp_vec dd;
  25. for (size_t i = 0; i < 10; ++i)
  26. {
  27. dd.sprandu(1000, 1, 0.15);
  28. double eig = rand();
  29. m += eig * dd * dd.t();
  30. }
  31. mat d(m);
  32. // Eigendecompose, getting first 5 eigenvectors.
  33. vec sp_eigval;
  34. mat sp_eigvec;
  35. eigs_sym(sp_eigval, sp_eigvec, m, 5);
  36. // Do the same for the dense case.
  37. vec eigval;
  38. mat eigvec;
  39. eig_sym(eigval, eigvec, d);
  40. for (uword i = 0; i < 5; ++i)
  41. {
  42. // It may be pointed the wrong direction.
  43. REQUIRE( sp_eigval[i] == Approx(eigval[i + 995]).epsilon(0.01) );
  44. for (uword j = 0; j < 1000; ++j)
  45. {
  46. REQUIRE( std::abs(sp_eigvec(j, i)) ==
  47. Approx(std::abs(eigvec(j, i + 995))).epsilon(0.01) );
  48. }
  49. }
  50. }
  51. }
  52. TEST_CASE("fn_eigs_float_test")
  53. {
  54. for (size_t trial = 0; trial < 10; ++trial)
  55. {
  56. // Test ARPACK decomposition of sparse matrices.
  57. SpMat<float> m(100, 100);
  58. SpCol<float> dd;
  59. for (size_t i = 0; i < 10; ++i)
  60. {
  61. dd.sprandu(100, 1, 0.15);
  62. float eig = rand();
  63. m += 0.000001 * eig * dd * dd.t();
  64. }
  65. Mat<float> d(m);
  66. // Eigendecompose, getting first 5 eigenvectors.
  67. Col<float> sp_eigval;
  68. Mat<float> sp_eigvec;
  69. eigs_sym(sp_eigval, sp_eigvec, m, 5);
  70. // Do the same for the dense case.
  71. Col<float> eigval;
  72. Mat<float> eigvec;
  73. eig_sym(eigval, eigvec, d);
  74. for (uword i = 0; i < 5; ++i)
  75. {
  76. // It may be pointed the wrong direction.
  77. REQUIRE( sp_eigval[i] == Approx(eigval[i + 95]).epsilon(0.01) );
  78. for (uword j = 0; j < 100; ++j)
  79. {
  80. REQUIRE(std::abs(sp_eigvec(j, i)) ==
  81. Approx(std::abs(eigvec(j, i + 95))).epsilon(0.01) );
  82. }
  83. }
  84. }
  85. }
  86. TEST_CASE("fn_eigs_sm_test")
  87. {
  88. for (size_t trial = 0; trial < 10; ++trial)
  89. {
  90. // Test ARPACK decomposition of sparse matrices.
  91. sp_mat m(100, 100);
  92. sp_vec dd;
  93. for (uword i = 0; i < 100; ++i)
  94. {
  95. m(i, i) = i + 10;
  96. }
  97. mat d(m);
  98. // Eigendecompose, getting first 5 eigenvectors.
  99. vec sp_eigval;
  100. mat sp_eigvec;
  101. eigs_sym(sp_eigval, sp_eigvec, m, 5, "sm");
  102. // Do the same for the dense case.
  103. vec eigval;
  104. mat eigvec;
  105. eig_sym(eigval, eigvec, d);
  106. for (size_t i = 0; i < 5; ++i)
  107. {
  108. // It may be pointed the wrong direction.
  109. REQUIRE( sp_eigval[i] == Approx(eigval[i]).epsilon(0.01) );
  110. for (size_t j = 0; j < 100; ++j)
  111. {
  112. REQUIRE( std::abs(sp_eigvec(j, i)) ==
  113. Approx(std::abs(eigvec(j, i))).epsilon(0.01) );
  114. }
  115. }
  116. }
  117. }