testlapack: add isIdentity helper
MODIFY
lapack/testlapack/dgetri.go
lapack/testlapack/dgetri.go
package testlapack
import (
- "math"
"testing"
"golang.org/x/exp/rand"
@@ -67,23 +66,8 @@ func DgetriTest(t *testing.T, impl Dgetrier) {
// Check that A(inv) * A = I.
ans := make([]float64, len(a))
bi.Dgemm(blas.NoTrans, blas.NoTrans, n, n, n, 1, aCopy, lda, a, lda, 0, ans, lda)
- isEye := true
- for i := 0; i < n; i++ {
- for j := 0; j < n; j++ {
- if i == j {
- // This tolerance is so high because computing matrix inverses
- // is very unstable.
- if math.Abs(ans[i*lda+j]-1) > 5e-2 {
- isEye = false
- }
- } else {
- if math.Abs(ans[i*lda+j]) > 5e-2 {
- isEye = false
- }
- }
- }
- }
- if !isEye {
+ // The tolerance is so high because computing matrix inverses is very unstable.
+ if !isIdentity(n, ans, lda, 5e-2) {
t.Errorf("Inv(A) * A != I. n = %v, lda = %v", n, lda)
}
}

MODIFY
lapack/testlapack/dlarfg.go
lapack/testlapack/dlarfg.go
@@ -104,21 +104,7 @@ func DlarfgTest(t *testing.T, impl Dlarfger) {
Data: make([]float64, n*n),
}
blas64.Gemm(blas.Trans, blas.NoTrans, 1, hmat, hmat, 0, eye)
- iseye := true
- for i := 0; i < n; i++ {
- for j := 0; j < n; j++ {
- if i == j {
- if math.Abs(eye.Data[i*n+j]-1) > 1e-14 {
- iseye = false
- }
- } else {
- if math.Abs(eye.Data[i*n+j]) > 1e-14 {
- iseye = false
- }
- }
- }
- }
- if !iseye {
+ if !isIdentity(n, eye.Data, n, 1e-14) {
t.Errorf("H^T * H is not I %v", eye)
}

MODIFY
lapack/testlapack/dtrti2.go
lapack/testlapack/dtrti2.go
package testlapack
import (
- "math"
"testing"
"golang.org/x/exp/rand"
@@ -149,23 +148,7 @@ func Dtrti2Test(t *testing.T, impl Dtrti2er) {
ans := make([]float64, len(a))
bi.Dgemm(blas.NoTrans, blas.NoTrans, n, n, n, 1, a, lda, aCopy, lda, 0, ans, lda)
// Check that ans is the identity matrix.
- iseye := true
- for i := 0; i < n; i++ {
- for j := 0; j < n; j++ {
- if i == j {
- if math.Abs(ans[i*lda+i]-1) > tol {
- iseye = false
- break
- }
- } else {
- if math.Abs(ans[i*lda+j]) > tol {
- iseye = false
- break
- }
- }
- }
- }
- if !iseye {
+ if !isIdentity(n, ans, lda, tol) {
t.Errorf("inv(A) * A != I. Upper = %v, unit = %v, ans = %v", uplo == blas.Upper, diag == blas.Unit, ans)
}
}

MODIFY
lapack/testlapack/dtrtri.go
lapack/testlapack/dtrtri.go
package testlapack
import (
- "math"
"testing"
"golang.org/x/exp/rand"
@@ -80,23 +79,7 @@ func DtrtriTest(t *testing.T, impl Dtrtrier) {
ans := make([]float64, len(a))
bi.Dgemm(blas.NoTrans, blas.NoTrans, n, n, n, 1, a, lda, aCopy, lda, 0, ans, lda)
// Check that ans is the identity matrix.
- iseye := true
- for i := 0; i < n; i++ {
- for j := 0; j < n; j++ {
- if i == j {
- if math.Abs(ans[i*lda+i]-1) > tol {
- iseye = false
- break
- }
- } else {
- if math.Abs(ans[i*lda+j]) > tol {
- iseye = false
- break
- }
- }
- }
- }
- if !iseye {
+ if !isIdentity(n, ans, lda, tol) {
t.Errorf("inv(A) * A != I. Upper = %v, unit = %v, n = %v, lda = %v",
uplo == blas.Upper, diag == blas.Unit, n, lda)
}

testlapack: add isIdentity helper