exprtk

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 4 Imported by: 0

README

Go Mathematical Expression Toolkit

Go Doc Reference Build Status Go Report Card

The Go Expression Toolkit (Go-ExprTk) is a wrapper library based on C++ Mathematical Expression Toolkit Library (ExprTk).

It is a simple to use, easy to integrate and extremely efficient run-time mathematical expression parser and evaluation engine.

Go-ExprTk supports numerous forms of functional, logical and vector processing semantics and is very easily extendible.

Installation

go get github.com/khan-lau/go-exprtk

Examples

package main

import (
	"fmt"
	"github.com/khan-lau/go-exprtk"
)

func main() {
	exprtkObj := exprtk.NewExprtk()
	exprtkObj.SetExpression("(x + 2)*(y-2)")
	exprtkObj.AddDoubleVariable("x")
	exprtkObj.AddDoubleVariable("y")

	err := exprtkObj.CompileExpression()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	
	exprtkObj.SetDoubleVariableValue("x", 18)
	exprtkObj.SetDoubleVariableValue("y", 32)

	fmt.Println(exprtkObj.GetEvaluatedValue())
}
package main

import (
	"fmt"
	"github.com/khan-lau/go-exprtk"
)

func main() {
	exprtkObj := exprtk.NewExprtk()
	exprtkObj.SetExpression("sum(x)")
	exprtkObj.AddVectorVariable("x", []float64{1, 5, 2, 4.2, 10, 6.5, 7, 8, 1.3})

	err := exprtkObj.CompileExpression()
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	exprtkObj.UpdateVectorVariable("x",  []float64{1.5, 5, 2.1, 4.2, 10, 6.5, 7, 8, 1.3})
	fmt.Println(exprtkObj.GetEvaluatedValue())
}

Check out more Examples

⚡ Features

The Go-ExprTk library has the following capabilities:

  • Mathematical operators (+, -, *, /, %, ^)

  • Functions (min, max, avg, sum, abs, ceil, floor, round, roundn, exp, log, log10, logn, pow, root, sqrt, clamp, inrange, swap)

  • Trigonometry (sin, cos, tan, acos, asin, atan, atan2, cosh, cot, csc, sec, sinh, tanh, d2r, r2d, d2g, g2d, hyp)

  • Equalities & Inequalities (=, ==, <>, !=, <, <=, >, >=)

  • Assignment (:=, +=, -=, *=, /=, %=)

  • Logical operators (and, nand, nor, not, or, xor, xnor, mand, mor)

  • Control structures (if-then-else, ternary conditional, switch case, return-statement)

  • Loop structures (while loop, for loop, repeat until loop, break, continue)

  • Optimization of expressions (constant folding, strength reduction, operator coupling, special functions and dead code elimination)

  • String operations (equalities, inequalities, logical operators, concatenation and sub-ranges)

  • Expression local variables, vectors and strings

  • User defined variables, vectors, strings, constants and function support

  • Multivariate function composition

  • Multiple sequence point and sub expression support

  • Numeric integration and differentiation

  • Vector Processing: BLAS-L1 (axpy, axpby, axpb), all/any-true/false, count, rotate-left/right, shift-left/right, sort, nth_element, iota, sum, kahan-sum, dot-product, copy

❤️ Credits

This module could not be possible without the ExprTk library by Arash Partow and the idea of creating the wrapper module by Narayana Rao G S

Published under MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ReservedList = []string{"if", "else", "for", "break", "and", "true", "abs", "var",
	"max", "min", "sum", "acos", "acosh", "asin", "asinh", "atan",
	"atanh", "atan2", "avg", "case", "ceil", "clamp", "mod",
	"continue", "cos", "cosh", "cot", "csc", "default",
	"deg2grad", "deg2rad", "equal", "erf", "erfc", "exp",
	"expm1", "false", "floor", "frac", "grad2deg", "nand",
	"hypot", "iclamp", "ilike", "in", "inrange", "mor", "nor",
	"like", "log10", "log1p", "mand", "mul", "ncdf", "log2",
	"not", "not_equal", "null", "or", "pow", "rad2deg", "logn",
	"repeat", "return", "root", "round", "roundn", "sec", "sgn",
	"shl", "shr", "sin", "sinc", "sinh", "sqrt", "swap",
	"switch", "tan", "tanh", "trunc", "until", "log",
	"while", "xnor", "xor", "&", "|",
	"x_sum", "x_avg", "x_min", "x_max", "x_first", "x_last", "x_dev", "x_diff", "x_wavg"}

Functions

func ExpressionVerify

func ExpressionVerify(expr string) error

表达式校验

func FreeExprtk

func FreeExprtk(obj GoExprtk)

释放内存

func IsLetterOrDigit

func IsLetterOrDigit(c byte) bool

c是字母或数字

func Version

func Version() string

Types

type GoExprtk

type GoExprtk struct {
	// contains filtered or unexported fields
}

GoExprtk ...Exprtk Structure

func NewExprtk

func NewExprtk() GoExprtk

NewExprtk ... Creates a new object 调用完后请使用FreeExprtk、Free释放内存

func (GoExprtk) AddDoubleVariable

func (obj GoExprtk) AddDoubleVariable(x string, val float64)

AddDoubleVariable ... Adds variable to the expression

func (*GoExprtk) AddFunction

func (obj *GoExprtk) AddFunction(funName string, cFun unsafe.Pointer, argNum int)

添加自定义函数

func (GoExprtk) AddStringVariable

func (obj GoExprtk) AddStringVariable(varName string, val string)

AddStringVariable ... Adds variable to the expression

func (GoExprtk) AddVectorVariable

func (obj GoExprtk) AddVectorVariable(x string, val []float64)

AddVectorVariable ... Adds variable to the expression

func (*GoExprtk) CompileExpression

func (obj *GoExprtk) CompileExpression() error

CompileExpression ... Compiles the Expression

func (*GoExprtk) Free

func (obj *GoExprtk) Free()

释放内存

func (GoExprtk) GetEvaluatedValue

func (obj GoExprtk) GetEvaluatedValue() float64

GetEvaluatedValue ... Returns the evaluated value

func (*GoExprtk) GetVariableList

func (obj *GoExprtk) GetVariableList() []string

获取自动注册的变量列表

func (*GoExprtk) SetExpression

func (obj *GoExprtk) SetExpression(expr string)

设置表达式 请通过GetVariableList()获取自动注册的变量列表

func (GoExprtk) UpdateDoubleVariableValue

func (obj GoExprtk) UpdateDoubleVariableValue(varName string, val float64)

SetDoubleVariableValue ... Sets value to the variable

func (GoExprtk) UpdateStringVariableValue

func (obj GoExprtk) UpdateStringVariableValue(varName string, val string)

SetStringVariableValue ... Sets value to the variable

func (GoExprtk) UpdateVectorVariableValue

func (obj GoExprtk) UpdateVectorVariableValue(varName string, val []float64)

SetVectorVariableValue ... Sets value to the variable

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL