github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/gonum/dlange.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/jingcheng-WU/gonum/lapack"
    11  )
    12  
    13  // Dlange returns the value of the specified norm of a general m×n matrix A:
    14  //  lapack.MaxAbs:       the maximum absolute value of any element.
    15  //  lapack.MaxColumnSum: the maximum column sum of the absolute values of the elements (1-norm).
    16  //  lapack.MaxRowSum:    the maximum row sum of the absolute values of the elements (infinity-norm).
    17  //  lapack.Frobenius:    the square root of the sum of the squares of the elements (Frobenius norm).
    18  // If norm == lapack.MaxColumnSum, work must be of length n, and this function will
    19  // panic otherwise. There are no restrictions on work for the other matrix norms.
    20  func (impl Implementation) Dlange(norm lapack.MatrixNorm, m, n int, a []float64, lda int, work []float64) float64 {
    21  	// TODO(btracey): These should probably be refactored to use BLAS calls.
    22  	switch {
    23  	case norm != lapack.MaxRowSum && norm != lapack.MaxColumnSum && norm != lapack.Frobenius && norm != lapack.MaxAbs:
    24  		panic(badNorm)
    25  	case m < 0:
    26  		panic(mLT0)
    27  	case n < 0:
    28  		panic(nLT0)
    29  	case lda < max(1, n):
    30  		panic(badLdA)
    31  	}
    32  
    33  	// Quick return if possible.
    34  	if m == 0 || n == 0 {
    35  		return 0
    36  	}
    37  
    38  	switch {
    39  	case len(a) < (m-1)*lda+n:
    40  		panic(badLdA)
    41  	case norm == lapack.MaxColumnSum && len(work) < n:
    42  		panic(shortWork)
    43  	}
    44  
    45  	switch norm {
    46  	case lapack.MaxAbs:
    47  		var value float64
    48  		for i := 0; i < m; i++ {
    49  			for j := 0; j < n; j++ {
    50  				value = math.Max(value, math.Abs(a[i*lda+j]))
    51  			}
    52  		}
    53  		return value
    54  	case lapack.MaxColumnSum:
    55  		for i := 0; i < n; i++ {
    56  			work[i] = 0
    57  		}
    58  		for i := 0; i < m; i++ {
    59  			for j := 0; j < n; j++ {
    60  				work[j] += math.Abs(a[i*lda+j])
    61  			}
    62  		}
    63  		var value float64
    64  		for i := 0; i < n; i++ {
    65  			value = math.Max(value, work[i])
    66  		}
    67  		return value
    68  	case lapack.MaxRowSum:
    69  		var value float64
    70  		for i := 0; i < m; i++ {
    71  			var sum float64
    72  			for j := 0; j < n; j++ {
    73  				sum += math.Abs(a[i*lda+j])
    74  			}
    75  			value = math.Max(value, sum)
    76  		}
    77  		return value
    78  	default:
    79  		// lapack.Frobenius
    80  		scale := 0.0
    81  		sum := 1.0
    82  		for i := 0; i < m; i++ {
    83  			rowscale, rowsum := impl.Dlassq(n, a[i*lda:], 1, 0, 1)
    84  			scale, sum = impl.Dcombssq(scale, sum, rowscale, rowsum)
    85  		}
    86  		return scale * math.Sqrt(sum)
    87  	}
    88  }