-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.go
372 lines (272 loc) · 6.97 KB
/
complex.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
package market
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"gonum.org/v1/gonum/mat"
)
// CDense is a type embedding of mat.CDense, for reading and writing
// complex-valued matrices in Matrix Market array format.
type CDense struct {
Object string
Format string
Field string
Symmetry string
mat *mat.CDense
}
// NewCDense initializes a new CDense dense matrix from a mat.CDense
// matrix.
func NewCDense(d *mat.CDense) *CDense {
return &CDense{
Object: mtxObjectMatrix,
Format: mtxFormatArray,
Field: mtxFieldComplex,
Symmetry: mtxSymmetryGeneral,
mat: d,
}
}
func (m *CDense) Do(fn func(i, j int, v complex128)) {
M, N := m.mat.Dims()
for j := 0; j < N; j++ {
for i := 0; i < M; i++ {
fn(i, j, m.mat.At(i, j))
}
}
}
// ToCDense returns a mat.CDense matrix that shares underlying storage
// with the receiver.
func (m *CDense) ToCDense() *mat.CDense { return m.mat }
// ToCMatrix returns a mat.CMatrix complex matrix that shares underlying
// storage with the receiver.
func (m *CDense) ToCMatrix() mat.CMatrix { return m.mat }
// MarshalText serializes the receiver to []byte in Matrix Market format
// and returns the result.
func (m *CDense) MarshalText() ([]byte, error) {
var b strings.Builder
if _, err := m.MarshalTextTo(&b); err != nil {
return nil, err
}
return []byte(b.String()), nil
}
// MarshalTextTo serializes the receiver to w in Matrix Market format
// and returns the result.
func (m *CDense) MarshalTextTo(w io.Writer) (int, error) {
var total int
t := mmType{m.Object, m.Format, m.Field, m.Symmetry}
if n, err := w.Write(t.Bytes()); err == nil {
total += n
} else {
return total, ErrUnwritable
}
M, N := m.mat.Dims()
if n, err := fmt.Fprintf(w, "%%\n %d %d\n", M, N); err == nil {
total += n
} else {
return total, ErrUnwritable
}
var a cmplxAligner
m.Do(a.Fit('f', -1, 128))
// entries in column major order
var buf = make([]byte, 0, 128)
for j := 0; j < N; j++ {
for i := 0; i < M; i++ {
buf = a.Append(buf[:0], m.mat.At(i, j), 'f', -1, 128)
buf = append(buf, '\n')
n, err := w.Write(buf)
if err != nil {
return total, ErrUnwritable
}
total += n
}
}
return total, nil
}
// UnmarshalText deserializes []byte from Matrix Market format
// into the receiver.
func (m *CDense) UnmarshalText(text []byte) error {
r := bytes.NewReader(text)
if _, err := m.UnmarshalTextFrom(r); err != nil {
return err
}
return nil
}
// UnmarshalTextFrom deserializes r from Matrix Market format
// into the receiver.
func (m *CDense) UnmarshalTextFrom(r io.Reader) (int, error) {
var n counter
r = io.TeeReader(r, &n)
scanner := bufio.NewScanner(r)
buf := make([]byte, maxScanTokenSize)
scanner.Buffer(buf, maxScanTokenSize)
// read header
t, err := scanHeader(scanner)
if err != nil {
return n.total, err
}
// apply header fields
m.Object = t.Object
m.Format = t.Format
m.Field = t.Field
m.Symmetry = t.Symmetry
switch t.index() {
case 7, 8, 9, 19:
if err := m.scanCoordinateData(scanner); err != nil {
return n.total, err
}
if err := scanner.Err(); err != nil {
return n.total, err
}
case 16, 17, 18, 20:
if err := m.scanArrayData(scanner); err != nil {
return n.total, err
}
if err := scanner.Err(); err != nil {
return n.total, err
}
default:
return n.total, ErrUnsupportedType
}
return n.total, nil
}
func (m *CDense) scanArrayData(scanner *bufio.Scanner) error {
var M, N, k int
for scanner.Scan() {
line := scanner.Text()
// blank line or comment (%, Unicode 37)
if r := []rune(line); len(r) == 0 || r[0] == 37 {
continue
}
if _, err := fmt.Sscanf(line, "%d %d", &M, &N); err != nil {
return ErrInputScanError
}
break
}
d := mat.NewCDense(M, N, nil)
for scanner.Scan() {
var vr, vi float64
line := scanner.Text()
// blank lines are allowed in data per design spec
if r := []rune(line); len(r) == 0 {
continue
}
// error out if data rows exceed expected non-zero entries
// (note that k is zero indexed)
if k == M*N {
return ErrInputScanError
}
if _, err := fmt.Sscanf(line, "%f %f", &vr, &vi); err != nil {
return ErrInputScanError
}
switch m.Symmetry {
case mtxSymmetrySymm:
// if above diagonal, move to diag
for k%M < int(k/M) {
k++
}
// if off diagonal, set value for symm element
if int(k/M) != k%M {
d.Set(int(k/M), k%M, complex(vr, vi))
}
case mtxSymmetrySkew:
// if on or above diagonal, move below diag
for k%M <= int(k/M) {
k++
}
// set skew value for symm element
d.Set(int(k/M), k%M, -complex(vr, vi))
case mtxSymmetryHermitian:
// if above diagonal, move to diag
for k%M < int(k/M) {
k++
}
// if off diagonal, set value for symm element
if int(k/M) != k%M {
d.Set(int(k/M), k%M, complex(vr, -vi))
}
}
d.Set(k%M, int(k/M), complex(vr, vi))
k++
}
// as skew-symmetric entries are below the diagonal in the matrix
// market specification, there are no entries in the last column. The
// counter k must must be advanced by one full column for
// skew-symmetric matrices prior to the check which follows, otherwise
// k == M * (N - 1) and the check will fail.
if m.Symmetry == mtxSymmetrySkew {
k += M
}
// compare counter k against expected number of entries (matrix size)
if k != M*N {
return ErrInputScanError
}
if err := scanner.Err(); err != nil {
return ErrInputScanError
}
m.mat = d
return nil
}
func (m *CDense) scanCoordinateData(scanner *bufio.Scanner) error {
var M, N, L, k int
for scanner.Scan() {
line := scanner.Text()
// blank line or comment (%, Unicode 37)
if r := []rune(line); len(r) == 0 || r[0] == 37 {
continue
}
if _, err := fmt.Sscanf(line, "%d %d %d", &M, &N, &L); err != nil {
return ErrInputScanError
}
break
}
d := mat.NewCDense(M, N, nil)
for scanner.Scan() {
var (
i, j int
vr, vi float64
)
line := scanner.Text()
// blank lines are allowed in data per design spec
if r := []rune(line); len(r) == 0 {
continue
}
// error out if data rows exceed expected non-zero entries
// (note that k is zero indexed)
if k == L {
return ErrInputScanError
}
if _, err := fmt.Sscanf(line, "%d %d %f %f", &i, &j, &vr, &vi); err != nil {
return ErrInputScanError
}
switch m.Symmetry {
case mtxSymmetrySymm:
// if off diagonal, set value for symm element
if i != j {
d.Set(j-1, i-1, complex(vr, vi))
}
case mtxSymmetrySkew:
// if off diagonal, set skew value for symm element
// (note. diagonal elements aren't allowed for skew mats)
if i != j {
d.Set(j-1, i-1, -complex(vr, vi))
}
case mtxSymmetryHermitian:
// if off diagonal, set value for symm element
if i != j {
d.Set(j-1, i-1, complex(vr, -vi))
}
}
d.Set(i-1, j-1, complex(vr, vi))
k++
}
// compare counter k against expected number of expected entries L
if k != L {
return ErrInputScanError
}
if err := scanner.Err(); err != nil {
return ErrInputScanError
}
m.mat = d
return nil
}