-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinate_example_test.go
65 lines (52 loc) · 1.45 KB
/
coordinate_example_test.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
package market
import (
"fmt"
"github.com/james-bowman/sparse"
"gonum.org/v1/gonum/mat"
)
func ExampleCOO_MarshalText() {
// mtx is a sparse matrix representation in coordinate format
mtx := sparse.NewCOO(4, 5, nil, nil, nil)
mtx.Set(0, 0, 0.944853346337906500)
mtx.Set(1, 1, 0.897566664045815500)
mtx.Set(2, 2, 0.402696290353813800)
// m is a COO matrix initialized with mtx
m := NewCOO(mtx)
// serialized m into []byte (mm)
mm, err := m.MarshalText()
if err != nil {
panic(err)
}
fmt.Println(string(mm))
// output:
// %%MatrixMarket matrix coordinate real general
// %
// 4 5 3
// 1 1 0.9448533463379065
// 2 2 0.8975666640458155
// 3 3 0.4026962903538138
}
func ExampleCOO_UnmarshalText() {
// mm is a real-valued sparse matrix in Matrix Market coordinate format
mm := []byte(
`%%MatrixMarket matrix coordinate real general
3 3 3
1 1 0.9448533463379065
2 2 0.8975666640458155
3 3 0.4026962903538138`,
)
// mtx is a coo matrix representation
mtx := sparse.NewCOO(3, 3, nil, nil, nil)
// m is a COO matrix initialized with mtx
m := NewCOO(mtx)
// deserialize mm into m
err := m.UnmarshalText(mm)
if err != nil {
panic(err)
}
fmt.Printf("%v", mat.Formatted(m.ToMatrix()))
// output:
// ⎡0.9448533463379065 0 0⎤
// ⎢ 0 0.8975666640458155 0⎥
// ⎣ 0 0 0.4026962903538138⎦
}