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