1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17 
18 
19 //! \addtogroup trimat_helper
20 //! @{
21 
22 
23 namespace trimat_helper
24 {
25 
26 
27 
28 template<typename eT>
29 inline
30 bool
is_triu(const Mat<eT> & A)31 is_triu(const Mat<eT>& A)
32   {
33   arma_extra_debug_sigprint();
34 
35   // NOTE: assuming that A has a square size
36 
37   const uword N   = A.n_rows;
38   const uword Nm1 = N-1;
39 
40   if(N < 2)  { return false; }
41 
42   const eT*   A_mem   = A.memptr();
43   const eT    eT_zero = eT(0);
44 
45   // quickly check bottom-left corner
46   const eT* A_col0 = A_mem;
47   const eT* A_col1 = A_col0 + N;
48 
49   if( (A_col0[N-2] != eT_zero) || (A_col0[Nm1] != eT_zero) || (A_col1[Nm1] != eT_zero) )  { return false; }
50 
51   // if we got to this point, do a thorough check
52 
53   const eT* A_col = A_mem;
54 
55   for(uword j=0; j < Nm1; ++j)
56     {
57     for(uword i=(j+1); i < N; ++i)
58       {
59       const eT A_ij = A_col[i];
60 
61       if(A_ij != eT_zero) { return false; }
62       }
63 
64     A_col += N;
65     }
66 
67   return true;
68   }
69 
70 
71 
72 template<typename eT>
73 inline
74 bool
is_tril(const Mat<eT> & A)75 is_tril(const Mat<eT>& A)
76   {
77   arma_extra_debug_sigprint();
78 
79   // NOTE: assuming that A has a square size
80 
81   const uword N = A.n_rows;
82 
83   if(N < 2)  { return false; }
84 
85   const eT eT_zero = eT(0);
86 
87   // quickly check top-right corner
88   const eT* A_colNm2 = A.colptr(N-2);
89   const eT* A_colNm1 = A_colNm2 + N;
90 
91   if( (A_colNm2[0] != eT_zero) || (A_colNm1[0] != eT_zero) || (A_colNm1[1] != eT_zero) )  { return false; }
92 
93   // if we got to this point, do a thorough check
94 
95   const eT* A_col = A.memptr() + N;
96 
97   for(uword j=1; j < N; ++j)
98     {
99     for(uword i=0; i < j; ++i)
100       {
101       const eT A_ij = A_col[i];
102 
103       if(A_ij != eT_zero) { return false; }
104       }
105 
106     A_col += N;
107     }
108 
109   return true;
110   }
111 
112 
113 
114 }  // end of namespace trimat_helper
115 
116 
117 //! @}
118