gitee.com/quant1x/gox@v1.7.6/num/internal/functions/construct.go (about)

     1  package functions
     2  
     3  import "golang.org/x/exp/constraints"
     4  
     5  type Number interface {
     6  	constraints.Float | constraints.Integer
     7  }
     8  
     9  func Zeros_Go[T constraints.Float](x []T, n int) {
    10  	Repeat_Go(x, 0, n)
    11  }
    12  
    13  func Ones_Go[T constraints.Float](x []T, n int) {
    14  	Repeat_Go(x, 1, n)
    15  }
    16  
    17  func Repeat_Go[T constraints.Float](x []T, a T, n int) {
    18  	for i := 0; i < n; i++ {
    19  		x[i] = a
    20  	}
    21  }
    22  
    23  func Range_Go[T constraints.Float](x []T, a T, n int) {
    24  	for i := 0; i < n; i++ {
    25  		x[i] = a
    26  		a += 1
    27  	}
    28  }
    29  
    30  func Gather_Go[T constraints.Float](dst, x []T, idx []int) {
    31  	for i := 0; i < len(idx); i++ {
    32  		dst[i] = x[idx[i]]
    33  	}
    34  }
    35  
    36  func Scatter_Go[T constraints.Float](dst, x []T, idx []int) {
    37  	for i := 0; i < len(idx); i++ {
    38  		dst[idx[i]] = x[i]
    39  	}
    40  }
    41  
    42  func Clone_Go[T constraints.Float](x, y []T) {
    43  	copy(x, y)
    44  }
    45  
    46  func Clone_Go_Loop[T constraints.Float](x, y []T) {
    47  	for i := 0; i < len(x); i++ {
    48  		x[i] = y[i]
    49  	}
    50  }
    51  
    52  func FromNumber_Go[T constraints.Float, F Number](x []T, y []F) {
    53  	for i := 0; i < len(y); i++ {
    54  		x[i] = T(y[i])
    55  	}
    56  }
    57  
    58  func ToNumber_Go[T Number, F constraints.Float](x []T, y []F) {
    59  	for i := 0; i < len(y); i++ {
    60  		x[i] = T(y[i])
    61  	}
    62  }
    63  
    64  func FromBool_Go[T constraints.Float](x []T, y []bool) {
    65  	for i := 0; i < len(y); i++ {
    66  		if y[i] {
    67  			x[i] = 1
    68  		} else {
    69  			x[i] = 0
    70  		}
    71  	}
    72  }
    73  
    74  func ToBool_Go[F constraints.Float](x []bool, y []F) {
    75  	for i := 0; i < len(y); i++ {
    76  		if y[i] != 0 {
    77  			x[i] = true
    78  		} else {
    79  			x[i] = false
    80  		}
    81  	}
    82  }