-
Notifications
You must be signed in to change notification settings - Fork 2
/
mat.go
355 lines (324 loc) · 7.59 KB
/
mat.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
package lap
import (
"errors"
"fmt"
)
var (
ErrSingular = errors.New("matrix is singular to working precision")
ErrAliasedData = errors.New("aliased data")
ErrRowAccess = errors.New("bad row access")
ErrColAccess = errors.New("bad column access")
ErrDim = errors.New("bad dimension")
errImmutable = errors.New("immutable matrix")
)
type Matrix interface {
At(i, j int) float64
Dims() (r, c int)
}
type matrixSetter interface {
Matrix
Set(i, j int, v float64)
}
// DenseM represents a row major storage matrix.
type DenseM struct {
data []float64
stride int
r, c int
}
// Dims returns the dimensions of the matrix.
func (d *DenseM) Dims() (int, int) { return d.r, d.c }
// At returns d's element at ith row, jth column.
func (d *DenseM) At(i, j int) float64 {
if i < 0 || i >= d.r {
panic(ErrRowAccess)
} else if j < 0 || j >= d.c {
panic(ErrColAccess)
}
return d.data[i*d.stride+j]
}
// Set sets d's element at ith row, jth column to v.
func (d *DenseM) Set(i, j int, v float64) {
if i < 0 || i >= d.r {
panic(ErrRowAccess)
} else if j < 0 || j >= d.c {
panic(ErrColAccess)
}
d.data[i*d.stride+j] = v
}
// Copy produces a copy of A with no overlapping memory.
// If the receiver is not initialized then the backing array is allocated
// automatically.
func (d *DenseM) Copy(A Matrix) (rowsCopied, colsCopied int) {
r, c := A.Dims()
if d.data == nil {
*d = *NewDenseMatrix(r, c, nil)
}
if r != d.r || c != d.c {
panic(ErrDim)
}
if Ad, ok := A.(*DenseM); ok && Ad.stride == d.stride {
n := copy(d.data, Ad.data)
if n != r*c {
panic("copy failed")
}
return d.r, d.c
}
for i := 0; i < d.r; i++ {
for j := 0; j < d.c; j++ {
d.data[i*d.stride+j] = A.At(i, j)
}
}
return d.r, d.c
}
// NewDenseMatrix produces a new (rxc) matrix backed by contiguous data.
// this function produces superior memory access patterns and prevents the rows
// of the output from being scattered in memory.
//
// data may be nil, in which case an array of zeros is returned
func NewDenseMatrix(r, c int, data []float64) (d *DenseM) {
if data == nil {
data = make([]float64, r*c)
}
return &DenseM{
data: data,
r: r,
c: c,
stride: c,
}
}
type eye int
func (e eye) Dims() (int, int) { return int(e), int(e) }
func (e eye) At(i, j int) float64 {
if i < 0 || i > int(e) {
panic(ErrRowAccess)
}
if j < 0 || j > int(e) {
panic(ErrColAccess)
}
if i == j {
return 1
}
return 0
}
// Eye is the square identity matrix of size N
func Eye(n int) Matrix {
return eye(n)
}
// Slice returns a new Matrix that shares backing data with the receiver.
// The returned matrix starts at {i,j} of the receiver and extends k-i rows
// and l-j columns. The final row in the resulting matrix is k-1 and the
// final column is l-1.
// Slice panics with ErrIndexOutOfRange if the slice is outside the capacity
// of the receiver.
func (d *DenseM) Slice(i, k, j, l int) *DenseM {
mr, mc := d.Dims()
if k <= i || l <= j {
// Common error or group with below?
panic(ErrDim)
}
if i < 0 || mr <= i || j < 0 || mc <= j || mr < k || mc < l {
panic(ErrDim)
}
return &DenseM{
data: d.data[i*d.stride+j : (k-1)*d.stride+l],
stride: d.stride,
r: k - i,
c: l - j,
}
}
type Transpose struct {
m Matrix
}
func (t Transpose) At(i, j int) float64 {
return t.m.At(j, i)
}
func (t Transpose) Dims() (int, int) {
c, r := t.m.Dims()
return r, c
}
func (t Transpose) IsMutable() bool {
_, ok := t.m.(matrixSetter)
return ok
}
func (t Transpose) Set(i, j int, v float64) {
M, ok := t.m.(matrixSetter)
if !ok {
panic(errImmutable)
}
M.Set(j, i, v)
}
// T returns the implicit transpose of A without copying.
func T(A Matrix) Matrix {
if t, ok := A.(Transpose); ok {
// If matrix is of underlying transpose type, we untranspose
// by unwrapping the transpose type
return t.m
}
return Transpose{m: A}
}
// MatMul computes the matrix-matrix product C = AB for (nxm) matrix A and (mxp)
// matrix B, storing the result in (nxp) matrix C.
func (C *DenseM) Mul(A, B Matrix) {
n, m := A.Dims()
mB, p := B.Dims()
if C.data == nil {
*C = *NewDenseMatrix(n, p, nil)
}
nC, pC := C.Dims()
if m != mB || nC != n || pC != p {
panic(ErrDim)
}
if aliasedData(C, A) || aliasedData(C, B) {
panic(ErrAliasedData)
}
for i := 0; i < n; i++ {
ridx := i * C.stride
for j := 0; j < p; j++ {
tmp := 0.0
for k := 0; k < m; k++ {
tmp += A.At(i, k) * B.At(k, j)
}
C.data[ridx+j] = tmp
}
}
}
// Sub stores the elementwise addition A+B in C.
func (C *DenseM) Add(A, B Matrix) {
rA, cA := A.Dims()
rB, cB := B.Dims()
if C.data == nil {
*C = *NewDenseMatrix(rA, cA, nil)
}
r, c := C.Dims()
if rA != r || rB != r || cA != c || cB != c {
panic(ErrDim)
}
for i := 0; i < r; i++ {
ridx := i * C.stride
for j := 0; j < c; j++ {
C.data[ridx+j] = A.At(i, j) + B.At(i, j)
}
}
}
// Sub stores the elementwise difference A-B in C.
func (C *DenseM) Sub(A, B Matrix) {
rA, cA := A.Dims()
rB, cB := B.Dims()
if C.data == nil {
*C = *NewDenseMatrix(rA, cA, nil)
}
r, c := C.Dims()
if rA != r || rB != r || cA != c || cB != c {
panic(ErrDim)
}
for i := 0; i < r; i++ {
ridx := i * C.stride
for j := 0; j < c; j++ {
C.data[ridx+j] = A.At(i, j) - B.At(i, j)
}
}
}
// Scale multiplies the elements of A by f, placing the result in the receiver.
func (C *DenseM) Scale(f float64, A Matrix) {
rA, cA := A.Dims()
if C.data == nil {
*C = *NewDenseMatrix(rA, cA, nil)
}
r, c := C.Dims()
if rA != r || cA != c {
panic(ErrDim)
}
for i := 0; i < r; i++ {
ridx := i * C.stride
for j := 0; j < c; j++ {
C.data[ridx+j] = f * A.At(i, j)
}
}
}
// SwapRows swaps rows i and j of A in-place.
func (A *DenseM) SwapRows(i, j int) {
iidx := i * A.stride
jidx := j * A.stride
for k := 0; k < A.c; k++ {
A.data[iidx+k], A.data[jidx+k] = A.data[jidx+k], A.data[iidx+k]
}
}
func (A *DenseM) SwapCols(i, j int) {
for k := 0; k < A.r; k++ {
ridx := k * A.stride
A.data[ridx+i], A.data[ridx+j] = A.data[ridx+j], A.data[ridx+i]
}
}
func (A *DenseM) RowView(i int) *DenseV {
if i >= A.r || i < 0 {
panic(ErrRowAccess)
}
return &DenseV{
data: A.data[i*A.stride : (i+1)*A.stride],
}
}
func (A *DenseM) ColView(j int) *DenseV {
if j >= A.c || j < 0 {
panic(ErrColAccess)
}
return &DenseV{
data: A.data[j:],
incMinusOne: A.stride - 1,
}
}
// CopyBlocks copies mrows rows and mcols columns of matrices
// passed in src.
func (dst *DenseM) CopyBlocks(mrows, mcols int, src []Matrix) error {
if len(src) != mrows*mcols {
return ErrDim
}
var tr, tc int
for i := 0; i < mrows; i++ {
r, _ := src[i*mcols].Dims()
tr += r
}
for j := 0; j < mcols; j++ {
_, c := src[j].Dims()
tc += c
}
if dst.data == nil {
*dst = *NewDenseMatrix(tr, tc, nil)
}
r, c := dst.Dims()
if r != tr || c != tc {
return ErrDim
}
var br int
for i := 0; i < mrows; i++ {
var bc int
h, _ := src[i*mcols].Dims()
for j := 0; j < mcols; j++ {
r, c := src[i*mcols+j].Dims()
if r != h {
return fmt.Errorf("matrix at %d,%d is wrong height: %d != %d: %w", i, j, r, h, ErrDim)
}
if i != 0 {
_, w := src[j].Dims()
if c != w {
return fmt.Errorf("matrix at %d,%d is wrong width: %d != %d: %w", i, j, c, w, ErrDim)
}
}
sli := dst.Slice(br, br+r, bc, bc+c)
sli.Copy(src[i*mcols+j])
bc += c
}
br += h
}
return nil
}
// DoSet iterates over all matrix elements calling fn on them and setting
// the value at i,j to the result of fn.
func (A *DenseM) DoSet(fn func(i, j int, v float64) float64) {
for i := 0; i < A.r; i++ {
offset := i * A.stride
for j := 0; j < A.c; j++ {
got := A.data[offset+j]
A.data[offset+j] = fn(i, j, got)
}
}
}