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