mirror of
https://github.com/gonum/gonum.git
synced 2025-10-22 06:39:26 +08:00
lapack/testlapack: test Dpbtf2 like Dpbtrf
This commit is contained in:

committed by
Vladimír Chalupecký

parent
7895aa8c2a
commit
e307a7a43c
@@ -61,7 +61,7 @@ func (Implementation) Dpbtf2(uplo blas.Uplo, n, kd int, ab []float64, ldab int)
|
|||||||
|
|
||||||
// Quick return if possible.
|
// Quick return if possible.
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ab) < (n-1)*ldab+kd {
|
if len(ab) < (n-1)*ldab+kd {
|
||||||
|
@@ -5,6 +5,8 @@
|
|||||||
package testlapack
|
package testlapack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"golang.org/x/exp/rand"
|
"golang.org/x/exp/rand"
|
||||||
@@ -13,40 +15,81 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Dpbtf2er interface {
|
type Dpbtf2er interface {
|
||||||
Dpbtf2(ul blas.Uplo, n, kd int, ab []float64, ldab int) (ok bool)
|
Dpbtf2(uplo blas.Uplo, n, kd int, ab []float64, ldab int) (ok bool)
|
||||||
Dpotrfer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dpbtf2Test tests Dpbtf2 on random symmetric positive definite band matrices
|
||||||
|
// by checking that the Cholesky factors multiply back to the original matrix.
|
||||||
func Dpbtf2Test(t *testing.T, impl Dpbtf2er) {
|
func Dpbtf2Test(t *testing.T, impl Dpbtf2er) {
|
||||||
// Test random symmetric banded matrices against the full version.
|
// TODO(vladimir-ch): include expected-failure test case.
|
||||||
rnd := rand.New(rand.NewSource(1))
|
rnd := rand.New(rand.NewSource(1))
|
||||||
|
for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 20} {
|
||||||
for _, n := range []int{5, 10, 20} {
|
for _, kd := range []int{0, (n + 1) / 4, (3*n - 1) / 4, (5*n + 1) / 4} {
|
||||||
for _, kb := range []int{0, 1, 3, n - 1} {
|
for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} {
|
||||||
for _, ldoff := range []int{0, 4} {
|
for _, ldab := range []int{kd + 1, kd + 1 + 7} {
|
||||||
for _, ul := range []blas.Uplo{blas.Upper, blas.Lower} {
|
dpbtf2Test(t, impl, rnd, uplo, n, kd, ldab)
|
||||||
ldab := kb + 1 + ldoff
|
|
||||||
band, sym := randSymBand(ul, n, kb, ldab, rnd)
|
|
||||||
|
|
||||||
// Compute the Cholesky decomposition of the symmetric matrix.
|
|
||||||
ok := impl.Dpotrf(ul, sym.N, sym.Data, sym.Stride)
|
|
||||||
if !ok {
|
|
||||||
panic("bad test: symmetric cholesky decomp failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute the Cholesky decomposition of the banded matrix.
|
|
||||||
ok = impl.Dpbtf2(band.Uplo, band.N, band.K, band.Data, band.Stride)
|
|
||||||
if !ok {
|
|
||||||
t.Errorf("SymBand cholesky decomp failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare the result to the Symmetric decomposition.
|
|
||||||
sb := symBandToSym(ul, band.Data, n, kb, ldab)
|
|
||||||
if !equalApproxSymmetric(sym, sb, 1e-10) {
|
|
||||||
t.Errorf("chol mismatch banded and sym. n = %v, kb = %v, ldoff = %v", n, kb, ldoff)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dpbtf2Test(t *testing.T, impl Dpbtf2er, rnd *rand.Rand, uplo blas.Uplo, n, kd int, ldab int) {
|
||||||
|
const tol = 1e-12
|
||||||
|
|
||||||
|
name := fmt.Sprintf("uplo=%v,n=%v,kd=%v,ldab=%v", string(uplo), n, kd, ldab)
|
||||||
|
|
||||||
|
// Allocate a band matrix and fill it with random numbers.
|
||||||
|
ab := make([]float64, n*ldab)
|
||||||
|
for i := range ab {
|
||||||
|
ab[i] = rnd.NormFloat64()
|
||||||
|
}
|
||||||
|
// Make sure that the matrix U or L has a sufficiently positive diagonal.
|
||||||
|
switch uplo {
|
||||||
|
case blas.Upper:
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
ab[i*ldab] = 2 + rnd.Float64()
|
||||||
|
}
|
||||||
|
case blas.Lower:
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
ab[i*ldab+kd] = 2 + rnd.Float64()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Compute U^T*U or L*L^T. The resulting (symmetric) matrix A will be positive definite.
|
||||||
|
dsbmm(uplo, n, kd, ab, ldab)
|
||||||
|
|
||||||
|
// Compute the Cholesky decomposition of A.
|
||||||
|
abFac := make([]float64, len(ab))
|
||||||
|
copy(abFac, ab)
|
||||||
|
ok := impl.Dpbtf2(uplo, n, kd, abFac, ldab)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("%v: bad test matrix, Dpbtf2 failed", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if n == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reconstruct an symmetric band matrix from the U^T*U or L*L^T factorization, overwriting abFac.
|
||||||
|
dsbmm(uplo, n, kd, abFac, ldab)
|
||||||
|
|
||||||
|
// Compute and check the max-norm distance between the reconstructed and original matrix A.
|
||||||
|
var diff float64
|
||||||
|
switch uplo {
|
||||||
|
case blas.Upper:
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
for j := 0; j < min(kd+1, n-i); j++ {
|
||||||
|
diff = math.Max(diff, math.Abs(abFac[i*ldab+j]-ab[i*ldab+j]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case blas.Lower:
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
for j := max(0, kd-i); j < kd+1; j++ {
|
||||||
|
diff = math.Max(diff, math.Abs(abFac[i*ldab+j]-ab[i*ldab+j]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if diff > tol {
|
||||||
|
t.Errorf("%v: unexpected result, diff=%v", name, diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user