gitee.com/quant1x/num@v0.3.2/arithmetics_sub.go (about)

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/x32"
     5  	"gitee.com/quant1x/num/x64"
     6  	"slices"
     7  )
     8  
     9  // Sub arithmetics 减法 z = x - y
    10  func Sub[T Number](x []T, y any) []T {
    11  	return BinaryOperations(x, y, x32.Sub, x64.Sub, __go_sub[T])
    12  }
    13  
    14  func __go_sub[T Number](x, y []T) []T {
    15  	d := slices.Clone(x)
    16  	for i := 0; i < len(x); i++ {
    17  		d[i] -= y[i]
    18  	}
    19  	return d
    20  }
    21  
    22  // SubInplace 减法 x = x - y
    23  func SubInplace[T Number](x []T, y any) []T {
    24  	var d any
    25  	length := len(x)
    26  	switch vs := any(x).(type) {
    27  	case []float32:
    28  		f32s := AnyToSlice[float32](y, length)
    29  		x32.Sub_Inplace(vs, f32s)
    30  		d = vs
    31  	case []float64:
    32  		f64s := AnyToSlice[float64](y, length)
    33  		x64.Sub_Inplace(vs, f64s)
    34  		d = vs
    35  	default:
    36  		ys := AnyToSlice[T](y, length)
    37  		__go_sub_inplace[T](x, ys)
    38  		d = x
    39  	}
    40  	return d.([]T)
    41  }
    42  
    43  func __go_sub_inplace[T Number](x, y []T) {
    44  	for i := 0; i < len(x); i++ {
    45  		x[i] -= y[i]
    46  	}
    47  }