-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.go
63 lines (54 loc) · 1.41 KB
/
util.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
package goop
import (
log "github.com/sirupsen/logrus"
)
// Sum returns the sum of the given expressions. It creates a new empty
// expression and adds to it the given expressions.
func Sum(exprs ...Expr) Expr {
newExpr := NewExpr(0)
for _, e := range exprs {
newExpr.Plus(e)
}
return newExpr
}
// SumVars returns the sum of the given variables. It creates a new empty
// expression and adds to it the given variables.
func SumVars(vs ...*Var) Expr {
newExpr := NewExpr(0)
for _, v := range vs {
newExpr.Plus(v)
}
return newExpr
}
// SumRow returns the sum of all the variables in a single specified row of
// a variable matrix.
func SumRow(vs [][]*Var, row int) Expr {
newExpr := NewExpr(0)
for col := 0; col < len(vs[0]); col++ {
newExpr.Plus(vs[row][col])
}
return newExpr
}
// SumCol returns the sum of all variables in a single specified column of
// a variable matrix.
func SumCol(vs [][]*Var, col int) Expr {
newExpr := NewExpr(0)
for row := 0; row < len(vs); row++ {
newExpr.Plus(vs[row][col])
}
return newExpr
}
// Dot returns the dot product of a vector of variables and slice of floats.
func Dot(vs []*Var, coeffs []float64) Expr {
if len(vs) != len(coeffs) {
log.WithFields(log.Fields{
"num_vars": len(vs),
"num_coeffs": len(coeffs),
}).Panic("Number of vars and coeffs mismatch")
}
newExpr := NewExpr(0)
for i := range vs {
newExpr.Plus(vs[i].Mult(coeffs[i]))
}
return newExpr
}