github.com/gopherd/gonum@v0.0.4/lapack/gonum/dgetrf.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  	"github.com/gopherd/gonum/blas"
     9  	"github.com/gopherd/gonum/blas/blas64"
    10  )
    11  
    12  // Dgetrf computes the LU decomposition of the m×n matrix A.
    13  // The LU decomposition is a factorization of A into
    14  //  A = P * L * U
    15  // where P is a permutation matrix, L is a unit lower triangular matrix, and
    16  // U is a (usually) non-unit upper triangular matrix. On exit, L and U are stored
    17  // in place into a.
    18  //
    19  // ipiv is a permutation vector. It indicates that row i of the matrix was
    20  // changed with ipiv[i]. ipiv must have length at least min(m,n), and will panic
    21  // otherwise. ipiv is zero-indexed.
    22  //
    23  // Dgetrf is the blocked version of the algorithm.
    24  //
    25  // Dgetrf returns whether the matrix A is singular. The LU decomposition will
    26  // be computed regardless of the singularity of A, but division by zero
    27  // will occur if the false is returned and the result is used to solve a
    28  // system of equations.
    29  func (impl Implementation) Dgetrf(m, n int, a []float64, lda int, ipiv []int) (ok bool) {
    30  	mn := min(m, n)
    31  	switch {
    32  	case m < 0:
    33  		panic(mLT0)
    34  	case n < 0:
    35  		panic(nLT0)
    36  	case lda < max(1, n):
    37  		panic(badLdA)
    38  	}
    39  
    40  	// Quick return if possible.
    41  	if mn == 0 {
    42  		return true
    43  	}
    44  
    45  	switch {
    46  	case len(a) < (m-1)*lda+n:
    47  		panic(shortA)
    48  	case len(ipiv) != mn:
    49  		panic(badLenIpiv)
    50  	}
    51  
    52  	bi := blas64.Implementation()
    53  
    54  	nb := impl.Ilaenv(1, "DGETRF", " ", m, n, -1, -1)
    55  	if nb <= 1 || mn <= nb {
    56  		// Use the unblocked algorithm.
    57  		return impl.Dgetf2(m, n, a, lda, ipiv)
    58  	}
    59  	ok = true
    60  	for j := 0; j < mn; j += nb {
    61  		jb := min(mn-j, nb)
    62  		blockOk := impl.Dgetf2(m-j, jb, a[j*lda+j:], lda, ipiv[j:j+jb])
    63  		if !blockOk {
    64  			ok = false
    65  		}
    66  		for i := j; i <= min(m-1, j+jb-1); i++ {
    67  			ipiv[i] = j + ipiv[i]
    68  		}
    69  		impl.Dlaswp(j, a, lda, j, j+jb-1, ipiv[:j+jb], 1)
    70  		if j+jb < n {
    71  			impl.Dlaswp(n-j-jb, a[j+jb:], lda, j, j+jb-1, ipiv[:j+jb], 1)
    72  			bi.Dtrsm(blas.Left, blas.Lower, blas.NoTrans, blas.Unit,
    73  				jb, n-j-jb, 1,
    74  				a[j*lda+j:], lda,
    75  				a[j*lda+j+jb:], lda)
    76  			if j+jb < m {
    77  				bi.Dgemm(blas.NoTrans, blas.NoTrans, m-j-jb, n-j-jb, jb, -1,
    78  					a[(j+jb)*lda+j:], lda,
    79  					a[j*lda+j+jb:], lda,
    80  					1, a[(j+jb)*lda+j+jb:], lda)
    81  			}
    82  		}
    83  	}
    84  	return ok
    85  }