forked from pa-m/mat32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangular.go
509 lines (464 loc) · 12.7 KB
/
triangular.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// Copyright ©2015 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 mat32
import (
"gonum.org/v1/gonum/blas"
"gonum.org/v1/gonum/blas/blas32"
)
var (
triDense *TriDense
_ Matrix = triDense
_ Triangular = triDense
_ RawTriangular = triDense
_ MutableTriangular = triDense
_ NonZeroDoer = triDense
_ RowNonZeroDoer = triDense
_ ColNonZeroDoer = triDense
)
const badTriCap = "mat: bad capacity for TriDense"
// TriDense represents an upper or lower triangular matrix in dense storage
// format.
type TriDense struct {
mat blas32.Triangular
cap int
}
// Triangular represents a triangular matrix. Triangular matrices are always square.
type Triangular interface {
Matrix
// Triangular returns the number of rows/columns in the matrix and its
// orientation.
Triangle() (n int, kind TriKind)
// TTri is the equivalent of the T() method in the Matrix interface but
// guarantees the transpose is of triangular type.
TTri() Triangular
}
// A RawTriangular can return a view of itself as a BLAS Triangular matrix.
type RawTriangular interface {
RawTriangular() blas32.Triangular
}
// A MutableTriangular can set elements of a triangular matrix.
type MutableTriangular interface {
Triangular
SetTri(i, j int, v float32)
}
var (
_ Matrix = TransposeTri{}
_ Triangular = TransposeTri{}
_ UntransposeTrier = TransposeTri{}
)
// TransposeTri is a type for performing an implicit transpose of a Triangular
// matrix. It implements the Triangular interface, returning values from the
// transpose of the matrix within.
type TransposeTri struct {
Triangular Triangular
}
// At returns the value of the element at row i and column j of the transposed
// matrix, that is, row j and column i of the Triangular field.
func (t TransposeTri) At(i, j int) float32 {
return t.Triangular.At(j, i)
}
// Dims returns the dimensions of the transposed matrix. Triangular matrices are
// square and thus this is the same size as the original Triangular.
func (t TransposeTri) Dims() (r, c int) {
c, r = t.Triangular.Dims()
return r, c
}
// T performs an implicit transpose by returning the Triangular field.
func (t TransposeTri) T() Matrix {
return t.Triangular
}
// Triangle returns the number of rows/columns in the matrix and its orientation.
func (t TransposeTri) Triangle() (int, TriKind) {
n, upper := t.Triangular.Triangle()
return n, !upper
}
// TTri performs an implicit transpose by returning the Triangular field.
func (t TransposeTri) TTri() Triangular {
return t.Triangular
}
// Untranspose returns the Triangular field.
func (t TransposeTri) Untranspose() Matrix {
return t.Triangular
}
func (t TransposeTri) UntransposeTri() Triangular {
return t.Triangular
}
// NewTriDense creates a new Triangular matrix with n rows and columns. If data == nil,
// a new slice is allocated for the backing slice. If len(data) == n*n, data is
// used as the backing slice, and changes to the elements of the returned TriDense
// will be reflected in data. If neither of these is true, NewTriDense will panic.
//
// The data must be arranged in row-major order, i.e. the (i*c + j)-th
// element in the data slice is the {i, j}-th element in the matrix.
// Only the values in the triangular portion corresponding to kind are used.
func NewTriDense(n int, kind TriKind, data []float32) *TriDense {
if n < 0 {
panic("mat: negative dimension")
}
if data != nil && len(data) != n*n {
panic(ErrShape)
}
if data == nil {
data = make([]float32, n*n)
}
uplo := blas.Lower
if kind == Upper {
uplo = blas.Upper
}
return &TriDense{
mat: blas32.Triangular{
N: n,
Stride: n,
Data: data,
Uplo: uplo,
Diag: blas.NonUnit,
},
cap: n,
}
}
func (t *TriDense) Dims() (r, c int) {
return t.mat.N, t.mat.N
}
// Triangle returns the dimension of t and its orientation. The returned
// orientation is only valid when n is not zero.
func (t *TriDense) Triangle() (n int, kind TriKind) {
return t.mat.N, TriKind(!t.IsZero()) && t.triKind()
}
func (t *TriDense) isUpper() bool {
return isUpperUplo(t.mat.Uplo)
}
func (t *TriDense) triKind() TriKind {
return TriKind(isUpperUplo(t.mat.Uplo))
}
func isUpperUplo(u blas.Uplo) bool {
switch u {
case blas.Upper:
return true
case blas.Lower:
return false
default:
panic("badTriangle")
}
}
// asSymBlas returns the receiver restructured as a blas32.Symmetric with the
// same backing memory. Panics if the receiver is unit.
// This returns a blas32.Symmetric and not a *SymDense because SymDense can only
// be upper triangular.
func (t *TriDense) asSymBlas() blas32.Symmetric {
if t.mat.Diag == blas.Unit {
panic("mat: cannot convert unit TriDense into blas32.Symmetric")
}
return blas32.Symmetric{
N: t.mat.N,
Stride: t.mat.Stride,
Data: t.mat.Data,
Uplo: t.mat.Uplo,
}
}
// T performs an implicit transpose by returning the receiver inside a Transpose.
func (t *TriDense) T() Matrix {
return Transpose{t}
}
// TTri performs an implicit transpose by returning the receiver inside a TransposeTri.
func (t *TriDense) TTri() Triangular {
return TransposeTri{t}
}
func (t *TriDense) RawTriangular() blas32.Triangular {
return t.mat
}
// Reset zeros the dimensions of the matrix so that it can be reused as the
// receiver of a dimensionally restricted operation.
//
// See the Reseter interface for more information.
func (t *TriDense) Reset() {
// N and Stride must be zeroed in unison.
t.mat.N, t.mat.Stride = 0, 0
// Defensively zero Uplo to ensure
// it is set correctly later.
t.mat.Uplo = 0
t.mat.Data = t.mat.Data[:0]
}
// IsZero returns whether the receiver is zero-sized. Zero-sized matrices can be the
// receiver for size-restricted operations. TriDense matrices can be zeroed using Reset.
func (t *TriDense) IsZero() bool {
// It must be the case that t.Dims() returns
// zeros in this case. See comment in Reset().
return t.mat.Stride == 0
}
// untranspose untransposes a matrix if applicable. If a is an Untransposer, then
// untranspose returns the underlying matrix and true. If it is not, then it returns
// the input matrix and false.
func untransposeTri(a Triangular) (Triangular, bool) {
if ut, ok := a.(UntransposeTrier); ok {
return ut.UntransposeTri(), true
}
return a, false
}
// reuseAs resizes a zero receiver to an n×n triangular matrix with the given
// orientation. If the receiver is non-zero, reuseAs checks that the receiver
// is the correct size and orientation.
func (t *TriDense) reuseAs(n int, kind TriKind) {
if n == 0 {
panic(ErrZeroLength)
}
ul := blas.Lower
if kind == Upper {
ul = blas.Upper
}
if t.mat.N > t.cap {
panic(badTriCap)
}
if t.IsZero() {
t.mat = blas32.Triangular{
N: n,
Stride: n,
Diag: blas.NonUnit,
Data: use(t.mat.Data, n*n),
Uplo: ul,
}
t.cap = n
return
}
if t.mat.N != n {
panic(ErrShape)
}
if t.mat.Uplo != ul {
panic(ErrTriangle)
}
}
// isolatedWorkspace returns a new TriDense matrix w with the size of a and
// returns a callback to defer which performs cleanup at the return of the call.
// This should be used when a method receiver is the same pointer as an input argument.
func (t *TriDense) isolatedWorkspace(a Triangular) (w *TriDense, restore func()) {
n, kind := a.Triangle()
if n == 0 {
panic(ErrZeroLength)
}
w = getWorkspaceTri(n, kind, false)
return w, func() {
t.Copy(w)
putWorkspaceTri(w)
}
}
// Copy 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. Only elements within the
// receiver's non-zero triangle are set.
//
// See the Copier interface for more information.
func (t *TriDense) Copy(a Matrix) (r, c int) {
r, c = a.Dims()
r = min(r, t.mat.N)
c = min(c, t.mat.N)
if r == 0 || c == 0 {
return 0, 0
}
switch a := a.(type) {
case RawMatrixer:
amat := a.RawMatrix()
if t.isUpper() {
for i := 0; i < r; i++ {
copy(t.mat.Data[i*t.mat.Stride+i:i*t.mat.Stride+c], amat.Data[i*amat.Stride+i:i*amat.Stride+c])
}
} else {
for i := 0; i < r; i++ {
copy(t.mat.Data[i*t.mat.Stride:i*t.mat.Stride+i+1], amat.Data[i*amat.Stride:i*amat.Stride+i+1])
}
}
case RawTriangular:
amat := a.RawTriangular()
aIsUpper := isUpperUplo(amat.Uplo)
tIsUpper := t.isUpper()
switch {
case tIsUpper && aIsUpper:
for i := 0; i < r; i++ {
copy(t.mat.Data[i*t.mat.Stride+i:i*t.mat.Stride+c], amat.Data[i*amat.Stride+i:i*amat.Stride+c])
}
case !tIsUpper && !aIsUpper:
for i := 0; i < r; i++ {
copy(t.mat.Data[i*t.mat.Stride:i*t.mat.Stride+i+1], amat.Data[i*amat.Stride:i*amat.Stride+i+1])
}
default:
for i := 0; i < r; i++ {
t.set(i, i, amat.Data[i*amat.Stride+i])
}
}
default:
isUpper := t.isUpper()
for i := 0; i < r; i++ {
if isUpper {
for j := i; j < c; j++ {
t.set(i, j, a.At(i, j))
}
} else {
for j := 0; j <= i; j++ {
t.set(i, j, a.At(i, j))
}
}
}
}
return r, c
}
// MulTri takes the product of triangular matrices a and b and places the result
// in the receiver. The size of a and b must match, and they both must have the
// same TriKind, or Mul will panic.
func (t *TriDense) MulTri(a, b Triangular) {
n, kind := a.Triangle()
nb, kindb := b.Triangle()
if n != nb {
panic(ErrShape)
}
if kind != kindb {
panic(ErrTriangle)
}
aU, _ := untransposeTri(a)
bU, _ := untransposeTri(b)
t.checkOverlapMatrix(bU)
t.checkOverlapMatrix(aU)
t.reuseAs(n, kind)
var restore func()
if t == aU {
t, restore = t.isolatedWorkspace(aU)
defer restore()
} else if t == bU {
t, restore = t.isolatedWorkspace(bU)
defer restore()
}
// TODO(btracey): Improve the set of fast-paths.
if kind == Upper {
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
var v float32
for k := i; k <= j; k++ {
v += a.At(i, k) * b.At(k, j)
}
t.SetTri(i, j, v)
}
}
return
}
for i := 0; i < n; i++ {
for j := 0; j <= i; j++ {
var v float32
for k := j; k <= i; k++ {
v += a.At(i, k) * b.At(k, j)
}
t.SetTri(i, j, v)
}
}
}
// ScaleTri multiplies the elements of a by f, placing the result in the receiver.
// If the receiver is non-zero, the size and kind of the receiver must match
// the input, or ScaleTri will panic.
func (t *TriDense) ScaleTri(f float32, a Triangular) {
n, kind := a.Triangle()
t.reuseAs(n, kind)
// TODO(btracey): Improve the set of fast-paths.
switch a := a.(type) {
case RawTriangular:
amat := a.RawTriangular()
if t != a {
t.checkOverlap(generalFromTriangular(amat))
}
if kind == Upper {
for i := 0; i < n; i++ {
ts := t.mat.Data[i*t.mat.Stride+i : i*t.mat.Stride+n]
as := amat.Data[i*amat.Stride+i : i*amat.Stride+n]
for i, v := range as {
ts[i] = v * f
}
}
return
}
for i := 0; i < n; i++ {
ts := t.mat.Data[i*t.mat.Stride : i*t.mat.Stride+i+1]
as := amat.Data[i*amat.Stride : i*amat.Stride+i+1]
for i, v := range as {
ts[i] = v * f
}
}
return
default:
t.checkOverlapMatrix(a)
isUpper := kind == Upper
for i := 0; i < n; i++ {
if isUpper {
for j := i; j < n; j++ {
t.set(i, j, f*a.At(i, j))
}
} else {
for j := 0; j <= i; j++ {
t.set(i, j, f*a.At(i, j))
}
}
}
}
}
// DoNonZero calls the function fn for each of the non-zero elements of t. The function fn
// takes a row/column index and the element value of t at (i, j).
func (t *TriDense) DoNonZero(fn func(i, j int, v float32)) {
if t.isUpper() {
for i := 0; i < t.mat.N; i++ {
for j := i; j < t.mat.N; j++ {
v := t.at(i, j)
if v != 0 {
fn(i, j, v)
}
}
}
return
}
for i := 0; i < t.mat.N; i++ {
for j := 0; j <= i; j++ {
v := t.at(i, j)
if v != 0 {
fn(i, j, v)
}
}
}
}
// DoRowNonZero calls the function fn for each of the non-zero elements of row i of t. The function fn
// takes a row/column index and the element value of t at (i, j).
func (t *TriDense) DoRowNonZero(i int, fn func(i, j int, v float32)) {
if i < 0 || t.mat.N <= i {
panic(ErrRowAccess)
}
if t.isUpper() {
for j := i; j < t.mat.N; j++ {
v := t.at(i, j)
if v != 0 {
fn(i, j, v)
}
}
return
}
for j := 0; j <= i; j++ {
v := t.at(i, j)
if v != 0 {
fn(i, j, v)
}
}
}
// DoColNonZero calls the function fn for each of the non-zero elements of column j of t. The function fn
// takes a row/column index and the element value of t at (i, j).
func (t *TriDense) DoColNonZero(j int, fn func(i, j int, v float32)) {
if j < 0 || t.mat.N <= j {
panic(ErrColAccess)
}
if t.isUpper() {
for i := 0; i <= j; i++ {
v := t.at(i, j)
if v != 0 {
fn(i, j, v)
}
}
return
}
for i := j; i < t.mat.N; i++ {
v := t.at(i, j)
if v != 0 {
fn(i, j, v)
}
}
}