From f08ea468d690b524286d156324f8795b7e64b198 Mon Sep 17 00:00:00 2001 From: Vladimir Chalupecky Date: Wed, 19 Dec 2018 17:22:32 +0100 Subject: [PATCH] lapack/testlapack: add tests for Dlauu2 and Dlauum --- lapack/gonum/lapack_test.go | 8 +++ lapack/testlapack/dlauu2.go | 112 ++++++++++++++++++++++++++++++++++++ lapack/testlapack/dlauum.go | 30 ++++++++++ 3 files changed, 150 insertions(+) create mode 100644 lapack/testlapack/dlauu2.go create mode 100644 lapack/testlapack/dlauum.go diff --git a/lapack/gonum/lapack_test.go b/lapack/gonum/lapack_test.go index eab28cef8d..5f51bf1e81 100644 --- a/lapack/gonum/lapack_test.go +++ b/lapack/gonum/lapack_test.go @@ -296,6 +296,14 @@ func TestDlatrs(t *testing.T) { testlapack.DlatrsTest(t, impl) } +func TestDlauu2(t *testing.T) { + testlapack.Dlauu2Test(t, impl) +} + +func TestDlauum(t *testing.T) { + testlapack.DlauumTest(t, impl) +} + func TestDorg2r(t *testing.T) { testlapack.Dorg2rTest(t, impl) } diff --git a/lapack/testlapack/dlauu2.go b/lapack/testlapack/dlauu2.go new file mode 100644 index 0000000000..66e04ec34d --- /dev/null +++ b/lapack/testlapack/dlauu2.go @@ -0,0 +1,112 @@ +// Copyright ©2018 The Gonum Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package testlapack + +import ( + "fmt" + "testing" + + "golang.org/x/exp/rand" + "gonum.org/v1/gonum/blas" + "gonum.org/v1/gonum/blas/blas64" +) + +type Dlauu2er interface { + Dlauu2(uplo blas.Uplo, n int, a []float64, lda int) +} + +func Dlauu2Test(t *testing.T, impl Dlauu2er) { + for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} { + name := "Upper" + if uplo == blas.Lower { + name = "Lower" + } + t.Run(name, func(t *testing.T) { + ns := []int{0, 1, 2, 3, 4, 5, 10, 25} + dlauuTest(t, impl.Dlauu2, uplo, ns) + }) + } +} + +func dlauuTest(t *testing.T, dlauu func(blas.Uplo, int, []float64, int), uplo blas.Uplo, ns []int) { + const tol = 1e-13 + + bi := blas64.Implementation() + rnd := rand.New(rand.NewSource(1)) + + for _, n := range ns { + loop: + for _, lda := range []int{max(1, n), n + 11} { + prefix := fmt.Sprintf("n=%v,lda=%v", n, lda) + + // Allocate n×n matrix A and fill it with random numbers. + // Only its uplo triangle will be used below. + a := make([]float64, n*lda) + for i := range a { + a[i] = rnd.NormFloat64() + } + // Create a copy of A. + aCopy := make([]float64, len(a)) + copy(aCopy, a) + + // Compute U*U^T or L^T*L using Dlauu?. + dlauu(uplo, n, a, lda) + + if n == 0 { + continue + } + + // * Check that the triangle opposite to uplo has not been modified. + // * Convert the result of Dlauu? into a dense symmetric matrix. + // * Zero out the triangle in aCopy opposite to uplo. + if uplo == blas.Upper { + for i := 1; i < n; i++ { + for j := 0; j < i; j++ { + if aCopy[i*lda+j] != a[i*lda+j] { + t.Errorf("%v: unexpected modification in upper triangle", prefix) + break loop + } + a[i*lda+j] = a[j*lda+i] + aCopy[i*lda+j] = 0 + } + } + } else { + for i := 0; i < n-1; i++ { + for j := i + 1; j < n; j++ { + if aCopy[i*lda+j] != a[i*lda+j] { + t.Errorf("%v: unexpected modification in lower triangle", prefix) + break loop + } + a[i*lda+j] = a[j*lda+i] + aCopy[i*lda+j] = 0 + } + } + } + + // Compute U*U^T or L^T*L using Dgemm with U and L + // represented as dense triangular matrices. + ldwant := n + want := make([]float64, n*ldwant) + if uplo == blas.Upper { + // Use aCopy as a dense representation of the upper triangular U. + u := aCopy + ldu := lda + // Compute U * U^T and store the result into want. + bi.Dgemm(blas.NoTrans, blas.Trans, n, n, n, + 1, u, ldu, u, ldu, 0, want, ldwant) + } else { + // Use aCopy as a dense representation of the lower triangular L. + l := aCopy + ldl := lda + // Compute L^T * L and store the result into want. + bi.Dgemm(blas.Trans, blas.NoTrans, n, n, n, + 1, l, ldl, l, ldl, 0, want, ldwant) + } + if !equalApprox(n, n, a, lda, want, tol) { + t.Errorf("%v: unexpected result", prefix) + } + } + } +} diff --git a/lapack/testlapack/dlauum.go b/lapack/testlapack/dlauum.go new file mode 100644 index 0000000000..604ae7d878 --- /dev/null +++ b/lapack/testlapack/dlauum.go @@ -0,0 +1,30 @@ +// Copyright ©2018 The Gonum Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package testlapack + +import ( + "testing" + + "gonum.org/v1/gonum/blas" +) + +type Dlauumer interface { + Dlauum(uplo blas.Uplo, n int, a []float64, lda int) +} + +func DlauumTest(t *testing.T, impl Dlauumer) { + for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} { + name := "Upper" + if uplo == blas.Lower { + name = "Lower" + } + t.Run(name, func(t *testing.T) { + // Include small and large sizes to make sure that both + // unblocked and blocked paths are taken. + ns := []int{0, 1, 2, 3, 4, 5, 10, 25, 31, 32, 33, 63, 64, 65, 127, 128, 129} + dlauuTest(t, impl.Dlauum, uplo, ns) + }) + } +}