Polynomial regression: Difference between revisions

→‎{{header|Go}}: add gonum/matrix example
No edit summary
(→‎{{header|Go}}: add gonum/matrix example)
Line 628:
 
=={{header|Go}}==
===Library gonum/matrix===
<lang go>package main
 
import (
"fmt"
 
"github.com/gonum/matrix/mat64"
)
 
var (
x = []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
y = []float64{1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321}
 
degree = 2
)
 
func main() {
a := Vandermonde(x, 2)
b := mat64.NewDense(11, 1, y)
fmt.Printf("%.3f\n", mat64.Formatted(mat64.QR(a).Solve(b)))
}
 
func Vandermonde(a []float64, degree int) *mat64.Dense {
x := mat64.NewDense(len(a), degree+1, nil)
for i := range a {
for j, p := 0, 1.; j <= degree; j, p = j+1, p*a[i] {
x.Set(i, j, p)
}
}
return x
}</lang>
{{out}}
<pre>
⎡1.000⎤
⎢2.000⎥
⎣3.000⎦
</pre>
 
===Library go.matrix===
Least squares solution using QR decomposition and package [http://github.com/skelterjohn/go.matrix go.matrix].
<lang go>package main
1,707

edits