// SolveCholesky finds the matrix m that solves A * m = b where A = L * L^T or // A = U^T * U, and U or L are represented by t, placing the result in the // receiver. func (m *Dense) SolveCholesky(t Triangular, b Matrix) { _, n := t.Dims() bm, bn := b.Dims() if n != bm { panic(ErrShape) } m.reuseAs(bm, bn) if b != m { m.Copy(b) } // TODO(btracey): Implement an algorithm that doesn't require a copy into // a blas64.Triangular. ta := getBlasTriangular(t) switch ta.Uplo { case blas.Upper: blas64.Trsm(blas.Left, blas.Trans, 1, ta, m.mat) blas64.Trsm(blas.Left, blas.NoTrans, 1, ta, m.mat) case blas.Lower: blas64.Trsm(blas.Left, blas.NoTrans, 1, ta, m.mat) blas64.Trsm(blas.Left, blas.Trans, 1, ta, m.mat) default: panic(badTriangle) } }
// SolveTri finds the matrix x that solves op(A) * X = B where A is a triangular // matrix and op is specified by trans. func (m *Dense) SolveTri(a Triangular, trans bool, b Matrix) { n, _ := a.Triangle() bm, bn := b.Dims() if n != bm { panic(ErrShape) } m.reuseAs(bm, bn) if b != m { m.Copy(b) } // TODO(btracey): Implement an algorithm that doesn't require a copy into // a blas64.Triangular. ta := getBlasTriangular(a) t := blas.NoTrans if trans { t = blas.Trans } switch ta.Uplo { case blas.Upper, blas.Lower: blas64.Trsm(blas.Left, t, 1, ta, m.mat) default: panic(badTriangle) } }
// Solve computes minimum norm least squares solution of a.x = b where b has as many rows as a. // A matrix x is returned that minimizes the two norm of Q*R*X-B. Solve will panic // if a is not full rank. func (f LQFactor) Solve(b *Dense) (x *Dense) { lq := f.LQ lDiag := f.lDiag m, n := lq.Dims() bm, bn := b.Dims() if bm != m { panic(ErrShape) } if !f.IsFullRank() { panic(ErrSingular) } x = NewDense(n, bn, nil) x.Copy(b) tau := make([]float64, m) for i := range tau { tau[i] = lq.at(i, i) lq.set(i, i, lDiag[i]) } lqT := blas64.Triangular{ // N omitted since it is not used by Trsm. Stride: lq.mat.Stride, Data: lq.mat.Data, Uplo: blas.Lower, Diag: blas.NonUnit, } x.mat.Rows = bm blas64.Trsm(blas.Left, blas.NoTrans, 1, lqT, x.mat) x.mat.Rows = n for i := range tau { lq.set(i, i, tau[i]) } f.applyQTo(x, true) return x }