1 /*
  2  *   Copyright (c) 2011, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 /* Based on
 34  *
 35       INTEGER FUNCTION ILA?LR( M, N, A, LDA )
 36  *
 37  *  -- LAPACK auxiliary routine (version 3.2.2)                        --
 38  *
 39  *  -- June 2010                                                       --
 40  *
 41  *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
 42  *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
 43  *
 44  */
 45 
 46 #ifndef FLENS_LAPACK_AUX_ILALR_TCC
 47 #define FLENS_LAPACK_AUX_ILALR_TCC 1
 48 
 49 #include <flens/blas/blas.h>
 50 #include <flens/lapack/lapack.h>
 51 
 52 namespace flens { namespace lapack {
 53 
 54 //== generic lapack implementation =============================================
 55 
 56 template <typename MA>
 57 typename GeMatrix<MA>::IndexType
 58 ilalr_generic(const GeMatrix<MA> &A)
 59 {
 60     using std::max;
 61 
 62     typedef typename GeMatrix<MA>::IndexType    IndexType;
 63     typedef typename GeMatrix<MA>::ElementType  T;
 64 
 65     const IndexType m = A.numRows();
 66     const IndexType n = A.numCols();
 67 
 68 //
 69 //  Quick test for the common case where one corner is non-zero.
 70 //
 71     if (m==0) {
 72         return m;
 73     }
 74     if ((A(m,1)!=T(0)) || A(m,n)!=T(0)) {
 75         return m;
 76     }
 77 //
 78 //  Scan up each column tracking the last zero row seen.
 79 //
 80     IndexType lastRow = 0;
 81     for (IndexType j=1; j<=n; ++j) {
 82         for (IndexType i=m; i>=1; --i) {
 83             if (A(i,j)!=T(0)) {
 84                 lastRow = max(lastRow, i);
 85                 break;
 86             }
 87         }
 88     }
 89     return lastRow;
 90 }
 91 
 92 //== interface for native lapack ===============================================
 93 
 94 
 95 //== public interface ==========================================================
 96 
 97 template <typename MA>
 98 typename GeMatrix<MA>::IndexType
 99 ilalr(const GeMatrix<MA> &A)
100 {
101     LAPACK_DEBUG_OUT("ilalr");
102 
103     using std::max;
104 
105     ASSERT(A.firstRow()==1);
106     ASSERT(A.firstCol()==1);
107 
108     return ilalr_generic(A);
109 }
110 
111 } } // namespace lapack, flens
112 
113 #endif // FLENS_LAPACK_AUX_ILALR_TCC