gonum.org/v1/gonum@v0.14.0/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  //
    14  //	for i, v := range s {
    15  //		dst[i] += v
    16  //	}
    17  func Add(dst, s []complex128) {
    18  	for i, v := range s {
    19  		dst[i] += v
    20  	}
    21  }
    22  
    23  // AddConst is
    24  //
    25  //	for i := range x {
    26  //		x[i] += alpha
    27  //	}
    28  func AddConst(alpha complex128, x []complex128) {
    29  	for i := range x {
    30  		x[i] += alpha
    31  	}
    32  }
    33  
    34  // CumSum is
    35  //
    36  //	if len(s) == 0 {
    37  //		return dst
    38  //	}
    39  //	dst[0] = s[0]
    40  //	for i, v := range s[1:] {
    41  //		dst[i+1] = dst[i] + v
    42  //	}
    43  //	return dst
    44  func CumSum(dst, s []complex128) []complex128 {
    45  	if len(s) == 0 {
    46  		return dst
    47  	}
    48  	dst[0] = s[0]
    49  	for i, v := range s[1:] {
    50  		dst[i+1] = dst[i] + v
    51  	}
    52  	return dst
    53  }
    54  
    55  // CumProd is
    56  //
    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 CumProd(dst, s []complex128) []complex128 {
    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  // Div is
    77  //
    78  //	for i, v := range s {
    79  //		dst[i] /= v
    80  //	}
    81  func Div(dst, s []complex128) {
    82  	for i, v := range s {
    83  		dst[i] /= v
    84  	}
    85  }
    86  
    87  // DivTo is
    88  //
    89  //	for i, v := range s {
    90  //		dst[i] = v / t[i]
    91  //	}
    92  //	return dst
    93  func DivTo(dst, s, t []complex128) []complex128 {
    94  	for i, v := range s {
    95  		dst[i] = v / t[i]
    96  	}
    97  	return dst
    98  }
    99  
   100  // DotUnitary is
   101  //
   102  //	for i, v := range x {
   103  //		sum += cmplx.Conj(v) * y[i]
   104  //	}
   105  //	return sum
   106  func DotUnitary(x, y []complex128) (sum complex128) {
   107  	for i, v := range x {
   108  		sum += cmplx.Conj(v) * y[i]
   109  	}
   110  	return sum
   111  }
   112  
   113  // L2DistanceUnitary returns the L2-norm of x-y.
   114  func L2DistanceUnitary(x, y []complex128) (norm float64) {
   115  	var scale float64
   116  	sumSquares := 1.0
   117  	for i, v := range x {
   118  		v -= y[i]
   119  		if v == 0 {
   120  			continue
   121  		}
   122  		absxi := cmplx.Abs(v)
   123  		if math.IsNaN(absxi) {
   124  			return math.NaN()
   125  		}
   126  		if scale < absxi {
   127  			s := scale / absxi
   128  			sumSquares = 1 + sumSquares*s*s
   129  			scale = absxi
   130  		} else {
   131  			s := absxi / scale
   132  			sumSquares += s * s
   133  		}
   134  	}
   135  	if math.IsInf(scale, 1) {
   136  		return math.Inf(1)
   137  	}
   138  	return scale * math.Sqrt(sumSquares)
   139  }
   140  
   141  // L2NormUnitary returns the L2-norm of x.
   142  func L2NormUnitary(x []complex128) (norm float64) {
   143  	var scale float64
   144  	sumSquares := 1.0
   145  	for _, v := range x {
   146  		if v == 0 {
   147  			continue
   148  		}
   149  		absxi := cmplx.Abs(v)
   150  		if math.IsNaN(absxi) {
   151  			return math.NaN()
   152  		}
   153  		if scale < absxi {
   154  			s := scale / absxi
   155  			sumSquares = 1 + sumSquares*s*s
   156  			scale = absxi
   157  		} else {
   158  			s := absxi / scale
   159  			sumSquares += s * s
   160  		}
   161  	}
   162  	if math.IsInf(scale, 1) {
   163  		return math.Inf(1)
   164  	}
   165  	return scale * math.Sqrt(sumSquares)
   166  }
   167  
   168  // Sum is
   169  //
   170  //	var sum complex128
   171  //	for i := range x {
   172  //	    sum += x[i]
   173  //	}
   174  func Sum(x []complex128) complex128 {
   175  	var sum complex128
   176  	for _, v := range x {
   177  		sum += v
   178  	}
   179  	return sum
   180  }