github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/native/drscl.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  	"math"
     9  
    10  	"github.com/gonum/blas/blas64"
    11  )
    12  
    13  // Drscl multiplies the vector x by 1/a being careful to avoid overflow or
    14  // underflow where possible.
    15  //
    16  // Drscl is an internal routine. It is exported for testing purposes.
    17  func (impl Implementation) Drscl(n int, a float64, x []float64, incX int) {
    18  	checkVector(n, x, incX)
    19  	bi := blas64.Implementation()
    20  	cden := a
    21  	cnum := 1.0
    22  	smlnum := dlamchS
    23  	bignum := 1 / smlnum
    24  	for {
    25  		cden1 := cden * smlnum
    26  		cnum1 := cnum / bignum
    27  		var mul float64
    28  		var done bool
    29  		switch {
    30  		case cnum != 0 && math.Abs(cden1) > math.Abs(cnum):
    31  			mul = smlnum
    32  			done = false
    33  			cden = cden1
    34  		case math.Abs(cnum1) > math.Abs(cden):
    35  			mul = bignum
    36  			done = false
    37  			cnum = cnum1
    38  		default:
    39  			mul = cnum / cden
    40  			done = true
    41  		}
    42  		bi.Dscal(n, mul, x, incX)
    43  		if done {
    44  			break
    45  		}
    46  	}
    47  }