Go Mathematical Expression Toolkit. Run-time mathematical expression parser and evaluation engine.

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/Pramod-Devireddy/go-exprtk

Examples

package main

import (
	"fmt"

	"github.com/Pramod-Devireddy/go-exprtk"
)

func main() {
	exprtkObj := exprtk.NewExprtk()
	defer exprtkObj.Delete()

	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())
}

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

Similar Resources

GopherLua: VM and compiler for Lua in Go

GopherLua: VM and compiler for Lua in Go. GopherLua is a Lua5.1 VM and compiler written in Go. GopherLua has a same goal with Lua: Be a scripting lang

Jan 9, 2023

A Lua 5.3 VM and compiler written in Go.

DCLua - Go Lua Compiler and VM: This is a Lua 5.3 VM and compiler written in Go. This is intended to allow easy embedding into Go programs, with minim

Dec 12, 2022

Grumpy is a Python to Go source code transcompiler and runtime.

Grumpy: Go running Python Overview Grumpy is a Python to Go source code transcompiler and runtime that is intended to be a near drop-in replacement fo

Jan 7, 2023

High-performance PHP application server, load-balancer and process manager written in Golang

High-performance PHP application server, load-balancer and process manager written in Golang

RoadRunner is an open-source (MIT licensed) high-performance PHP application server, load balancer, and process manager. It supports running as a serv

Dec 30, 2022

Runcmd - just golang binary that runs commands from url or local file and logs output

runcmd just golang binary that runs commands from url or local file and logs out

Feb 2, 2022

Expression evaluation engine for Go: fast, non-Turing complete, dynamic typing, static typing

Expression evaluation engine for Go: fast, non-Turing complete, dynamic typing, static typing

Expr Expr package provides an engine that can compile and evaluate expressions. An expression is a one-liner that returns a value (mostly, but not lim

Jan 1, 2023

Expression evaluation engine for Go: fast, non-Turing complete, dynamic typing, static typing

Expression evaluation engine for Go: fast, non-Turing complete, dynamic typing, static typing

Expr Expr package provides an engine that can compile and evaluate expressions. An expression is a one-liner that returns a value (mostly, but not lim

Dec 30, 2022

Expression evaluation engine for Go: fast, non-Turing complete, dynamic typing, static typing

Expression evaluation engine for Go: fast, non-Turing complete, dynamic typing, static typing

Expr Expr package provides an engine that can compile and evaluate expressions. An expression is a one-liner that returns a value (mostly, but not lim

Dec 30, 2022

Simple expression evaluation engine for Go

🌶️ chili Currently in development, Unstable (API may change in future) Simple expression evaluation engine. Expression is one liner that evalutes int

Nov 8, 2022

Suan - Mathematical expression calculation tool

suan Suan( 算 ) is a CLI tool to calculate given mathematical expression. Current

Feb 14, 2022

Fast, portable, non-Turing complete expression evaluation with gradual typing (Go)

Common Expression Language The Common Expression Language (CEL) is a non-Turing complete language designed for simplicity, speed, safety, and portabil

Jan 3, 2023

Expression evaluation in golang

Expression evaluation in golang

Gval Gval (Go eVALuate) provides support for evaluating arbitrary expressions, in particular Go-like expressions. Evaluate Gval can evaluate expressio

Dec 27, 2022

Fast, portable, non-Turing complete expression evaluation with gradual typing (Go)

Common Expression Language The Common Expression Language (CEL) is a non-Turing complete language designed for simplicity, speed, safety, and portabil

Dec 24, 2022

Expression evaluation in golang

Expression evaluation in golang

Gval Gval (Go eVALuate) provides support for evaluating arbitrary expressions, in particular Go-like expressions. Evaluate Gval can evaluate expressio

Dec 27, 2022

Arbitrary expression evaluation for golang

govaluate Provides support for evaluating arbitrary C-like artithmetic/string expressions. Why can't you just write these expressions in code? Sometim

Jan 2, 2023

Tpf2-tpnetmap-toolkit - A toolkit to create svg map images from TransportFever2 world data

Tpf2-tpnetmap-toolkit - A toolkit to create svg map images from TransportFever2 world data

tpf2-tpnetmap-toolkit TransportFever2 のワールドデータから svg のマップ画像を作成するツールキットです。 1. 導入方

Feb 17, 2022

a cheat-sheet for mathematical notation in code form

math-as-code Chinese translation (中文版) Python version (English) This is a reference to ease developers into mathematical notation by showing compariso

Dec 27, 2022

Document mathematical Go code beautifully

mathfmt Document mathematical Go code beautifully. Write mathematical formulae in a LaTeX-ish syntax Super/subscripts formatted with Unicode character

Nov 9, 2022

Time-Based One-Time Password (TOTP) and HMAC-Based One-Time Password (HOTP) library for Go.

otpgo HMAC-Based and Time-Based One-Time Password (HOTP and TOTP) library for Go. Implements RFC 4226 and RFC 6238. Contents Supported Operations Read

Dec 19, 2022
Comments
  • QUESTION: is there a way to access to the nth element of an array?

    QUESTION: is there a way to access to the nth element of an array?

    I'm doing this:

    exp := exprtk.NewExprtk()
    exp.SetExpression("v[1];")
    exp.AddVectorVariable("v")
    err := exp.CompileExpression()
    if err != nil {
            return err.Error()
    }
    exp.SetVectorVariableValue("v", []float64{4.0, 4.0, 4.0, 4.0})
    res := exp.GetEvaluatedValue()
    

    and as a result I get the following exception

    fatal error: unexpected signal during runtime execution
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7a7278]
    
    runtime stack:
    runtime.throw(0x95471b, 0x2a)
            /usr/local/go/src/runtime/panic.go:1117 +0x72
    runtime.sigpanic()
            /usr/local/go/src/runtime/signal_unix.go:718 +0x2e5
    
    goroutine 1 [syscall]:
    runtime.cgocall(0x766440, 0xc000055e48, 0x765cb5)
            /usr/local/go/src/runtime/cgocall.go:154 +0x5b fp=0xc000055e18 sp=0xc000055de0 pc=0x6cea5b
    github.com/Pramod-Devireddy/go-exprtk._Cfunc_getEvaluatedValue(0x293c470, 0x0)
            _cgo_gotypes.go:130 +0x45 fp=0xc000055e48 sp=0xc000055e18 pc=0x7658a5
    github.com/Pramod-Devireddy/go-exprtk.GoExprtk.GetEvaluatedValue.func1(0x293c470, 0x4010000000000000)
            /home/dgago/go/pkg/mod/github.com/!pramod-!devireddy/[email protected]/exprtk.go:75 +0x4d fp=0xc000055e78 sp=0xc000055e48 pc=0x7660ad
    github.com/Pramod-Devireddy/go-exprtk.GoExprtk.GetEvaluatedValue(0x293c470, 0x94dbda)
            /home/dgago/go/pkg/mod/github.com/!pramod-!devireddy/[email protected]/exprtk.go:75 +0x2b fp=0xc000055e98 sp=0xc000055e78 pc=0x765dab
    main.Eval2(0x72c3a5, 0xc000000180)
            /home/dgago/r/me/sa-rl-be/main.go:186 +0x133 fp=0xc000055f18 sp=0xc000055e98 pc=0x7662d3
    main.main()
            /home/dgago/r/me/sa-rl-be/main.go:14 +0x26 fp=0xc000055f88 sp=0xc000055f18 pc=0x766106
    runtime.main()
            /usr/local/go/src/runtime/proc.go:225 +0x256 fp=0xc000055fe0 sp=0xc000055f88 pc=0x700e36
    runtime.goexit()
            /usr/local/go/src/runtime/asm_amd64.s:1371 +0x1 fp=0xc000055fe8 sp=0xc000055fe0 pc=0x730261
    exit status 2
    

    Environment is Ubuntu Linux 20.04. Go Version 1.16.

    Thanks in advance!

Suan - Mathematical expression calculation tool

suan Suan( 算 ) is a CLI tool to calculate given mathematical expression. Current

Feb 14, 2022
Fast, portable, non-Turing complete expression evaluation with gradual typing (Go)

Common Expression Language The Common Expression Language (CEL) is a non-Turing complete language designed for simplicity, speed, safety, and portabil

Dec 24, 2022
Expression evaluation in golang
Expression evaluation in golang

Gval Gval (Go eVALuate) provides support for evaluating arbitrary expressions, in particular Go-like expressions. Evaluate Gval can evaluate expressio

Dec 27, 2022
Arbitrary expression evaluation for golang

govaluate Provides support for evaluating arbitrary C-like artithmetic/string expressions. Why can't you just write these expressions in code? Sometim

Jan 2, 2023
Logexp - Logical expression compiler for golang

Logical Expression Compiler Functions: - Compile(exp string) - Match(text string

Jan 24, 2022
A parser library for Go
A parser library for Go

A dead simple parser package for Go V2 Introduction Tutorial Tag syntax Overview Grammar syntax Capturing Capturing boolean value Streaming Lexing Sta

Dec 30, 2022
PHP parser written in Go
PHP parser written in Go

PHP Parser written in Go This project uses goyacc and ragel tools to create PHP parser. It parses source code into AST. It can be used to write static

Dec 25, 2022
ECMAScript/JavaScript engine in pure Go

goja ECMAScript 5.1(+) implementation in Go. Goja is an implementation of ECMAScript 5.1 in pure Go with emphasis on standard compliance and performan

Jan 1, 2023
Promise to the Go compiler that your Reads and Writes are well-behaved

noescape go get lukechampine.com/noescape noescape provides Read and Write functions that do not heap-allocate their argument. Normally, when you pas

Dec 22, 2022
Go compiler for small places. Microcontrollers, WebAssembly, and command-line tools. Based on LLVM.

TinyGo - Go compiler for small places TinyGo is a Go compiler intended for use in small places such as microcontrollers, WebAssembly (Wasm), and comma

Jan 4, 2023