func main() { m := len(yGiven) n := degree + 1 y := matrix.MakeDenseMatrix(yGiven, m, 1) x := matrix.Zeros(m, n) for i := 0; i < m; i++ { ip := float64(1) for j := 0; j < n; j++ { x.Set(i, j, ip) ip *= xGiven[i] } } q, r := x.QR() qty, err := q.Transpose().Times(y) if err != nil { fmt.Println(err) return } c := make([]float64, n) for i := n - 1; i >= 0; i-- { c[i] = qty.Get(i, 0) for j := i + 1; j < n; j++ { c[i] -= c[j] * r.Get(i, j) } c[i] /= r.Get(i, i) } fmt.Println(c) }
func polyfit(x, y *matrix.DenseMatrix, n int) *matrix.DenseMatrix { m := x.Cols() a := matrix.Zeros(m, n+1) for i := 0; i < m; i++ { for j := 0; j <= n; j++ { a.Set(i, j, math.Pow(x.Get(0, i), float64(j))) } } return lsqr(a, y.Transpose()) }
func solveUT(r, b *matrix.DenseMatrix) *matrix.DenseMatrix { n := r.Cols() x := matrix.Zeros(n, 1) for k := n - 1; k >= 0; k-- { sum := 0. for j := k + 1; j < n; j++ { sum += r.Get(k, j) * x.Get(j, 0) } x.Set(k, 0, (b.Get(k, 0)-sum)/r.Get(k, k)) } return x }
func givens() (x, y *matrix.DenseMatrix) { height := []float64{1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83} weight := []float64{52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46} m := len(height) n := 3 y = matrix.MakeDenseMatrix(weight, m, 1) x = matrix.Zeros(m, n) for i := 0; i < m; i++ { ip := float64(1) for j := 0; j < n; j++ { x.Set(i, j, ip) ip *= height[i] } } return }
func unitVector(n int) *matrix.DenseMatrix { vec := matrix.Zeros(n, 1) vec.Set(0, 0, 1) return vec }