lapack/testlapack: simplify randSymBand and use it in Dpb* tests

This commit is contained in:
Vladimir Chalupecky
2019-06-16 18:51:40 +02:00
committed by Vladimír Chalupecký
parent e307a7a43c
commit ce6986a678
4 changed files with 50 additions and 145 deletions

View File

@@ -6,7 +6,6 @@ package testlapack
import (
"fmt"
"math"
"testing"
"golang.org/x/exp/rand"
@@ -44,24 +43,8 @@ func dpbtrfTest(t *testing.T, impl Dpbtrfer, uplo blas.Uplo, n, kd int, ldab int
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)
// Generate a random symmetric positive definite band matrix.
ab := randSymBand(uplo, n, kd, ldab, rnd)
// Compute the Cholesky decomposition of A.
abFac := make([]float64, len(ab))
@@ -71,31 +54,13 @@ func dpbtrfTest(t *testing.T, impl Dpbtrfer, uplo blas.Uplo, n, kd int, ldab int
t.Fatalf("%v: bad test matrix, Dpbtrf 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)
dist := distSymBand(uplo, n, kd, abFac, ldab, ab, ldab)
if dist > tol*float64(n) {
t.Errorf("%v: unexpected result, diff=%v", name, dist)
}
}