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

     1  //go:build ignore
     2  
     3  package define
     4  
     5  type Vector[T any] struct {
     6  	Values []T
     7  	Offset uintptr
     8  	Inc    uintptr
     9  }
    10  
    11  func Apply1[T any](
    12  	xs Vector[T],
    13  	n uintptr,
    14  	fn func(x T) T,
    15  ) {
    16  	xi := xs.Offset
    17  	for i := uintptr(0); i < n; i++ {
    18  		xs.Values[xi] = fn(xs.Values[xi])
    19  		xi += xs.Inc
    20  	}
    21  }
    22  
    23  func AddConst[T float32 | float64 | complex64 | complex128](alpha T, xs []T, n uintptr) {
    24  	Apply1(
    25  		Vector[T]{Values: xs, Offset: 0, Inc: 1},
    26  		n, func(x T) T {
    27  			return x + alpha
    28  		})
    29  }
    30  
    31  func AddConst2[T float32 | float64 | complex64 | complex128](alpha T, xs []T, n uintptr) {
    32  	for i := uintptr(0); i < n; i++ {
    33  		xs[i] += alpha
    34  	}
    35  }