// 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] }