gonum.org/v1/gonum@v0.15.1-0.20240517103525-f853624cb1bb/lapack/gonum/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 gonum
     6  
     7  import (
     8  	"math"
     9  
    10  	"gonum.org/v1/gonum/blas/blas64"
    11  )
    12  
    13  // Dgetf2 computes the LU decomposition of an m×n matrix A using partial
    14  // pivoting with row interchanges.
    15  //
    16  // The LU decomposition is a factorization of A into
    17  //
    18  //	A = P * L * U
    19  //
    20  // where P is a permutation matrix, L is a lower triangular with unit diagonal
    21  // elements (lower trapezoidal if m > n), and U is upper triangular (upper
    22  // trapezoidal if m < n).
    23  //
    24  // On entry, a contains the matrix A. On return, L and U are stored in place
    25  // into a, and P is represented by ipiv.
    26  //
    27  // ipiv contains a sequence of row interchanges. It indicates that row i of the
    28  // matrix was interchanged with ipiv[i]. ipiv must have length min(m,n), and
    29  // Dgetf2 will panic otherwise. ipiv is zero-indexed.
    30  //
    31  // Dgetf2 returns whether the matrix A is nonsingular. The LU decomposition will
    32  // be computed regardless of the singularity of A, but the result should not be
    33  // used to solve a system of equation.
    34  //
    35  // Dgetf2 is an internal routine. It is exported for testing purposes.
    36  func (Implementation) Dgetf2(m, n int, a []float64, lda int, ipiv []int) (ok bool) {
    37  	mn := min(m, n)
    38  	switch {
    39  	case m < 0:
    40  		panic(mLT0)
    41  	case n < 0:
    42  		panic(nLT0)
    43  	case lda < max(1, n):
    44  		panic(badLdA)
    45  	}
    46  
    47  	// Quick return if possible.
    48  	if mn == 0 {
    49  		return true
    50  	}
    51  
    52  	switch {
    53  	case len(a) < (m-1)*lda+n:
    54  		panic(shortA)
    55  	case len(ipiv) != mn:
    56  		panic(badLenIpiv)
    57  	}
    58  
    59  	bi := blas64.Implementation()
    60  
    61  	sfmin := dlamchS
    62  	ok = true
    63  	for j := 0; j < mn; j++ {
    64  		// Find a pivot and test for singularity.
    65  		jp := j + bi.Idamax(m-j, a[j*lda+j:], lda)
    66  		ipiv[j] = jp
    67  		if a[jp*lda+j] == 0 {
    68  			ok = false
    69  		} else {
    70  			// Swap the rows if necessary.
    71  			if jp != j {
    72  				bi.Dswap(n, a[j*lda:], 1, a[jp*lda:], 1)
    73  			}
    74  			if j < m-1 {
    75  				aj := a[j*lda+j]
    76  				if math.Abs(aj) >= sfmin {
    77  					bi.Dscal(m-j-1, 1/aj, a[(j+1)*lda+j:], lda)
    78  				} else {
    79  					for i := 0; i < m-j-1; i++ {
    80  						a[(j+1)*lda+j] = a[(j+1)*lda+j] / a[lda*j+j]
    81  					}
    82  				}
    83  			}
    84  		}
    85  		if j < mn-1 {
    86  			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)
    87  		}
    88  	}
    89  	return ok
    90  }