Debian Codegraph @

formula/formula_test.go

raw symbols SCIP
1 package formula
2
3 import (
4 "fmt"
5 "math"
6 "testing"
7
8 "gonum.org/v1/gonum/floats"
9
10 "github.com/kshedden/dstream/dstream"
11 )
12
13 func tokEq(a, b []*token) bool {
14 if len(a) != len(b) {
15 return false
16 }
17 for i, x := range a {
18 if *b[i] != *x {
19 return false
20 }
21 }
22 return true
23 }
24
25 func colSetEq(a, b *ColSet) bool {
26
27 if len(a.Names) != len(b.Names) {
28 return false
29 }
30 for i, x := range a.Names {
31 if x != b.Names[i] {
32 return false
33 }
34 }
35
36 if len(a.Data) != len(b.Data) {
37 return false
38 }
39 eq := func(x, y float64) bool { return math.Abs(x-y) < 1e-5 }
40 for i, x := range a.Data {
41 if !floats.EqualFunc(x.([]float64), b.Data[i].([]float64), eq) {
42 return false
43 }
44 }
45
46 return true
47 }
48
49 func TestLexParse(t *testing.T) {
50
51 v, err := lex("(A + b)*c + d*f(e)")
52 if err != nil {
53 t.Fail()
54 return
55 }
56 exp := []*token{&token{symbol: leftp}, &token{name: "A"},
57 &token{symbol: plus}, &token{name: "b"}, &token{symbol: rightp},
58 &token{symbol: times}, &token{name: "c"}, &token{symbol: plus},
59 &token{name: "d"}, &token{symbol: times},
60 &token{symbol: funct, name: "f(e)", funcn: "f", arg: "e"}}
61
62 if !tokEq(v, exp) {
63 t.Fail()
64 }
65
66 b, err := parse(v)
67 if err != nil {
68 t.Fail()
69 return
70 }
71 exp = []*token{&token{name: "A"}, &token{name: "b"},
72 &token{symbol: plus}, &token{name: "c"}, &token{symbol: times},
73 &token{name: "d"},
74 &token{symbol: funct, name: "f(e)", funcn: "f", arg: "e"},
75 &token{symbol: times}, &token{symbol: plus},
76 }
77
78 if !tokEq(b, exp) {
79 t.Fail()
80 }
81 }
82
83 // Create some functions
84 func makeFuncs() map[string]Func {
85 funcs := make(map[string]Func)
86 funcs["square"] = func(na string, x []float64) *ColSet {
87 y := make([]float64, len(x))
88 for i, v := range x {
89 y[i] = v * v
90 }
91 return &ColSet{Names: []string{na}, Data: []interface{}{y}}
92 }
93 funcs["pbase"] = func(na string, x []float64) *ColSet {
94 y := make([]float64, len(x))
95 z := make([]float64, len(x))
96 for i, v := range x {
97 y[i] = v * v
98 z[i] = v * v * v
99 }
100 return &ColSet{Names: []string{na + "^2", na + "^3"}, Data: []interface{}{y, z}}
101 }
102 return funcs
103 }
104
105 func simpleData() ([][]interface{}, []string) {
106
107 rawData := make([][]interface{}, 4)
108 rawNames := []string{"x1", "x2", "x3", "x4"}
109
110 rawData[0] = []interface{}{
111 []float64{0, 1, 2},
112 []float64{3, 4},
113 }
114
115 rawData[1] = []interface{}{
116 []string{"0", "0", "0"},
117 []string{"1", "1"},
118 }
119
120 rawData[2] = []interface{}{
121 []string{"a", "b", "a"},
122 []string{"b", "a"},
123 }
124
125 rawData[3] = []interface{}{
126 []float64{-1, 0, 1},
127 []float64{0, -1},
128 }
129
130 return rawData, rawNames
131 }
132
133 func TestSingle(t *testing.T) {
134
135 rawData, rawNames := simpleData()
136 funcs := makeFuncs()
137
138 type rec struct {
139 formula string
140 reflevels map[string]string
141 expected []*ColSet
142 funcs map[string]Func
143 }
144
145 for ip, pr := range []rec{
146 {
147 formula: "x1 + x2 + x1*x2",
148 reflevels: map[string]string{"x2": "0"},
149 expected: []*ColSet{
150 &ColSet{
151 Names: []string{"x1", "x2[1]", "x1:x2[1]"},
152 Data: []interface{}{
153 []float64{0, 1, 2},
154 []float64{0, 0, 0},
155 []float64{0, 0, 0},
156 },
157 },
158 &ColSet{
159 Names: []string{"x1", "x2[1]", "x1:x2[1]"},
160 Data: []interface{}{
161 []float64{3, 4},
162 []float64{1, 1},
163 []float64{3, 4},
164 },
165 },
166 },
167 },
168 {
169 formula: "x1 + x2 + x1*x2",
170 reflevels: map[string]string{"x2": "1"},
171 expected: []*ColSet{
172 &ColSet{
173 Names: []string{"x1", "x2[0]", "x1:x2[0]"},
174 Data: []interface{}{
175 []float64{0, 1, 2},
176 []float64{1, 1, 1},
177 []float64{0, 1, 2},
178 },
179 },
180 &ColSet{
181 Names: []string{"x1", "x2[0]", "x1:x2[0]"},
182 Data: []interface{}{
183 []float64{3, 4},
184 []float64{0, 0},
185 []float64{0, 0},
186 },
187 },
188 },
189 },
190 {
191 formula: "x1",
192 expected: []*ColSet{
193 &ColSet{
194 Names: []string{"x1"},
195 Data: []interface{}{
196 []float64{0, 1, 2},
197 },
198 },
199 &ColSet{
200 Names: []string{"x1"},
201 Data: []interface{}{
202 []float64{3, 4},
203 },
204 },
205 },
206 },
207 {
208 formula: "( ( x2*x3))",
209 reflevels: map[string]string{"x2": "0", "x3": "a"},
210 expected: []*ColSet{
211 &ColSet{
212 Names: []string{"x2[1]:x3[b]"},
213 Data: []interface{}{
214 []float64{0, 0, 0},
215 },
216 },
217 &ColSet{
218 Names: []string{"x2[1]:x3[b]"},
219 Data: []interface{}{
220 []float64{1, 0},
221 },
222 },
223 },
224 },
225 {
226 formula: "(x1+x2)*(x3+x4)",
227 reflevels: map[string]string{"x2": "0", "x3": "a"},
228 expected: []*ColSet{
229 &ColSet{
230 Names: []string{"x1:x3[b]", "x1:x4", "x2[1]:x3[b]", "x2[1]:x4"},
231 Data: []interface{}{
232 []float64{0, 1, 0},
233 []float64{0, 0, 2},
234 []float64{0, 0, 0},
235 []float64{0, 0, 0},
236 },
237 },
238 &ColSet{
239 Names: []string{"x1:x3[b]", "x1:x4", "x2[1]:x3[b]", "x2[1]:x4"},
240 Data: []interface{}{
241 []float64{3, 0},
242 []float64{0, -4},
243 []float64{1, 0},
244 []float64{0, -1},
245 },
246 },
247 },
248 },
249 {
250 formula: "x4 + (x1+x2)*x3",
251 reflevels: map[string]string{"x2": "1", "x3": "a"},
252 expected: []*ColSet{
253 &ColSet{
254 Names: []string{"x4", "x1:x3[b]", "x2[0]:x3[b]"},
255 Data: []interface{}{
256 []float64{-1, 0, 1},
257 []float64{0, 1, 0},
258 []float64{0, 1, 0},
259 },
260 },
261 &ColSet{
262 Names: []string{"x4", "x1:x3[b]", "x2[0]:x3[b]"},
263 Data: []interface{}{
264 []float64{0, -1},
265 []float64{3, 0},
266 []float64{0, 0},
267 },
268 },
269 },
270 },
271 {
272 formula: "1 + x1",
273 expected: []*ColSet{
274 &ColSet{
275 Names: []string{"icept", "x1"},
276 Data: []interface{}{
277 []float64{1, 1, 1},
278 []float64{0, 1, 2},
279 },
280 },
281 &ColSet{
282 Names: []string{"icept", "x1"},
283 Data: []interface{}{
284 []float64{1, 1},
285 []float64{3, 4},
286 },
287 },
288 },
289 },
290 {
291 formula: "x1 + 1",
292 expected: []*ColSet{
293 &ColSet{
294 Names: []string{"x1", "icept"},
295 Data: []interface{}{
296 []float64{0, 1, 2},
297 []float64{1, 1, 1},
298 },
299 },
300 &ColSet{
301 Names: []string{"x1", "icept"},
302 Data: []interface{}{
303 []float64{3, 4},
304 []float64{1, 1},
305 },
306 },
307 },
308 },
309 {
310 formula: "square(x1) + 1",
311 expected: []*ColSet{
312 &ColSet{
313 Names: []string{"square(x1)", "icept"},
314 Data: []interface{}{
315 []float64{0, 1, 4},
316 []float64{1, 1, 1},
317 },
318 },
319 &ColSet{
320 Names: []string{"square(x1)", "icept"},
321 Data: []interface{}{
322 []float64{9, 16},
323 []float64{1, 1},
324 },
325 },
326 },
327 },
328 {
329 formula: "1 + pbase(x1)",
330 expected: []*ColSet{
331 &ColSet{
332 Names: []string{"icept", "pbase(x1)^2", "pbase(x1)^3"},
333 Data: []interface{}{
334 []float64{1, 1, 1},
335 []float64{0, 1, 4},
336 []float64{0, 1, 8},
337 },
338 },
339 &ColSet{
340 Names: []string{"icept", "pbase(x1)^2", "pbase(x1)^3"},
341 Data: []interface{}{
342 []float64{1, 1},
343 []float64{9, 16},
344 []float64{27, 64},
345 },
346 },
347 },
348 },
349 {
350 formula: "1 + square(x1)",
351 expected: []*ColSet{
352 &ColSet{
353 Names: []string{"icept", "square(x1)"},
354 Data: []interface{}{
355 []float64{1, 1, 1},
356 []float64{0, 1, 4},
357 },
358 },
359 &ColSet{
360 Names: []string{"icept", "square(x1)"},
361 Data: []interface{}{
362 []float64{1, 1},
363 []float64{9, 16},
364 },
365 },
366 },
367 },
368 } {
369 dp := dstream.NewFromArrays(rawData, rawNames)
370 fp := New(pr.formula, dp).RefLevels(pr.reflevels).Funcs(funcs).Done()
371 fr := fp.(*FormulaParser)
372
373 chunk := 0
374 for fp.Next() {
375 if !colSetEq(pr.expected[chunk], fr.Data) {
376 fmt.Printf("Mismatch:\nip=%d\n", ip)
377 fmt.Printf("chunk=%d\n", chunk)
378 fmt.Printf("Expected: %v\n", pr.expected[chunk])
379 fmt.Printf("Observed: %v\n", fr.Data)
380 t.Fail()
381 }
382 chunk++
383 }
384
385 if fr.ErrorState != nil {
386 fmt.Printf("ip=%d %v\n", ip, fr.ErrorState)
387 t.Fail()
388 }
389
390 if chunk != len(pr.expected) {
391 fmt.Printf("ip=%d wrong number of chunks\n", ip)
392 t.Fail()
393 }
394 }
395 }
396
397 func TestMulti(t *testing.T) {
398
399 rawData, rawNames := simpleData()
400 funcs := makeFuncs()
401
402 type rec struct {
403 formulas []string
404 reflevels map[string]string
405 expected []*ColSet
406 funcs map[string]Func
407 }
408
409 for ip, pr := range []rec{
410 {
411 formulas: []string{"x1"},
412 reflevels: nil,
413 expected: []*ColSet{
414 &ColSet{
415 Names: []string{"x1"},
416 Data: []interface{}{
417 []float64{0, 1, 2},
418 },
419 },
420 &ColSet{
421 Names: []string{"x1"},
422 Data: []interface{}{
423 []float64{3, 4},
424 },
425 },
426 },
427 },
428 {
429 formulas: []string{"x1", "x1+x2"},
430 reflevels: map[string]string{"x2": "1"},
431 expected: []*ColSet{
432 &ColSet{
433 Names: []string{"x1", "x2[0]"},
434 Data: []interface{}{
435 []float64{0, 1, 2},
436 []float64{1, 1, 1},
437 },
438 },
439 &ColSet{
440 Names: []string{"x1", "x2[0]"},
441 Data: []interface{}{
442 []float64{3, 4},
443 []float64{0, 0},
444 },
445 },
446 },
447 },
448 {
449 formulas: []string{"x1", "square(x1) + x2"},
450 reflevels: map[string]string{"x2": "1"},
451 expected: []*ColSet{
452 &ColSet{
453 Names: []string{"x1", "square(x1)", "x2[0]"},
454 Data: []interface{}{
455 []float64{0, 1, 2},
456 []float64{0, 1, 4},
457 []float64{1, 1, 1},
458 },
459 },
460 &ColSet{
461 Names: []string{"x1", "square(x1)", "x2[0]"},
462 Data: []interface{}{
463 []float64{3, 4},
464 []float64{9, 16},
465 []float64{0, 0},
466 },
467 },
468 },
469 },
470 } {
471 dp := dstream.NewFromArrays(rawData, rawNames)
472 fp := NewMulti(pr.formulas, dp).RefLevels(pr.reflevels).Funcs(funcs).Done()
473 fr := fp.(*FormulaParser)
474
475 for chunk := 0; fp.Next(); chunk++ {
476 if !colSetEq(pr.expected[chunk], fr.Data) {
477 fmt.Printf("Mismatch:\nip=%d\n", ip)
478 fmt.Printf("chunk=%d\n", chunk)
479 fmt.Printf("Expected: %v\n", pr.expected[chunk])
480 fmt.Printf("Observed: %v\n", fr.Data)
481 t.Fail()
482 }
483 }
484 }
485 }