1 | package pge
|
---|
2 |
|
---|
3 | import (
|
---|
4 | // "fmt"
|
---|
5 | "sort"
|
---|
6 |
|
---|
7 | expr "github.com/verdverm/go-symexpr"
|
---|
8 | )
|
---|
9 |
|
---|
10 | func (PS *PgeSearch) ExpandMethod1(O expr.Expr) (ret []expr.Expr) {
|
---|
11 | O.Sort()
|
---|
12 | ret = make([]expr.Expr, 0)
|
---|
13 | // fmt.Printf("Expanding expression: %v\n", O)
|
---|
14 |
|
---|
15 | for i := 0; i < O.Size(); i++ {
|
---|
16 | I := i
|
---|
17 | E := O.GetExpr(&I)
|
---|
18 | switch E.ExprType() {
|
---|
19 | case expr.ADD:
|
---|
20 | tmp := PS.AddTermToExprMethod1(O, E, i)
|
---|
21 | ret = append(ret, tmp[:]...)
|
---|
22 |
|
---|
23 | case expr.MUL:
|
---|
24 | tmp := PS.WidenTermInExprMethod1(O, E, i)
|
---|
25 | ret = append(ret, tmp[:]...)
|
---|
26 |
|
---|
27 | case expr.VAR:
|
---|
28 | tmp := PS.DeepenTermInExprMethod1(O, E, i)
|
---|
29 | ret = append(ret, tmp[:]...)
|
---|
30 |
|
---|
31 | default: // expr.DIV,expr.COS,expr.SIN,expr.EXP,expr.LOG,expr.ABS,expr.POW
|
---|
32 | continue
|
---|
33 |
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | return ret
|
---|
38 | }
|
---|
39 |
|
---|
40 | // add another term to an add expr
|
---|
41 | func (PS *PgeSearch) AddTermToExprMethod1(O, E expr.Expr, pos int) (ret []expr.Expr) {
|
---|
42 | ret = make([]expr.Expr, 0)
|
---|
43 | A := E.(*expr.Add)
|
---|
44 |
|
---|
45 | // f() + cL
|
---|
46 | for _, L := range PS.GenLeafs {
|
---|
47 | c := new(expr.Constant)
|
---|
48 | c.P = -1
|
---|
49 | l := L.Clone()
|
---|
50 |
|
---|
51 | // mul it
|
---|
52 | M := expr.NewMul()
|
---|
53 | M.Insert(c)
|
---|
54 | M.Insert(l)
|
---|
55 |
|
---|
56 | // skip if the same term already exists in the add
|
---|
57 | skip := false
|
---|
58 | for _, e := range A.CS {
|
---|
59 | if e == nil {
|
---|
60 | continue
|
---|
61 | }
|
---|
62 | // fmt.Printf("ACMP %v %v\n", M, e)
|
---|
63 | if e.AmIAlmostSame(M) || M.AmIAlmostSame(e) {
|
---|
64 | skip = true
|
---|
65 | break
|
---|
66 | }
|
---|
67 | }
|
---|
68 | if skip {
|
---|
69 | continue
|
---|
70 | }
|
---|
71 |
|
---|
72 | C := O.Clone()
|
---|
73 | P := pos
|
---|
74 | AM := C.GetExpr(&P).(*expr.Add)
|
---|
75 | AM.Insert(M)
|
---|
76 | sort.Sort(AM)
|
---|
77 | C.CalcExprStats()
|
---|
78 | good := PS.cnfg.treecfg.CheckExpr(C)
|
---|
79 | if good {
|
---|
80 | ret = append(ret, C)
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | // f() + c*node(L)
|
---|
85 | for _, N := range PS.GenNodes {
|
---|
86 | for _, L := range PS.GenLeafs {
|
---|
87 |
|
---|
88 | c := new(expr.Constant)
|
---|
89 | c.P = -1
|
---|
90 |
|
---|
91 | l := L.Clone()
|
---|
92 | n := N.Clone()
|
---|
93 | p := 1
|
---|
94 | n.SetExpr(&p, l)
|
---|
95 |
|
---|
96 | var E expr.Expr
|
---|
97 | if N.ExprType() == expr.DIV {
|
---|
98 | E = expr.NewDiv(c, l)
|
---|
99 | } else {
|
---|
100 | // mul it
|
---|
101 | M := expr.NewMul()
|
---|
102 | M.Insert(c)
|
---|
103 | M.Insert(n)
|
---|
104 | E = M
|
---|
105 | }
|
---|
106 |
|
---|
107 | // skip if the same term already exists in the add
|
---|
108 | skip := false
|
---|
109 | for _, e := range A.CS {
|
---|
110 | if e == nil {
|
---|
111 | continue
|
---|
112 | }
|
---|
113 | // fmt.Printf("ACMP %v %v\n", M, e)
|
---|
114 | if e.AmIAlmostSame(E) || E.AmIAlmostSame(e) {
|
---|
115 | skip = true
|
---|
116 | break
|
---|
117 | }
|
---|
118 | }
|
---|
119 | if skip {
|
---|
120 | continue
|
---|
121 | }
|
---|
122 |
|
---|
123 | // fmt.Println(E.String())
|
---|
124 |
|
---|
125 | C := O.Clone()
|
---|
126 | P := pos
|
---|
127 | AM := C.GetExpr(&P).(*expr.Add)
|
---|
128 | AM.Insert(E)
|
---|
129 | sort.Sort(AM)
|
---|
130 | C.CalcExprStats()
|
---|
131 | good := PS.cnfg.treecfg.CheckExpr(C)
|
---|
132 | if good {
|
---|
133 | ret = append(ret, C)
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | return ret
|
---|
139 | }
|
---|
140 |
|
---|
141 | // add complexity to a single multiplication term
|
---|
142 | func (PS *PgeSearch) WidenTermInExprMethod1(O, E expr.Expr, pos int) (ret []expr.Expr) {
|
---|
143 | ret = make([]expr.Expr, 0)
|
---|
144 |
|
---|
145 | // insert leafs f()*L
|
---|
146 | for _, L := range PS.GenLeafs {
|
---|
147 | l := L.Clone()
|
---|
148 | C := O.Clone()
|
---|
149 | P := pos
|
---|
150 | e := C.GetExpr(&P)
|
---|
151 | // fmt.Printf("pos(%d): %v\n", pos, e)
|
---|
152 | M := e.(*expr.Mul)
|
---|
153 | M.Insert(l)
|
---|
154 | sort.Sort(M)
|
---|
155 | C.CalcExprStats()
|
---|
156 | good := PS.cnfg.treecfg.CheckExpr(C)
|
---|
157 | if good {
|
---|
158 | ret = append(ret, C)
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | // insert node(L) : f() * c*node(L)
|
---|
163 | for _, N := range PS.GenNodes {
|
---|
164 | for _, L := range PS.GenLeafs {
|
---|
165 | c := new(expr.Constant)
|
---|
166 | c.P = -1
|
---|
167 |
|
---|
168 | l := L.Clone()
|
---|
169 | n := N.Clone()
|
---|
170 | p := 1
|
---|
171 | n.SetExpr(&p, l)
|
---|
172 |
|
---|
173 | var E expr.Expr
|
---|
174 | if N.ExprType() == expr.DIV {
|
---|
175 | E = expr.NewDiv(c, l)
|
---|
176 | } else {
|
---|
177 | // mul it
|
---|
178 | M := expr.NewMul()
|
---|
179 | M.Insert(c)
|
---|
180 | M.Insert(n)
|
---|
181 | E = M
|
---|
182 | }
|
---|
183 |
|
---|
184 | C := O.Clone()
|
---|
185 | P := pos
|
---|
186 | e := C.GetExpr(&P)
|
---|
187 | // fmt.Printf("pos(%d): %v\n", pos, e)
|
---|
188 | M := e.(*expr.Mul)
|
---|
189 |
|
---|
190 | M.Insert(E)
|
---|
191 | sort.Sort(M)
|
---|
192 | C.CalcExprStats()
|
---|
193 | good := PS.cnfg.treecfg.CheckExpr(C)
|
---|
194 | if good {
|
---|
195 | ret = append(ret, C)
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 | return ret
|
---|
200 | }
|
---|
201 |
|
---|
202 | // change any term to something more complex...
|
---|
203 | func (PS *PgeSearch) DeepenTermInExprMethod1(O, E expr.Expr, pos int) []expr.Expr {
|
---|
204 | exprs := make([]expr.Expr, 0)
|
---|
205 |
|
---|
206 | // make into add
|
---|
207 | A := expr.NewAdd()
|
---|
208 | A.Insert(E.Clone())
|
---|
209 | OA := A.Clone()
|
---|
210 | exprs = append(exprs, PS.AddTermToExprMethod1(OA, A, 0)[:]...)
|
---|
211 |
|
---|
212 | // make into mul
|
---|
213 | M := expr.NewMul()
|
---|
214 | M.Insert(E.Clone())
|
---|
215 | OM := M.Clone()
|
---|
216 | exprs = append(exprs, PS.WidenTermInExprMethod1(OM, M, 0)[:]...)
|
---|
217 |
|
---|
218 | // // make into div
|
---|
219 | // if E.ExprType() != expr.DIV {
|
---|
220 | // D := new(expr.Div)
|
---|
221 | // c := new(expr.Constant)
|
---|
222 | // c.P = -1
|
---|
223 | // D.Numer = c
|
---|
224 | // D.Denom = E.Clone()
|
---|
225 | // exprs = append(exprs, D)
|
---|
226 | // }
|
---|
227 |
|
---|
228 | // make inside of nodes
|
---|
229 | for _, N := range PS.GenNodes {
|
---|
230 | if N.ExprType() == expr.DIV {
|
---|
231 | continue
|
---|
232 | }
|
---|
233 | T := N.Clone()
|
---|
234 | P := 1
|
---|
235 | T.SetExpr(&P, E.Clone())
|
---|
236 | exprs = append(exprs, T)
|
---|
237 | }
|
---|
238 |
|
---|
239 | ret := make([]expr.Expr, 0)
|
---|
240 | for _, e := range exprs {
|
---|
241 | C := O.Clone()
|
---|
242 | P := pos
|
---|
243 | C.SetExpr(&P, e)
|
---|
244 | ret = append(ret, C)
|
---|
245 | }
|
---|
246 | return ret
|
---|
247 | }
|
---|