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