-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvector_dense_gradient.go
306 lines (246 loc) · 7.41 KB
/
vector_dense_gradient.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
/* Copyright (C) 2015-2020 Philipp Benner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package autodiff
/* -------------------------------------------------------------------------- */
//import "fmt"
import "bytes"
import "encoding/json"
/* vector type declaration
* -------------------------------------------------------------------------- */
type DenseGradient struct {
S Scalar
}
/* cloning
* -------------------------------------------------------------------------- */
func (obj DenseGradient) Clone() DenseGradient {
return DenseGradient{obj.S.CloneScalar()}
}
/* native vector methods
* -------------------------------------------------------------------------- */
func (obj DenseGradient) AT(i int) ConstFloat64 {
return ConstFloat64(obj.S.GetDerivative(i))
}
/* const interface
* -------------------------------------------------------------------------- */
func (obj DenseGradient) CloneConstVector() ConstVector {
return obj.Clone()
}
func (obj DenseGradient) Dim() int {
return obj.S.GetN()
}
func (obj DenseGradient) Int8At(i int) int8 {
return int8(obj.S.GetDerivative(i))
}
func (obj DenseGradient) Int16At(i int) int16 {
return int16(obj.S.GetDerivative(i))
}
func (obj DenseGradient) Int32At(i int) int32 {
return int32(obj.S.GetDerivative(i))
}
func (obj DenseGradient) Int64At(i int) int64 {
return int64(obj.S.GetDerivative(i))
}
func (obj DenseGradient) IntAt(i int) int {
return int(obj.S.GetDerivative(i))
}
func (obj DenseGradient) Float32At(i int) float32 {
return float32(obj.S.GetDerivative(i))
}
func (obj DenseGradient) Float64At(i int) float64 {
return float64(obj.S.GetDerivative(i))
}
func (obj DenseGradient) ConstAt(i int) ConstScalar {
return ConstFloat64(obj.S.GetDerivative(i))
}
func (obj DenseGradient) ConstSlice(i, j int) ConstVector {
x := make([]float64, j-i)
for k := i; k < j; k++ {
x[k] = obj.S.GetDerivative(k)
}
return NewDenseFloat64Vector(x)
}
func (obj DenseGradient) AsConstMatrix(n, m int) ConstMatrix {
if n*m != obj.Dim() {
panic("Matrix dimension does not fit input vector!")
}
r := NullDenseFloat64Vector(obj.Dim())
r.Set(obj)
return r.AsConstMatrix(n, m)
}
/* imlement ConstScalarContainer
* -------------------------------------------------------------------------- */
func (obj DenseGradient) Reduce(f func(Scalar, ConstScalar) Scalar, r Scalar) Scalar {
for i := 0; i < obj.Dim(); i++ {
r = f(r, obj.ConstAt(i))
}
return r
}
func (obj DenseGradient) ElementType() ScalarType {
return ConstFloat64Type
}
/* type conversion
* -------------------------------------------------------------------------- */
func (obj DenseGradient) String() string {
var buffer bytes.Buffer
buffer.WriteString("[")
for i := 0; i < obj.Dim(); i++ {
if i != 0 {
buffer.WriteString(", ")
}
buffer.WriteString(obj.ConstAt(i).String())
}
buffer.WriteString("]")
return buffer.String()
}
func (obj DenseGradient) Table() string {
var buffer bytes.Buffer
for i := 0; i < obj.Dim(); i++ {
if i != 0 {
buffer.WriteString(" ")
}
buffer.WriteString(obj.ConstAt(i).String())
}
return buffer.String()
}
/* json
* -------------------------------------------------------------------------- */
func (obj DenseGradient) MarshalJSON() ([]byte, error) {
r := make([]float64, obj.Dim())
for it := obj.ConstIterator(); it.Ok(); it.Next() {
r[it.Index()] = it.GetConst().GetFloat64()
}
return json.MarshalIndent(r, "", " ")
}
/* iterator methods
* -------------------------------------------------------------------------- */
func (obj DenseGradient) ConstIterator() VectorConstIterator {
return obj.ITERATOR()
}
func (obj DenseGradient) ConstIteratorFrom(i int) VectorConstIterator {
return obj.ITERATOR_FROM(i)
}
func (obj DenseGradient) ConstJointIterator(b ConstVector) VectorConstJointIterator {
return obj.JOINT_ITERATOR(b)
}
func (obj DenseGradient) ITERATOR() *DenseGradientIterator {
r := DenseGradientIterator{obj, -1}
r.Next()
return &r
}
func (obj DenseGradient) ITERATOR_FROM(i int) *DenseGradientIterator {
r := DenseGradientIterator{obj, i-1}
r.Next()
return &r
}
func (obj DenseGradient) JOINT_ITERATOR(b ConstVector) *DenseGradientJointIterator {
r := DenseGradientJointIterator{}
r.it1 = obj.ITERATOR()
r.it2 = b.ConstIterator()
r.idx = -1
r.Next()
return &r
}
/* const iterator
* -------------------------------------------------------------------------- */
type DenseGradientIterator struct {
v DenseGradient
i int
}
func (obj *DenseGradientIterator) GetConst() ConstScalar {
return obj.v.ConstAt(obj.i)
}
func (obj *DenseGradientIterator) GET() ConstFloat64 {
return obj.v.AT(obj.i)
}
func (obj *DenseGradientIterator) Ok() bool {
return obj.i < obj.v.Dim()
}
func (obj *DenseGradientIterator) Next() {
obj.i++
}
func (obj *DenseGradientIterator) Index() int {
return obj.i
}
func (obj *DenseGradientIterator) Clone() *DenseGradientIterator {
return &DenseGradientIterator{obj.v, obj.i}
}
func (obj *DenseGradientIterator) CloneConstIterator() VectorConstIterator {
return &DenseGradientIterator{obj.v, obj.i}
}
/* joint iterator
* -------------------------------------------------------------------------- */
type DenseGradientJointIterator struct {
it1 *DenseGradientIterator
it2 VectorConstIterator
idx int
s1 ConstFloat64
s2 ConstScalar
}
func (obj *DenseGradientJointIterator) Index() int {
return obj.idx
}
func (obj *DenseGradientJointIterator) Ok() bool {
return obj.s1.GetFloat64() != 0.0 || !(obj.s2 == nil || obj.s2.GetFloat64() == 0.0)
}
func (obj *DenseGradientJointIterator) Next() {
ok1 := obj.it1.Ok()
ok2 := obj.it2.Ok()
obj.s1 = 0.0
obj.s2 = nil
if ok1 {
obj.idx = obj.it1.Index()
obj.s1 = obj.it1.GET()
}
if ok2 {
switch {
case obj.idx > obj.it2.Index() || !ok1:
obj.idx = obj.it2.Index()
obj.s1 = 0.0
obj.s2 = obj.it2.GetConst()
case obj.idx == obj.it2.Index():
obj.s2 = obj.it2.GetConst()
}
}
}
func (obj *DenseGradientJointIterator) GetConst() (ConstScalar, ConstScalar) {
return obj.s1, obj.s2
}
func (obj *DenseGradientJointIterator) GET() (ConstFloat64, ConstScalar) {
return obj.s1, obj.s2
}
func (obj *DenseGradientJointIterator) CloneConstJointIterator() VectorConstJointIterator {
r := DenseGradientJointIterator{}
r.it1 = obj.it1.Clone()
r.it2 = obj.it2.CloneConstIterator()
r.idx = obj.idx
r.s1 = obj.s1
r.s2 = obj.s2
return &r
}
/* math
* -------------------------------------------------------------------------- */
// Test if elements in a equal elements in b.
func (a DenseGradient) Equals(b ConstVector, epsilon float64) bool {
if a.Dim() != b.Dim() {
panic("VEqual(): Vector dimensions do not match!")
}
for i := 0; i < a.Dim(); i++ {
if !a.ConstAt(i).Equals(b.ConstAt(i), epsilon) {
return false
}
}
return true
}