github.com/gopherd/gonum@v0.0.4/mat/eigen_example_test.go (about) 1 // Copyright ©2018 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 "log" 10 11 "github.com/gopherd/gonum/mat" 12 ) 13 14 func ExampleEigenSym() { 15 a := mat.NewSymDense(2, []float64{ 16 7, 0.5, 17 0.5, 1, 18 }) 19 fmt.Printf("A = %v\n\n", mat.Formatted(a, mat.Prefix(" "))) 20 21 var eigsym mat.EigenSym 22 ok := eigsym.Factorize(a, true) 23 if !ok { 24 log.Fatal("Symmetric eigendecomposition failed") 25 } 26 fmt.Printf("Eigenvalues of A:\n%1.3f\n\n", eigsym.Values(nil)) 27 28 var ev mat.Dense 29 eigsym.VectorsTo(&ev) 30 fmt.Printf("Eigenvectors of A:\n%1.3f\n\n", mat.Formatted(&ev)) 31 32 // Output: 33 // A = ⎡ 7 0.5⎤ 34 // ⎣0.5 1⎦ 35 // 36 // Eigenvalues of A: 37 // [0.959 7.041] 38 // 39 // Eigenvectors of A: 40 // ⎡ 0.082 -0.997⎤ 41 // ⎣-0.997 -0.082⎦ 42 // 43 } 44 45 func ExampleEigen() { 46 a := mat.NewDense(2, 2, []float64{ 47 1, -1, 48 1, 1, 49 }) 50 fmt.Printf("A = %v\n\n", mat.Formatted(a, mat.Prefix(" "))) 51 52 var eig mat.Eigen 53 ok := eig.Factorize(a, mat.EigenLeft) 54 if !ok { 55 log.Fatal("Eigendecomposition failed") 56 } 57 fmt.Printf("Eigenvalues of A:\n%v\n", eig.Values(nil)) 58 59 // Output: 60 // A = ⎡ 1 -1⎤ 61 // ⎣ 1 1⎦ 62 // 63 // Eigenvalues of A: 64 // [(1+1i) (1-1i)] 65 }