trimat_helper.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 trimat_helper
  16. //! @{
  17. namespace trimat_helper
  18. {
  19. template<typename eT>
  20. inline
  21. bool
  22. is_triu(const Mat<eT>& A)
  23. {
  24. arma_extra_debug_sigprint();
  25. // NOTE: assuming that A has a square size
  26. const uword N = A.n_rows;
  27. const uword Nm1 = N-1;
  28. if(N < 2) { return false; }
  29. const eT* A_mem = A.memptr();
  30. const eT eT_zero = eT(0);
  31. // quickly check bottom-left corner
  32. const eT* A_col0 = A_mem;
  33. const eT* A_col1 = A_col0 + N;
  34. if( (A_col0[N-2] != eT_zero) || (A_col0[Nm1] != eT_zero) || (A_col1[Nm1] != eT_zero) ) { return false; }
  35. // if we got to this point, do a thorough check
  36. const eT* A_col = A_mem;
  37. for(uword j=0; j < Nm1; ++j)
  38. {
  39. for(uword i=(j+1); i < N; ++i)
  40. {
  41. const eT A_ij = A_col[i];
  42. if(A_ij != eT_zero) { return false; }
  43. }
  44. A_col += N;
  45. }
  46. return true;
  47. }
  48. template<typename eT>
  49. inline
  50. bool
  51. is_tril(const Mat<eT>& A)
  52. {
  53. arma_extra_debug_sigprint();
  54. // NOTE: assuming that A has a square size
  55. const uword N = A.n_rows;
  56. if(N < 2) { return false; }
  57. const eT eT_zero = eT(0);
  58. // quickly check top-right corner
  59. const eT* A_colNm2 = A.colptr(N-2);
  60. const eT* A_colNm1 = A_colNm2 + N;
  61. if( (A_colNm2[0] != eT_zero) || (A_colNm1[0] != eT_zero) || (A_colNm1[1] != eT_zero) ) { return false; }
  62. // if we got to this point, do a thorough check
  63. const eT* A_col = A.memptr() + N;
  64. for(uword j=1; j < N; ++j)
  65. {
  66. for(uword i=0; i < j; ++i)
  67. {
  68. const eT A_ij = A_col[i];
  69. if(A_ij != eT_zero) { return false; }
  70. }
  71. A_col += N;
  72. }
  73. return true;
  74. }
  75. } // end of namespace trimat_helper
  76. //! @}