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.
10 "gonum.org/v1/gonum/blas/blas64"
13 // Dgetf2 computes the LU decomposition of the m×n matrix A.
14 // The LU decomposition is a factorization of a into
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
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.
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.
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) {
32 checkMatrix(m, n, a, lda)
39 bi := blas64.Implementation()
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)
49 // Swap the rows if necessary.
51 bi.Dswap(n, a[j*lda:], 1, a[jp*lda:], 1)
55 if math.Abs(aj) >= sfmin {
56 bi.Dscal(m-j-1, 1/aj, a[(j+1)*lda+j:], lda)
58 for i := 0; i < m-j-1; i++ {
59 a[(j+1)*lda+j] = a[(j+1)*lda+j] / a[lda*j+j]
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)