github.com/gopherd/gonum@v0.0.4/lapack/gonum/iladlc.go (about)

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gonum
     6  
     7  // Iladlc scans a matrix for its last non-zero column. Returns -1 if the matrix
     8  // is all zeros.
     9  //
    10  // Iladlc is an internal routine. It is exported for testing purposes.
    11  func (Implementation) Iladlc(m, n int, a []float64, lda int) int {
    12  	switch {
    13  	case m < 0:
    14  		panic(mLT0)
    15  	case n < 0:
    16  		panic(nLT0)
    17  	case lda < max(1, n):
    18  		panic(badLdA)
    19  	}
    20  
    21  	if n == 0 || m == 0 {
    22  		return -1
    23  	}
    24  
    25  	if len(a) < (m-1)*lda+n {
    26  		panic(shortA)
    27  	}
    28  
    29  	// Test common case where corner is non-zero.
    30  	if a[n-1] != 0 || a[(m-1)*lda+(n-1)] != 0 {
    31  		return n - 1
    32  	}
    33  
    34  	// Scan each row tracking the highest column seen.
    35  	highest := -1
    36  	for i := 0; i < m; i++ {
    37  		for j := n - 1; j >= 0; j-- {
    38  			if a[i*lda+j] != 0 {
    39  				highest = max(highest, j)
    40  				break
    41  			}
    42  		}
    43  	}
    44  	return highest
    45  }