Jump to content

QR decomposition: Difference between revisions

→‎Library QR, gonum/matrix: more library changes
(→‎Library QR, gonum/matrix: update for library changes, also reorganize code a bit.)
(→‎Library QR, gonum/matrix: more library changes)
Line 1,116:
 
===Library QR, gonum/matrix===
<lang go>importpackage (main
 
import (
"fmt"
 
Line 1,129 ⟶ 1,131:
-4, 24, -41,
})
fvar :=qr mat64.QR(a)
qr.Factorize(a)
fmt.Printf("q: %.3f\n\n", mat64.Formatted(f.Q(), mat64.Prefix(" ")))
var q, r mat64.Dense
fmt.Printf("r: %.3f\n\n", mat64.Formatted(f.R(), mat64.Prefix(" ")))
q.QFromQR(&qr)
r.RFromQR(&qr)
fmt.Printf("q: %.3f\n\n", mat64.Formatted(f.Q()&q, mat64.Prefix(" ")))
fmt.Printf("r: %.3f\n\n", mat64.Formatted(f.R()&r, mat64.Prefix(" ")))
 
// task 2: use qr decomp for polynomial regression example
Line 1,138 ⟶ 1,144:
a = Vandermonde(x, 2)
b := mat64.NewDense(11, 1, y)
qr.Factorize(a)
var f mat64.Dense
f.SolveQR(&qr, false, b)
fmt.Printf("polyfit: %.3f\n",
mat64.Formatted(mat64.QR(a).Solve(b)&f, mat64.Prefix(" ")))
}
 
Line 1,153 ⟶ 1,162:
{{out}}
<pre>
q: ⎡-0.857 0.394 - 0.331⎤
⎢-0.429 -0.903 -0.034⎥
⎣ 0.286 -0.171 - 0.943⎦
 
r: ⎡ -14.000 -21.000 14.000⎤
⎢ 0.000 -175.000 70.000⎥
⎣ 0.000 0.000 -35.000⎦
 
polyfit: ⎡1.000⎤
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.