zeno

package module
v0.0.0-...-ef3c048 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2019 License: MIT Imports: 6 Imported by: 0

README

zeno

Build Status GoDoc Go Report Card

zeno is a math expression parser capable of:

  • Generating a postfix equivalent to given expression
  • Create a binary operation tree
  • Calculate the result of the expression (without variables)
  • Generate LaTeX representation of an operation tree

Install

Use golang package manager to install zeno

$ go get -u github.com/xzebra/zeno

Usage

import "github.com/xzebra/zeno"

int main() {
    expression := "-2.1483*3.14^3+4"
    // Get a postfix representation of the expression
    postfix, _ := zeno.ToPostfix(expression)
    fmt.Println(postfix) // "-2.1483 3.14 3 ^ * 4 +"

    // Get the binary tree representation
    tree, _ := zeno.PostfixToTree(postfix)
    // You can also get it using ToTree
    tree, _ = zeno.ToTree(expression)

    // After that you can get the result (if the expression 
    // doesn't have variables) or its LaTeX representation.
    fmt.Println(tree.Calculate()) // -62.509529055200005
    fmt.Println(tree.LaTeX()) // "neg(2.1483)\cdot3.14^3+4"
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Remember to update tests according to the changes.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorParenthesis   = fmt.Errorf("mismatched parenthesis")
	ErrorUnknownSymbol = fmt.Errorf("unknown symbol")
	ErrorEmptyString   = fmt.Errorf("cannot parse empty string")
	ErrorNotEnoughArgs = fmt.Errorf("no args in function call")
)
View Source
var (
	ErrorVariableNumeric = fmt.Errorf("trying to use variable as numeric")
)
View Source
var (
	ErrorZeroDivision = fmt.Errorf("can't divide by 0")
)

Functions

func CalculateExpression

func CalculateExpression(exp string) (float64, error)

CalculateExpression returns the numerical value of the input expression or an error in case something went wrong.

func ToPostfix

func ToPostfix(exp string) (string, error)

ToPostfix returns the postfix representation of the input expression or an error if input format is wrong.

Types

type Constant

type Constant struct {
	Value float64
}

Constant represents a numeric constant expression

func (*Constant) LaTeX

func (c *Constant) LaTeX(x, y *Operation) string

func (*Constant) Operate

func (c *Constant) Operate(x, y *Operation) (float64, error)

type Function

type Function struct {
	Name string
}

Function represents a mathematical function

func (*Function) LaTeX

func (f *Function) LaTeX(x, y *Operation) string

func (*Function) Operate

func (f *Function) Operate(x, y *Operation) (float64, error)

type Operation

type Operation struct {
	// can be constant, variable, func or operator
	Operator Operator

	// operands
	Left  *Operation
	Right *Operation
}

Operation is a binary tree containing the operands and the operation to perform

func PostfixToTree

func PostfixToTree(postfix string) (*Operation, error)

PostfixToTree translates a postfix expression into a binary tree data structure that can operate the expression.

func ToTree

func ToTree(exp string) (*Operation, error)

ToTree parses an expression and returns a binary tree representation of it or an error if a wrong format was used.

func (*Operation) LaTeX

func (o *Operation) LaTeX() string

func (*Operation) Operate

func (o *Operation) Operate() (float64, error)

type Operator

type Operator interface {
	Operate(x, y *Operation) (float64, error)
	LaTeX(x, y *Operation) string
}

Operator is the representation of a math expression operator.

type SimpleOperator

type SimpleOperator struct {
	Type byte
}

SimpleOperator represents basic operators such as addition, subtraction...

func (*SimpleOperator) LaTeX

func (o *SimpleOperator) LaTeX(x, y *Operation) string

func (*SimpleOperator) Operate

func (o *SimpleOperator) Operate(x, y *Operation) (float64, error)

type Variable

type Variable struct {
	Name string
}

Variable represent single character expression tokens. f.e.: "x" in "x + 1"

func (*Variable) LaTeX

func (v *Variable) LaTeX(x, y *Operation) string

func (*Variable) Operate

func (v *Variable) Operate(x, y *Operation) (float64, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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