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.
10 "gonum.org/v1/gonum/mat"
13 func ExampleDense_Add() {
14 // Initialize two matrices, a and b.
15 a := mat.NewDense(2, 2, []float64{
19 b := mat.NewDense(2, 2, []float64{
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.
30 // Print the result using the formatter.
31 fc := mat.Formatted(&c, mat.Prefix(" "), mat.Squeeze())
32 fmt.Printf("c = %v", fc)
40 func ExampleDense_Sub() {
41 // Initialize two matrices, a and b.
42 a := mat.NewDense(2, 2, []float64{
46 b := mat.NewDense(2, 2, []float64{
51 // Subtract b from a, placing the result into a.
54 // Print the result using the formatter.
55 fa := mat.Formatted(a, mat.Prefix(" "), mat.Squeeze())
56 fmt.Printf("a = %v", fa)
64 func ExampleDense_MulElem() {
65 // Initialize two matrices, a and b.
66 a := mat.NewDense(2, 2, []float64{
70 b := mat.NewDense(2, 2, []float64{
75 // Multiply the elements of a and b, placing the result into a.
78 // Print the result using the formatter.
79 fa := mat.Formatted(a, mat.Prefix(" "), mat.Squeeze())
80 fmt.Printf("a = %v", fa)
88 func ExampleDense_DivElem() {
89 // Initialize two matrices, a and b.
90 a := mat.NewDense(2, 2, []float64{
94 b := mat.NewDense(2, 2, []float64{
99 // Divide the elements of a by b, placing the result into a.
102 // Print the result using the formatter.
103 fa := mat.Formatted(a, mat.Prefix(" "), mat.Squeeze())
104 fmt.Printf("a = %v", fa)
112 func ExampleDense_Inverse() {
113 // Initialize two matrices, a and ia.
114 a := mat.NewDense(2, 2, []float64{
120 // Take the inverse of a and place the result in ia.
123 // Print the result using the formatter.
124 fa := mat.Formatted(&ia, mat.Prefix(" "), mat.Squeeze())
125 fmt.Printf("ia = %.2g\n\n", fa)
127 // Confirm that A * A^-1 = I
130 fr := mat.Formatted(&r, mat.Prefix(" "), mat.Squeeze())
131 fmt.Printf("r = %v\n\n", fr)
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{
146 // Print the result using the formatter.
147 fx := mat.Formatted(&x, mat.Prefix(" "), mat.Squeeze())
148 fmt.Printf("x = %v", fx)
162 func ExampleDense_Mul() {
163 // Initialize two matrices, a and b.
164 a := mat.NewDense(2, 2, []float64{
168 b := mat.NewDense(2, 3, []float64{
173 // Take the matrix product of a and b and place the result in c.
177 // Print the result using the formatter.
178 fc := mat.Formatted(&c, mat.Prefix(" "), mat.Squeeze())
179 fmt.Printf("c = %v", fc)
187 func ExampleDense_Exp() {
188 // Initialize a matrix a with some data.
189 a := mat.NewDense(2, 2, []float64{
194 // Take the exponential of the matrix and place the result in m.
198 // Print the result using the formatter.
199 fm := mat.Formatted(&m, mat.Prefix(" "), mat.Squeeze())
200 fmt.Printf("m = %4.2f", fm)
208 func ExampleDense_Pow() {
209 // Initialize a matrix with some data.
210 a := mat.NewDense(2, 2, []float64{
215 // Take the second power of matrix a and place the result in m.
219 // Print the result using the formatter.
220 fm := mat.Formatted(&m, mat.Prefix(" "), mat.Squeeze())
221 fmt.Printf("m = %v\n\n", fm)
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.
228 // Print the result using the formatter.
229 fn := mat.Formatted(&n, mat.Prefix(" "), mat.Squeeze())
230 fmt.Printf("n = %v", fn)
241 func ExampleDense_Scale() {
242 // Initialize a matrix with some data.
243 a := mat.NewDense(2, 2, []float64{
248 // Scale the matrix by a factor of 0.25 and place the result in m.
252 // Print the result using the formatter.
253 fm := mat.Formatted(&m, mat.Prefix(" "), mat.Squeeze())
254 fmt.Printf("m = %4.3f", fm)