-
Notifications
You must be signed in to change notification settings - Fork 4
/
matrix.go
345 lines (316 loc) · 10.1 KB
/
matrix.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
/* 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 "encoding/json"
/* -------------------------------------------------------------------------- */
type MatrixConstIterator interface {
CloneConstIterator() MatrixConstIterator
GetConst() ConstScalar
Ok () bool
Next ()
Index () (int, int)
}
type MatrixMagicIterator interface {
CloneMagicIterator() MatrixMagicIterator
GetConst() ConstScalar
GetMagic() MagicScalar
Ok () bool
Next ()
Index () (int, int)
}
type MatrixConstJointIterator interface {
CloneConstJointIterator() MatrixConstJointIterator
GetConst() (ConstScalar, ConstScalar)
Ok () bool
Next ()
Index () (int, int)
}
type MatrixIterator interface {
CloneIterator() MatrixIterator
GetConst() ConstScalar
Get () Scalar
Ok () bool
Next ()
Index () (int, int)
}
type MatrixJointIterator interface {
CloneJointIterator() MatrixJointIterator
GetConst() (ConstScalar, ConstScalar)
Get () (Scalar, ConstScalar)
Ok () bool
Next ()
Index () (int, int)
}
/* matrix type declaration
* -------------------------------------------------------------------------- */
type constMatrix interface {
CloneConstMatrix () ConstMatrix
Dims () (int, int)
Equals (ConstMatrix, float64) bool
Table () string
Int8At (i, j int) int8
Int16At (i, j int) int16
Int32At (i, j int) int32
Int64At (i, j int) int64
IntAt (i, j int) int
Float32At (i, j int) float32
Float64At (i, j int) float64
ConstAt (i, j int) ConstScalar
ConstSlice (rfrom, rto, cfrom, cto int) ConstMatrix
ConstRow (i int) ConstVector
ConstCol (j int) ConstVector
ConstDiag () ConstVector
AsConstVector () ConstVector
ConstIterator () MatrixConstIterator
ConstIteratorFrom(i, j int) MatrixConstIterator
IsSymmetric (float64) bool
// private methods
storageLocation () uintptr
// json
json.Marshaler
}
type ConstMatrix interface {
ConstScalarContainer
constMatrix
}
type matrix interface {
constMatrix
CloneMatrix () Matrix
// other methods
At (i, j int) Scalar
Reset ()
// basic methods
Set (ConstMatrix)
Row (i int) Vector
Col (j int) Vector
Diag () Vector
T () Matrix
Tip ()
Export (string) error
Slice (rfrom, rto, cfrom, cto int) Matrix
Swap (int, int, int, int)
SwapRows (int, int) error
SwapColumns (int, int) error
PermuteRows ([]int) error
PermuteColumns ([]int) error
SymmetricPermutation(pi []int) error
SetIdentity ()
// returns all elements of the matrix as
// a vector, the order is unspecified
AsVector () Vector
// iterators
Iterator () MatrixIterator
IteratorFrom (i, j int) MatrixIterator
JointIterator (b ConstMatrix) MatrixJointIterator
// math operations
MaddM(a, b ConstMatrix) Matrix
MaddS(a ConstMatrix, b ConstScalar) Matrix
MsubM(a, b ConstMatrix) Matrix
MsubS(a ConstMatrix, b ConstScalar) Matrix
MmulM(a, b ConstMatrix) Matrix
MmulS(a ConstMatrix, b ConstScalar) Matrix
MdivM(a, b ConstMatrix) Matrix
MdivS(a ConstMatrix, b ConstScalar) Matrix
MdotM(a, b ConstMatrix) Matrix
Outer(a, b ConstVector) Matrix
Jacobian(f func(ConstVector) ConstVector, x_ MagicVector) Matrix
Hessian (f func(ConstVector) ConstScalar, x_ MagicVector) Matrix
}
type Matrix interface {
ScalarContainer
matrix
}
type MagicMatrix interface {
MagicScalarContainer
matrix
CloneMagicMatrix () MagicMatrix
MagicAt (i, j int) MagicScalar
MagicSlice (rfrom, rto, cfrom, cto int) MagicMatrix
MagicT () MagicMatrix
ResetDerivatives ()
AsMagicVector () MagicVector
MagicIterator () MatrixMagicIterator
MagicIteratorFrom (i, j int) MatrixMagicIterator
}
/* constructors
* -------------------------------------------------------------------------- */
func NullDenseMatrix(t ScalarType, rows, cols int) Matrix {
switch t {
case Int8Type:
return NullDenseInt8Matrix(rows, cols)
case Int16Type:
return NullDenseInt16Matrix(rows, cols)
case Int32Type:
return NullDenseInt32Matrix(rows, cols)
case Int64Type:
return NullDenseInt64Matrix(rows, cols)
case IntType:
return NullDenseIntMatrix(rows, cols)
case Float32Type:
return NullDenseFloat32Matrix(rows, cols)
case Float64Type:
return NullDenseFloat64Matrix(rows, cols)
case Real32Type:
return NullDenseReal32Matrix(rows, cols)
case Real64Type:
return NullDenseReal64Matrix(rows, cols)
default:
panic("unknown type")
}
}
func AsDenseMatrix(t ScalarType, m ConstMatrix) Matrix {
switch t {
case Int8Type:
return AsDenseInt8Matrix(m)
case Int16Type:
return AsDenseInt16Matrix(m)
case Int32Type:
return AsDenseInt32Matrix(m)
case Int64Type:
return AsDenseInt64Matrix(m)
case IntType:
return AsDenseIntMatrix(m)
case Float32Type:
return AsDenseFloat32Matrix(m)
case Float64Type:
return AsDenseFloat64Matrix(m)
case Real32Type:
return AsDenseReal32Matrix(m)
case Real64Type:
return AsDenseReal64Matrix(m)
default:
panic("unknown type")
}
}
func NullDenseMagicMatrix(t ScalarType, rows, cols int) MagicMatrix {
switch t {
case Real32Type:
return NullDenseReal32Matrix(rows, cols)
case Real64Type:
return NullDenseReal64Matrix(rows, cols)
default:
panic("unknown type")
}
}
func AsDenseMagicMatrix(t ScalarType, m ConstMatrix) MagicMatrix {
switch t {
case Real32Type:
return AsDenseReal32Matrix(m)
case Real64Type:
return AsDenseReal64Matrix(m)
default:
panic("unknown type")
}
}
func NullSparseMatrix(t ScalarType, rows, cols int) Matrix {
switch t {
case Int8Type:
return NullSparseInt8Matrix(rows, cols)
case Int16Type:
return NullSparseInt16Matrix(rows, cols)
case Int32Type:
return NullSparseInt32Matrix(rows, cols)
case Int64Type:
return NullSparseInt64Matrix(rows, cols)
case IntType:
return NullSparseIntMatrix(rows, cols)
case Float32Type:
return NullSparseFloat32Matrix(rows, cols)
case Float64Type:
return NullSparseFloat64Matrix(rows, cols)
case Real32Type:
return NullSparseReal32Matrix(rows, cols)
case Real64Type:
return NullSparseReal64Matrix(rows, cols)
default:
panic("unknown type")
}
}
func AsSparseMatrix(t ScalarType, m ConstMatrix) Matrix {
switch t {
case Int8Type:
return AsSparseInt8Matrix(m)
case Int16Type:
return AsSparseInt16Matrix(m)
case Int32Type:
return AsSparseInt32Matrix(m)
case Int64Type:
return AsSparseInt64Matrix(m)
case IntType:
return AsSparseIntMatrix(m)
case Float32Type:
return AsSparseFloat32Matrix(m)
case Float64Type:
return AsSparseFloat64Matrix(m)
case Real32Type:
return AsSparseReal32Matrix(m)
case Real64Type:
return AsSparseReal64Matrix(m)
default:
panic("unknown type")
}
}
func NullSparseMagicMatrix(t ScalarType, rows, cols int) MagicMatrix {
switch t {
case Real32Type:
return NullSparseReal32Matrix(rows, cols)
case Real64Type:
return NullSparseReal64Matrix(rows, cols)
default:
panic("unknown type")
}
}
func AsSparseMagicMatrix(t ScalarType, m ConstMatrix) MagicMatrix {
switch t {
case Real32Type:
return AsSparseReal32Matrix(m)
case Real64Type:
return AsSparseReal64Matrix(m)
default:
panic("unknown type")
}
}
/* constructors for special types of matrices
* -------------------------------------------------------------------------- */
func DenseIdentityMatrix(t ScalarType, dim int) Matrix {
matrix := NullDenseMatrix(t, dim, dim)
for i := 0; i < dim; i++ {
matrix.At(i, i).SetFloat64(1.0)
}
return matrix
}
func SparseIdentityMatrix(t ScalarType, dim int) Matrix {
matrix := NullSparseMatrix(t, dim, dim)
for i := 0; i < dim; i++ {
matrix.At(i, i).SetFloat64(1.0)
}
return matrix
}
func DenseMagicIdentityMatrix(t ScalarType, dim int) MagicMatrix {
matrix := NullDenseMagicMatrix(t, dim, dim)
for i := 0; i < dim; i++ {
matrix.At(i, i).SetFloat64(1.0)
}
return matrix
}
func SparseMagicIdentityMatrix(t ScalarType, dim int) MagicMatrix {
matrix := NullSparseMagicMatrix(t, dim, dim)
for i := 0; i < dim; i++ {
matrix.At(i, i).SetFloat64(1.0)
}
return matrix
}