mee

package module
v0.0.0-...-4ee45f1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2022 License: BSD-3-Clause Imports: 7 Imported by: 1

README

mee - Mathematical expression evaluation

Uses Knetic/govaluate

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultExprChanges = ExprChanges{
	{"pi", "π"},
	{"**", "^"},
	{"+.", "+0."},
	{"-.", "-0."},
	{"*.", "*0."},
	{"/.", "/0."},
	{"^.", "^0."},
	{")(", ")*("},
}

DefaultExprChanges is the default ExprChanges for expressions

View Source
var DefaultFunctions = Functions{
	"cos": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "cos")
		if err != nil {
			return 0, err
		}
		y := math.Cos(args[0].(float64))
		return y, nil
	},
	"sin": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "sin")
		if err != nil {
			return 0, err
		}
		y := math.Sin(args[0].(float64))
		return y, nil
	},
	"tan": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "tan")
		if err != nil {
			return 0, err
		}
		y := math.Tan(args[0].(float64))
		return y, nil
	},
	"pow": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(2, len(args), "pow")
		if err != nil {
			return 0, err
		}
		y := math.Pow(args[0].(float64), args[1].(float64))
		return y, nil
	},
	"abs": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "abs")
		if err != nil {
			return 0, err
		}
		y := math.Abs(args[0].(float64))
		return y, nil
	},
	"fact": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "fact")
		if err != nil {
			return 0, err
		}
		var facts [100]float64
		y := FactorialMemoization(int(args[0].(float64)), facts)
		return y, nil
	},
	"round": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "round")
		if err != nil {
			return 0, err
		}
		y := math.Round(args[0].(float64))
		return y, nil
	},
	"ceil": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "ceil")
		if err != nil {
			return 0, err
		}
		y := math.Ceil(args[0].(float64))
		return y, nil
	},
	"floor": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "floor")
		if err != nil {
			return 0, err
		}
		y := math.Floor(args[0].(float64))
		return y, nil
	},
	"mod": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(2, len(args), "mod")
		if err != nil {
			return 0, err
		}
		y := math.Mod(args[0].(float64), args[1].(float64))
		return y, nil
	},
	"rand": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "rand")
		if err != nil {
			return 0, err
		}
		y := rand.Float64() * args[0].(float64)
		return y, nil
	},
	"sqrt": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "sqrt")
		if err != nil {
			return 0, err
		}
		y := math.Sqrt(args[0].(float64))
		return y, nil
	},
	"ln": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "ln")
		if err != nil {
			return 0, err
		}
		y := math.Log(args[0].(float64))
		return y, nil
	},
	"csc": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "csc")
		if err != nil {
			return 0, err
		}
		y := 1 / math.Sin(args[0].(float64))
		return y, nil
	},
	"sec": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "sec")
		if err != nil {
			return 0, err
		}
		y := 1 / math.Cos(args[0].(float64))
		return y, nil
	},
	"cot": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "cot")
		if err != nil {
			return 0, err
		}
		y := 1 / math.Tan(args[0].(float64))
		return y, nil
	},
	"arcsin": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "arcsin")
		if err != nil {
			return 0, err
		}
		y := math.Asin(args[0].(float64))
		return y, nil
	},
	"arccos": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "arccos")
		if err != nil {
			return 0, err
		}
		y := math.Acos(args[0].(float64))
		return y, nil
	},
	"arctan": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(1, len(args), "arctan")
		if err != nil {
			return 0, err
		}
		y := math.Atan(args[0].(float64))
		return y, nil
	},
	"if": func(args ...interface{}) (interface{}, error) {
		err := CheckArgs(3, len(args), "if")
		if err != nil {
			return 0, err
		}
		if args[0].(bool) {
			return args[1].(float64), nil
		}
		return args[2].(float64), nil
	},
}

DefaultFunctions are a standard set of functions to be used for evaluating expressions

Functions

func CheckArgs

func CheckArgs(need, have int, funcName string) error

CheckArgs checks whether a function has the right number of arguments. Returns nil if it does, returns an error if there is the wrong number of arguments.

func FactorialMemoization

func FactorialMemoization(n int, facts [100]float64) (res float64)

FactorialMemoization is used to take the factorial for the fact() function

Types

type Expr

type Expr struct {
	Expr   string
	Val    *govaluate.EvaluableExpression
	Params Params
}

Expr contains the expression string, the expression value, and the parameters

func NewExpr

func NewExpr(expr string) Expr

NewExpr makes a new expression with given string

func (*Expr) Compile

func (ex *Expr) Compile(functions Functions) error

Compile makes formatting changes to the expression, compiles the value of the expression, and makes the parameters

func (*Expr) DefaultParams

func (ex *Expr) DefaultParams()

DefaultParams makes the parameters if they don't already exist and adds pi and e

func (*Expr) Eval

func (ex *Expr) Eval() (float64, error)

Eval evaluates an expression with float64 value

func (*Expr) EvalBool

func (ex *Expr) EvalBool() (bool, error)

EvalBool evaluates an expression with bool value

func (*Expr) PrepareExpr

func (ex *Expr) PrepareExpr(functions Functions) (string, Functions)

PrepareExpr makes changes to the expression so it can be graphed well, and returns the expression string that should be used for evaluation and the functions that should be used for evaluation

func (*Expr) RemoveParam

func (ex *Expr) RemoveParam(name string)

RemoveParam removes a parameter from an expression

func (*Expr) SetParam

func (ex *Expr) SetParam(name string, value interface{})

SetParam sets a parameter for an expression

type ExprChange

type ExprChange struct {
	Old string
	New string
}

ExprChange contains a string in an expression that should be replaced with another string

type ExprChanges

type ExprChanges []ExprChange

ExprChanges contains multiple ExprChange structs

type Functions

type Functions map[string]govaluate.ExpressionFunction

Functions are a map of named functions

type Params

type Params map[string]interface{}

Params are the parameters for an expression

Jump to

Keyboard shortcuts

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