github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/vector/define/reduce.go (about)

     1  package define
     2  
     3  import . "github.com/egonelbre/exp/vector/vector"
     4  
     5  func Sum[T float32 | float64 | complex64 | complex128](xs []T, n uintptr) T {
     6  	return Reduce1(
     7  		Vector[T]{Values: xs, Offset: 0, Inc: 1},
     8  		n, 0, func(r, x T) T {
     9  			return r + x
    10  		})
    11  }
    12  
    13  func Dot[T float32 | float64 | complex64 | complex128](xs, ys []T, n uintptr) T {
    14  	return Reduce2(
    15  		Vector[T]{Values: xs, Offset: 0, Inc: 1},
    16  		Vector[T]{Values: ys, Offset: 0, Inc: 1},
    17  		n, 0, func(r, x, y T) T {
    18  			return r + x*y
    19  		})
    20  }
    21  
    22  func CumSum[T float32 | float64 | complex64 | complex128](xs []T, n uintptr) {
    23  	var result T
    24  	Apply1(
    25  		Vector[T]{Values: xs, Offset: 0, Inc: 1},
    26  		n, func(x T) T {
    27  			result += x
    28  			return result
    29  		})
    30  }
    31  
    32  func CumProd[T float32 | float64 | complex64 | complex128](xs []T, n uintptr) {
    33  	var result T = 1 // TODO: not correct for complex
    34  	Apply1(
    35  		Vector[T]{Values: xs, Offset: 0, Inc: 1},
    36  		n, func(x T) T {
    37  			result += x
    38  			return result
    39  		})
    40  }