Debian Codegraph @

dstream/linapply.go

raw symbols SCIP
1 package dstream
2
3 import "fmt"
4
5 type linapply struct {
6 xform
7
8 coeffs [][]float64
9 basename string
10 }
11
12 func (a *linapply) init() {
13
14 snames := a.source.Names()
15 names := make([]string, len(snames), len(snames)+len(a.coeffs))
16 copy(names, snames)
17 for j := 0; j < len(a.coeffs); j++ {
18 names = append(names, fmt.Sprintf("%s%d", a.basename, j))
19 }
20 a.names = names
21
22 a.bdata = make([]interface{}, len(a.names))
23 }
24
25 // Linapply adds new variables to Dstream by taking linear
26 // combinations of the other variables in the Dstream.
27 func Linapply(data Dstream, coeffs [][]float64, basename string) Dstream {
28
29 a := &linapply{
30 xform: xform{
31 source: data,
32 },
33 coeffs: coeffs,
34 basename: basename,
35 }
36 a.init()
37
38 return a
39 }
40
41 func (a *linapply) Next() bool {
42
43 f := a.source.Next()
44 if !f {
45 return false
46 }
47
48 // Copy the existing variables directly
49 for j := 0; j < a.source.NumVar(); j++ {
50 a.bdata[j] = a.source.GetPos(j)
51 }
52
53 n := ilen(a.GetPos(0))
54 q := a.source.NumVar()
55
56 // Loop over new variables to create
57 for j, v := range a.coeffs {
58
59 if len(v) != q {
60 msg := fmt.Sprintf("coefs (d=%d) does not conform with variables (d=%d)", len(v), q)
61 panic(msg)
62 }
63
64 // Create space for new variable
65 var z []float64
66 if a.bdata[q+j] != nil {
67 z = a.bdata[q+j].([]float64)
68 }
69 z = resizeFloat64(z, n)
70 a.bdata[q+j] = z
71 zeroFloat(z)
72
73 // Loop over summands
74 for k := 0; k < q; k++ {
75 switch x := a.bdata[k].(type) {
76 case []float64:
77 for i := range z {
78 z[i] += v[k] * x[i]
79 }
80 case []string:
81 // do nothing
82 default:
83 panic("unknown type")
84 }
85 }
86 }
87
88 return true
89 }