OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / mat / gsvd_example_test.go
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         "gonum.org/v1/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
27         u := gsvd.UTo(nil)
28         v := gsvd.VTo(nil)
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 mat.Dense
39         q.Mul(gsvd.ZeroRTo(nil), gsvd.QTo(nil))
40         fmt.Printf("\nCommon basis vectors\n\n\tQ^T = %.4f\n",
41                 mat.Formatted(q.T(), mat.Prefix("\t      ")))
42
43         // Calculate the antisymmetric angular distances for each eigenvariable.
44         fmt.Println("\nSignificance:")
45         for i := 0; i < 3; i++ {
46                 fmt.Printf("\teigenvar_%d: %+.4f\n", i, math.Atan(s1[i]/s2[i])-math.Pi/4)
47         }
48
49         // Output:
50         //
51         // Africa
52         //      s1 = [1.0000 0.9344 0.5118]
53         //
54         //      U = Dims(21, 21)
55         //          ⎡-0.0005   0.0142  ...  ...  -0.0060  -0.0055⎤
56         //          ⎢-0.0010   0.0019             0.0071   0.0075⎥
57         //           .
58         //           .
59         //           .
60         //          ⎢-0.0007  -0.0024             0.9999  -0.0001⎥
61         //          ⎣-0.0010  -0.0016  ...  ...  -0.0001   0.9999⎦
62         //
63         // Latin America/Caribbean
64         //      s2 = [0.0047 0.3563 0.8591]
65         //
66         //      V = Dims(14, 14)
67         //          ⎡ 0.1362   0.0008  ...  ...   0.0700   0.2636⎤
68         //          ⎢ 0.1830  -0.0040             0.2908   0.7834⎥
69         //           .
70         //           .
71         //           .
72         //          ⎢-0.2598  -0.0324             0.9339  -0.2170⎥
73         //          ⎣-0.8386   0.1494  ...  ...  -0.1639   0.4121⎦
74         //
75         // Common basis vectors
76         //
77         //      Q^T = ⎡ -8172.4084   -4524.2933    4813.9616⎤
78         //            ⎢ 22581.8020   12397.1070  -16364.8933⎥
79         //            ⎣ -8910.8462  -10902.1488   15762.8719⎦
80         //
81         // Significance:
82         //      eigenvar_0: +0.7807
83         //      eigenvar_1: +0.4211
84         //      eigenvar_2: -0.2482
85 }