OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / mat / hogsvd_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
11         "gonum.org/v1/gonum/mat"
12 )
13
14 func ExampleHOGSVD() {
15         // Perform an HOGSVD factorization on food production/consumption data for the
16         // three years 1990, 2000 and 2014.
17         //
18         // See Ponnapalli et al. doi:10.1371/journal.pone.0028072 and
19         // Alter at al. doi:10.1073/pnas.0530258100 for more details.
20         var gsvd mat.HOGSVD
21         ok := gsvd.Factorize(FAO.Africa, FAO.Asia, FAO.LatinAmericaCaribbean, FAO.Oceania)
22         if !ok {
23                 log.Fatalf("HOGSVD factorization failed: %v", gsvd.Err())
24         }
25
26         for i, n := range []string{"Africa", "Asia", "Latin America/Caribbean", "Oceania"} {
27                 u := gsvd.UTo(nil, i)
28                 s := gsvd.Values(nil, i)
29                 fmt.Printf("%s\n\ts_%d = %.4f\n\n\tU_%[2]d = %.4[4]f\n",
30                         n, i, s, mat.Formatted(u, mat.Prefix("\t      ")))
31         }
32
33         v := gsvd.VTo(nil)
34         fmt.Printf("\nCommon basis vectors\n\n\tV^T = %.4f",
35                 mat.Formatted(v.T(), mat.Prefix("\t      ")))
36
37         // Output:
38         //
39         // Africa
40         //      s_0 = [45507.3278 18541.9293 21503.0778]
41         //
42         //      U_0 = ⎡-0.0005  -0.0039  -0.0019⎤
43         //            ⎢-0.0010  -0.0007  -0.0012⎥
44         //            ⎢-1.0000  -0.0507  -0.9964⎥
45         //            ⎢-0.0022  -0.2906  -0.0415⎥
46         //            ⎢ 0.0001  -0.0127  -0.0016⎥
47         //            ⎢ 0.0003  -0.0067  -0.0010⎥
48         //            ⎢ 0.0003  -0.0022  -0.0003⎥
49         //            ⎢-0.0086  -0.9550   0.0734⎥
50         //            ⎢ 0.0017   0.0002   0.0059⎥
51         //            ⎢-0.0002  -0.0088  -0.0014⎥
52         //            ⎢-0.0006  -0.0078  -0.0001⎥
53         //            ⎢-0.0005  -0.0076   0.0003⎥
54         //            ⎢ 0.0001  -0.0090   0.0008⎥
55         //            ⎢-0.0005  -0.0050   0.0029⎥
56         //            ⎢-0.0011  -0.0078  -0.0012⎥
57         //            ⎢-0.0014  -0.0058  -0.0002⎥
58         //            ⎢ 0.0007  -0.0095   0.0020⎥
59         //            ⎢-0.0008  -0.0081  -0.0009⎥
60         //            ⎢ 0.0004  -0.0092   0.0006⎥
61         //            ⎢-0.0007  -0.0079  -0.0006⎥
62         //            ⎣-0.0011  -0.0076  -0.0010⎦
63         // Asia
64         //      s_1 = [77228.2804 8413.7024 14711.1879]
65         //
66         //      U_1 = ⎡ 0.0005  -0.0080   0.0011⎤
67         //            ⎢ 0.0008  -0.0108   0.0016⎥
68         //            ⎢-0.9998   0.0612   0.9949⎥
69         //            ⎢ 0.0007  -0.5734  -0.0468⎥
70         //            ⎢ 0.0001  -0.0265  -0.0022⎥
71         //            ⎢ 0.0001  -0.0165  -0.0019⎥
72         //            ⎢ 0.0000  -0.0070  -0.0013⎥
73         //            ⎢ 0.0196  -0.8148   0.0893⎥
74         //            ⎢ 0.0002  -0.0063   0.0012⎥
75         //            ⎢-0.0001  -0.0135  -0.0013⎥
76         //            ⎢-0.0004  -0.0135   0.0019⎥
77         //            ⎢-0.0005  -0.0132   0.0014⎥
78         //            ⎢ 0.0003  -0.0155   0.0045⎥
79         //            ⎢-0.0003  -0.0130   0.0025⎥
80         //            ⎢-0.0007  -0.0105   0.0016⎥
81         //            ⎢-0.0006  -0.0129   0.0007⎥
82         //            ⎢-0.0006  -0.0178  -0.0023⎥
83         //            ⎢-0.0003  -0.0149   0.0016⎥
84         //            ⎢-0.0001  -0.0134   0.0030⎥
85         //            ⎢-0.0004  -0.0154   0.0010⎥
86         //            ⎣-0.0009  -0.0147  -0.0019⎦
87         // Latin America/Caribbean
88         //      s_2 = [274.1364 20736.3116 729.6947]
89         //
90         //      U_2 = ⎡ 0.1060  -0.0021   0.0174⎤
91         //            ⎢ 0.1415  -0.0016   0.0289⎥
92         //            ⎢ 0.2350  -0.2669  -0.9212⎥
93         //            ⎢ 0.0290  -0.0118  -0.0429⎥
94         //            ⎢ 0.0226  -0.0043  -0.0213⎥
95         //            ⎢ 0.0117  -0.0016  -0.0197⎥
96         //            ⎢-0.6263  -0.9635   0.2234⎥
97         //            ⎢ 0.2334  -0.0013   0.1275⎥
98         //            ⎢-0.0358  -0.0085  -0.0498⎥
99         //            ⎢-0.1238  -0.0054   0.0313⎥
100         //            ⎢-0.0421  -0.0059   0.0528⎥
101         //            ⎢-0.1471  -0.0056   0.0350⎥
102         //            ⎢-0.2158  -0.0052  -0.0044⎥
103         //            ⎣-0.6154  -0.0078  -0.2717⎦
104         // Oceania
105         //      s_3 = [8954.1914 6942.6316 17233.0561]
106         //
107         //      U_3 = ⎡-0.0080  -0.0012  -0.0040⎤
108         //            ⎢ 0.0004  -0.0014   0.0001⎥
109         //            ⎢ 0.9973  -0.0315   0.9991⎥
110         //            ⎢ 0.0473  -0.7426  -0.0359⎥
111         //            ⎢ 0.0018  -0.0342  -0.0020⎥
112         //            ⎢-0.0005  -0.0148  -0.0016⎥
113         //            ⎢-0.0004  -0.0047  -0.0007⎥
114         //            ⎢-0.0246  -0.6642  -0.0138⎥
115         //            ⎢ 0.0003  -0.0287  -0.0023⎥
116         //            ⎢-0.0011  -0.0148  -0.0014⎥
117         //            ⎢-0.0108  -0.0198  -0.0039⎥
118         //            ⎢-0.0149  -0.0183  -0.0048⎥
119         //            ⎢-0.0178  -0.0208  -0.0075⎥
120         //            ⎢-0.0266  -0.0063  -0.0016⎥
121         //            ⎢-0.0012  -0.0234  -0.0006⎥
122         //            ⎢-0.0084  -0.0184  -0.0030⎥
123         //            ⎢-0.0232  -0.0191  -0.0124⎥
124         //            ⎢-0.0072  -0.0226  -0.0035⎥
125         //            ⎢-0.0150  -0.0144  -0.0045⎥
126         //            ⎢-0.0068  -0.0227  -0.0034⎥
127         //            ⎣-0.0127  -0.0136  -0.0049⎦
128         //
129         // Common basis vectors
130         //
131         //      V^T = ⎡-0.0897  -0.4460  -0.8905⎤
132         //            ⎢-0.4911  -0.5432  -0.6810⎥
133         //            ⎣ 0.0644   0.2841   0.9566⎦
134 }