github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/momath/math.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  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  )
    24  
    25  func Acos(arg, result *vector.Vector) error {
    26  	argCol := vector.MustTCols[float64](arg)
    27  	resCol := vector.MustTCols[float64](result)
    28  	nulls.Set(result.Nsp, arg.Nsp)
    29  	for i, v := range argCol {
    30  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
    31  			if v < -1 || v > 1 {
    32  				// MySQL is totally F***ed.
    33  				// return moerr.NewError(moerr.INVALID_ARGUMENT, fmt.Sprintf("acos argument %v is not valid", v))
    34  				nulls.Add(result.Nsp, uint64(i))
    35  			} else {
    36  				resCol[i] = math.Acos(v)
    37  			}
    38  		}
    39  	}
    40  	return nil
    41  }
    42  
    43  func Atan(arg, result *vector.Vector) error {
    44  	argCol := vector.MustTCols[float64](arg)
    45  	resCol := vector.MustTCols[float64](result)
    46  	nulls.Set(result.Nsp, arg.Nsp)
    47  	for i, v := range argCol {
    48  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
    49  			resCol[i] = math.Atan(v)
    50  		}
    51  	}
    52  	return nil
    53  }
    54  
    55  func AtanWithTwoArg(firstArg, secondArg, result *vector.Vector) error {
    56  	firstCol := vector.MustTCols[float64](firstArg)
    57  	secondCol := vector.MustTCols[float64](secondArg)
    58  	resCol := vector.MustTCols[float64](result)
    59  	for i, v := range firstCol {
    60  		if v == 0 {
    61  			return moerr.NewInvalidArgNoCtx("Atan first input", 0)
    62  		}
    63  		resCol[i] = math.Atan(secondCol[i] / v)
    64  	}
    65  	return nil
    66  }
    67  
    68  func Cos(arg, result *vector.Vector) error {
    69  	argCol := vector.MustTCols[float64](arg)
    70  	resCol := vector.MustTCols[float64](result)
    71  	nulls.Set(result.Nsp, arg.Nsp)
    72  	for i, v := range argCol {
    73  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
    74  			resCol[i] = math.Cos(v)
    75  		}
    76  	}
    77  	return nil
    78  }
    79  
    80  func Cot(arg, result *vector.Vector) error {
    81  	argCol := vector.MustTCols[float64](arg)
    82  	resCol := vector.MustTCols[float64](result)
    83  	nulls.Set(result.Nsp, arg.Nsp)
    84  	for i, v := range argCol {
    85  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
    86  			if v == 0 {
    87  				panic(moerr.NewInvalidArgNoCtx("cot", "cot(0)"))
    88  			} else {
    89  				resCol[i] = math.Tan(math.Pi/2.0 - v)
    90  			}
    91  		}
    92  	}
    93  	return nil
    94  }
    95  
    96  func Exp(arg, result *vector.Vector) error {
    97  	argCol := vector.MustTCols[float64](arg)
    98  	resCol := vector.MustTCols[float64](result)
    99  	nulls.Set(result.Nsp, arg.Nsp)
   100  	for i, v := range argCol {
   101  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
   102  			resCol[i] = math.Exp(v)
   103  		}
   104  	}
   105  	return nil
   106  }
   107  
   108  func Ln(arg, result *vector.Vector) error {
   109  	argCol := vector.MustTCols[float64](arg)
   110  	resCol := vector.MustTCols[float64](result)
   111  	nulls.Set(result.Nsp, arg.Nsp)
   112  	for i, v := range argCol {
   113  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
   114  			if v <= 0 {
   115  				return moerr.NewInvalidArgNoCtx("ln", v)
   116  			} else {
   117  				resCol[i] = math.Log(v)
   118  			}
   119  		}
   120  	}
   121  	return nil
   122  }
   123  
   124  func Sin(arg, result *vector.Vector) error {
   125  	argCol := vector.MustTCols[float64](arg)
   126  	resCol := vector.MustTCols[float64](result)
   127  	nulls.Set(result.Nsp, arg.Nsp)
   128  	for i, v := range argCol {
   129  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
   130  			resCol[i] = math.Sin(v)
   131  		}
   132  	}
   133  	return nil
   134  }
   135  
   136  func Sinh(arg, result *vector.Vector) error {
   137  	argCol := vector.MustTCols[float64](arg)
   138  	resCol := vector.MustTCols[float64](result)
   139  	nulls.Set(result.Nsp, arg.Nsp)
   140  	for i, v := range argCol {
   141  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
   142  			r := math.Sinh(v)
   143  			if math.IsInf(r, 0) {
   144  				return moerr.NewOutOfRangeNoCtx("float64", "DOUBLE value is out of range in 'sinh(%v)'", v)
   145  			}
   146  			resCol[i] = r
   147  		}
   148  	}
   149  	return nil
   150  }
   151  
   152  func Tan(arg, result *vector.Vector) error {
   153  	argCol := vector.MustTCols[float64](arg)
   154  	resCol := vector.MustTCols[float64](result)
   155  	nulls.Set(result.Nsp, arg.Nsp)
   156  	for i, v := range argCol {
   157  		if !nulls.Contains(arg.Nsp, (uint64)(i)) {
   158  			resCol[i] = math.Tan(v)
   159  		}
   160  	}
   161  	return nil
   162  }