-
Notifications
You must be signed in to change notification settings - Fork 9
/
formula.go
253 lines (234 loc) · 5.63 KB
/
formula.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package toolkit
import (
"errors"
"math"
"strings"
)
var signs string = "()^*/+-"
var signList []string = []string{"^", "*", "/", "+", "-"}
type Formula struct {
Parm string
Value float64
Negate bool
Divide bool
Power bool
SubFormulas []*Formula
BaseOp string
Txt string
}
func NewFormula(formulaTxt string) (*Formula, error) {
fm, e := parseFormula(formulaTxt)
if e != nil {
return nil, errors.New("NewFormula: " + e.Error())
}
return fm, nil
}
func parseFormula(txt string) (*Formula, error) {
return parseFormulaSub(txt, []*Formula{})
}
func parseFormulaSub(formulaTxt string, fisubs []*Formula) (*Formula, error) {
//-- building the parts
if formulaTxt == "" {
return nil, errors.New("parseFormula:Formula is empty")
}
var parts []string
originalFormula := formulaTxt
txtLen := len(formulaTxt)
tmp := ""
inBracket := false
for i := 0; i < txtLen; i++ {
c := string(formulaTxt[i])
if c == "(" && !inBracket {
if tmp != "" {
parts = append(parts, tmp)
}
inBracket = true
tmp = ""
} else if c == ")" && inBracket {
fi, efi := parseFormulaSub(tmp, fisubs)
if efi != nil {
return nil, errors.New("parseFormula: " + tmp + "," + efi.Error())
} else if fi == nil {
return nil, errors.New(Sprintf("parseFormula: %s unable to parse it into *Formula", tmp))
}
fisubs = append(fisubs, fi)
parts = append(parts, Sprintf("@b_%d", len(fisubs)-1))
inBracket = false
tmp = ""
} else if i == txtLen-1 {
tmp += c
parts = append(parts, tmp)
} else {
tmp += c
}
}
formulaTxt = ""
for _, part := range parts {
formulaTxt += part
}
if !(strings.HasPrefix(formulaTxt, "-") || strings.HasPrefix(formulaTxt, "+")) {
formulaTxt = "+" + formulaTxt
}
//var fsigns []string
//var fvalues []string
var fparts []string
txtLen = len(formulaTxt)
tmp = ""
for i := 0; i < txtLen; i++ {
c := string(formulaTxt[txtLen-1-i])
tmp = c + tmp
if HasMember([]string{"+", "-"}, c) {
fparts = append(fparts, tmp)
tmp = ""
}
}
ret := new(Formula)
ret.Txt = originalFormula
if len(fparts) == 1 {
fpart := fparts[0]
multiVariable := false
if splis, _ := Split(fpart, signList); len(splis) > 1 {
multiVariable = true
}
ret.BaseOp = "+"
if isMultiply(fpart) {
ret.BaseOp = "*"
}
tmp := ""
fpartlen := len(fpart)
isPower := false
for fpartidx := fpartlen - 1; fpartidx >= 0; fpartidx-- {
c := string(fpart[fpartidx])
if ((c == "*" || c == "/" || c == "^") && ret.BaseOp == "*") ||
((c == "+" || c == "-") && ret.BaseOp == "+") || fpartidx == 0 {
isPower = c == "^"
var subfi *Formula
isnegate := false
if fpartidx == 0 && c == "-" {
tmp = c + tmp
}
if strings.HasPrefix(tmp, "-") {
isnegate = true
tmp = tmp[1:]
//Println("Negating: ", tmp, " ")
}
if strings.Contains(tmp, "@b_") {
//--- it is a subfunction that already defined
formulaIndex := int(-1)
formulaIndex = ToInt(tmp[3:], RoundingAuto)
if formulaIndex >= len(fisubs) {
return nil, errors.New(Sprintf("parseFormula: %s Subformula index-%d is not available", tmp, formulaIndex))
}
subfi = fisubs[formulaIndex]
} else {
//--- not a subfunction already defined
subfi = new(Formula)
if !strings.Contains(tmp, "@") {
//-- it is a value
f64 := ToFloat64(tmp, 4, RoundingAuto)
if tmp != "0" && f64 == float64(0) {
return nil, errors.New("parseFormula: " + tmp + " Can not render to float")
}
//Printf("%s value is %.2f\n", tmp, f64)
subfi.Value = f64
//ret.Value = f64
} else {
//-- it is a.Parm
subfi.Parm = tmp
//ret.Parm = tmp
}
}
if multiVariable {
subfi.Divide = c == "/"
subfi.Negate = isnegate
subfi.Power = isPower
ret.SubFormulas = append(ret.SubFormulas, subfi)
} else {
ret.Divide = c == "/"
ret.Power = isPower
ret.Negate = isnegate
ret.Parm = subfi.Parm
ret.Value = subfi.Value
ret.BaseOp = subfi.BaseOp
ret.SubFormulas = subfi.SubFormulas
}
tmp = ""
} else {
tmp = c + tmp
}
}
} else {
for _, fpart := range fparts {
fi, efi := parseFormulaSub(fpart, fisubs)
if efi != nil {
return nil, errors.New("parseFormula: " + fpart + " " + efi.Error())
}
if isMultiply(fpart) {
ret.BaseOp = "*"
} else {
ret.BaseOp = "+"
}
ret.SubFormulas = append(ret.SubFormulas, fi)
}
}
/*
Printf("Origin:%s NewFormula:%s FParts:%s\n%s\n\n",
originalFormula, formulaTxt,
JsonString(fparts),
JsonStringIndent(ret, ""))
*/
return ret, nil
}
func isMultiply(fpart string) bool {
if strings.Contains(fpart, "*") || strings.Contains(fpart, "/") || strings.Contains(fpart, "^") {
return true
}
return false
}
func (f *Formula) Run(in M) float64 {
var ret float64
dbg := ""
if f.BaseOp == "+" {
ret = float64(0)
} else {
ret = float64(1)
}
if len(f.SubFormulas) == 0 {
if f.Parm != "" {
ret = float64(in.GetFloat64(f.Parm))
} else {
ret = f.Value
}
if f.Negate {
ret = -ret
}
} else {
isPower := false
lastvalue := float64(0)
for idx, sf := range f.SubFormulas {
v := sf.Run(in)
if sf.Divide {
v = 1.0 / v
}
if f.BaseOp == "+" {
ret += v
} else {
if isPower {
ret *= math.Pow(v, lastvalue)
isPower = false
} else if !sf.Power {
ret = ret * v
}
isPower = sf.Power
//ret = 2016.0 * 0.1
//Println(v, " ", ret)
}
lastvalue = v
dbg += Sprintf("%d=%.2f ", idx, v)
}
}
//fcopy := *f
//fcopy.SubFormulas = []*Formula{}
//Printf("Formula: %s Value: %.2f f:%s Trace:%s\n\n", f.Txt, ret, JsonString(fcopy), dbg)
return ret
}