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

     1  package functions
     2  
     3  import (
     4  	"gitee.com/quant1x/num/internal/partial"
     5  	"math"
     6  	"slices"
     7  )
     8  
     9  func Mean_AVX2_F64(x []float64) float64 {
    10  	return Sum_AVX2_F64(x) / float64(len(x))
    11  }
    12  
    13  func Mean_AVX2_F32(x []float32) float32 {
    14  	return Sum_AVX2_F32(x) / float32(len(x))
    15  }
    16  
    17  func Median_AVX2_F64(x []float64) float64 {
    18  	if len(x)%2 == 1 {
    19  		x = slices.Clone(x)
    20  		i := len(x) / 2
    21  		partial.TopK(x, i+1)
    22  		return x[i]
    23  	}
    24  	return Quantile_AVX2_F64(x, 0.5)
    25  }
    26  
    27  func Median_AVX2_F32(x []float32) float32 {
    28  	if len(x)%2 == 1 {
    29  		x = slices.Clone(x)
    30  		i := len(x) / 2
    31  		partial.TopK(x, i+1)
    32  		return x[i]
    33  	}
    34  	return Quantile_AVX2_F32(x, 0.5)
    35  }
    36  
    37  func Quantile_AVX2_F64(x []float64, q float64) float64 {
    38  	if len(x) == 1 {
    39  		return x[0]
    40  	}
    41  	if q == 0 {
    42  		return Min_AVX2_F64(x)
    43  	}
    44  	if q == 1 {
    45  		return Max_AVX2_F64(x)
    46  	}
    47  	x = slices.Clone(x)
    48  	f := float64(len(x)-1) * q
    49  	i := int(math.Floor(f))
    50  	if q < 0.5 {
    51  		partial.TopK(x, i+2)
    52  		a := Max_AVX2_F64(x[:i+1])
    53  		b := x[i+1]
    54  		return a + (b-a)*(f-float64(i))
    55  	} else {
    56  		partial.TopK(x, i+1)
    57  		a := x[i]
    58  		b := Min_AVX2_F64(x[i+1:])
    59  		return a + (b-a)*(f-float64(i))
    60  	}
    61  }
    62  
    63  func Quantile_AVX2_F32(x []float32, q float32) float32 {
    64  	if len(x) == 1 {
    65  		return x[0]
    66  	}
    67  	if q == 0 {
    68  		return Min_AVX2_F32(x)
    69  	}
    70  	if q == 1 {
    71  		return Max_AVX2_F32(x)
    72  	}
    73  	x = slices.Clone(x)
    74  	f := float32(len(x)-1) * q
    75  	i := int(math.Floor(float64(f)))
    76  	if q < 0.5 {
    77  		partial.TopK(x, i+2)
    78  		a := Max_AVX2_F32(x[:i+1])
    79  		b := x[i+1]
    80  		return a + (b-a)*(f-float32(i))
    81  	} else {
    82  		partial.TopK(x, i+1)
    83  		a := x[i]
    84  		b := Min_AVX2_F32(x[i+1:])
    85  		return a + (b-a)*(f-float32(i))
    86  	}
    87  }