forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.go
349 lines (305 loc) · 7.19 KB
/
vector.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright ©2013 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mat64
import (
"math"
"github.com/gonum/blas"
"github.com/gonum/blas/blas64"
)
var (
vector *Vector
_ Matrix = vector
// _ Cloner = vector
// _ Viewer = vector
// _ Subvectorer = vector
// _ Adder = vector
// _ Suber = vector
// _ Muler = vector
// _ Dotter = vector
// _ ElemMuler = vector
// _ Scaler = vector
// _ Applyer = vector
// _ Normer = vector
// _ Sumer = vector
// _ Stacker = vector
// _ Augmenter = vector
// _ Equaler = vector
// _ ApproxEqualer = vector
// _ RawMatrixLoader = vector
// _ RawMatrixer = vector
)
// Vector represents a column vector.
type Vector struct {
mat blas64.Vector
n int
// A BLAS vector can have a negative increment, but allowing this
// in the mat64 type complicates a lot of code, and doesn't gain anything.
// Vector must have positive increment in this package.
}
// NewVector creates a new Vector of length n. If len(data) == n, data is used
// as the backing data slice. If data == nil, a new slice is allocated. If
// neither of these is true, NewVector will panic.
func NewVector(n int, data []float64) *Vector {
if len(data) != n && data != nil {
panic(ErrShape)
}
if data == nil {
data = make([]float64, n)
}
return &Vector{
mat: blas64.Vector{
Inc: 1,
Data: data,
},
n: n,
}
}
// ViewVec returns a sub-vector view of the receiver starting at element i and
// extending n columns. If i is out of range, or if n is zero or extend beyond the
// bounds of the Vector ViewVec will panic with ErrIndexOutOfRange. The returned
// Vector retains reference to the underlying vector.
func (v *Vector) ViewVec(i, n int) *Vector {
if i+n > v.n {
panic(ErrIndexOutOfRange)
}
return &Vector{
n: n,
mat: blas64.Vector{
Inc: v.mat.Inc,
Data: v.mat.Data[i*v.mat.Inc:],
},
}
}
func (v *Vector) Dims() (r, c int) { return v.n, 1 }
// Len returns the length of the vector.
func (v *Vector) Len() int {
return v.n
}
func (v *Vector) Reset() {
v.mat.Data = v.mat.Data[:0]
v.mat.Inc = 0
v.n = 0
}
func (v *Vector) RawVector() blas64.Vector {
return v.mat
}
// CopyVec makes a copy of elements of a into the receiver. It is similar to the
// built-in copy; it copies as much as the overlap between the two matrices and
// returns the number of rows and columns it copied.
func (v *Vector) CopyVec(a *Vector) (n int) {
n = min(v.Len(), a.Len())
blas64.Copy(n, a.mat, v.mat)
return n
}
// AddVec adds a and b element-wise, placing the result in the receiver.
func (v *Vector) AddVec(a, b *Vector) {
ar := a.Len()
br := b.Len()
if ar != br {
panic(ErrShape)
}
v.reuseAs(ar)
amat, bmat := a.RawVector(), b.RawVector()
for i := 0; i < v.n; i++ {
v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] + bmat.Data[i*bmat.Inc]
}
}
// SubVec subtracts the vector b from a, placing the result in the receiver.
func (v *Vector) SubVec(a, b *Vector) {
ar := a.Len()
br := b.Len()
if ar != br {
panic(ErrShape)
}
v.reuseAs(ar)
amat, bmat := a.RawVector(), b.RawVector()
for i := 0; i < v.n; i++ {
v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] - bmat.Data[i*bmat.Inc]
}
}
// MulElemVec performs element-wise multiplication of a and b, placing the result
// in the receiver.
func (v *Vector) MulElemVec(a, b *Vector) {
ar := a.Len()
br := b.Len()
if ar != br {
panic(ErrShape)
}
v.reuseAs(ar)
amat, bmat := a.RawVector(), b.RawVector()
for i := 0; i < v.n; i++ {
v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] * bmat.Data[i*bmat.Inc]
}
}
// DivElemVec performs element-wise division of a by b, placing the result
// in the receiver.
func (v *Vector) DivElemVec(a, b *Vector) {
ar := a.Len()
br := b.Len()
if ar != br {
panic(ErrShape)
}
v.reuseAs(ar)
amat, bmat := a.RawVector(), b.RawVector()
for i := 0; i < v.n; i++ {
v.mat.Data[i*v.mat.Inc] = amat.Data[i*amat.Inc] / bmat.Data[i*bmat.Inc]
}
}
// MulVec computes a * b if trans == false and a^T * b if trans == true. The
// result is stored into the receiver. MulVec panics if the number of columns in
// a does not equal the number of rows in b.
func (v *Vector) MulVec(a Matrix, trans bool, b *Vector) {
ar, ac := a.Dims()
br := b.Len()
if trans {
if ar != br {
panic(ErrShape)
}
} else {
if ac != br {
panic(ErrShape)
}
}
var w Vector
if v != a && v != b {
w = *v
}
if w.n == 0 {
if trans {
w.mat.Data = use(w.mat.Data, ac)
} else {
w.mat.Data = use(w.mat.Data, ar)
}
w.mat.Inc = 1
w.n = ar
if trans {
w.n = ac
}
} else {
if trans {
if ac != w.n {
panic(ErrShape)
}
} else {
if ar != w.n {
panic(ErrShape)
}
}
}
switch a := a.(type) {
case RawSymmetricer:
amat := a.RawSymmetric()
blas64.Symv(1, amat, b.mat, 0, w.mat)
case RawTriangular:
w.CopyVec(b)
amat := a.RawTriangular()
ta := blas.NoTrans
if trans {
ta = blas.Trans
}
blas64.Trmv(ta, amat, w.mat)
case RawMatrixer:
amat := a.RawMatrix()
t := blas.NoTrans
if trans {
t = blas.Trans
}
blas64.Gemv(t, 1, amat, b.mat, 0, w.mat)
case Vectorer:
if trans {
col := make([]float64, ar)
for c := 0; c < ac; c++ {
w.mat.Data[c*w.mat.Inc] = blas64.Dot(ar,
blas64.Vector{Inc: 1, Data: a.Col(col, c)},
b.mat,
)
}
} else {
row := make([]float64, ac)
for r := 0; r < ar; r++ {
w.mat.Data[r*w.mat.Inc] = blas64.Dot(ac,
blas64.Vector{Inc: 1, Data: a.Row(row, r)},
b.mat,
)
}
}
default:
if trans {
col := make([]float64, ar)
for c := 0; c < ac; c++ {
for i := range col {
col[i] = a.At(i, c)
}
var f float64
for i, e := range col {
f += e * b.mat.Data[i*b.mat.Inc]
}
w.mat.Data[c*w.mat.Inc] = f
}
} else {
row := make([]float64, ac)
for r := 0; r < ar; r++ {
for i := range row {
row[i] = a.At(r, i)
}
var f float64
for i, e := range row {
f += e * b.mat.Data[i*b.mat.Inc]
}
w.mat.Data[r*w.mat.Inc] = f
}
}
}
*v = w
}
// Equals compares the vectors represented by b and the receiver and returns true
// if the vectors are element-wise equal.
func (v *Vector) EqualsVec(b *Vector) bool {
n := v.Len()
nb := b.Len()
if n != nb {
return false
}
for i := 0; i < n; i++ {
if v.mat.Data[i*v.mat.Inc] != b.mat.Data[i*b.mat.Inc] {
return false
}
}
return true
}
// EqualsApproxVec compares the vectors represented by b and the receiver, with
// tolerance for element-wise equality specified by epsilon.
func (v *Vector) EqualsApproxVec(b *Vector, epsilon float64) bool {
n := v.Len()
nb := b.Len()
if n != nb {
return false
}
for i := 0; i < n; i++ {
if math.Abs(v.mat.Data[i*v.mat.Inc]-b.mat.Data[i*b.mat.Inc]) > epsilon {
return false
}
}
return true
}
// reuseAs resizes an empty vector to a r×1 vector,
// or checks that a non-empty matrix is r×1.
func (v *Vector) reuseAs(r int) {
if v.isZero() {
v.mat = blas64.Vector{
Inc: 1,
Data: use(v.mat.Data, r),
}
v.n = r
return
}
if r != v.n {
panic(ErrShape)
}
}
func (v *Vector) isZero() bool {
// It must be the case that v.Dims() returns
// zeros in this case. See comment in Reset().
return v.mat.Inc == 0
}