op

package
v0.0.0-...-8433fb5 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcHeight

func CalcHeight(op Operator) (d uint)

CalcHeight returns the height of an Operator. The height of an Operator is the height of it's root node. The height of a node is the number of edges on the longest path between the node and a leaf.

func Count

func Count(op Operator, condition func(Operator) bool) (n uint)

Count returns the number of Operators that match a condition.

func CountConsts

func CountConsts(op Operator) uint

CountConsts returns the number of Consts in an Operator.

func CountDistinctVars

func CountDistinctVars(op Operator) uint

CountDistinctVars returns the number of distinct Vars in an Operator.

func CountLeaves

func CountLeaves(op Operator) uint

CountLeaves returns the number of leaves in an Operator.

func CountOps

func CountOps(op Operator) uint

CountOps returns the number of Operators in an Operator.

func CountVars

func CountVars(op Operator) uint

CountVars returns the number of Vars in an Operator.

func GetConsts

func GetConsts(op Operator) []float64

GetConsts returns the Consts contained in an Operator. Pre-order traversal is used.

func GetVars

func GetVars(op Operator) []uint

GetVars returns the Vars contained in an Operator. Pre-order traversal is used.

Types

type Abs

type Abs struct {
	Op Operator
}

The Abs operator.

func (Abs) Arity

func (abs Abs) Arity() uint

Arity of Abs is 1.

func (Abs) Diff

func (abs Abs) Diff(i uint) Operator

Diff compute the following derivative: |u|' = uu' / |u|.

func (Abs) Eval

func (abs Abs) Eval(X [][]float64) []float64

Eval computes the absolute value of each value.

func (Abs) Name

func (abs Abs) Name() string

Name of Abs is "abs".

func (Abs) Operand

func (abs Abs) Operand(i uint) Operator

Operand returns Abs's operand or nil.

func (Abs) SetOperand

func (abs Abs) SetOperand(i uint, op Operator) Operator

SetOperand replaces Abs's operand if i is equal to 0.

func (Abs) Simplify

func (abs Abs) Simplify() Operator

Simplify Abs.

func (Abs) String

func (abs Abs) String() string

String formatting.

type Add

type Add struct {
	Left, Right Operator
}

The Add operator.

func (Add) Arity

func (add Add) Arity() uint

Arity of Add is 2.

func (Add) Diff

func (add Add) Diff(i uint) Operator

Diff computes the following derivative: (u + v)' = u' + v'

func (Add) Eval

func (add Add) Eval(X [][]float64) []float64

Eval sums aligned values.

func (Add) Name

func (add Add) Name() string

Name of Add is "add".

func (Add) Operand

func (add Add) Operand(i uint) Operator

Operand returns one of Add's operands, or nil.

func (Add) SetOperand

func (add Add) SetOperand(i uint, op Operator) Operator

SetOperand replaces one of Add's operands if i is equal to 0 or 1.

func (Add) Simplify

func (add Add) Simplify() Operator

Simplify Add.

func (Add) String

func (add Add) String() string

String formatting.

type CodeDisplay

type CodeDisplay struct{}

CodeDisplay outputs an code-like representation of an Operator.

Example
var (
	op   = Cos{Add{Const{42}, Var{0}}}
	disp = CodeDisplay{}
)
fmt.Println(disp.Apply(op))
Output:

cos(add(42, x0))

func (CodeDisplay) Apply

func (displayer CodeDisplay) Apply(op Operator) string

Apply CodeDisplay.

type Const

type Const struct {
	Value float64
}

The Const operator always returns the same value.

func (Const) Arity

func (c Const) Arity() uint

Arity of Const is 0.

func (Const) Diff

func (c Const) Diff(i uint) Operator

Diff computes the following derivative: c' = 0

func (Const) Eval

func (c Const) Eval(X [][]float64) []float64

Eval returns the same value.

func (Const) Name

func (c Const) Name() string

Name of Const is it's value.

func (Const) Operand

func (c Const) Operand(i uint) Operator

Operand returns nil.

func (Const) SetOperand

func (c Const) SetOperand(i uint, op Operator) Operator

SetOperand returns the Const without any modifications.

func (Const) Simplify

func (c Const) Simplify() Operator

Simplify returns the Const without any modifications.

func (Const) String

func (c Const) String() string

String formatting.

type Cos

type Cos struct {
	Op Operator
}

The Cos operator.

func (Cos) Arity

func (cos Cos) Arity() uint

Arity of Cos is 1.

func (Cos) Diff

func (cos Cos) Diff(i uint) Operator

Diff computes the following derivative: cos(u)' = u' * -sin(u)

func (Cos) Eval

func (cos Cos) Eval(X [][]float64) []float64

Eval computes the cosine of each value.

func (Cos) Name

func (cos Cos) Name() string

Name of Cos is "cos".

func (Cos) Operand

func (cos Cos) Operand(i uint) Operator

Operand returns Cos's operand or nil.

func (Cos) SetOperand

func (cos Cos) SetOperand(i uint, op Operator) Operator

SetOperand replaces Cos's operand if i is equal to 0.

func (Cos) Simplify

func (cos Cos) Simplify() Operator

Simplify Cos.

func (Cos) String

func (cos Cos) String() string

String formatting.

type DirDisplay

type DirDisplay struct {
	TabSize int
}

DirDisplay outputs a directory like representation of an Operator.

Example
var (
	op   = Cos{Add{Const{42}, Var{0}}}
	disp = DirDisplay{TabSize: 4}
)
fmt.Println(disp.Apply(op))
Output:

cos
    add
        42
        x0

func (DirDisplay) Apply

func (displayer DirDisplay) Apply(op Operator) string

Apply DirDisplay.

type Displayer

type Displayer interface {
	Apply(Operator) string
}

A Displayer outputs a string representation of a Operator.

type Div

type Div struct {
	Left, Right Operator
}

The Div operator.

func (Div) Arity

func (div Div) Arity() uint

Arity of Div is 2.

func (Div) Diff

func (div Div) Diff(i uint) Operator

Diff computes the following derivative: (u / v)' = (u'v - uv') / v²

func (Div) Eval

func (div Div) Eval(X [][]float64) []float64

Eval divides aligned values.

func (Div) Name

func (div Div) Name() string

Name of Div is "div".

func (Div) Operand

func (div Div) Operand(i uint) Operator

Operand returns one of Div's operands, or nil.

func (Div) SetOperand

func (div Div) SetOperand(i uint, op Operator) Operator

SetOperand replaces one of Div's operands if i is equal to 0 or 1.

func (Div) Simplify

func (div Div) Simplify() Operator

Simplify Div.

func (Div) String

func (div Div) String() string

String formatting.

type GraphvizDisplay

type GraphvizDisplay struct{}

GraphvizDisplay outputs a Graphviz representation of an Operator. Each branch is indexed with a global counter and is labelled with the Operator's Name method.

Example
var (
	op   = Cos{Add{Const{42}, Var{0}}}
	disp = GraphvizDisplay{}
)
fmt.Println(disp.Apply(op))
Output:

digraph G {
  0 [label="cos"];
  0 -> 1;
  1 [label="add"];
  1 -> 2;
  2 [label="42"];
  1 -> 3;
  3 [label="x0"];
}

func (GraphvizDisplay) Apply

func (displayer GraphvizDisplay) Apply(op Operator) string

Apply Graphviz display.

type If

type If struct {
	Condition Operator
	Lower     Operator
	Upper     Operator
}

The If operator.

func (If) Arity

func (iff If) Arity() uint

Arity of If is 3.

func (If) Diff

func (iff If) Diff(i uint) Operator

Diff compute the following derivative: (1 / u)' = -u' / u².

func (If) Eval

func (iff If) Eval(X [][]float64) []float64

Eval computes

func (If) Name

func (iff If) Name() string

Name of If is "if".

func (If) Operand

func (iff If) Operand(i uint) Operator

Operand returns If's operand or nil.

func (If) SetOperand

func (iff If) SetOperand(i uint, op Operator) Operator

SetOperand replaces If's operand if i is equal to 0.

func (If) Simplify

func (iff If) Simplify() Operator

Simplify If.

func (If) String

func (iff If) String() string

String formatting.

type Inv

type Inv struct {
	Op Operator
}

The Inv operator.

func (Inv) Arity

func (inv Inv) Arity() uint

Arity of Inv is 1.

func (Inv) Diff

func (inv Inv) Diff(i uint) Operator

Diff compute the following derivative: (1 / u)' = -u' / u².

func (Inv) Eval

func (inv Inv) Eval(X [][]float64) []float64

Eval computes the inverse of each value.

func (Inv) Name

func (inv Inv) Name() string

Name of Inv is "inv".

func (Inv) Operand

func (inv Inv) Operand(i uint) Operator

Operand returns Inv's operand or nil.

func (Inv) SetOperand

func (inv Inv) SetOperand(i uint, op Operator) Operator

SetOperand replaces Inv's operand if i is equal to 0.

func (Inv) Simplify

func (inv Inv) Simplify() Operator

Simplify Inv.

func (Inv) String

func (inv Inv) String() string

String formatting.

type Max

type Max struct {
	Left, Right Operator
}

The Max operator.

func (Max) Arity

func (max Max) Arity() uint

Arity of Max is 2.

func (Max) Diff

func (max Max) Diff(i uint) Operator

Diff computes the following derivative: max(u, v)' = ((u + v + |u - v|) / 2)'

func (Max) Eval

func (max Max) Eval(X [][]float64) []float64

Eval takes the maximum along aligned values.

func (Max) Name

func (max Max) Name() string

Name of Max is "max".

func (Max) Operand

func (max Max) Operand(i uint) Operator

Operand returns one of Max's operands, or nil.

func (Max) SetOperand

func (max Max) SetOperand(i uint, op Operator) Operator

SetOperand replaces one of Max's operands if i is equal to 0 or 1.

func (Max) Simplify

func (max Max) Simplify() Operator

Simplify Max.

func (Max) String

func (max Max) String() string

String formatting.

type Min

type Min struct {
	Left, Right Operator
}

The Min operator.

func (Min) Arity

func (min Min) Arity() uint

Arity of Min is 2.

func (Min) Diff

func (min Min) Diff(i uint) Operator

Diff computes the following derivative: min(u, v)' = ((u + v - |u - v|) / 2)'

func (Min) Eval

func (min Min) Eval(X [][]float64) []float64

Eval takes the minimum along aligned values.

func (Min) Name

func (min Min) Name() string

Name of Min is "min".

func (Min) Operand

func (min Min) Operand(i uint) Operator

Operand returns one of Min's operands, or nil.

func (Min) SetOperand

func (min Min) SetOperand(i uint, op Operator) Operator

SetOperand replaces one of Min's operands if i is equal to 0 or 1.

func (Min) Simplify

func (min Min) Simplify() Operator

Simplify Min.

func (Min) String

func (min Min) String() string

String formatting.

type Mul

type Mul struct {
	Left, Right Operator
}

The Mul operator.

func (Mul) Arity

func (mul Mul) Arity() uint

Arity of Mul is 2.

func (Mul) Diff

func (mul Mul) Diff(i uint) Operator

Diff computes the following derivative: (uv)' = u'v + uv'

func (Mul) Eval

func (mul Mul) Eval(X [][]float64) []float64

Eval multiplies aligned values.

func (Mul) Name

func (mul Mul) Name() string

Name of Mul is "mul".

func (Mul) Operand

func (mul Mul) Operand(i uint) Operator

Operand returns one of Mul's operands, or nil.

func (Mul) SetOperand

func (mul Mul) SetOperand(i uint, op Operator) Operator

SetOperand replaces one of Mul's operands if i is equal to 0 or 1.

func (Mul) Simplify

func (mul Mul) Simplify() Operator

Simplify Mul.

func (Mul) String

func (mul Mul) String() string

String formatting.

type Neg

type Neg struct {
	Op Operator
}

The Neg operator.

func (Neg) Arity

func (neg Neg) Arity() uint

Arity of Neg is 1.

func (Neg) Diff

func (neg Neg) Diff(i uint) Operator

Diff compute the following derivative: (-u)' = -u'.

func (Neg) Eval

func (neg Neg) Eval(X [][]float64) []float64

Eval computes the opposite of each value.

func (Neg) Name

func (neg Neg) Name() string

Name of Neg is "neg".

func (Neg) Operand

func (neg Neg) Operand(i uint) Operator

Operand returns Neg's operand or nil.

func (Neg) SetOperand

func (neg Neg) SetOperand(i uint, op Operator) Operator

SetOperand replaces Neg's operand if i is equal to 0.

func (Neg) Simplify

func (neg Neg) Simplify() Operator

Simplify Neg.

func (Neg) String

func (neg Neg) String() string

String formatting.

type Operator

type Operator interface {
	Eval(X [][]float64) []float64
	Arity() uint
	Operand(i uint) Operator
	SetOperand(i uint, op Operator) Operator
	Simplify() Operator
	Diff(i uint) Operator
	Name() string
	String() string
}

An Operator is a mathematical operator. It has operands that are themselves Operators.

func ParseFunc

func ParseFunc(name string) (Operator, error)

ParseFunc parses a name and returns the corresponding Operator.

func ParseFuncs

func ParseFuncs(names, sep string) ([]Operator, error)

ParseFuncs parses a string into a slice of Operators.

func ParseOp

func ParseOp(serial SerialOp) (Operator, error)

func Replace

func Replace(
	op Operator,
	when func(Operator) bool,
	how func(Operator) Operator,
	stopAtFirst bool,
) Operator

Replace replaces an Operator if a given condition is met.

func ReplaceAt

func ReplaceAt(op Operator, pos uint, with Operator) Operator

ReplaceAt replaces op's suboperator at position pos.

func Sample

func Sample(
	op Operator,
	weight func(op Operator, depth uint, rng *rand.Rand) float64,
	rng *rand.Rand,
) (Operator, uint)

Sample returns a random suboperator.

func Select

func Select(op Operator, pos uint) Operator

Select returns the nth Operator in an Operator.

func SetConsts

func SetConsts(op Operator, values []float64) Operator

SetConsts sets the value of each Const. Pre-order traversal is used.

type SerialOp

type SerialOp struct {
	Type     string     `json:"type"`
	Value    string     `json:"value"`
	Operands []SerialOp `json:"operands"`
}

func SerializeOp

func SerializeOp(op Operator) SerialOp

type Sin

type Sin struct {
	Op Operator
}

The Sin operator.

func (Sin) Arity

func (sin Sin) Arity() uint

Arity of Sin is 1.

func (Sin) Diff

func (sin Sin) Diff(i uint) Operator

Diff computes the following derivative: sin(u)' = u' * cos(u)

func (Sin) Eval

func (sin Sin) Eval(X [][]float64) []float64

Eval computes the sine of each value.

func (Sin) Name

func (sin Sin) Name() string

Name of Sin is "sin".

func (Sin) Operand

func (sin Sin) Operand(i uint) Operator

Operand returns Sin's operand or nil.

func (Sin) SetOperand

func (sin Sin) SetOperand(i uint, op Operator) Operator

SetOperand replaces Sin's operand if i is equal to 0.

func (Sin) Simplify

func (sin Sin) Simplify() Operator

Simplify Sin.

func (Sin) String

func (sin Sin) String() string

String formatting.

type Square

type Square struct {
	Op Operator
}

The Square operator.

func (Square) Arity

func (square Square) Arity() uint

Arity of Square is 1.

func (Square) Diff

func (square Square) Diff(i uint) Operator

Diff computes the following derivative: (u²)' = 2u'u

func (Square) Eval

func (square Square) Eval(X [][]float64) []float64

Eval computes the square of each value.

func (Square) Name

func (square Square) Name() string

Name of Square is "square".

func (Square) Operand

func (square Square) Operand(i uint) Operator

Operand returns Square's operand or nil.

func (Square) SetOperand

func (square Square) SetOperand(i uint, op Operator) Operator

SetOperand replaces Square's operand if i is equal to 0.

func (Square) Simplify

func (square Square) Simplify() Operator

Simplify Square.

func (Square) String

func (square Square) String() string

String formatting.

type Sub

type Sub struct {
	Left, Right Operator
}

The Sub operator.

func (Sub) Arity

func (sub Sub) Arity() uint

Arity of Sub is 2.

func (Sub) Diff

func (sub Sub) Diff(i uint) Operator

Diff computes the following derivative: (u - v)' = u' - v'

func (Sub) Eval

func (sub Sub) Eval(X [][]float64) []float64

Eval computes the difference between Left and Right.

func (Sub) Name

func (sub Sub) Name() string

Name of Sub is "sub".

func (Sub) Operand

func (sub Sub) Operand(i uint) Operator

Operand returns one of Sub's operands, or nil.

func (Sub) SetOperand

func (sub Sub) SetOperand(i uint, op Operator) Operator

SetOperand replaces one of Sub's operands if i is equal to 0 or 1.

func (Sub) Simplify

func (sub Sub) Simplify() Operator

Simplify Sub.

func (Sub) String

func (sub Sub) String() string

String formatting.

type Var

type Var struct {
	Index uint
}

The Var operator.

func (Var) Arity

func (v Var) Arity() uint

Arity of Var is 0.

func (Var) Diff

func (v Var) Diff(i uint) Operator

Diff computes the following derivative: x' = 1. However if i not equal to the Var's Index then a 0 Const is returned.

func (Var) Eval

func (v Var) Eval(X [][]float64) []float64

Eval returns the ith column of a matrix.

func (Var) Name

func (v Var) Name() string

Name of Var is "xi" where i is the Var's Index.

func (Var) Operand

func (v Var) Operand(i uint) Operator

Operand returns nil.

func (Var) SetOperand

func (v Var) SetOperand(i uint, op Operator) Operator

SetOperand returns the Const without any modifications.

func (Var) Simplify

func (v Var) Simplify() Operator

Simplify returns the Const without any modifications.

func (Var) String

func (v Var) String() string

String formatting.

Jump to

Keyboard shortcuts

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