github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/native/dtrti2.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  // Dtrti2 computes the inverse of a triangular matrix, storing the result in place
    13  // into a. This is the BLAS level 2 version of the algorithm.
    14  //
    15  // Dtrti2 is an internal routine. It is exported for testing purposes.
    16  func (impl Implementation) Dtrti2(uplo blas.Uplo, diag blas.Diag, n int, a []float64, lda int) {
    17  	checkMatrix(n, n, a, lda)
    18  	if uplo != blas.Upper && uplo != blas.Lower {
    19  		panic(badUplo)
    20  	}
    21  	if diag != blas.NonUnit && diag != blas.Unit {
    22  		panic(badDiag)
    23  	}
    24  	bi := blas64.Implementation()
    25  
    26  	nonUnit := diag == blas.NonUnit
    27  	// TODO(btracey): Replace this with a row-major ordering.
    28  	if uplo == blas.Upper {
    29  		for j := 0; j < n; j++ {
    30  			var ajj float64
    31  			if nonUnit {
    32  				ajj = 1 / a[j*lda+j]
    33  				a[j*lda+j] = ajj
    34  				ajj *= -1
    35  			} else {
    36  				ajj = -1
    37  			}
    38  			bi.Dtrmv(blas.Upper, blas.NoTrans, diag, j, a, lda, a[j:], lda)
    39  			bi.Dscal(j, ajj, a[j:], lda)
    40  		}
    41  		return
    42  	}
    43  	for j := n - 1; j >= 0; j-- {
    44  		var ajj float64
    45  		if nonUnit {
    46  			ajj = 1 / a[j*lda+j]
    47  			a[j*lda+j] = ajj
    48  			ajj *= -1
    49  		} else {
    50  			ajj = -1
    51  		}
    52  		if j < n-1 {
    53  			bi.Dtrmv(blas.Lower, blas.NoTrans, diag, n-j-1, a[(j+1)*lda+j+1:], lda, a[(j+1)*lda+j:], lda)
    54  			bi.Dscal(n-j-1, ajj, a[(j+1)*lda+j:], lda)
    55  		}
    56  	}
    57  }