From 42f8ea225ed9b1bc84948819e95f2406b4048a9b Mon Sep 17 00:00:00 2001 From: Vladimir Chalupecky Date: Tue, 8 Nov 2016 17:33:00 +0100 Subject: [PATCH] testlapack: add test for Dlacn2 Resolves #200 --- cgo/lapack_test.go | 4 +++ native/lapack_test.go | 4 +++ testlapack/dlacn2.go | 72 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 testlapack/dlacn2.go diff --git a/cgo/lapack_test.go b/cgo/lapack_test.go index fc7fffdf..c3acb035 100644 --- a/cgo/lapack_test.go +++ b/cgo/lapack_test.go @@ -44,6 +44,10 @@ func (bl blockedTranslate) Dorgl2(m, n, k int, a []float64, lda int, tau, work [ impl.Dorglq(m, n, k, a, lda, tau, work, len(work)) } +func TestDlacn2(t *testing.T) { + testlapack.Dlacn2Test(t, impl) +} + func TestDlacpy(t *testing.T) { testlapack.DlacpyTest(t, impl) } diff --git a/native/lapack_test.go b/native/lapack_test.go index e2bb0dff..1b74da3b 100644 --- a/native/lapack_test.go +++ b/native/lapack_test.go @@ -100,6 +100,10 @@ func TestDlabrd(t *testing.T) { testlapack.DlabrdTest(t, impl) } +func TestDlacn2(t *testing.T) { + testlapack.Dlacn2Test(t, impl) +} + func TestDlacpy(t *testing.T) { testlapack.DlacpyTest(t, impl) } diff --git a/testlapack/dlacn2.go b/testlapack/dlacn2.go new file mode 100644 index 00000000..1fe99906 --- /dev/null +++ b/testlapack/dlacn2.go @@ -0,0 +1,72 @@ +// Copyright ©2016 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 ( + "math" + "math/rand" + "testing" + + "github.com/gonum/blas" + "github.com/gonum/blas/blas64" +) + +type Dlacn2er interface { + Dlacn2(n int, v, x []float64, isgn []int, est float64, kase int, isave *[3]int) (float64, int) +} + +func Dlacn2Test(t *testing.T, impl Dlacn2er) { + rnd := rand.New(rand.NewSource(1)) + for _, n := range []int{1, 2, 3, 4, 5, 7, 10, 15, 20, 100} { + for cas := 0; cas < 10; cas++ { + a := randomGeneral(n, n, n, rnd) + + // Compute the 1-norm of A explicitly. + var norm1 float64 + for j := 0; j < n; j++ { + var sum float64 + for i := 0; i < n; i++ { + sum += math.Abs(a.Data[i*a.Stride+j]) + } + if sum > norm1 { + norm1 = sum + } + } + + // Compute the estimate of 1-norm using Dlanc2. + x := make([]float64, n) + work := make([]float64, n) + v := make([]float64, n) + isgn := make([]int, n) + var ( + kase int + isave [3]int + got float64 + ) + loop: + for { + got, kase = impl.Dlacn2(n, v, x, isgn, got, kase, &isave) + switch kase { + default: + panic("Dlacn2 returned invalid value of kase") + case 0: + break loop + case 1: + blas64.Gemv(blas.NoTrans, 1, a, blas64.Vector{1, x}, 0, blas64.Vector{1, work}) + copy(x, work) + case 2: + blas64.Gemv(blas.Trans, 1, a, blas64.Vector{1, x}, 0, blas64.Vector{1, work}) + copy(x, work) + } + } + + // Check that got is either accurate enough or a + // lower estimate of the 1-norm of A. + if math.Abs(got-norm1) > 1e-8 && got > norm1 { + t.Errorf("Case n=%v: not lower estimate. 1-norm %v, estimate %v", n, norm1, got) + } + } + } +}