github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/native/dgetf2.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 native
     6  
     7  import (
     8  	"math"
     9  
    10  	"github.com/gonum/blas/blas64"
    11  )
    12  
    13  // Dgetf2 computes the LU decomposition of the m×n matrix A.
    14  // The LU decomposition is a factorization of a into
    15  //  A = P * L * U
    16  // where P is a permutation matrix, L is a unit lower triangular matrix, and
    17  // U is a (usually) non-unit upper triangular matrix. On exit, L and U are stored
    18  // in place into a.
    19  //
    20  // ipiv is a permutation vector. It indicates that row i of the matrix was
    21  // changed with ipiv[i]. ipiv must have length at least min(m,n), and will panic
    22  // otherwise. ipiv is zero-indexed.
    23  //
    24  // Dgetf2 returns whether the matrix A is singular. The LU decomposition will
    25  // be computed regardless of the singularity of A, but division by zero
    26  // will occur if the false is returned and the result is used to solve a
    27  // system of equations.
    28  //
    29  // Dgetf2 is an internal routine. It is exported for testing purposes.
    30  func (Implementation) Dgetf2(m, n int, a []float64, lda int, ipiv []int) (ok bool) {
    31  	mn := min(m, n)
    32  	checkMatrix(m, n, a, lda)
    33  	if len(ipiv) < mn {
    34  		panic(badIpiv)
    35  	}
    36  	if m == 0 || n == 0 {
    37  		return true
    38  	}
    39  	bi := blas64.Implementation()
    40  	sfmin := dlamchS
    41  	ok = true
    42  	for j := 0; j < mn; j++ {
    43  		// Find a pivot and test for singularity.
    44  		jp := j + bi.Idamax(m-j, a[j*lda+j:], lda)
    45  		ipiv[j] = jp
    46  		if a[jp*lda+j] == 0 {
    47  			ok = false
    48  		} else {
    49  			// Swap the rows if necessary.
    50  			if jp != j {
    51  				bi.Dswap(n, a[j*lda:], 1, a[jp*lda:], 1)
    52  			}
    53  			if j < m-1 {
    54  				aj := a[j*lda+j]
    55  				if math.Abs(aj) >= sfmin {
    56  					bi.Dscal(m-j-1, 1/aj, a[(j+1)*lda+j:], lda)
    57  				} else {
    58  					for i := 0; i < m-j-1; i++ {
    59  						a[(j+1)*lda+j] = a[(j+1)*lda+j] / a[lda*j+j]
    60  					}
    61  				}
    62  			}
    63  		}
    64  		if j < mn-1 {
    65  			bi.Dger(m-j-1, n-j-1, -1, a[(j+1)*lda+j:], lda, a[j*lda+j+1:], 1, a[(j+1)*lda+j+1:], lda)
    66  		}
    67  	}
    68  	return ok
    69  }