testlapack: add isIdentity helper

This commit is contained in:
Vladimir Chalupecky
2019-01-03 23:34:53 +01:00
committed by Vladimír Chalupecký
parent 27d556d1f9
commit 87489715e5
6 changed files with 39 additions and 100 deletions

View File

@@ -5,7 +5,6 @@
package testlapack
import (
"math"
"testing"
"golang.org/x/exp/rand"
@@ -21,40 +20,19 @@ func TestDlagsy(t *testing.T) {
if lda == 0 {
lda = max(1, n)
}
// D is the identity matrix I.
d := make([]float64, n)
for i := range d {
d[i] = 1
}
a := blas64.General{
Rows: n,
Cols: n,
Stride: lda,
Data: nanSlice(n * lda),
}
work := make([]float64, a.Rows+a.Cols)
Dlagsy(a.Rows, 0, d, a.Data, a.Stride, rnd, work)
isIdentity := true
identityLoop:
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
aij := a.Data[i*a.Stride+j]
if math.IsNaN(aij) {
isIdentity = false
}
if i == j && math.Abs(aij-1) > tol {
isIdentity = false
}
if i != j && math.Abs(aij) > tol {
isIdentity = false
}
if !isIdentity {
break identityLoop
}
}
}
if !isIdentity {
// Allocate an n×n symmetric matrix A and fill it with NaNs.
a := nanSlice(n * lda)
work := make([]float64, 2*n)
// Compute A = U * D * U^T where U is a random orthogonal matrix.
Dlagsy(n, 0, d, a, lda, rnd, work)
// A should be the identity matrix because
// A = U * D * U^T = U * I * U^T = U * U^T = I.
if !isIdentity(n, a, lda, tol) {
t.Errorf("Case n=%v,lda=%v: unexpected result", n, lda)
}
}