github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/mat/format_example_test.go (about) 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 "github.com/jingcheng-WU/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 ExampleFormatted_mATLAB() { 60 a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6}) 61 62 // Create a matrix formatting value using MATLAB format... 63 fa := mat.Formatted(a, mat.FormatMATLAB()) 64 65 // and then print with and without layout formatting. 66 fmt.Printf("standard syntax:\na = %v\n\n", fa) 67 fmt.Printf("layout syntax:\na = %#v\n\n", fa) 68 69 // Output: 70 // standard syntax: 71 // a = [1 2 3; 0 4 5; 0 0 6] 72 // 73 // layout syntax: 74 // a = [ 75 // 1 2 3 76 // 0 4 5 77 // 0 0 6 78 // ] 79 } 80 81 func ExampleFormatted_python() { 82 a := mat.NewDense(3, 3, []float64{1, 2, 3, 0, 4, 5, 0, 0, 6}) 83 84 // Create a matrix formatting value with a prefix using Python format... 85 fa := mat.Formatted(a, mat.Prefix(" "), mat.FormatPython()) 86 87 // and then print with and without layout formatting. 88 fmt.Printf("standard syntax:\na = %v\n\n", fa) 89 fmt.Printf("layout syntax:\na = %#v\n\n", fa) 90 91 // Output: 92 // standard syntax: 93 // a = [[1, 2, 3], [0, 4, 5], [0, 0, 6]] 94 // 95 // layout syntax: 96 // a = [[1, 2, 3], 97 // [0, 4, 5], 98 // [0, 0, 6]] 99 } 100 101 func ExampleExcerpt() { 102 // Excerpt allows diagnostic display of very large 103 // matrices and vectors. 104 105 // The big matrix is too large to properly print... 106 big := mat.NewDense(100, 100, nil) 107 for i := 0; i < 100; i++ { 108 big.Set(i, i, 1) 109 } 110 111 // so only print corner excerpts of the matrix. 112 fmt.Printf("excerpt big identity matrix: %v\n\n", 113 mat.Formatted(big, mat.Prefix(" "), mat.Excerpt(3))) 114 115 // The long vector is also too large, ... 116 long := mat.NewVecDense(100, nil) 117 for i := 0; i < 100; i++ { 118 long.SetVec(i, float64(i)) 119 } 120 121 // ... so print end excerpts of the vector, 122 fmt.Printf("excerpt long column vector: %v\n\n", 123 mat.Formatted(long, mat.Prefix(" "), mat.Excerpt(3))) 124 // or its transpose. 125 fmt.Printf("excerpt long row vector: %v\n", 126 mat.Formatted(long.T(), mat.Prefix(" "), mat.Excerpt(3))) 127 128 // Output: 129 // excerpt big identity matrix: Dims(100, 100) 130 // ⎡1 0 0 ... ... 0 0 0⎤ 131 // ⎢0 1 0 0 0 0⎥ 132 // ⎢0 0 1 0 0 0⎥ 133 // . 134 // . 135 // . 136 // ⎢0 0 0 1 0 0⎥ 137 // ⎢0 0 0 0 1 0⎥ 138 // ⎣0 0 0 ... ... 0 0 1⎦ 139 // 140 // excerpt long column vector: Dims(100, 1) 141 // ⎡ 0⎤ 142 // ⎢ 1⎥ 143 // ⎢ 2⎥ 144 // . 145 // . 146 // . 147 // ⎢97⎥ 148 // ⎢98⎥ 149 // ⎣99⎦ 150 // 151 // excerpt long row vector: Dims(1, 100) 152 // [ 0 1 2 ... ... 97 98 99] 153 }