github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/mat/gsvd_example_test.go (about) 1 // Copyright ©2017 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 "math" 11 12 "github.com/jingcheng-WU/gonum/mat" 13 ) 14 15 func ExampleGSVD() { 16 // Perform a GSVD factorization on food production/consumption data for the 17 // three years 1990, 2000 and 2014, for Africa and Latin America/Caribbean. 18 // 19 // See Lee et al. doi:10.1371/journal.pone.0030098 and 20 // Alter at al. doi:10.1073/pnas.0530258100 for more details. 21 var gsvd mat.GSVD 22 ok := gsvd.Factorize(FAO.Africa, FAO.LatinAmericaCaribbean, mat.GSVDU|mat.GSVDV|mat.GSVDQ) 23 if !ok { 24 log.Fatal("GSVD factorization failed") 25 } 26 var u, v mat.Dense 27 gsvd.UTo(&u) 28 gsvd.VTo(&v) 29 30 s1 := gsvd.ValuesA(nil) 31 s2 := gsvd.ValuesB(nil) 32 33 fmt.Printf("Africa\n\ts1 = %.4f\n\n\tU = %.4f\n\n", 34 s1, mat.Formatted(&u, mat.Prefix("\t "), mat.Excerpt(2))) 35 fmt.Printf("Latin America/Caribbean\n\ts2 = %.4f\n\n\tV = %.4f\n", 36 s2, mat.Formatted(&v, mat.Prefix("\t "), mat.Excerpt(2))) 37 38 var q, zR mat.Dense 39 gsvd.QTo(&q) 40 gsvd.ZeroRTo(&zR) 41 q.Mul(&zR, &q) 42 fmt.Printf("\nCommon basis vectors\n\n\tQᵀ = %.4f\n", 43 mat.Formatted(q.T(), mat.Prefix("\t "))) 44 45 // Calculate the antisymmetric angular distances for each eigenvariable. 46 fmt.Println("\nSignificance:") 47 for i := 0; i < 3; i++ { 48 fmt.Printf("\teigenvar_%d: %+.4f\n", i, math.Atan(s1[i]/s2[i])-math.Pi/4) 49 } 50 51 // Output: 52 // 53 // Africa 54 // s1 = [1.0000 0.9344 0.5118] 55 // 56 // U = Dims(21, 21) 57 // ⎡-0.0005 0.0142 ... ... -0.0060 -0.0055⎤ 58 // ⎢-0.0010 0.0019 0.0071 0.0075⎥ 59 // . 60 // . 61 // . 62 // ⎢-0.0007 -0.0024 0.9999 -0.0001⎥ 63 // ⎣-0.0010 -0.0016 ... ... -0.0001 0.9999⎦ 64 // 65 // Latin America/Caribbean 66 // s2 = [0.0047 0.3563 0.8591] 67 // 68 // V = Dims(14, 14) 69 // ⎡ 0.1362 0.0008 ... ... 0.0700 0.2636⎤ 70 // ⎢ 0.1830 -0.0040 0.2908 0.7834⎥ 71 // . 72 // . 73 // . 74 // ⎢-0.2598 -0.0324 0.9339 -0.2170⎥ 75 // ⎣-0.8386 0.1494 ... ... -0.1639 0.4121⎦ 76 // 77 // Common basis vectors 78 // 79 // Qᵀ = ⎡ 14508.5881 4524.2933 -4813.9616⎤ 80 // ⎢ 15562.9323 12397.1070 -16364.8933⎥ 81 // ⎣-14262.7217 -10902.1488 15762.8719⎦ 82 // 83 // Significance: 84 // eigenvar_0: +0.7807 85 // eigenvar_1: +0.4211 86 // eigenvar_2: -0.2482 87 }