github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/native/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 native
     6  
     7  import (
     8  	"github.com/gonum/blas"
     9  	"github.com/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  	checkMatrix(m, n, a, lda)
    32  	if len(ipiv) < mn {
    33  		panic(badIpiv)
    34  	}
    35  	if m == 0 || n == 0 {
    36  		return false
    37  	}
    38  	bi := blas64.Implementation()
    39  	nb := impl.Ilaenv(1, "DGETRF", " ", m, n, -1, -1)
    40  	if nb <= 1 || nb >= min(m, n) {
    41  		// Use the unblocked algorithm.
    42  		return impl.Dgetf2(m, n, a, lda, ipiv)
    43  	}
    44  	ok = true
    45  	for j := 0; j < mn; j += nb {
    46  		jb := min(mn-j, nb)
    47  		blockOk := impl.Dgetf2(m-j, jb, a[j*lda+j:], lda, ipiv[j:])
    48  		if !blockOk {
    49  			ok = false
    50  		}
    51  		for i := j; i <= min(m-1, j+jb-1); i++ {
    52  			ipiv[i] = j + ipiv[i]
    53  		}
    54  		impl.Dlaswp(j, a, lda, j, j+jb-1, ipiv[:j+jb], 1)
    55  		if j+jb < n {
    56  			impl.Dlaswp(n-j-jb, a[j+jb:], lda, j, j+jb-1, ipiv[:j+jb], 1)
    57  			bi.Dtrsm(blas.Left, blas.Lower, blas.NoTrans, blas.Unit,
    58  				jb, n-j-jb, 1,
    59  				a[j*lda+j:], lda,
    60  				a[j*lda+j+jb:], lda)
    61  			if j+jb < m {
    62  				bi.Dgemm(blas.NoTrans, blas.NoTrans, m-j-jb, n-j-jb, jb, -1,
    63  					a[(j+jb)*lda+j:], lda,
    64  					a[j*lda+j+jb:], lda,
    65  					1, a[(j+jb)*lda+j+jb:], lda)
    66  			}
    67  		}
    68  	}
    69  	return ok
    70  }