Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
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())
}
Ejemplo n.º 3
0
func householder(a *matrix.DenseMatrix) *matrix.DenseMatrix {
	m := a.Rows()
	s := sign(a.Get(0, 0))
	e := unitVector(m)
	u := matrix.Sum(a, matrix.Scaled(e, a.TwoNorm()*s))
	v := matrix.Scaled(u, 1/u.Get(0, 0))
	// (error checking skipped in this solution)
	prod, _ := v.Transpose().TimesDense(v)
	β := 2 / prod.Get(0, 0)

	prod, _ = v.TimesDense(v.Transpose())
	return matrix.Difference(matrix.Eye(m), matrix.Scaled(prod, β))
}
Ejemplo n.º 4
0
func qr(a *matrix.DenseMatrix) (q, r *matrix.DenseMatrix) {
	m := a.Rows()
	n := a.Cols()
	q = matrix.Eye(m)

	last := n - 1
	if m == n {
		last--
	}
	for i := 0; i <= last; i++ {
		// (copy is only for compatibility with an older version of gomatrix)
		b := a.GetMatrix(i, i, m-i, n-i).Copy()
		x := b.GetColVector(0)
		h := matrix.Eye(m)
		h.SetMatrix(i, i, householder(x))
		q, _ = q.TimesDense(h)
		a, _ = h.TimesDense(a)
	}
	return q, a
}