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

     1  package x32
     2  
     3  import (
     4  	"gitee.com/quant1x/num/internal/functions"
     5  	"gitee.com/quant1x/num/math32"
     6  	"gitee.com/quant1x/pkg/testify/require"
     7  	"testing"
     8  )
     9  
    10  const d = 0.001
    11  
    12  var empty = []float32{}
    13  var one = []float32{1}
    14  var two = []float32{1, 2}
    15  var eight = []float32{1, 2, 3, 4, 5, 6, 7, 8} // AVX register
    16  var nine = []float32{1, 2, 3, 4, 5, 6, 7, 8, 9}
    17  var accel = []bool{false, functions.Info().Acceleration}
    18  
    19  func TestSpecial(t *testing.T) {
    20  	for _, accel := range accel {
    21  		functions.SetAcceleration(accel)
    22  
    23  		dst := make([]float32, 4)
    24  
    25  		require.Empty(t, Sin(empty))
    26  		require.InDeltaSlice(t, []float32{0.8415}, Sin(one), d)
    27  		require.InDeltaSlice(t, []float32{0.8415, 0.9093, 0.1411, -0.7568, -0.9589, -0.2794, 0.6570, 0.9894}, Sin(eight), d)
    28  		require.InDeltaSlice(t, []float32{0.8415, 0.9093, 0.1411, -0.7568, -0.9589, -0.2794, 0.6570, 0.9894, 0.4121}, Sin(nine), d)
    29  		require.Panics(t, func() { Sin_Into(dst, nine) })
    30  
    31  		require.Empty(t, Cos(empty))
    32  		require.InDeltaSlice(t, Sin(AddNumber(one, math32.Pi/2)), Cos(one), d)
    33  		require.InDeltaSlice(t, Sin(AddNumber(eight, math32.Pi/2)), Cos(eight), d)
    34  		require.InDeltaSlice(t, Sin(AddNumber(nine, math32.Pi/2)), Cos(nine), d)
    35  		require.Panics(t, func() { Cos_Into(dst, nine) })
    36  
    37  		const e = 0.001
    38  
    39  		require.Empty(t, Exp(empty))
    40  		require.InEpsilonSlice(t, []float32{2.7183}, Exp(one), e)
    41  		require.InEpsilonSlice(t, []float32{2.7183, 7.3891, 20.0855, 54.5982, 148.4132, 403.4288, 1096.6332, 2980.9580}, Exp(eight), e)
    42  		require.InEpsilonSlice(t, []float32{2.7183, 7.3891, 20.0855, 54.5982, 148.4132, 403.4288, 1096.6332, 2980.9580, 8103.0839}, Exp(nine), e)
    43  		require.Panics(t, func() { Exp_Into(dst, nine) })
    44  
    45  		require.Empty(t, Log(empty))
    46  		require.True(t, math32.IsNaN(Log([]float32{-1})[0]))
    47  		require.InDeltaSlice(t, one, Log(Exp(one)), d)
    48  		require.InDeltaSlice(t, eight, Log(Exp(eight)), d)
    49  		require.InDeltaSlice(t, nine, Log(Exp(nine)), d)
    50  		require.Panics(t, func() { Log_Into(dst, nine) })
    51  
    52  		invln2 := float32(1.4426950408889634)
    53  		invln10 := float32(0.4342944819032518)
    54  
    55  		require.Empty(t, Log2(empty))
    56  		require.True(t, math32.IsNaN(Log2([]float32{-1})[0]))
    57  		require.InDeltaSlice(t, MulNumber(Log(one), invln2), Log2(one), d)
    58  		require.InDeltaSlice(t, MulNumber(Log(eight), invln2), Log2(eight), d)
    59  		require.InDeltaSlice(t, MulNumber(Log(nine), invln2), Log2(nine), d)
    60  		require.Panics(t, func() { Log2_Into(dst, nine) })
    61  
    62  		require.Empty(t, Log10(empty))
    63  		require.True(t, math32.IsNaN(Log10([]float32{-1})[0]))
    64  		require.InDeltaSlice(t, MulNumber(Log(one), invln10), Log10(one), d)
    65  		require.InDeltaSlice(t, MulNumber(Log(eight), invln10), Log10(eight), d)
    66  		require.InDeltaSlice(t, MulNumber(Log(nine), invln10), Log10(nine), d)
    67  		require.Panics(t, func() { Log10_Into(dst, nine) })
    68  	}
    69  }
    70  
    71  func TestConstruction(t *testing.T) {
    72  	for _, accel := range accel {
    73  		functions.SetAcceleration(accel)
    74  
    75  		dst := make([]float32, 4)
    76  
    77  		require.Equal(t, empty, FromFloat64([]float64{}))
    78  		require.Equal(t, []float32{5}, FromFloat64([]float64{5}))
    79  		require.Equal(t, []float32{5, -5}, FromFloat64([]float64{5, -5}))
    80  		require.Equal(t, []float32{5, -5, 5, 1}, FromFloat64([]float64{5, -5, 5, 1}))
    81  		require.Equal(t, []float32{5, -5, 5, 1, 0}, FromFloat64([]float64{5, -5, 5, 1, 0}))
    82  		require.Panics(t, func() { FromFloat64_Into(dst, []float64{1, 2, 3, 4, 5}) })
    83  	}
    84  }