github.com/gopherd/gonum@v0.0.4/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 "github.com/gopherd/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 // A = P * L * U 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 18 // in place into a. 19 // 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. 23 // 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. 28 // 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) { 31 mn := min(m, n) 32 switch { 33 case m < 0: 34 panic(mLT0) 35 case n < 0: 36 panic(nLT0) 37 case lda < max(1, n): 38 panic(badLdA) 39 } 40 41 // Quick return if possible. 42 if mn == 0 { 43 return true 44 } 45 46 switch { 47 case len(a) < (m-1)*lda+n: 48 panic(shortA) 49 case len(ipiv) != mn: 50 panic(badLenIpiv) 51 } 52 53 bi := blas64.Implementation() 54 55 sfmin := dlamchS 56 ok = true 57 for j := 0; j < mn; j++ { 58 // Find a pivot and test for singularity. 59 jp := j + bi.Idamax(m-j, a[j*lda+j:], lda) 60 ipiv[j] = jp 61 if a[jp*lda+j] == 0 { 62 ok = false 63 } else { 64 // Swap the rows if necessary. 65 if jp != j { 66 bi.Dswap(n, a[j*lda:], 1, a[jp*lda:], 1) 67 } 68 if j < m-1 { 69 aj := a[j*lda+j] 70 if math.Abs(aj) >= sfmin { 71 bi.Dscal(m-j-1, 1/aj, a[(j+1)*lda+j:], lda) 72 } else { 73 for i := 0; i < m-j-1; i++ { 74 a[(j+1)*lda+j] = a[(j+1)*lda+j] / a[lda*j+j] 75 } 76 } 77 } 78 } 79 if j < mn-1 { 80 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) 81 } 82 } 83 return ok 84 }