github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/gonum/dlassq.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 "math"
     8  
     9  // Dlassq updates a sum of squares in scaled form. The input parameters scale and
    10  // sumsq represent the current scale and total sum of squares. These values are
    11  // updated with the information in the first n elements of the vector specified
    12  // by x and incX.
    13  //
    14  // Dlassq is an internal routine. It is exported for testing purposes.
    15  func (impl Implementation) Dlassq(n int, x []float64, incx int, scale float64, sumsq float64) (scl, smsq float64) {
    16  	switch {
    17  	case n < 0:
    18  		panic(nLT0)
    19  	case incx <= 0:
    20  		panic(badIncX)
    21  	case len(x) < 1+(n-1)*incx:
    22  		panic(shortX)
    23  	}
    24  
    25  	if n == 0 {
    26  		return scale, sumsq
    27  	}
    28  
    29  	for ix := 0; ix <= (n-1)*incx; ix += incx {
    30  		absxi := math.Abs(x[ix])
    31  		if absxi > 0 || math.IsNaN(absxi) {
    32  			if scale < absxi {
    33  				sumsq = 1 + sumsq*(scale/absxi)*(scale/absxi)
    34  				scale = absxi
    35  			} else {
    36  				sumsq += (absxi / scale) * (absxi / scale)
    37  			}
    38  		}
    39  	}
    40  	return scale, sumsq
    41  }