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