Documentation ¶
Index ¶
- Variables
- func CheckArgs(need, have int, funcName string) error
- func FactorialMemoization(n int, facts [100]float64) (res float64)
- type Expr
- func (ex *Expr) Compile(functions Functions) error
- func (ex *Expr) DefaultParams()
- func (ex *Expr) Eval() (float64, error)
- func (ex *Expr) EvalBool() (bool, error)
- func (ex *Expr) PrepareExpr(functions Functions) (string, Functions)
- func (ex *Expr) RemoveParam(name string)
- func (ex *Expr) SetParam(name string, value interface{})
- type ExprChange
- type ExprChanges
- type Functions
- type Params
Constants ¶
This section is empty.
Variables ¶
var DefaultExprChanges = ExprChanges{
{"pi", "π"},
{"**", "^"},
{"+.", "+0."},
{"-.", "-0."},
{"*.", "*0."},
{"/.", "/0."},
{"^.", "^0."},
{")(", ")*("},
}
DefaultExprChanges is the default ExprChanges for expressions
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 ¶
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 ¶
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 (*Expr) Compile ¶
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) PrepareExpr ¶
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 ¶
RemoveParam removes a parameter from an expression
type ExprChange ¶
ExprChange contains a string in an expression that should be replaced with another string
type Functions ¶
type Functions map[string]govaluate.ExpressionFunction
Functions are a map of named functions