OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / mat / format_example_test.go
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.
4
5 package mat_test
6
7 import (
8         "fmt"
9
10         "gonum.org/v1/gonum/mat"
11 )
12
13 func ExampleFormatted() {
14         a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6})
15
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())
19
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)
23
24         // Modify the matrix...
25         a.Set(0, 2, 0)
26
27         // and print it without zero value elements.
28         fmt.Printf("after modification with only non-zero values:\na = % v\n\n", fa)
29
30         // Modify the matrix again...
31         a.Set(0, 2, 123.456)
32
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.
36
37         // Output:
38         // with all values:
39         // a = ⎡1  2  3⎤
40         //     ⎢0  4  5⎥
41         //     ⎣0  0  6⎦
42         //
43         // with only non-zero values:
44         // a = ⎡1  2  3⎤
45         //     ⎢.  4  5⎥
46         //     ⎣.  .  6⎦
47         //
48         // after modification with only non-zero values:
49         // a = ⎡1  2  .⎤
50         //     ⎢.  4  5⎥
51         //     ⎣.  .  6⎦
52         //
53         // after modification with scientific notation:
54         // a = ⎡1  2  1.2e+02⎤
55         //     ⎢0  4        5⎥
56         //     ⎣0  0        6⎦
57 }
58
59 func ExampleExcerpt() {
60         // Excerpt allows diagnostic display of very large
61         // matrices and vectors.
62
63         // The big matrix is too large to properly print...
64         big := mat.NewDense(100, 100, nil)
65         for i := 0; i < 100; i++ {
66                 big.Set(i, i, 1)
67         }
68
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)))
72
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))
77         }
78
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)))
82         // or its transpose.
83         fmt.Printf("excerpt long row vector: %v\n",
84                 mat.Formatted(long.T(), mat.Prefix(" "), mat.Excerpt(3)))
85
86         // Output:
87         // excerpt big identity matrix: Dims(100, 100)
88         //  ⎡1  0  0  ...  ...  0  0  0⎤
89         //  ⎢0  1  0            0  0  0⎥
90         //  ⎢0  0  1            0  0  0⎥
91         //   .
92         //   .
93         //   .
94         //  ⎢0  0  0            1  0  0⎥
95         //  ⎢0  0  0            0  1  0⎥
96         //  ⎣0  0  0  ...  ...  0  0  1⎦
97         //
98         // excerpt long column vector: Dims(100, 1)
99         //  ⎡ 0⎤
100         //  ⎢ 1⎥
101         //  ⎢ 2⎥
102         //   .
103         //   .
104         //   .
105         //  ⎢97⎥
106         //  ⎢98⎥
107         //  ⎣99⎦
108         //
109         // excerpt long row vector: Dims(1, 100)
110         //  [ 0   1   2  ...  ...  97  98  99]
111
112 }