OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / blas / testblas / common_test.go
1 package testblas
2
3 import (
4         "math"
5         "math/cmplx"
6         "testing"
7
8         "golang.org/x/exp/rand"
9
10         "gonum.org/v1/gonum/blas"
11         "gonum.org/v1/gonum/floats"
12 )
13
14 func TestFlattenBanded(t *testing.T) {
15         for i, test := range []struct {
16                 dense     [][]float64
17                 ku        int
18                 kl        int
19                 condensed [][]float64
20         }{
21                 {
22                         dense:     [][]float64{{3}},
23                         ku:        0,
24                         kl:        0,
25                         condensed: [][]float64{{3}},
26                 },
27                 {
28                         dense: [][]float64{
29                                 {3, 4, 0},
30                         },
31                         ku: 1,
32                         kl: 0,
33                         condensed: [][]float64{
34                                 {3, 4},
35                         },
36                 },
37                 {
38                         dense: [][]float64{
39                                 {3, 4, 0, 0, 0},
40                         },
41                         ku: 1,
42                         kl: 0,
43                         condensed: [][]float64{
44                                 {3, 4},
45                         },
46                 },
47                 {
48                         dense: [][]float64{
49                                 {3, 4, 0},
50                                 {0, 5, 8},
51                                 {0, 0, 2},
52                                 {0, 0, 0},
53                                 {0, 0, 0},
54                         },
55                         ku: 1,
56                         kl: 0,
57                         condensed: [][]float64{
58                                 {3, 4},
59                                 {5, 8},
60                                 {2, math.NaN()},
61                                 {math.NaN(), math.NaN()},
62                                 {math.NaN(), math.NaN()},
63                         },
64                 },
65                 {
66                         dense: [][]float64{
67                                 {3, 4, 6},
68                                 {0, 5, 8},
69                                 {0, 0, 2},
70                                 {0, 0, 0},
71                                 {0, 0, 0},
72                         },
73                         ku: 2,
74                         kl: 0,
75                         condensed: [][]float64{
76                                 {3, 4, 6},
77                                 {5, 8, math.NaN()},
78                                 {2, math.NaN(), math.NaN()},
79                                 {math.NaN(), math.NaN(), math.NaN()},
80                                 {math.NaN(), math.NaN(), math.NaN()},
81                         },
82                 },
83                 {
84                         dense: [][]float64{
85                                 {3, 4, 6},
86                                 {1, 5, 8},
87                                 {0, 6, 2},
88                                 {0, 0, 7},
89                                 {0, 0, 0},
90                         },
91                         ku: 2,
92                         kl: 1,
93                         condensed: [][]float64{
94                                 {math.NaN(), 3, 4, 6},
95                                 {1, 5, 8, math.NaN()},
96                                 {6, 2, math.NaN(), math.NaN()},
97                                 {7, math.NaN(), math.NaN(), math.NaN()},
98                                 {math.NaN(), math.NaN(), math.NaN(), math.NaN()},
99                         },
100                 },
101                 {
102                         dense: [][]float64{
103                                 {1, 2, 0},
104                                 {3, 4, 5},
105                                 {6, 7, 8},
106                                 {0, 9, 10},
107                                 {0, 0, 11},
108                         },
109                         ku: 1,
110                         kl: 2,
111                         condensed: [][]float64{
112                                 {math.NaN(), math.NaN(), 1, 2},
113                                 {math.NaN(), 3, 4, 5},
114                                 {6, 7, 8, math.NaN()},
115                                 {9, 10, math.NaN(), math.NaN()},
116                                 {11, math.NaN(), math.NaN(), math.NaN()},
117                         },
118                 },
119                 {
120                         dense: [][]float64{
121                                 {1, 0, 0},
122                                 {3, 4, 0},
123                                 {6, 7, 8},
124                                 {0, 9, 10},
125                                 {0, 0, 11},
126                         },
127                         ku: 0,
128                         kl: 2,
129                         condensed: [][]float64{
130                                 {math.NaN(), math.NaN(), 1},
131                                 {math.NaN(), 3, 4},
132                                 {6, 7, 8},
133                                 {9, 10, math.NaN()},
134                                 {11, math.NaN(), math.NaN()},
135                         },
136                 },
137                 {
138                         dense: [][]float64{
139                                 {1, 0, 0, 0, 0},
140                                 {3, 4, 0, 0, 0},
141                                 {1, 3, 5, 0, 0},
142                         },
143                         ku: 0,
144                         kl: 2,
145                         condensed: [][]float64{
146                                 {math.NaN(), math.NaN(), 1},
147                                 {math.NaN(), 3, 4},
148                                 {1, 3, 5},
149                         },
150                 },
151         } {
152                 condensed := flattenBanded(test.dense, test.ku, test.kl)
153                 correct := flatten(test.condensed)
154                 if !floats.Same(condensed, correct) {
155                         t.Errorf("Case %v mismatch. Want %v, got %v.", i, correct, condensed)
156                 }
157         }
158 }
159
160 func TestFlattenTriangular(t *testing.T) {
161         for i, test := range []struct {
162                 a   [][]float64
163                 ans []float64
164                 ul  blas.Uplo
165         }{
166                 {
167                         a: [][]float64{
168                                 {1, 2, 3},
169                                 {0, 4, 5},
170                                 {0, 0, 6},
171                         },
172                         ul:  blas.Upper,
173                         ans: []float64{1, 2, 3, 4, 5, 6},
174                 },
175                 {
176                         a: [][]float64{
177                                 {1, 0, 0},
178                                 {2, 3, 0},
179                                 {4, 5, 6},
180                         },
181                         ul:  blas.Lower,
182                         ans: []float64{1, 2, 3, 4, 5, 6},
183                 },
184         } {
185                 a := flattenTriangular(test.a, test.ul)
186                 if !floats.Equal(a, test.ans) {
187                         t.Errorf("Case %v. Want %v, got %v.", i, test.ans, a)
188                 }
189         }
190 }
191
192 func TestPackUnpackAsHermitian(t *testing.T) {
193         rnd := rand.New(rand.NewSource(1))
194         for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} {
195                 for _, n := range []int{1, 2, 5, 50} {
196                         for _, lda := range []int{max(1, n), n + 11} {
197                                 a := makeZGeneral(nil, n, n, lda)
198                                 for i := 0; i < n; i++ {
199                                         for j := i; j < n; j++ {
200                                                 a[i*lda+j] = complex(rnd.NormFloat64(), rnd.NormFloat64())
201                                                 if i != j {
202                                                         a[j*lda+i] = cmplx.Conj(a[i*lda+j])
203                                                 }
204                                         }
205                                 }
206                                 aCopy := make([]complex128, len(a))
207                                 copy(aCopy, a)
208
209                                 ap := zPack(uplo, n, a, lda)
210                                 if !zsame(a, aCopy) {
211                                         t.Errorf("Case uplo=%v,n=%v,lda=%v: zPack modified a", uplo, n, lda)
212                                 }
213
214                                 apCopy := make([]complex128, len(ap))
215                                 copy(apCopy, ap)
216
217                                 art := zUnpackAsHermitian(uplo, n, ap)
218                                 if !zsame(ap, apCopy) {
219                                         t.Errorf("Case uplo=%v,n=%v,lda=%v: zUnpackAsHermitian modified ap", uplo, n, lda)
220                                 }
221
222                                 // Copy the round-tripped A into a matrix with the same stride
223                                 // as the original.
224                                 got := makeZGeneral(nil, n, n, lda)
225                                 for i := 0; i < n; i++ {
226                                         copy(got[i*lda:i*lda+n], art[i*n:i*n+n])
227                                 }
228                                 if !zsame(got, a) {
229                                         t.Errorf("Case uplo=%v,n=%v,lda=%v: zPack and zUnpackAsHermitian do not roundtrip", uplo, n, lda)
230                                 }
231                         }
232                 }
233         }
234 }