decomp_eig_gen.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2015 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2015 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("decomp_eig_gen_1")
  19. {
  20. mat A =
  21. "\
  22. 0.061198 0.201990 0.019678 -0.493936 -0.126745;\
  23. 0.437242 0.058956 -0.149362 -0.045465 0.296153;\
  24. -0.492474 -0.031309 0.314156 0.419733 0.068317;\
  25. 0.336352 0.411541 0.458476 -0.393139 -0.135040;\
  26. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768;\
  27. ";
  28. cx_vec eigvals1 =
  29. {
  30. cx_double(-0.431507827005653, +0.336567219978257),
  31. cx_double(-0.431507827005653, -0.336567219978257),
  32. cx_double( 0.509611570246060, +0.000000000000000),
  33. cx_double( 0.020403541882623, +0.255686097698784),
  34. cx_double( 0.020403541882623, -0.255686097698784)
  35. };
  36. cx_vec eigvals2 = eig_gen(A);
  37. cx_vec eigvals3;
  38. bool status = eig_gen(eigvals3, A);
  39. cx_vec eigvals4;
  40. cx_mat eigvecs4;
  41. eig_gen(eigvals4, eigvecs4, A);
  42. cx_vec eigvals5;
  43. cx_mat leigvecs5;
  44. cx_mat reigvecs5;
  45. eig_gen(eigvals5, leigvecs5, reigvecs5, A);
  46. cx_mat B = eigvecs4 * diagmat(eigvals4) * inv( eigvecs4);
  47. cx_mat Cl = inv(trans(leigvecs5)) * diagmat(eigvals5) * trans(leigvecs5);
  48. cx_mat Cr = reigvecs5 * diagmat(eigvals5) * inv(reigvecs5);
  49. REQUIRE( status == true );
  50. REQUIRE( accu(abs(eigvals2 - eigvals1)) == Approx(0.0).epsilon(0.0001) );
  51. REQUIRE( accu(abs(eigvals3 - eigvals1)) == Approx(0.0).epsilon(0.0001) );
  52. REQUIRE( accu(abs(eigvals4 - eigvals1)) == Approx(0.0).epsilon(0.0001) );
  53. REQUIRE( accu(abs(eigvals5 - eigvals1)) == Approx(0.0).epsilon(0.0001) );
  54. REQUIRE( accu(abs(A - B )) == Approx(0.0).epsilon(0.0001) );
  55. REQUIRE( accu(abs(A - Cl )) == Approx(0.0).epsilon(0.0001) );
  56. REQUIRE( accu(abs(A - Cr )) == Approx(0.0).epsilon(0.0001) );
  57. }
  58. TEST_CASE("decomp_eig_gen_2")
  59. {
  60. cx_mat A =
  61. {
  62. { cx_double( 0.111205, +0.074101), cx_double(-0.225872, -0.068474), cx_double(-0.192660, +0.236887), cx_double( 0.355204, -0.355735) },
  63. { cx_double( 0.119869, +0.217667), cx_double(-0.412722, +0.366157), cx_double( 0.069916, -0.222238), cx_double( 0.234987, -0.072355) },
  64. { cx_double( 0.003791, +0.183253), cx_double(-0.212887, -0.172758), cx_double( 0.168689, -0.393418), cx_double( 0.008795, -0.289654) },
  65. { cx_double(-0.331639, -0.166660), cx_double( 0.436969, -0.313498), cx_double(-0.431574, +0.017421), cx_double(-0.104165, +0.145246) }
  66. };
  67. cx_vec eigvals1 =
  68. {
  69. cx_double(-0.47418, +0.60377),
  70. cx_double( 0.15084, -0.44209),
  71. cx_double(-0.15790, -0.35629),
  72. cx_double( 0.24426, +0.38670)
  73. };
  74. cx_vec eigvals2 = eig_gen(A);
  75. cx_vec eigvals3;
  76. bool status = eig_gen(eigvals3, A);
  77. cx_vec eigvals4;
  78. cx_mat eigvecs4;
  79. eig_gen(eigvals4, eigvecs4, A);
  80. cx_vec eigvals5;
  81. cx_mat leigvecs5;
  82. cx_mat reigvecs5;
  83. eig_gen(eigvals5, leigvecs5, reigvecs5, A);
  84. cx_mat B = eigvecs4 * diagmat(eigvals4) * inv( eigvecs4);
  85. cx_mat Cl = inv(trans(leigvecs5)) * diagmat(eigvals5) * trans(leigvecs5);
  86. cx_mat Cr = reigvecs5 * diagmat(eigvals5) * inv(reigvecs5);
  87. REQUIRE( status == true );
  88. REQUIRE( accu(abs(eigvals2 - eigvals1)) == Approx(0.0).epsilon(0.0001) );
  89. REQUIRE( accu(abs(eigvals3 - eigvals1)) == Approx(0.0).epsilon(0.0001) );
  90. REQUIRE( accu(abs(eigvals4 - eigvals1)) == Approx(0.0).epsilon(0.0001) );
  91. REQUIRE( accu(abs(eigvals5 - eigvals1)) == Approx(0.0).epsilon(0.0001) );
  92. REQUIRE( accu(abs(A - B )) == Approx(0.0).epsilon(0.0001) );
  93. REQUIRE( accu(abs(A - Cl )) == Approx(0.0).epsilon(0.0001) );
  94. REQUIRE( accu(abs(A - Cr )) == Approx(0.0).epsilon(0.0001) );
  95. }
  96. TEST_CASE("decomp_eig_gen_3")
  97. {
  98. mat A(5,5,fill::randu);
  99. cx_vec eigvals(10, fill::randu);
  100. cx_mat eigvecs(10, 10, fill::randu);
  101. A(0,0) = datum::inf;
  102. bool status = eig_gen(eigvals, eigvecs, A);
  103. REQUIRE( status == false );
  104. REQUIRE( eigvals.n_elem == uword(0) );
  105. REQUIRE( eigvecs.n_elem == uword(0) );
  106. }
  107. TEST_CASE("decomp_eig_gen_4")
  108. {
  109. mat A(5,6,fill::randu);
  110. cx_vec eigvals;
  111. cx_mat eigvecs;
  112. REQUIRE_THROWS( eig_gen(eigvals, eigvecs, A) );
  113. }