op_symmat_meat.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 op_symmat
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. op_symmat::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_symmat>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const unwrap<T1> tmp(in.m);
  25. const Mat<eT>& A = tmp.M;
  26. arma_debug_check( (A.is_square() == false), "symmatu()/symmatl(): given matrix must be square sized" );
  27. const uword N = A.n_rows;
  28. const bool upper = (in.aux_uword_a == 0);
  29. if(&out != &A)
  30. {
  31. out.copy_size(A);
  32. if(upper)
  33. {
  34. // upper triangular: copy the diagonal and the elements above the diagonal
  35. for(uword i=0; i<N; ++i)
  36. {
  37. const eT* A_data = A.colptr(i);
  38. eT* out_data = out.colptr(i);
  39. arrayops::copy( out_data, A_data, i+1 );
  40. }
  41. }
  42. else
  43. {
  44. // lower triangular: copy the diagonal and the elements below the diagonal
  45. for(uword i=0; i<N; ++i)
  46. {
  47. const eT* A_data = A.colptr(i);
  48. eT* out_data = out.colptr(i);
  49. arrayops::copy( &out_data[i], &A_data[i], N-i );
  50. }
  51. }
  52. }
  53. if(upper)
  54. {
  55. // reflect elements across the diagonal from upper triangle to lower triangle
  56. for(uword col=1; col < N; ++col)
  57. {
  58. const eT* coldata = out.colptr(col);
  59. for(uword row=0; row < col; ++row)
  60. {
  61. out.at(col,row) = coldata[row];
  62. }
  63. }
  64. }
  65. else
  66. {
  67. // reflect elements across the diagonal from lower triangle to upper triangle
  68. for(uword col=0; col < N; ++col)
  69. {
  70. const eT* coldata = out.colptr(col);
  71. for(uword row=(col+1); row < N; ++row)
  72. {
  73. out.at(col,row) = coldata[row];
  74. }
  75. }
  76. }
  77. }
  78. template<typename T1>
  79. inline
  80. void
  81. op_symmat_cx::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_symmat_cx>& in)
  82. {
  83. arma_extra_debug_sigprint();
  84. typedef typename T1::elem_type eT;
  85. const unwrap<T1> tmp(in.m);
  86. const Mat<eT>& A = tmp.M;
  87. arma_debug_check( (A.is_square() == false), "symmatu()/symmatl(): given matrix must be square sized" );
  88. const uword N = A.n_rows;
  89. const bool upper = (in.aux_uword_a == 0);
  90. const bool do_conj = (in.aux_uword_b == 1);
  91. if(&out != &A)
  92. {
  93. out.copy_size(A);
  94. if(upper)
  95. {
  96. // upper triangular: copy the diagonal and the elements above the diagonal
  97. for(uword i=0; i<N; ++i)
  98. {
  99. const eT* A_data = A.colptr(i);
  100. eT* out_data = out.colptr(i);
  101. arrayops::copy( out_data, A_data, i+1 );
  102. }
  103. }
  104. else
  105. {
  106. // lower triangular: copy the diagonal and the elements below the diagonal
  107. for(uword i=0; i<N; ++i)
  108. {
  109. const eT* A_data = A.colptr(i);
  110. eT* out_data = out.colptr(i);
  111. arrayops::copy( &out_data[i], &A_data[i], N-i );
  112. }
  113. }
  114. }
  115. if(do_conj)
  116. {
  117. if(upper)
  118. {
  119. // reflect elements across the diagonal from upper triangle to lower triangle
  120. for(uword col=1; col < N; ++col)
  121. {
  122. const eT* coldata = out.colptr(col);
  123. for(uword row=0; row < col; ++row)
  124. {
  125. out.at(col,row) = std::conj(coldata[row]);
  126. }
  127. }
  128. }
  129. else
  130. {
  131. // reflect elements across the diagonal from lower triangle to upper triangle
  132. for(uword col=0; col < N; ++col)
  133. {
  134. const eT* coldata = out.colptr(col);
  135. for(uword row=(col+1); row < N; ++row)
  136. {
  137. out.at(col,row) = std::conj(coldata[row]);
  138. }
  139. }
  140. }
  141. }
  142. else // don't do complex conjugation
  143. {
  144. if(upper)
  145. {
  146. // reflect elements across the diagonal from upper triangle to lower triangle
  147. for(uword col=1; col < N; ++col)
  148. {
  149. const eT* coldata = out.colptr(col);
  150. for(uword row=0; row < col; ++row)
  151. {
  152. out.at(col,row) = coldata[row];
  153. }
  154. }
  155. }
  156. else
  157. {
  158. // reflect elements across the diagonal from lower triangle to upper triangle
  159. for(uword col=0; col < N; ++col)
  160. {
  161. const eT* coldata = out.colptr(col);
  162. for(uword row=(col+1); row < N; ++row)
  163. {
  164. out.at(col,row) = coldata[row];
  165. }
  166. }
  167. }
  168. }
  169. }
  170. //! @}