OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / lapack / testlapack / dlascl.go
1 // Copyright ©2016 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/lapack"
15 )
16
17 type Dlascler interface {
18         Dlascl(kind lapack.MatrixType, kl, ku int, cfrom, cto float64, m, n int, a []float64, lda int)
19 }
20
21 func DlasclTest(t *testing.T, impl Dlascler) {
22         const tol = 1e-16
23
24         rnd := rand.New(rand.NewSource(1))
25         for ti, test := range []struct {
26                 m, n int
27         }{
28                 {0, 0},
29                 {1, 1},
30                 {1, 10},
31                 {10, 1},
32                 {2, 2},
33                 {2, 11},
34                 {11, 2},
35                 {3, 3},
36                 {3, 11},
37                 {11, 3},
38                 {11, 11},
39                 {11, 100},
40                 {100, 11},
41         } {
42                 m := test.m
43                 n := test.n
44                 for _, extra := range []int{0, 11} {
45                         for _, kind := range []lapack.MatrixType{lapack.General, lapack.UpperTri, lapack.LowerTri} {
46                                 a := randomGeneral(m, n, n+extra, rnd)
47                                 aCopy := cloneGeneral(a)
48                                 cfrom := rnd.NormFloat64()
49                                 cto := rnd.NormFloat64()
50                                 scale := cto / cfrom
51
52                                 impl.Dlascl(kind, -1, -1, cfrom, cto, m, n, a.Data, a.Stride)
53
54                                 prefix := fmt.Sprintf("Case #%v: kind=%v,m=%v,n=%v,extra=%v", ti, kind, m, n, extra)
55                                 if !generalOutsideAllNaN(a) {
56                                         t.Errorf("%v: out-of-range write to A", prefix)
57                                 }
58                                 switch kind {
59                                 case lapack.General:
60                                         for i := 0; i < m; i++ {
61                                                 for j := 0; j < n; j++ {
62                                                         want := scale * aCopy.Data[i*aCopy.Stride+j]
63                                                         got := a.Data[i*a.Stride+j]
64                                                         if math.Abs(want-got) > tol {
65                                                                 t.Errorf("%v: unexpected A[%v,%v]=%v, want %v", prefix, i, j, got, want)
66                                                         }
67                                                 }
68                                         }
69                                 case lapack.UpperTri:
70                                         for i := 0; i < m; i++ {
71                                                 for j := i; j < n; j++ {
72                                                         want := scale * aCopy.Data[i*aCopy.Stride+j]
73                                                         got := a.Data[i*a.Stride+j]
74                                                         if math.Abs(want-got) > tol {
75                                                                 t.Errorf("%v: unexpected A[%v,%v]=%v, want %v", prefix, i, j, got, want)
76                                                         }
77                                                 }
78                                         }
79                                         for i := 0; i < m; i++ {
80                                                 for j := 0; j < min(i, n); j++ {
81                                                         if a.Data[i*a.Stride+j] != aCopy.Data[i*aCopy.Stride+j] {
82                                                                 t.Errorf("%v: unexpected modification in lower triangle of A", prefix)
83                                                         }
84                                                 }
85                                         }
86                                 case lapack.LowerTri:
87                                         for i := 0; i < m; i++ {
88                                                 for j := 0; j <= min(i, n-1); j++ {
89                                                         want := scale * aCopy.Data[i*aCopy.Stride+j]
90                                                         got := a.Data[i*a.Stride+j]
91                                                         if math.Abs(want-got) > tol {
92                                                                 t.Errorf("%v: unexpected A[%v,%v]=%v, want %v", prefix, i, j, got, want)
93                                                         }
94                                                 }
95                                         }
96                                         for i := 0; i < m; i++ {
97                                                 for j := i + 1; j < n; j++ {
98                                                         if a.Data[i*a.Stride+j] != aCopy.Data[i*aCopy.Stride+j] {
99                                                                 t.Errorf("%v: unexpected modification in upper triangle of A", prefix)
100                                                         }
101                                                 }
102                                         }
103                                 }
104                         }
105                 }
106         }
107 }