1 // Copyright ©2015 The Gonum Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 "gonum.org/v1/gonum/mat"
13 func ExampleFormatted() {
14 a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6})
16 // Create a matrix formatting value with a prefix and calculating each column
17 // width individually...
18 fa := mat.Formatted(a, mat.Prefix(" "), mat.Squeeze())
20 // and then print with and without zero value elements.
21 fmt.Printf("with all values:\na = %v\n\n", fa)
22 fmt.Printf("with only non-zero values:\na = % v\n\n", fa)
24 // Modify the matrix...
27 // and print it without zero value elements.
28 fmt.Printf("after modification with only non-zero values:\na = % v\n\n", fa)
30 // Modify the matrix again...
33 // and print it using scientific notation for large exponents.
34 fmt.Printf("after modification with scientific notation:\na = %.2g\n\n", fa)
35 // See golang.org/pkg/fmt/ floating-point verbs for a comprehensive list.
43 // with only non-zero values:
48 // after modification with only non-zero values:
53 // after modification with scientific notation:
59 func ExampleExcerpt() {
60 // Excerpt allows diagnostic display of very large
61 // matrices and vectors.
63 // The big matrix is too large to properly print...
64 big := mat.NewDense(100, 100, nil)
65 for i := 0; i < 100; i++ {
69 // so only print corner excerpts of the matrix.
70 fmt.Printf("excerpt big identity matrix: %v\n\n",
71 mat.Formatted(big, mat.Prefix(" "), mat.Excerpt(3)))
73 // The long vector is also too large, ...
74 long := mat.NewVecDense(100, nil)
75 for i := 0; i < 100; i++ {
76 long.SetVec(i, float64(i))
79 // ... so print end excerpts of the vector,
80 fmt.Printf("excerpt long column vector: %v\n\n",
81 mat.Formatted(long, mat.Prefix(" "), mat.Excerpt(3)))
83 fmt.Printf("excerpt long row vector: %v\n",
84 mat.Formatted(long.T(), mat.Prefix(" "), mat.Excerpt(3)))
87 // excerpt big identity matrix: Dims(100, 100)
88 // ⎡1 0 0 ... ... 0 0 0⎤
96 // ⎣0 0 0 ... ... 0 0 1⎦
98 // excerpt long column vector: Dims(100, 1)
109 // excerpt long row vector: Dims(1, 100)
110 // [ 0 1 2 ... ... 97 98 99]