This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmatrix.go
140 lines (123 loc) · 3.18 KB
/
mmatrix.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
package golis
import (
"bytes"
"fmt"
"strconv"
"gonum.org/v1/gonum/mat"
)
// convertMatrixWithVector - convert matrix and vector to byte slice in
// Matrix Market format
// See description:
// https://math.nist.gov/MatrixMarket/formats.html
//
// Coordinate Format for Sparse Matrices
// Format of MM : coordinate
// Type of output data : matrix with vector
// Type of values : real
// Type of matrix : general
func convertMatrixWithVector(A, b mat.Matrix) []byte {
// TODO : add to specific package mmatrix
var buf bytes.Buffer
buf.WriteString("%%MatrixMarket matrix coordinate real general\n")
rA, cA := A.Dims()
rb, cb := b.Dims()
if cb != 1 {
panic(fmt.Errorf("Input `b` is not vector: [%d,%d]", rb, cb))
}
// amount of non-zero values
var nonZeros int
// TODO add optimization for SparseMatrix
switch v := A.(type) {
case *SparseMatrix:
nonZeros = len(v.data.ts)
default:
for i := 0; i < rA; i++ {
for j := 0; j < cA; j++ {
if A.At(i, j) != 0.0 {
nonZeros++
}
}
}
}
// write sizes
// add string "1 0" for indicate that is matrix with vector
buf.WriteString(fmt.Sprintf("%d %d %d 1 0\n", rA, cA, nonZeros))
// write matrix A
switch v := A.(type) {
case *SparseMatrix:
v.compress()
for i := range v.data.ts {
r := int(v.data.ts[i].position % int64(v.r))
c := int(v.data.ts[i].position / int64(v.r))
buf.WriteString(fmt.Sprintf("%d %d %20.16e\n", r+1, c+1, v.data.ts[i].d))
}
default:
for i := 0; i < rA; i++ {
for j := 0; j < cA; j++ {
if A.At(i, j) != 0.0 {
buf.WriteString(fmt.Sprintf("%d %d %20.16e\n", i+1, j+1, A.At(i, j)))
}
}
}
}
// write vector b must be Dense
for i := 0; i < rb; i++ {
buf.WriteString(fmt.Sprintf("%d %20.16e\n", i+1, b.At(i, 0)))
}
return buf.Bytes()
}
// ParseSparseMatrix returns sparse matrix parsed from byte slice in
// MatrixMarket format and error, if exist
//
// Example:
//
// %%MatrixMarket vector coordinate real general
// 3
// 1 -5.49999999999999822364e+00
// 2 2.49999999999999955591e+00
// 3 4.99999999999999911182e+00
//
func ParseSparseMatrix(b []byte) (mat.Matrix, error) {
lines := bytes.Split(b, []byte("\n"))
// TODO: check vector
// TODO: check real
// convert size of vector
s, err := strconv.ParseInt(string(lines[1]), 10, 64)
if err != nil {
err = fmt.Errorf("Cannot parse size `%v`: %v", string(lines[1]), err)
return nil, err
}
v := mat.NewDense(int(s), 1, nil)
// convert values
for i := range lines {
if i < 2 {
continue
}
if len(bytes.TrimSpace(lines[i])) == 0 {
continue
}
pars := bytes.Split(lines[i], []byte(" "))
// parse index
s, err := strconv.ParseInt(string(pars[0]), 10, 64)
if err != nil {
err = fmt.Errorf("Cannot parse index `%v`: %v", string(pars[0]), err)
return nil, err
}
pos := int(s - 1) // in MatrixMarket index from 1, but not zero
// parse value
var val float64
for pos := 1; pos < len(pars); pos++ {
if len(pars[pos]) == 0 {
continue
}
s, err := strconv.ParseFloat(string(pars[pos]), 64)
if err != nil {
err = fmt.Errorf("Cannot parse value `%v`: %v", string(pars[pos]), err)
return nil, err
}
val = s
}
v.Set(pos, 0, val)
}
return v, nil
}