OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / lapack / testlapack / matgen_test.go
1 // Copyright ©2017 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         "math"
9         "testing"
10
11         "golang.org/x/exp/rand"
12
13         "gonum.org/v1/gonum/blas/blas64"
14 )
15
16 func TestDlagsy(t *testing.T) {
17         const tol = 1e-14
18         rnd := rand.New(rand.NewSource(1))
19         for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 50} {
20                 for _, lda := range []int{0, 2*n + 1} {
21                         if lda == 0 {
22                                 lda = max(1, n)
23                         }
24                         d := make([]float64, n)
25                         for i := range d {
26                                 d[i] = 1
27                         }
28                         a := blas64.General{
29                                 Rows:   n,
30                                 Cols:   n,
31                                 Stride: lda,
32                                 Data:   nanSlice(n * lda),
33                         }
34                         work := make([]float64, a.Rows+a.Cols)
35
36                         Dlagsy(a.Rows, 0, d, a.Data, a.Stride, rnd, work)
37
38                         isIdentity := true
39                 identityLoop:
40                         for i := 0; i < n; i++ {
41                                 for j := 0; j < n; j++ {
42                                         aij := a.Data[i*a.Stride+j]
43                                         if math.IsNaN(aij) {
44                                                 isIdentity = false
45                                         }
46                                         if i == j && math.Abs(aij-1) > tol {
47                                                 isIdentity = false
48                                         }
49                                         if i != j && math.Abs(aij) > tol {
50                                                 isIdentity = false
51                                         }
52                                         if !isIdentity {
53                                                 break identityLoop
54                                         }
55                                 }
56                         }
57                         if !isIdentity {
58                                 t.Errorf("Case n=%v,lda=%v: unexpected result", n, lda)
59                         }
60                 }
61         }
62 }
63
64 func TestDlagge(t *testing.T) {
65         rnd := rand.New(rand.NewSource(1))
66         for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 50} {
67                 for _, lda := range []int{0, 2*n + 1} {
68                         if lda == 0 {
69                                 lda = max(1, n)
70                         }
71                         d := make([]float64, n)
72                         for i := range d {
73                                 d[i] = 1
74                         }
75                         a := blas64.General{
76                                 Rows:   n,
77                                 Cols:   n,
78                                 Stride: lda,
79                                 Data:   nanSlice(n * lda),
80                         }
81                         work := make([]float64, a.Rows+a.Cols)
82
83                         Dlagge(a.Rows, a.Cols, 0, 0, d, a.Data, a.Stride, rnd, work)
84
85                         if !isOrthonormal(a) {
86                                 t.Errorf("Case n=%v,lda=%v: unexpected result", n, lda)
87                         }
88                 }
89         }
90
91 }