forked from cpmech/gosl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse_matrix.go
373 lines (339 loc) · 11.5 KB
/
sparse_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
// Copyright 2016 The Gosl 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 la
import (
"bytes"
"math"
"math/cmplx"
"strings"
"github.com/cpmech/gosl/chk"
"github.com/cpmech/gosl/io"
)
// Triplet is a simple representation of a sparse matrix, where the indices and values
// of this matrix are stored directly.
type Triplet struct {
m, n int // matrix dimension (rows, columns)
pos, max int // current position and max number of entries allowed (non-zeros, including repetitions)
i, j []int // indices for each x values (size=max)
x []float64 // values for each i, j (size=max)
}
// CCMatrix represents a sparse matrix using the so-called "column-compressed format".
type CCMatrix struct {
m, n int // matrix dimension (rows, columns)
nnz int // number of non-zeros
p, i []int // pointers and row indices (len(p)=n+1, len(i)=nnz)
x []float64 // values (len(x)=nnz)
}
// NewTriplet returns a new Triplet. This is a wrapper to new(Triplet) followed by Init()
func NewTriplet(m, n, max int) (o *Triplet) {
o = new(Triplet)
o.Init(m, n, max)
return
}
// Init allocates all memory required to hold a sparse matrix in triplet form
func (o *Triplet) Init(m, n, max int) {
o.m, o.n, o.pos, o.max = m, n, 0, max
o.i = make([]int, max)
o.j = make([]int, max)
o.x = make([]float64, max)
}
// Put inserts an element to a pre-allocated (with Init) triplet matrix
func (o *Triplet) Put(i, j int, x float64) {
if o.pos >= o.max {
chk.Panic("cannot put item because max number of items has been exceeded (pos = %d, max = %d)", o.pos, o.max)
}
o.i[o.pos], o.j[o.pos], o.x[o.pos] = i, j, x
o.pos++
}
// PutMatAndMatT adds the content of a matrix "a" and its transpose "at" to triplet "o"
// ex: 0 1 2 3 4 5
// [... ... ... a00 a10 ...] 0
// [... ... ... a01 a11 ...] 1
// [... ... ... a02 a12 ...] 2 [. at .]
// [a00 a01 a02 ... ... ...] 3 => [a . .]
// [a10 a11 a12 ... ... ...] 4 [. . .]
// [... ... ... ... ... ...] 5
func (o *Triplet) PutMatAndMatT(a *Triplet) {
if a.n+a.m > o.m || a.n+a.m > o.n {
chk.Panic("cannot put larger matrix into sparse matrix.\nb := [[.. at] [a ..]] with len(a)=(%d,%d) and len(b)=(%d,%d)", a.m, a.n, o.m, o.n)
}
for k := 0; k < a.pos; k++ {
o.Put(a.n+a.i[k], a.j[k], a.x[k]) // puts a
o.Put(a.j[k], a.n+a.i[k], a.x[k]) // puts at
}
}
// PutCCMatAndMatT adds the content of a compressed-column matrix "a" and its transpose "at" to triplet "o"
// ex: 0 1 2 3 4 5
// [... ... ... a00 a10 ...] 0
// [... ... ... a01 a11 ...] 1
// [... ... ... a02 a12 ...] 2 [. at .]
// [a00 a01 a02 ... ... ...] 3 => [a . .]
// [a10 a11 a12 ... ... ...] 4 [. . .]
// [... ... ... ... ... ...] 5
func (o *Triplet) PutCCMatAndMatT(a *CCMatrix) {
if a.n+a.m > o.m || a.n+a.m > o.n {
chk.Panic("cannot put larger matrix into sparse matrix.\nb := [[.. at] [a ..]] with len(a)=(%d,%d) and len(b)=(%d,%d)", a.m, a.n, o.m, o.n)
}
for j := 0; j < a.n; j++ {
for k := a.p[j]; k < a.p[j+1]; k++ {
o.Put(a.n+a.i[k], j, a.x[k]) // puts a
o.Put(j, a.n+a.i[k], a.x[k]) // puts at
}
}
}
// Start (re)starts index for inserting items using the Put command
func (o *Triplet) Start() {
o.pos = 0
}
// Len returns the number of items just inserted in the triplet
func (o *Triplet) Len() int {
return o.pos
}
// Max returns the maximum number of items that can be inserted in the triplet
func (o *Triplet) Max() int {
return o.max
}
// ToDense returns the dense matrix corresponding to this Triplet
func (o *Triplet) ToDense() (a *Matrix) {
a = NewMatrix(o.m, o.n)
for k := 0; k < o.max; k++ {
a.Add(o.i[k], o.j[k], o.x[k])
}
return
}
// WriteSmat writes a ".smat" file that can be visualised with vismatrix
//
// NOTE: this method will create a CCMatrix first because
// duplicates must be added before saving the file
//
// dirout -- directory for output. will be created
// fnkey -- filename key (filename without extension). ".smat" will be added
// tol -- tolerance to skip zero values
func (o *Triplet) WriteSmat(dirout, fnkey string, tol float64) (cmat *CCMatrix) {
cmat = o.ToMatrix(nil)
cmat.WriteSmat(dirout, fnkey, tol)
return
}
// ReadSmat reads ".smat" file
//
// m n nnz
// i j x
// ...
// i j x
//
func (o *Triplet) ReadSmat(filename string) {
io.ReadLines(filename, func(idx int, line string) (stop bool) {
r := strings.Fields(line)
if idx == 0 {
if len(r) != 3 {
chk.Panic("number of columns in header must be 3 (m,n,nnz)\n")
}
m, n, nnz := io.Atoi(r[0]), io.Atoi(r[1]), io.Atoi(r[2])
o.Init(m, n, nnz)
} else {
if len(r) != 3 {
chk.Panic("number of columns in data lines must be 4 (i,j,x)\n")
}
i, j, x := io.Atoi(r[0]), io.Atoi(r[1]), io.Atof(r[2])
o.Put(i, j, x)
}
return
})
}
// WriteSmat writes a ".smat" file that can be visualised with vismatrix
//
// NOTE: CCMatrix must be used to generate the resulting values because
// duplicates must be added before saving file
//
// dirout -- directory for output. will be created
// fnkey -- filename key (filename without extension). ".smat" will be added
// tol -- tolerance to skip zero values
func (o *CCMatrix) WriteSmat(dirout, fnkey string, tol float64) {
var bfa, bfb bytes.Buffer
var nnz int
for j := 0; j < o.n; j++ {
for p := o.p[j]; p < o.p[j+1]; p++ {
if math.Abs(o.x[p]) > tol {
io.Ff(&bfb, " %d %d %23.15e\n", o.i[p], j, o.x[p])
nnz++
}
}
}
io.Ff(&bfa, "%d %d %d\n", o.m, o.n, nnz)
io.WriteFileVD(dirout, fnkey+".smat", &bfa, &bfb)
}
// ToDense converts a column-compressed matrix to dense form
func (o *CCMatrix) ToDense() (res *Matrix) {
res = NewMatrix(o.m, o.n)
for j := 0; j < o.n; j++ {
for p := o.p[j]; p < o.p[j+1]; p++ {
res.Set(o.i[p], j, o.x[p])
}
}
return
}
// Set sets column-compressed matrix directly
func (o *CCMatrix) Set(m, n int, Ap, Ai []int, Ax []float64) {
if len(Ap)-1 != n {
chk.Panic("len(Ap) must be equal to n. %d != %d", len(Ap), n)
}
nnz := len(Ai)
if len(Ax) != nnz {
chk.Panic("len(Ax) must be equal to len(Ai) == nnz. %d != %d", len(Ax), nnz)
}
if Ap[n] != nnz {
chk.Panic("last item in Ap must be equal to nnz. %d != %d", Ap[n], nnz)
}
o.m, o.n, o.nnz = m, n, nnz
o.p, o.i, o.x = Ap, Ai, Ax
}
// complex /////////////////////////////////////////////////////////////////////////////////////////
// TripletC is a simple representation of a sparse matrix, where the indices and values
// of this matrix are stored directly. (complex version)
type TripletC struct {
m, n int // matrix dimension (rows, columns)
pos, max int // current position and max number of entries allowed (non-zeros, including repetitions)
i, j []int // indices for each x values (size=max)
x []complex128 // values for each i, j (size=max)
}
// CCMatrixC represents a sparse matrix using the so-called "column-compressed format".
// (complex version)
type CCMatrixC struct {
m, n int // matrix dimension (rows, columns)
nnz int // number of non-zeros
p, i []int // pointers and row indices (len(p)=n+1, len(i)=nnz)
x []complex128 // values (len(x)=nnz)
}
// NewTripletC returns a new TripletC. This is a wrapper to new(TripletC) followed by Init()
func NewTripletC(m, n, max int) (o *TripletC) {
o = new(TripletC)
o.Init(m, n, max)
return
}
// Init allocates all memory required to hold a sparse matrix in triplet (complex) form
func (o *TripletC) Init(m, n, max int) {
o.m, o.n, o.pos, o.max = m, n, 0, max
o.i = make([]int, max)
o.j = make([]int, max)
o.x = make([]complex128, max)
}
// Put inserts an element to a pre-allocated (with Init) triplet (complex) matrix
func (o *TripletC) Put(i, j int, x complex128) {
if o.pos >= o.max {
chk.Panic("cannot put item because max number of items has been exceeded (pos = %d, max = %d)", o.pos, o.max)
}
o.i[o.pos], o.j[o.pos], o.x[o.pos] = i, j, x
o.pos++
}
// Start (re)starts index for inserting items using the Put command
func (o *TripletC) Start() {
o.pos = 0
}
// Len returns the number of items just inserted in the complex triplet
func (o *TripletC) Len() int {
return o.pos
}
// Max returns the maximum number of items that can be inserted in the complex triplet
func (o *TripletC) Max() int {
return o.max
}
// ToDense returns the dense matrix corresponding to this Triplet
func (o *TripletC) ToDense() (a *MatrixC) {
a = NewMatrixC(o.m, o.n)
for k := 0; k < o.max; k++ {
a.Add(o.i[k], o.j[k], o.x[k])
}
return
}
// WriteSmat writes a ".smat" file that can be visualised with vismatrix
//
// NOTE: this method will create a CCMatrixC first because
// duplicates must be added before saving the file
//
// dirout -- directory for output. will be created
// fnkey -- filename key (filename without extension). ".smat" will be added
// tol -- tolerance to skip zero values
func (o *TripletC) WriteSmat(dirout, fnkey string, tol float64) (cmat *CCMatrixC) {
cmat = o.ToMatrix(nil)
cmat.WriteSmat(dirout, fnkey, tol)
return
}
// ReadSmat reads ".smat" file
//
// m n nnz
// i j xReal xImag
// ...
// i j xReal xImag
//
func (o *TripletC) ReadSmat(filename string) {
io.ReadLines(filename, func(idx int, line string) (stop bool) {
r := strings.Fields(line)
if idx == 0 {
if len(r) != 3 {
chk.Panic("number of columns in header must be 3 (m,n,nnz)\n")
}
m, n, nnz := io.Atoi(r[0]), io.Atoi(r[1]), io.Atoi(r[2])
o.Init(m, n, nnz)
} else {
if len(r) != 4 {
chk.Panic("number of columns in data lines must be 4 (i,j,xReal,xImag)\n")
}
i, j, x := io.Atoi(r[0]), io.Atoi(r[1]), complex(io.Atof(r[2]), io.Atof(r[3]))
o.Put(i, j, x)
}
return
})
}
// WriteSmat writes a ".smat" file that can be visualised with vismatrix
//
// NOTE: CCMatrix must be used to generate the resulting values because
// duplicates must be added before saving file
//
// dirout -- directory for output. will be created
// fnkey -- filename key (filename without extension). ".smat" will be added
// tol -- tolerance to skip zero values
func (o *CCMatrixC) WriteSmat(dirout, fnkey string, tol float64) {
var bfa, bfb bytes.Buffer
var nnz int
for j := 0; j < o.n; j++ {
for p := o.p[j]; p < o.p[j+1]; p++ {
if math.Abs(real(o.x[p])) > tol || math.Abs(imag(o.x[p])) > tol {
io.Ff(&bfb, " %d %d %23.15e %+23.15e\n", o.i[p], j, real(o.x[p]), imag(o.x[p]))
nnz++
}
}
}
io.Ff(&bfa, "%d %d %d\n", o.m, o.n, nnz)
io.WriteFileVD(dirout, fnkey+".smat", &bfa, &bfb)
}
// WriteSmatAbs writes a ".smat" file that can be visualised with vismatrix (abs(complex) version)
//
// NOTE: CCMatrix must be used to generate the resulting values because
// duplicates must be added before saving file
//
// tol -- tolerance to skip zero values
func (o *CCMatrixC) WriteSmatAbs(dirout, fnkey string, tol float64) {
var bfa, bfb bytes.Buffer
var nnz int
for j := 0; j < o.n; j++ {
for p := o.p[j]; p < o.p[j+1]; p++ {
if math.Abs(real(o.x[p])) > tol || math.Abs(imag(o.x[p])) > tol {
io.Ff(&bfb, " %d %d %23.15e\n", o.i[p], j, cmplx.Abs(o.x[p]))
nnz++
}
}
}
io.Ff(&bfa, "%d %d %d\n", o.m, o.n, nnz)
io.WriteFileVD(dirout, fnkey+".smat", &bfa, &bfb)
}
// ToDense converts a column-compressed matrix (complex) to dense form
func (o *CCMatrixC) ToDense() (res *MatrixC) {
res = NewMatrixC(o.m, o.n)
for j := 0; j < o.n; j++ {
for p := o.p[j]; p < o.p[j+1]; p++ {
res.Set(o.i[p], j, o.x[p])
}
}
return
}