|
| 1 | +// Copyright ©2021 The Gonum Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package testlapack |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "math" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "golang.org/x/exp/rand" |
| 13 | + |
| 14 | + "gonum.org/v1/gonum/blas" |
| 15 | + "gonum.org/v1/gonum/blas/blas64" |
| 16 | +) |
| 17 | + |
| 18 | +type Dgetc2er interface { |
| 19 | + Dgetc2(n int, a []float64, lda int, ipiv, jpiv []int) (k int) |
| 20 | +} |
| 21 | + |
| 22 | +func Dgetc2Test(t *testing.T, impl Dgetc2er) { |
| 23 | + const tol = 1e-12 |
| 24 | + rnd := rand.New(rand.NewSource(1)) |
| 25 | + for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 20} { |
| 26 | + for _, lda := range []int{n, n + 5} { |
| 27 | + dgetc2Test(t, impl, rnd, n, lda, tol) |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func dgetc2Test(t *testing.T, impl Dgetc2er, rnd *rand.Rand, n, lda int, tol float64) { |
| 33 | + name := fmt.Sprintf("n=%v,lda=%v", n, lda) |
| 34 | + if lda == 0 { |
| 35 | + lda = 1 |
| 36 | + } |
| 37 | + // Generate a random general matrix A. |
| 38 | + a := randomGeneral(n, n, lda, rnd) |
| 39 | + // ipiv and jpiv are outputs. |
| 40 | + ipiv := make([]int, n) |
| 41 | + jpiv := make([]int, n) |
| 42 | + for i := 0; i < n; i++ { |
| 43 | + ipiv[i], jpiv[i] = -1, -1 // Set to non-indices. |
| 44 | + } |
| 45 | + // Copy to store output (LU decomposition). |
| 46 | + lu := cloneGeneral(a) |
| 47 | + k := impl.Dgetc2(n, lu.Data, lu.Stride, ipiv, jpiv) |
| 48 | + if k >= 0 { |
| 49 | + t.Logf("%v: matrix was perturbed at %d", name, k) |
| 50 | + } |
| 51 | + |
| 52 | + // Verify all indices are set. |
| 53 | + for i := 0; i < n; i++ { |
| 54 | + if ipiv[i] < 0 { |
| 55 | + t.Errorf("%v: ipiv[%d] is negative", name, i) |
| 56 | + } |
| 57 | + if jpiv[i] < 0 { |
| 58 | + t.Errorf("%v: jpiv[%d] is negative", name, i) |
| 59 | + } |
| 60 | + } |
| 61 | + bi := blas64.Implementation() |
| 62 | + // Construct L and U triangular matrices from Dgetc2 output. |
| 63 | + L := zeros(n, n, lda) |
| 64 | + U := zeros(n, n, lda) |
| 65 | + for i := 0; i < n; i++ { |
| 66 | + for j := 0; j < n; j++ { |
| 67 | + idx := i*lda + j |
| 68 | + if j >= i { // On upper triangle and setting of L's unit diagonal elements. |
| 69 | + U.Data[idx] = lu.Data[idx] |
| 70 | + if j == i { |
| 71 | + L.Data[idx] = 1.0 |
| 72 | + } |
| 73 | + } else if i > j { // On diagonal or lower triangle. |
| 74 | + L.Data[idx] = lu.Data[idx] |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + work := zeros(n, n, lda) |
| 79 | + bi.Dgemm(blas.NoTrans, blas.NoTrans, n, n, n, 1, L.Data, L.Stride, U.Data, U.Stride, 0, work.Data, work.Stride) |
| 80 | + |
| 81 | + // Apply Permutations P and Q to L*U. |
| 82 | + for i := n - 1; i >= 0; i-- { |
| 83 | + ipv, jpv := ipiv[i], jpiv[i] |
| 84 | + if ipv != i { |
| 85 | + bi.Dswap(n, work.Data[i*lda:], 1, work.Data[ipv*lda:], 1) |
| 86 | + } |
| 87 | + if jpv != i { |
| 88 | + bi.Dswap(n, work.Data[i:], work.Stride, work.Data[jpv:], work.Stride) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // A should be reconstructed by now. |
| 93 | + for i := range work.Data { |
| 94 | + if math.Abs(work.Data[i]-a.Data[i]) > tol { |
| 95 | + t.Errorf("%v: matrix %d idx not equal after reconstruction. got %g, expected %g", name, i, work.Data[i], a.Data[i]) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments