gitee.com/quant1x/num@v0.3.2/internal/functions/construct.go (about)

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