Expr package provides an engine that can compile and evaluate math expressions. An expression is a one-liner that returns a value (mostly, but not limited to, float64 and []float64). It is designed for simplicity, speed and safety.
The purpose of the package is to allow users to use expressions for fast vector math operations. It is a perfect candidate for the foundation of a time series database and machine learning feature engineering. The idea is to let configure things in a dynamic way without recompile of a program:
# Get a new vector from the addition of cos() and sin() of other variables
cos(X) + 0.33 * sin(Y)
# Get a new norm vector from vector X
(X - nanmin(X)) / (nanmax(X) - nanmin(X))- Seamless integration with Go (no need to redefine types)
- Static typing
out, err := expr.Compile(`name + age`) // err: invalid operation + (mismatched types string and int) // | name + age // | .....^
- User-friendly error messages.
- Reasonable set of basic operators.
- Dozens of Numpy-like builtin math functions:
abs,acos,acosh,asin,asinh,atan,atanh,cbrt,ceil,cos,cosh,erf,erfc,erfcinv,erfinv,exp,exp2,expm1,floor,gamma,j0,j1,log,log10,log1p,log2,logb,round,roundtoeven,sin,sinh,sqrt,tan,tanh,trunc,y0,y1,maximum,minimum,mod,pow,remainder,nanmin,nanmax,nanmean,nanstd,nansum,nanprod.2 * (nanmean(Scores) - minimum(Elevation, Temp))
go get github.com/regel/expr
package main
import (
"fmt"
"github.com/regel/expr"
"github.com/regel/expr/ast"
)
func main() {
code := `1 + (sum(Features) / 2)`
program, err := expr.Compile(code)
if err != nil {
panic(err)
}
env := &ast.Env{
"Features": []float64{0.5, 1.0, 1.5},
}
output, err := expr.Run(program, env)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", output)
}