mirror of
https://github.com/gonum/gonum.git
synced 2025-10-06 23:52:47 +08:00
mat: move SolveQR* onto QR
This commit is contained in:
14
mat/qr.go
14
mat/qr.go
@@ -117,7 +117,7 @@ func (qr *QR) QTo(dst *Dense) *Dense {
|
|||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
// SolveQR finds a minimum-norm solution to a system of linear equations defined
|
// Solve finds a minimum-norm solution to a system of linear equations defined
|
||||||
// by the matrices A and b, where A is an m×n matrix represented in its QR factorized
|
// by the matrices A and b, where A is an m×n matrix represented in its QR factorized
|
||||||
// form. If A is singular or near-singular a Condition error is returned. Please
|
// form. If A is singular or near-singular a Condition error is returned. Please
|
||||||
// see the documentation for Condition for more information.
|
// see the documentation for Condition for more information.
|
||||||
@@ -125,8 +125,8 @@ func (qr *QR) QTo(dst *Dense) *Dense {
|
|||||||
// The minimization problem solved depends on the input parameters.
|
// The minimization problem solved depends on the input parameters.
|
||||||
// If trans == false, find X such that ||A*X - b||_2 is minimized.
|
// If trans == false, find X such that ||A*X - b||_2 is minimized.
|
||||||
// If trans == true, find the minimum norm solution of A^T * X = b.
|
// If trans == true, find the minimum norm solution of A^T * X = b.
|
||||||
// The solution matrix, X, is stored in place into the receiver.
|
// The solution matrix, X, is stored in place into m.
|
||||||
func (m *Dense) SolveQR(qr *QR, trans bool, b Matrix) error {
|
func (qr *QR) Solve(m *Dense, trans bool, b Matrix) error {
|
||||||
r, c := qr.qr.Dims()
|
r, c := qr.qr.Dims()
|
||||||
br, bc := b.Dims()
|
br, bc := b.Dims()
|
||||||
|
|
||||||
@@ -184,9 +184,9 @@ func (m *Dense) SolveQR(qr *QR, trans bool, b Matrix) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SolveQRVec finds a minimum-norm solution to a system of linear equations.
|
// SolveVec finds a minimum-norm solution to a system of linear equations.
|
||||||
// Please see Dense.SolveQR for the full documentation.
|
// Please see QR.Solve for the full documentation.
|
||||||
func (v *Vector) SolveQRVec(qr *QR, trans bool, b *Vector) error {
|
func (qr *QR) SolveVec(v *Vector, trans bool, b *Vector) error {
|
||||||
if v != b {
|
if v != b {
|
||||||
v.checkOverlap(b.mat)
|
v.checkOverlap(b.mat)
|
||||||
}
|
}
|
||||||
@@ -198,5 +198,5 @@ func (v *Vector) SolveQRVec(qr *QR, trans bool, b *Vector) error {
|
|||||||
} else {
|
} else {
|
||||||
v.reuseAs(c)
|
v.reuseAs(c)
|
||||||
}
|
}
|
||||||
return v.asDense().SolveQR(qr, trans, b.asDense())
|
return qr.Solve(v.asDense(), trans, b.asDense())
|
||||||
}
|
}
|
||||||
|
@@ -101,9 +101,9 @@ func TestSolveQR(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
var x Dense
|
var x Dense
|
||||||
qr := &QR{}
|
var qr QR
|
||||||
qr.Factorize(a)
|
qr.Factorize(a)
|
||||||
x.SolveQR(qr, trans, b)
|
qr.Solve(&x, trans, b)
|
||||||
|
|
||||||
// Test that the normal equations hold.
|
// Test that the normal equations hold.
|
||||||
// A^T * A * x = A^T * b if !trans
|
// A^T * A * x = A^T * b if !trans
|
||||||
@@ -154,9 +154,9 @@ func TestSolveQRVec(t *testing.T) {
|
|||||||
b.SetVec(i, rand.Float64())
|
b.SetVec(i, rand.Float64())
|
||||||
}
|
}
|
||||||
var x Vector
|
var x Vector
|
||||||
qr := &QR{}
|
var qr QR
|
||||||
qr.Factorize(a)
|
qr.Factorize(a)
|
||||||
x.SolveQRVec(qr, trans, b)
|
qr.SolveVec(&x, trans, b)
|
||||||
|
|
||||||
// Test that the normal equations hold.
|
// Test that the normal equations hold.
|
||||||
// A^T * A * x = A^T * b if !trans
|
// A^T * A * x = A^T * b if !trans
|
||||||
@@ -192,13 +192,13 @@ func TestSolveQRCond(t *testing.T) {
|
|||||||
qr.Factorize(test)
|
qr.Factorize(test)
|
||||||
b := NewDense(m, 2, nil)
|
b := NewDense(m, 2, nil)
|
||||||
var x Dense
|
var x Dense
|
||||||
if err := x.SolveQR(&qr, false, b); err == nil {
|
if err := qr.Solve(&x, false, b); err == nil {
|
||||||
t.Error("No error for near-singular matrix in matrix solve.")
|
t.Error("No error for near-singular matrix in matrix solve.")
|
||||||
}
|
}
|
||||||
|
|
||||||
bvec := NewVector(m, nil)
|
bvec := NewVector(m, nil)
|
||||||
var xvec Vector
|
var xvec Vector
|
||||||
if err := xvec.SolveQRVec(&qr, false, bvec); err == nil {
|
if err := qr.SolveVec(&xvec, false, bvec); err == nil {
|
||||||
t.Error("No error for near-singular matrix in matrix solve.")
|
t.Error("No error for near-singular matrix in matrix solve.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -95,7 +95,7 @@ func (m *Dense) Solve(a, b Matrix) error {
|
|||||||
case ar > ac:
|
case ar > ac:
|
||||||
var qr QR
|
var qr QR
|
||||||
qr.Factorize(a)
|
qr.Factorize(a)
|
||||||
return m.SolveQR(&qr, false, b)
|
return qr.Solve(m, false, b)
|
||||||
default:
|
default:
|
||||||
var lq LQ
|
var lq LQ
|
||||||
lq.Factorize(a)
|
lq.Factorize(a)
|
||||||
|
Reference in New Issue
Block a user