-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.go
469 lines (425 loc) · 10.8 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright (c) Harri Rautila, 2013
// This file is part of github.com/hrautila/cmat package. It is free software,
// distributed under the terms of GNU Lesser General Public License Version 3, or
// any later version. See the COPYING tile included in this archive.
package cmat
import (
"math"
"fmt"
)
func indexMin(a, b int) int {
if a < b {
return a
}
return b
}
// Column majoe double precision matrix.
type FloatMatrix struct {
elems []float64
step int
rows int
cols int
}
type FlagBits int
// align values with bit values in gomas-package
const (
LOWER = 0x1
UPPER = 0x2
SYMM = 0x4
HERM = 0x8
UNIT = 0x10
NONE = 0
STRICT = UNIT
)
func imin(a, b int) int {
if a < b {
return a
}
return b
}
// Make new matrix of size r rows, c cols.
func NewMatrix(r, s int) *FloatMatrix {
ebuf := make([]float64, r*s, r*s)
return &FloatMatrix{ebuf, r, r, s}
}
// Make a new copy of matrix
func NewCopy(A *FloatMatrix) *FloatMatrix {
B := NewMatrix(A.Size())
B.Copy(A)
return B
}
// Make a new matrix and use ebuf as element storage. cap(ebuf) must not be less than
// rows*cols.
func MakeMatrix(rows, cols int, ebuf []float64) *FloatMatrix {
if int(cap(ebuf)) < rows*cols {
return nil;
}
return &FloatMatrix{ebuf, rows, rows, cols}
}
// Set matrix size and storage. Minimum size for ebuf is stride*cols.
// If stride zero or negative then rows is used as row stride.
// Returns nil if buffer capasity too small. Otherwise returns A.
func (A *FloatMatrix) SetBuf(rows, cols, stride int, ebuf []float64) *FloatMatrix {
if stride <= 0 {
stride = rows
}
if int(cap(ebuf)) < stride*cols {
return nil;
}
A.elems = ebuf
A.rows = rows
A.cols = cols
A.step = stride
return A
}
// Get size of the matrix as tuple (rows, cols).
func (A *FloatMatrix) Size() (int, int) {
return A.rows, A.cols
}
// Get row stride of the matrix.
func (A *FloatMatrix) Stride() int {
return A.step
}
// Get number of elements in matrix.
func (A *FloatMatrix) Len() int {
return A.rows*A.cols;
}
func (A *FloatMatrix) IsVector() bool {
return A != nil && (A.rows == 1 || A.cols == 1)
}
// Return raw element array.
func (A *FloatMatrix) Data() []float64 {
return A.elems
}
// Make A submatrix of B. Returns A.
func (A *FloatMatrix) SubMatrix(B *FloatMatrix, row, col int, sizes ...int) *FloatMatrix {
var nr, nc, step int
if row < 0 {
row += B.rows
}
if col < 0 {
col += B.cols
}
nr = B.rows - row
nc = B.cols - col
step = B.step
switch len(sizes) {
case 2:
nr = sizes[0]
nc = sizes[1]
step = B.step
case 3:
nr = sizes[0]
nc = sizes[1]
step = sizes[2]
}
A.step = step
A.rows = nr
A.cols = nc
if row >= 0 && row < B.rows && col >= 0 && col < B.cols {
A.elems = B.elems[row+col*B.step:]
} else {
A.elems = nil
A.rows = 0
A.cols = 0
}
return A
}
// Make X subvector of Y, X = Y[offset:offset+nlen]
func (X *FloatMatrix) SubVector(Y *FloatMatrix, offset, nlen int) *FloatMatrix {
if ! Y.IsVector() {
return nil
}
if Y.rows == 1 {
return X.SubMatrix(Y, 0, offset, 1, nlen)
}
return X.SubMatrix(Y, offset, 0, nlen, 1)
}
// Make R a row vector of A i.e. R = A[row,:]
func (R *FloatMatrix) Row(A *FloatMatrix, row int, sizes ...int) *FloatMatrix {
if row >= A.rows {
return nil
}
if row < 0 {
row += A.rows
}
var col int = 0
var nc int = A.cols
if len(sizes) == 1 {
col = sizes[0]
nc = A.cols - col
} else if len(sizes) == 2 {
col = sizes[0]
nc = sizes[1]
}
if col + nc > A.cols {
return nil
}
R.step = A.step
R.rows = 1
R.cols = nc
if row >= 0 && row < A.rows && col < A.cols {
R.elems = A.elems[row+col*A.step:]
} else {
R.elems = nil
R.rows = 0
R.cols = 0
}
return R
}
// Make C column of A. C = A[:,col]. Parameter sizes is singleton (row) and column
// vector starts at `row` and extends to the last element of the column. Alternatively
// sizes can be tuple of (row, numelems) and column vector starts at `row` and extends
// `numelems` elements. Function returns C.
func (C *FloatMatrix) Column(A *FloatMatrix, col int, sizes ...int) *FloatMatrix {
if A == nil || C == nil {
return nil
}
if col >= A.cols {
return nil
}
var row int = 0
var nr int = A.rows
if len(sizes) == 1 {
row = sizes[0]
nr = A.rows - row
} else if len(sizes) == 2 {
row = sizes[0]
nr = sizes[1]
}
if row + nr > A.rows {
return nil
}
C.step = A.step
C.rows = nr
C.cols = 1
if row < A.rows && col < A.cols {
C.elems = A.elems[row+col*A.step:]
} else {
C.elems = nil
C.rows = 0
C.cols = 0
}
return C
}
// Return matrix diagonal as row vector. If optional parameter n < 0 returns
// n'th sub-diagonal. If n > 0 returns n'th super-diagonal and if n == 0 returns
// main diagonal
func (D *FloatMatrix) Diag(A *FloatMatrix, n... int) *FloatMatrix {
if len(n) == 0 || n[0] == 0 {
// main diagonal;
return D.SubMatrix(A, 0, 0, 1, imin(A.rows, A.cols), A.step+1)
}
if n[0] > 0 {
// super-diagonal
return D.SubMatrix(A, 0, n[0], 1, imin(A.rows, A.cols-n[0]), A.step+1)
}
// subdiagonal
return D.SubMatrix(A, -n[0], 0, 1, imin(A.rows+n[0], A.cols), A.step+1)
}
// Get element at [i, j]. Returns NaN if indexes are invalid. Negative indexes
// counted from end.
func (A *FloatMatrix) Get(i, j int) float64 {
if A.rows == 0 || A.cols == 0 {
return 0.0
}
if i < 0 {
i += A.rows
}
if j < 0 {
j += A.cols
}
if i < 0 || i >= A.rows || j < 0 || j >= A.cols {
return math.NaN()
}
return A.elems[i+j*A.step]
}
// Get element at [i, j]. Unsafe version without checks and negative indexes
func (A *FloatMatrix) GetUnsafe(i, j int) float64 {
return A.elems[i+j*A.step]
}
// Get element at index i. Returns NaN if index is invalid.
func (A *FloatMatrix) GetAt(i int) float64 {
if i < 0 {
i += A.rows*A.cols
}
if i < 0 || i >= A.rows*A.cols {
return math.NaN()
}
if A.cols == 1 {
return A.elems[i]
}
if A.rows == 1 {
return A.elems[i*A.step]
}
c := i / A.rows
r := i % A.rows
return A.elems[r+c*A.step]
}
// Get element at index i. Unsafe vesrsion
func (A *FloatMatrix) GetAtUnsafe(i int) float64 {
c := i / A.rows
r := i % A.rows
return A.elems[r+c*A.step]
}
// Set element at [i, j]
func (A *FloatMatrix) Set(i, j int, v float64) {
if A.rows == 0 || A.cols == 0 {
return
}
if i < 0 {
i += A.rows
}
if j < 0 {
j += A.cols
}
if i < 0 || i >= A.rows || j < 0 || j >= A.cols {
return
}
A.elems[i+j*A.step] = v
}
// Set element at [i, j]
func (A *FloatMatrix) SetUnsafe(i, j int, v float64) {
A.elems[i+j*A.step] = v
}
// Set element at index i.
func (A *FloatMatrix) SetAt(i int, v float64) {
if i < 0 {
i += A.rows*A.cols
}
if i < 0 || i >= A.rows*A.cols {
return
}
if A.cols == 1 {
A.elems[i] = v
} else if A.rows == 1 {
A.elems[i*A.step] = v
} else {
c := i / A.rows
r := i % A.rows
A.elems[r+c*A.step] = v;
}
}
// Set element at index i.
func (A *FloatMatrix) SetAtUnsafe(i int, v float64) {
c := i / A.rows
r := i % A.rows
A.elems[r+c*A.step] = v;
}
// Make A copy of B.
func (A *FloatMatrix) Copy(B *FloatMatrix) *FloatMatrix {
if B == nil || A == nil {
return nil
}
if B.rows != A.rows || B.cols != A.cols {
return nil;
}
if B.rows == 1 {
// row vector
for j := 0; j < B.cols; j++ {
A.elems[j*A.step] = B.elems[j*B.step]
}
return B
}
// copy by column
for j := 0; j < B.cols; j++ {
copy(A.elems[j*A.step:], B.elems[j*B.step:B.rows+j*B.step])
}
return B
}
// Transpose matrix, A = B.T
func (A *FloatMatrix) Transpose(B *FloatMatrix) *FloatMatrix {
if B == nil || A == nil {
return nil
}
if A.rows != B.cols || A.cols != B.rows {
return nil
}
for j := 0; j < B.cols; j++ {
for i := 0; i < B.rows; i++ {
A.elems[j+i*A.step] = B.elems[i+j*B.step]
}
}
return B
}
// Absolute tolerance. Values v1, v2 are equal within tolerance if ABS(v1-v2) < ABSTOL + RELTOL*ABS(v2)
const ABSTOL = 1e-8
// Relative tolerance
const RELTOL = 1.0000000000000001e-05
func inTolerance(a, b, atol, rtol float64) bool {
df := math.Abs(a - b)
ref := atol + rtol * math.Abs(b)
if df > ref {
return false
}
return true
}
// Test if matrix A is equal to B within given tolenrances. Tolerances are given
// as tuple (abstol, reltol). If no tolerances are given default constants ABSTOL and
// RELTOL are used.
func (A *FloatMatrix) AllClose(B *FloatMatrix, tols ...float64) bool {
var atol, rtol float64 = ABSTOL, RELTOL
if A.rows != B.rows || A.cols != B.cols {
return false
}
if len(tols) == 2 {
atol = tols[0]
rtol = tols[1]
}
if A.rows == 1 {
// row vector
for j := 0; j < A.cols; j++ {
if ! inTolerance(A.elems[j*A.step], B.elems[j*B.step], atol, rtol) {
return false
}
}
return true
}
for j := 0; j < A.cols; j++ {
for i := 0; i < A.rows; i++ {
if ! inTolerance(A.elems[i+j*A.step], B.elems[i+j*B.step], atol, rtol) {
return false
}
}
}
return true
}
// Convert matrix to string with spesific element format.
func (A *FloatMatrix) ToStringPartial(format string, rowpart, colpart int) string {
s := ""
if A == nil {
return "<nil>"
}
for i := 0; i < A.rows; i++ {
if i > 0 {
s += "\n"
}
s += "["
for j := 0; j < A.cols; j++ {
if j > 0 {
s += ", "
}
s += fmt.Sprintf(format, A.elems[i+j*A.step])
if colpart > 0 && A.cols > colpart && j == (colpart/2 - 1) {
s += ", ..."
j = A.cols - (colpart/2+1)
}
}
s += "]"
if rowpart > 0 && A.rows > rowpart && i == (rowpart/2 - 1) {
s += "\n ...."
i = A.rows - (rowpart/2+1)
}
}
return s
}
func (A *FloatMatrix) ToString(format string) string {
return A.ToStringPartial(format, 18, 9)
}
func (A *FloatMatrix) String() string {
return A.ToStringPartial("%9.2e", 18, 9)
}
// Local Variables:
// tab-width: 4
// indent-tabs-mode: nil
// End: