lapack/testlapack: simplify randSymBand and use it in Dpb* tests
MODIFY
lapack/testlapack/dpbtf2.go
lapack/testlapack/dpbtf2.go
@@ -6,7 +6,6 @@ package testlapack
import (
"fmt"
- "math"
"testing"
"golang.org/x/exp/rand"
@@ -39,24 +38,8 @@ func dpbtf2Test(t *testing.T, impl Dpbtf2er, rnd *rand.Rand, uplo blas.Uplo, n,
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))
@@ -66,30 +49,12 @@ func dpbtf2Test(t *testing.T, impl Dpbtf2er, rnd *rand.Rand, uplo blas.Uplo, n,
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)
+ dist := distSymBand(uplo, n, kd, abFac, ldab, ab, ldab)
+ if dist > tol {
+ t.Errorf("%v: unexpected result, diff=%v", name, dist)
}
}

MODIFY
lapack/testlapack/dpbtrf.go
lapack/testlapack/dpbtrf.go
@@ -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)
}
}

MODIFY
lapack/testlapack/dpbtrs.go
lapack/testlapack/dpbtrs.go
@@ -46,25 +46,8 @@ func dpbtrsTest(t *testing.T, impl Dpbtrser, rnd *rand.Rand, uplo blas.Uplo, n,
name := fmt.Sprintf("uplo=%v,n=%v,kd=%v,nrhs=%v,ldab=%v,ldb=%v", string(uplo), n, kd, nrhs, ldab, ldb)
- // 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] = float64(n) + rnd.Float64()
- }
- case blas.Lower:
- for i := 0; i < n; i++ {
- ab[i*ldab+kd] = float64(n) + rnd.Float64()
- }
- }
- // Compute U^T*U or L*L^T. The resulting (symmetric) matrix A will be
- // positive definite and well-conditioned.
- 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))

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