github.com/m3db/m3@v1.5.0/src/query/functions/linear/math.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package linear
    22  
    23  import (
    24  	"fmt"
    25  	"math"
    26  
    27  	"github.com/m3db/m3/src/query/block"
    28  	"github.com/m3db/m3/src/query/functions/lazy"
    29  	"github.com/m3db/m3/src/query/parser"
    30  )
    31  
    32  const (
    33  	// AbsType takes absolute value of each datapoint in the series.
    34  	AbsType = "abs"
    35  
    36  	// CeilType rounds each value in the timeseries up to the nearest integer.
    37  	CeilType = "ceil"
    38  
    39  	// FloorType rounds each value in the timeseries down to the nearest integer.
    40  	FloorType = "floor"
    41  
    42  	// ExpType calculates the exponential function for all values.
    43  	// Special cases are: Exp(+Inf) = +Inf and Exp(NaN) = NaN
    44  	ExpType = "exp"
    45  
    46  	// SqrtType calculates the square root for all values.
    47  	SqrtType = "sqrt"
    48  
    49  	// Special cases for all of the following include:
    50  	// ln(+Inf) = +Inf
    51  	// ln(0) = -Inf
    52  	// ln(x < 0) = NaN
    53  	// ln(NaN) = NaN
    54  
    55  	// LnType calculates the natural logarithm for all values.
    56  	LnType = "ln"
    57  
    58  	// Log2Type calculates the binary logarithm for all values.
    59  	Log2Type = "log2"
    60  
    61  	// Log10Type calculates the decimal logarithm for values.
    62  	Log10Type = "log10"
    63  )
    64  
    65  var (
    66  	mathFuncs = map[string]block.ValueTransform{
    67  		AbsType:   math.Abs,
    68  		CeilType:  math.Ceil,
    69  		FloorType: math.Floor,
    70  		ExpType:   math.Exp,
    71  		SqrtType:  math.Sqrt,
    72  		LnType:    math.Log,
    73  		Log2Type:  math.Log2,
    74  		Log10Type: math.Log10,
    75  	}
    76  )
    77  
    78  // NewMathOp creates a new math op based on the type.
    79  func NewMathOp(opType string) (parser.Params, error) {
    80  	if fn, ok := mathFuncs[opType]; ok {
    81  		lazyOpts := block.NewLazyOptions().SetValueTransform(fn)
    82  		return lazy.NewLazyOp(opType, lazyOpts)
    83  	}
    84  
    85  	return nil, fmt.Errorf("unknown math type: %s", opType)
    86  }