gonum.org/v1/gonum@v0.14.0/mat/qr_example_test.go (about) 1 // Copyright ©2022 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 ExampleQR_solveTo() { 15 // QR factorization can be used for solving linear inverse problems, 16 // as this is a more numerically stable technique than direct 17 // matrix inversion. 18 // 19 // Here, we want to solve: 20 // Ax = b 21 22 var ( 23 a = mat.NewDense(4, 2, []float64{0, 1, 1, 1, 1, 1, 2, 1}) 24 b = mat.NewDense(4, 1, []float64{1, 0, 2, 1}) 25 x = mat.NewDense(2, 1, nil) 26 ) 27 28 var qr mat.QR 29 qr.Factorize(a) 30 31 err := qr.SolveTo(x, false, b) 32 if err != nil { 33 log.Fatalf("could not solve QR: %+v", err) 34 } 35 fmt.Printf("%.3f\n", mat.Formatted(x)) 36 37 // Output: 38 // ⎡0.000⎤ 39 // ⎣1.000⎦ 40 }