github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/momath/math_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package momath
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/testutil"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestLn(t *testing.T) {
    27  	as := []float64{1, math.Exp(0), math.Exp(1), math.Exp(10), math.Exp(100), math.Exp(99), math.Exp(-1)}
    28  	cs := make([]float64, 7)
    29  
    30  	av := testutil.MakeFloat64Vector(as, nil)
    31  	cv := testutil.MakeFloat64Vector(cs, nil)
    32  
    33  	err := Ln(av, cv)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	cols := vector.MustTCols[float64](cv)
    38  	require.Equal(t, []float64{0.0, 0.0, 1.0, 10.0, 100.0, 99.0, -1}, cols)
    39  }
    40  
    41  func TestExP(t *testing.T) {
    42  	as := []float64{-1, 0, 1, 2, 10, 100}
    43  	cs := make([]float64, 6)
    44  
    45  	av := testutil.MakeFloat64Vector(as, nil)
    46  	cv := testutil.MakeFloat64Vector(cs, nil)
    47  
    48  	err := Exp(av, cv)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  	cols := vector.MustTCols[float64](cv)
    53  	require.Equal(t, []float64{math.Exp(-1), math.Exp(0), math.Exp(1), math.Exp(2), math.Exp(10), math.Exp(100)}, cols)
    54  }
    55  
    56  func TestSin(t *testing.T) {
    57  	as := []float64{-math.Pi / 2, 0, math.Pi / 2}
    58  	cs := make([]float64, 3)
    59  
    60  	av := testutil.MakeFloat64Vector(as, nil)
    61  	cv := testutil.MakeFloat64Vector(cs, nil)
    62  
    63  	err := Sin(av, cv)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  	cols := vector.MustTCols[float64](cv)
    68  	require.Equal(t, []float64{-1, 0, 1}, cols)
    69  }
    70  
    71  func TestCos(t *testing.T) {
    72  	as := []float64{-math.Pi, 0, math.Pi}
    73  	cs := make([]float64, 3)
    74  
    75  	av := testutil.MakeFloat64Vector(as, nil)
    76  	cv := testutil.MakeFloat64Vector(cs, nil)
    77  
    78  	err := Cos(av, cv)
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	cols := vector.MustTCols[float64](cv)
    83  	require.Equal(t, []float64{-1, 1, -1}, cols)
    84  }
    85  
    86  func TestTan(t *testing.T) {
    87  	as := []float64{0}
    88  	cs := make([]float64, 1)
    89  
    90  	av := testutil.MakeFloat64Vector(as, nil)
    91  	cv := testutil.MakeFloat64Vector(cs, nil)
    92  
    93  	err := Tan(av, cv)
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  	cols := vector.MustTCols[float64](cv)
    98  	require.Equal(t, []float64{0}, cols)
    99  }
   100  
   101  func TestSinh(t *testing.T) {
   102  	as := []float64{0}
   103  	cs := make([]float64, 1)
   104  
   105  	av := testutil.MakeFloat64Vector(as, nil)
   106  	cv := testutil.MakeFloat64Vector(cs, nil)
   107  
   108  	err := Sinh(av, cv)
   109  	if err != nil {
   110  		panic(err)
   111  	}
   112  	cols := vector.MustTCols[float64](cv)
   113  	require.Equal(t, []float64{0}, cols)
   114  }
   115  
   116  func TestAcos(t *testing.T) {
   117  	as := []float64{1}
   118  	cs := make([]float64, 1)
   119  
   120  	av := testutil.MakeFloat64Vector(as, nil)
   121  	cv := testutil.MakeFloat64Vector(cs, nil)
   122  
   123  	err := Acos(av, cv)
   124  	if err != nil {
   125  		panic(err)
   126  	}
   127  	cols := vector.MustTCols[float64](cv)
   128  	require.Equal(t, []float64{0}, cols)
   129  }
   130  
   131  func TestAtan(t *testing.T) {
   132  	as := []float64{0}
   133  	cs := make([]float64, 1)
   134  
   135  	av := testutil.MakeFloat64Vector(as, nil)
   136  	cv := testutil.MakeFloat64Vector(cs, nil)
   137  
   138  	err := Atan(av, cv)
   139  	if err != nil {
   140  		panic(err)
   141  	}
   142  	cols := vector.MustTCols[float64](cv)
   143  	require.Equal(t, []float64{0}, cols)
   144  }
   145  
   146  func TestAtanWithTwoArg(t *testing.T) {
   147  	firstCol := []float64{-1, 1, 1, 1, 1.0, 1.0}
   148  	secondCol := []float64{1, 0, -1, 1, -1.0, 1.0}
   149  	resultCol := make([]float64, 6)
   150  	firstVec := testutil.MakeFloat64Vector(firstCol, nil)
   151  	secondVec := testutil.MakeFloat64Vector(secondCol, nil)
   152  	resultVector := testutil.MakeFloat64Vector(resultCol, nil)
   153  	err := AtanWithTwoArg(firstVec, secondVec, resultVector)
   154  	if err != nil {
   155  		panic(err)
   156  	}
   157  	cols := vector.MustTCols[float64](resultVector)
   158  	require.Equal(t, []float64{-0.7853981633974483, 0, -0.7853981633974483, 0.7853981633974483, -0.7853981633974483, 0.7853981633974483}, cols)
   159  
   160  }