OSDN Git Service

test (#52)
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / mat / dense_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
10         "gonum.org/v1/gonum/mat"
11 )
12
13 func ExampleDense_Add() {
14         // Initialize two matrices, a and b.
15         a := mat.NewDense(2, 2, []float64{
16                 1, 0,
17                 1, 0,
18         })
19         b := mat.NewDense(2, 2, []float64{
20                 0, 1,
21                 0, 1,
22         })
23
24         // Add a and b, placing the result into c.
25         // Notice that the size is automatically adjusted
26         // when the receiver has zero size.
27         var c mat.Dense
28         c.Add(a, b)
29
30         // Print the result using the formatter.
31         fc := mat.Formatted(&c, mat.Prefix("    "), mat.Squeeze())
32         fmt.Printf("c = %v", fc)
33
34         // Output:
35         //
36         // c = ⎡1  1⎤
37         //     ⎣1  1⎦
38 }
39
40 func ExampleDense_Sub() {
41         // Initialize two matrices, a and b.
42         a := mat.NewDense(2, 2, []float64{
43                 1, 1,
44                 1, 1,
45         })
46         b := mat.NewDense(2, 2, []float64{
47                 1, 0,
48                 0, 1,
49         })
50
51         // Subtract b from a, placing the result into a.
52         a.Sub(a, b)
53
54         // Print the result using the formatter.
55         fa := mat.Formatted(a, mat.Prefix("    "), mat.Squeeze())
56         fmt.Printf("a = %v", fa)
57
58         // Output:
59         //
60         // a = ⎡0  1⎤
61         //     ⎣1  0⎦
62 }
63
64 func ExampleDense_MulElem() {
65         // Initialize two matrices, a and b.
66         a := mat.NewDense(2, 2, []float64{
67                 1, 2,
68                 3, 4,
69         })
70         b := mat.NewDense(2, 2, []float64{
71                 1, 2,
72                 3, 4,
73         })
74
75         // Multiply the elements of a and b, placing the result into a.
76         a.MulElem(a, b)
77
78         // Print the result using the formatter.
79         fa := mat.Formatted(a, mat.Prefix("    "), mat.Squeeze())
80         fmt.Printf("a = %v", fa)
81
82         // Output:
83         //
84         // a = ⎡1   4⎤
85         //     ⎣9  16⎦
86 }
87
88 func ExampleDense_DivElem() {
89         // Initialize two matrices, a and b.
90         a := mat.NewDense(2, 2, []float64{
91                 5, 10,
92                 15, 20,
93         })
94         b := mat.NewDense(2, 2, []float64{
95                 5, 5,
96                 5, 5,
97         })
98
99         // Divide the elements of a by b, placing the result into a.
100         a.DivElem(a, b)
101
102         // Print the result using the formatter.
103         fa := mat.Formatted(a, mat.Prefix("    "), mat.Squeeze())
104         fmt.Printf("a = %v", fa)
105
106         // Output:
107         //
108         // a = ⎡1  2⎤
109         //     ⎣3  4⎦
110 }
111
112 func ExampleDense_Inverse() {
113         // Initialize two matrices, a and ia.
114         a := mat.NewDense(2, 2, []float64{
115                 4, 0,
116                 0, 4,
117         })
118         var ia mat.Dense
119
120         // Take the inverse of a and place the result in ia.
121         ia.Inverse(a)
122
123         // Print the result using the formatter.
124         fa := mat.Formatted(&ia, mat.Prefix("     "), mat.Squeeze())
125         fmt.Printf("ia = %.2g\n\n", fa)
126
127         // Confirm that A * A^-1 = I
128         var r mat.Dense
129         r.Mul(a, &ia)
130         fr := mat.Formatted(&r, mat.Prefix("    "), mat.Squeeze())
131         fmt.Printf("r = %v\n\n", fr)
132
133         // The Inverse operation, however, is numerically unstable,
134         // and should typically be avoided.
135         // For example, a common need is to find x = A^-1 * b.
136         // In this case, the SolveVec method of VecDense
137         // (if b is a Vector) or Solve method of Dense (if b is a
138         // matrix) should used instead of computing the Inverse of A.
139         b := mat.NewDense(2, 2, []float64{
140                 2, 0,
141                 0, 2,
142         })
143         var x mat.Dense
144         x.Solve(a, b)
145
146         // Print the result using the formatter.
147         fx := mat.Formatted(&x, mat.Prefix("    "), mat.Squeeze())
148         fmt.Printf("x = %v", fx)
149
150         // Output:
151         //
152         // ia = ⎡0.25    -0⎤
153         //      ⎣   0  0.25⎦
154         //
155         // r = ⎡1  0⎤
156         //     ⎣0  1⎦
157         //
158         // x = ⎡0.5    0⎤
159         //     ⎣  0  0.5⎦
160 }
161
162 func ExampleDense_Mul() {
163         // Initialize two matrices, a and b.
164         a := mat.NewDense(2, 2, []float64{
165                 4, 0,
166                 0, 4,
167         })
168         b := mat.NewDense(2, 3, []float64{
169                 4, 0, 0,
170                 0, 0, 4,
171         })
172
173         // Take the matrix product of a and b and place the result in c.
174         var c mat.Dense
175         c.Mul(a, b)
176
177         // Print the result using the formatter.
178         fc := mat.Formatted(&c, mat.Prefix("    "), mat.Squeeze())
179         fmt.Printf("c = %v", fc)
180
181         // Output:
182         //
183         // c = ⎡16  0   0⎤
184         //     ⎣ 0  0  16⎦
185 }
186
187 func ExampleDense_Exp() {
188         // Initialize a matrix a with some data.
189         a := mat.NewDense(2, 2, []float64{
190                 1, 0,
191                 0, 1,
192         })
193
194         // Take the exponential of the matrix and place the result in m.
195         var m mat.Dense
196         m.Exp(a)
197
198         // Print the result using the formatter.
199         fm := mat.Formatted(&m, mat.Prefix("    "), mat.Squeeze())
200         fmt.Printf("m = %4.2f", fm)
201
202         // Output:
203         //
204         // m = ⎡2.72  0.00⎤
205         //     ⎣0.00  2.72⎦
206 }
207
208 func ExampleDense_Pow() {
209         // Initialize a matrix with some data.
210         a := mat.NewDense(2, 2, []float64{
211                 4, 4,
212                 4, 4,
213         })
214
215         // Take the second power of matrix a and place the result in m.
216         var m mat.Dense
217         m.Pow(a, 2)
218
219         // Print the result using the formatter.
220         fm := mat.Formatted(&m, mat.Prefix("    "), mat.Squeeze())
221         fmt.Printf("m = %v\n\n", fm)
222
223         // Take the zeroth power of matrix a and place the result in n.
224         // We expect an identity matrix of the same size as matrix a.
225         var n mat.Dense
226         n.Pow(a, 0)
227
228         // Print the result using the formatter.
229         fn := mat.Formatted(&n, mat.Prefix("    "), mat.Squeeze())
230         fmt.Printf("n = %v", fn)
231
232         // Output:
233         //
234         // m = ⎡32  32⎤
235         //     ⎣32  32⎦
236         //
237         // n = ⎡1  0⎤
238         //     ⎣0  1⎦
239 }
240
241 func ExampleDense_Scale() {
242         // Initialize a matrix with some data.
243         a := mat.NewDense(2, 2, []float64{
244                 4, 4,
245                 4, 4,
246         })
247
248         // Scale the matrix by a factor of 0.25 and place the result in m.
249         var m mat.Dense
250         m.Scale(0.25, a)
251
252         // Print the result using the formatter.
253         fm := mat.Formatted(&m, mat.Prefix("    "), mat.Squeeze())
254         fmt.Printf("m = %4.3f", fm)
255
256         // Output:
257         //
258         // m = ⎡1.000  1.000⎤
259         //     ⎣1.000  1.000⎦
260 }