github.com/gopherd/gonum@v0.0.4/internal/asm/c128/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 c128 6 7 import ( 8 "math" 9 "math/cmplx" 10 ) 11 12 // Add is 13 // for i, v := range s { 14 // dst[i] += v 15 // } 16 func Add(dst, s []complex128) { 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 complex128, x []complex128) { 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 []complex128) []complex128 { 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 []complex128) []complex128 { 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 []complex128) { 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 []complex128) []complex128 { 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 += cmplx.Conj(v) * y[i] 97 // } 98 // return sum 99 func DotUnitary(x, y []complex128) (sum complex128) { 100 for i, v := range x { 101 sum += cmplx.Conj(v) * y[i] 102 } 103 return sum 104 } 105 106 // L2DistanceUnitary returns the L2-norm of x-y. 107 func L2DistanceUnitary(x, y []complex128) (norm float64) { 108 var scale float64 109 sumSquares := 1.0 110 for i, v := range x { 111 v -= y[i] 112 if v == 0 { 113 continue 114 } 115 absxi := cmplx.Abs(v) 116 if math.IsNaN(absxi) { 117 return math.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 math.IsInf(scale, 1) { 129 return math.Inf(1) 130 } 131 return scale * math.Sqrt(sumSquares) 132 } 133 134 // L2NormUnitary returns the L2-norm of x. 135 func L2NormUnitary(x []complex128) (norm float64) { 136 var scale float64 137 sumSquares := 1.0 138 for _, v := range x { 139 if v == 0 { 140 continue 141 } 142 absxi := cmplx.Abs(v) 143 if math.IsNaN(absxi) { 144 return math.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 math.IsInf(scale, 1) { 156 return math.Inf(1) 157 } 158 return scale * math.Sqrt(sumSquares) 159 } 160 161 // Sum is 162 // var sum complex128 163 // for i := range x { 164 // sum += x[i] 165 // } 166 func Sum(x []complex128) complex128 { 167 var sum complex128 168 for _, v := range x { 169 sum += v 170 } 171 return sum 172 }