github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/internal/asm/f64/stubs_noasm.go (about)

     1  // Copyright ©2016 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  // +build !amd64 noasm gccgo safe
     6  
     7  package f64
     8  
     9  import "math"
    10  
    11  // L1Norm is
    12  //  for _, v := range x {
    13  //  	sum += math.Abs(v)
    14  //  }
    15  //  return sum
    16  func L1Norm(x []float64) (sum float64) {
    17  	for _, v := range x {
    18  		sum += math.Abs(v)
    19  	}
    20  	return sum
    21  }
    22  
    23  // L1NormInc is
    24  //  for i := 0; i < n*incX; i += incX {
    25  //  	sum += math.Abs(x[i])
    26  //  }
    27  //  return sum
    28  func L1NormInc(x []float64, n, incX int) (sum float64) {
    29  	for i := 0; i < n*incX; i += incX {
    30  		sum += math.Abs(x[i])
    31  	}
    32  	return sum
    33  }
    34  
    35  // Add is
    36  //  for i, v := range s {
    37  //  	dst[i] += v
    38  //  }
    39  func Add(dst, s []float64) {
    40  	for i, v := range s {
    41  		dst[i] += v
    42  	}
    43  }
    44  
    45  // AddConst is
    46  //  for i := range x {
    47  //  	x[i] += alpha
    48  //  }
    49  func AddConst(alpha float64, x []float64) {
    50  	for i := range x {
    51  		x[i] += alpha
    52  	}
    53  }
    54  
    55  // CumSum is
    56  //  if len(s) == 0 {
    57  //  	return dst
    58  //  }
    59  //  dst[0] = s[0]
    60  //  for i, v := range s[1:] {
    61  //  	dst[i+1] = dst[i] + v
    62  //  }
    63  //  return dst
    64  func CumSum(dst, s []float64) []float64 {
    65  	if len(s) == 0 {
    66  		return dst
    67  	}
    68  	dst[0] = s[0]
    69  	for i, v := range s[1:] {
    70  		dst[i+1] = dst[i] + v
    71  	}
    72  	return dst
    73  }
    74  
    75  // CumProd is
    76  //  if len(s) == 0 {
    77  //  	return dst
    78  //  }
    79  //  dst[0] = s[0]
    80  //  for i, v := range s[1:] {
    81  //  	dst[i+1] = dst[i] * v
    82  //  }
    83  //  return dst
    84  func CumProd(dst, s []float64) []float64 {
    85  	if len(s) == 0 {
    86  		return dst
    87  	}
    88  	dst[0] = s[0]
    89  	for i, v := range s[1:] {
    90  		dst[i+1] = dst[i] * v
    91  	}
    92  	return dst
    93  }
    94  
    95  // Div is
    96  //  for i, v := range s {
    97  //  	dst[i] /= v
    98  //  }
    99  func Div(dst, s []float64) {
   100  	for i, v := range s {
   101  		dst[i] /= v
   102  	}
   103  }
   104  
   105  // DivTo is
   106  //  for i, v := range s {
   107  //  	dst[i] = v / t[i]
   108  //  }
   109  //  return dst
   110  func DivTo(dst, s, t []float64) []float64 {
   111  	for i, v := range s {
   112  		dst[i] = v / t[i]
   113  	}
   114  	return dst
   115  }
   116  
   117  // L1Dist is
   118  //  var norm float64
   119  //  for i, v := range s {
   120  //  	norm += math.Abs(t[i] - v)
   121  //  }
   122  //  return norm
   123  func L1Dist(s, t []float64) float64 {
   124  	var norm float64
   125  	for i, v := range s {
   126  		norm += math.Abs(t[i] - v)
   127  	}
   128  	return norm
   129  }
   130  
   131  // LinfDist is
   132  //  var norm float64
   133  //  if len(s) == 0 {
   134  //  	return 0
   135  //  }
   136  //  norm = math.Abs(t[0] - s[0])
   137  //  for i, v := range s[1:] {
   138  //  	absDiff := math.Abs(t[i+1] - v)
   139  //  	if absDiff > norm || math.IsNaN(norm) {
   140  //  		norm = absDiff
   141  //  	}
   142  //  }
   143  //  return norm
   144  func LinfDist(s, t []float64) float64 {
   145  	var norm float64
   146  	if len(s) == 0 {
   147  		return 0
   148  	}
   149  	norm = math.Abs(t[0] - s[0])
   150  	for i, v := range s[1:] {
   151  		absDiff := math.Abs(t[i+1] - v)
   152  		if absDiff > norm || math.IsNaN(norm) {
   153  			norm = absDiff
   154  		}
   155  	}
   156  	return norm
   157  }
   158  
   159  // Sum is
   160  //  var sum float64
   161  //  for i := range x {
   162  //      sum += x[i]
   163  //  }
   164  func Sum(x []float64) float64 {
   165  	var sum float64
   166  	for _, v := range x {
   167  		sum += v
   168  	}
   169  	return sum
   170  }