forked from gonum/gonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_example_test.go
153 lines (128 loc) · 3.87 KB
/
format_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
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
// Copyright ©2015 The Gonum 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 mat_test
import (
"fmt"
"gonum.org/v1/gonum/mat"
)
func ExampleFormatted() {
a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6})
// Create a matrix formatting value with a prefix and calculating each column
// width individually...
fa := mat.Formatted(a, mat.Prefix(" "), mat.Squeeze())
// and then print with and without zero value elements.
fmt.Printf("with all values:\na = %v\n\n", fa)
fmt.Printf("with only non-zero values:\na = % v\n\n", fa)
// Modify the matrix...
a.Set(0, 2, 0)
// and print it without zero value elements.
fmt.Printf("after modification with only non-zero values:\na = % v\n\n", fa)
// Modify the matrix again...
a.Set(0, 2, 123.456)
// and print it using scientific notation for large exponents.
fmt.Printf("after modification with scientific notation:\na = %.2g\n\n", fa)
// See golang.org/pkg/fmt/ floating-point verbs for a comprehensive list.
// Output:
// with all values:
// a = ⎡1 2 3⎤
// ⎢0 4 5⎥
// ⎣0 0 6⎦
//
// with only non-zero values:
// a = ⎡1 2 3⎤
// ⎢. 4 5⎥
// ⎣. . 6⎦
//
// after modification with only non-zero values:
// a = ⎡1 2 .⎤
// ⎢. 4 5⎥
// ⎣. . 6⎦
//
// after modification with scientific notation:
// a = ⎡1 2 1.2e+02⎤
// ⎢0 4 5⎥
// ⎣0 0 6⎦
}
func ExampleFormatted_mATLAB() {
a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6})
// Create a matrix formatting value using MATLAB format...
fa := mat.Formatted(a, mat.FormatMATLAB())
// and then print with and without layout formatting.
fmt.Printf("standard syntax:\na = %v\n\n", fa)
fmt.Printf("layout syntax:\na = %#v\n\n", fa)
// Output:
// standard syntax:
// a = [1 2 3; 0 4 5; 0 0 6]
//
// layout syntax:
// a = [
// 1 2 3
// 0 4 5
// 0 0 6
// ]
}
func ExampleFormatted_python() {
a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6})
// Create a matrix formatting value with a prefix using Python format...
fa := mat.Formatted(a, mat.Prefix(" "), mat.FormatPython())
// and then print with and without layout formatting.
fmt.Printf("standard syntax:\na = %v\n\n", fa)
fmt.Printf("layout syntax:\na = %#v\n\n", fa)
// Output:
// standard syntax:
// a = [[1, 2, 3], [0, 4, 5], [0, 0, 6]]
//
// layout syntax:
// a = [[1, 2, 3],
// [0, 4, 5],
// [0, 0, 6]]
}
func ExampleExcerpt() {
// Excerpt allows diagnostic display of very large
// matrices and vectors.
// The big matrix is too large to properly print...
big := mat.NewDense(100, 100, nil)
for i := 0; i < 100; i++ {
big.Set(i, i, 1)
}
// so only print corner excerpts of the matrix.
fmt.Printf("excerpt big identity matrix: %v\n\n",
mat.Formatted(big, mat.Prefix(" "), mat.Excerpt(3)))
// The long vector is also too large, ...
long := mat.NewVecDense(100, nil)
for i := 0; i < 100; i++ {
long.SetVec(i, float64(i))
}
// ... so print end excerpts of the vector,
fmt.Printf("excerpt long column vector: %v\n\n",
mat.Formatted(long, mat.Prefix(" "), mat.Excerpt(3)))
// or its transpose.
fmt.Printf("excerpt long row vector: %v\n",
mat.Formatted(long.T(), mat.Prefix(" "), mat.Excerpt(3)))
// Output:
// excerpt big identity matrix: Dims(100, 100)
// ⎡1 0 0 ... ... 0 0 0⎤
// ⎢0 1 0 0 0 0⎥
// ⎢0 0 1 0 0 0⎥
// .
// .
// .
// ⎢0 0 0 1 0 0⎥
// ⎢0 0 0 0 1 0⎥
// ⎣0 0 0 ... ... 0 0 1⎦
//
// excerpt long column vector: Dims(100, 1)
// ⎡ 0⎤
// ⎢ 1⎥
// ⎢ 2⎥
// .
// .
// .
// ⎢97⎥
// ⎢98⎥
// ⎣99⎦
//
// excerpt long row vector: Dims(1, 100)
// [ 0 1 2 ... ... 97 98 99]
}