gonum.org/v1/gonum@v0.14.0/lapack/gonum/dtrtri.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  	"gonum.org/v1/gonum/blas"
     9  	"gonum.org/v1/gonum/blas/blas64"
    10  )
    11  
    12  // Dtrtri computes the inverse of a triangular matrix, storing the result in place
    13  // into a. This is the BLAS level 3 version of the algorithm which builds upon
    14  // Dtrti2 to operate on matrix blocks instead of only individual columns.
    15  //
    16  // Dtrtri will not perform the inversion if the matrix is singular, and returns
    17  // a boolean indicating whether the inversion was successful.
    18  func (impl Implementation) Dtrtri(uplo blas.Uplo, diag blas.Diag, n int, a []float64, lda int) (ok bool) {
    19  	switch {
    20  	case uplo != blas.Upper && uplo != blas.Lower:
    21  		panic(badUplo)
    22  	case diag != blas.NonUnit && diag != blas.Unit:
    23  		panic(badDiag)
    24  	case n < 0:
    25  		panic(nLT0)
    26  	case lda < max(1, n):
    27  		panic(badLdA)
    28  	}
    29  
    30  	if n == 0 {
    31  		return true
    32  	}
    33  
    34  	if len(a) < (n-1)*lda+n {
    35  		panic(shortA)
    36  	}
    37  
    38  	if diag == blas.NonUnit {
    39  		for i := 0; i < n; i++ {
    40  			if a[i*lda+i] == 0 {
    41  				return false
    42  			}
    43  		}
    44  	}
    45  
    46  	bi := blas64.Implementation()
    47  
    48  	nb := impl.Ilaenv(1, "DTRTRI", "UD", n, -1, -1, -1)
    49  	if nb <= 1 || nb > n {
    50  		impl.Dtrti2(uplo, diag, n, a, lda)
    51  		return true
    52  	}
    53  	if uplo == blas.Upper {
    54  		for j := 0; j < n; j += nb {
    55  			jb := min(nb, n-j)
    56  			bi.Dtrmm(blas.Left, blas.Upper, blas.NoTrans, diag, j, jb, 1, a, lda, a[j:], lda)
    57  			bi.Dtrsm(blas.Right, blas.Upper, blas.NoTrans, diag, j, jb, -1, a[j*lda+j:], lda, a[j:], lda)
    58  			impl.Dtrti2(blas.Upper, diag, jb, a[j*lda+j:], lda)
    59  		}
    60  		return true
    61  	}
    62  	nn := ((n - 1) / nb) * nb
    63  	for j := nn; j >= 0; j -= nb {
    64  		jb := min(nb, n-j)
    65  		if j+jb <= n-1 {
    66  			bi.Dtrmm(blas.Left, blas.Lower, blas.NoTrans, diag, n-j-jb, jb, 1, a[(j+jb)*lda+j+jb:], lda, a[(j+jb)*lda+j:], lda)
    67  			bi.Dtrsm(blas.Right, blas.Lower, blas.NoTrans, diag, n-j-jb, jb, -1, a[j*lda+j:], lda, a[(j+jb)*lda+j:], lda)
    68  		}
    69  		impl.Dtrti2(blas.Lower, diag, jb, a[j*lda+j:], lda)
    70  	}
    71  	return true
    72  }