1 |
|
---|
2 | Symbolic Expressions (just math for now)
|
---|
3 | ========================================
|
---|
4 | - represetation: as an AST tree
|
---|
5 | - manipulation: add,rm, simplify, derivative
|
---|
6 | - evaluation: explicit, RK4 integration, non-linear regression via levmar->MINPACK
|
---|
7 | - printing: String() print, prettyprint, serialization
|
---|
8 |
|
---|
9 | ``` go
|
---|
10 | type exprType int
|
---|
11 |
|
---|
12 | const (
|
---|
13 | NULL exprType = iota
|
---|
14 | CONSTANT // indexed constant, useful for regression tasks
|
---|
15 | CONSTANTF // floating point constant
|
---|
16 | TIME // useful when looking at time series and RK4 integration
|
---|
17 | SYSTEM // i use this like a variable that changes between experiments, but not with time (mass,size,etc.)
|
---|
18 | VAR // a canonical variable
|
---|
19 |
|
---|
20 | NEG
|
---|
21 | ABS
|
---|
22 | SQRT
|
---|
23 | SIN
|
---|
24 | COS
|
---|
25 | TAN
|
---|
26 | EXP
|
---|
27 | LOG
|
---|
28 | POWI // Expr^Integer
|
---|
29 | POWF // Expr^Float
|
---|
30 |
|
---|
31 | POWE // Expr^Expr
|
---|
32 | DIV
|
---|
33 |
|
---|
34 | ADD // these can have more than two child nodes
|
---|
35 | MUL // this eases simplification
|
---|
36 |
|
---|
37 | EXPR_MAX
|
---|
38 | STARTVAR // for serialization reduction of variables
|
---|
39 | )
|
---|
40 |
|
---|
41 | // Expr is the interface to all node types for the AST of mathematical expression
|
---|
42 | //
|
---|
43 |
|
---|
44 | type Expr interface {
|
---|
45 |
|
---|
46 | // types.go (this file)
|
---|
47 | ExprType() exprType
|
---|
48 | Clone() Expr
|
---|
49 |
|
---|
50 | // stats.go
|
---|
51 | Size() int
|
---|
52 | Depth() int
|
---|
53 | Height() int
|
---|
54 | NumChildren() int
|
---|
55 | CalcExprStats(currDepth int) (mySize int)
|
---|
56 |
|
---|
57 | // compare.go
|
---|
58 | AmILess(rhs Expr) bool
|
---|
59 | AmIEqual(rhs Expr) bool
|
---|
60 | AmISame(rhs Expr) bool // equality without coefficient values/index
|
---|
61 | AmIAlmostSame(rhs Expr) bool // adds flexibility to mul comparison to AmISame
|
---|
62 | Sort()
|
---|
63 |
|
---|
64 | // has.go
|
---|
65 | HasVar() bool
|
---|
66 | HasVarI(i int) bool
|
---|
67 | NumVar() int
|
---|
68 |
|
---|
69 | // DFS for a floating point valued ConstantF
|
---|
70 | HasConst() bool
|
---|
71 | // DFS for a indexed valued Constant
|
---|
72 | HasConstI(i int) bool
|
---|
73 | // Counts the number of indexed Constant nodes
|
---|
74 | NumConstants() int
|
---|
75 |
|
---|
76 | // convert.go
|
---|
77 |
|
---|
78 | // Converts indexed Constant nodes to ConstantF nodes
|
---|
79 | // using the input slice as the values for replacement
|
---|
80 | ConvertToConstantFs(cs []float64) Expr
|
---|
81 | // DFS converting float valued constants to indexed constants
|
---|
82 | // the input should be an empty slice
|
---|
83 | // the output is an appended slice the size = |ConstantF| in the tree
|
---|
84 | ConvertToConstants(cs []float64) ([]float64, Expr)
|
---|
85 | // IndexConstants( ci int ) int
|
---|
86 |
|
---|
87 | // getset.go
|
---|
88 | // DFS retrieval of a node by index
|
---|
89 | GetExpr(pos *int) Expr
|
---|
90 | // DFS replacement of a node and it's subtree
|
---|
91 | // replaced is used to discontinue the DFS after replacement
|
---|
92 | // replace_me gets triggered when pos == 0 and informs the parent node to replace the respective child node
|
---|
93 | SetExpr(pos *int, e Expr) (replace_me, replaced bool)
|
---|
94 |
|
---|
95 | // print.go
|
---|
96 |
|
---|
97 | // prints the AST
|
---|
98 | String() string
|
---|
99 |
|
---|
100 | // creates an integer representation of the AST in ~prefix notation
|
---|
101 | // The input is an empty slice, output is the representation.
|
---|
102 | // The output is generally the ExprType integer value
|
---|
103 | // Associative operators (+ & *) also include the number of children.
|
---|
104 | // The terminal nodes include the index when appropriate.
|
---|
105 | Serial([]int) []int
|
---|
106 |
|
---|
107 | // Pretty print acts like String, but replaces the internal indexed
|
---|
108 | // formatting with user specified strings and values
|
---|
109 | PrettyPrint(dnames, snames []string, cvals []float64) string
|
---|
110 | // WriteString( buf *bytes.Buffer )
|
---|
111 |
|
---|
112 | // eval.go
|
---|
113 | // Evaluates an expression at one point
|
---|
114 | // t is a time value
|
---|
115 | // x are the input Var values
|
---|
116 | // c are the indexed Constant values
|
---|
117 | // s are the indexed System values
|
---|
118 | // the output is the result of DFS evaluation
|
---|
119 | Eval(t float64, x, c, s []float64) float64
|
---|
120 |
|
---|
121 | // simp.go
|
---|
122 | Simplify(rules SimpRules) Expr
|
---|
123 |
|
---|
124 | // deriv.go
|
---|
125 | // Calculate the derivative w.r.t. Var_i
|
---|
126 | DerivVar(i int) Expr
|
---|
127 | // Calculate the derivative w.r.t. Constant_i
|
---|
128 | DerivConst(i int) Expr
|
---|
129 | }
|
---|
130 | ```
|
---|
131 |
|
---|
132 | Tony Worm Sept, 2012
|
---|