OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / lapack / internal / testdata / dlahr2test / main.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 // dlahr2test generates test data for Dlahr2. Test cases are stored in
6 // gzip-compressed JSON file testlapack/testdata/dlahr2data.json.gz which is
7 // read during testing by testlapack/dlahr2.go.
8 //
9 // This program uses cgo to call Fortran version of DLAHR2. Therefore, matrices
10 // passed to the Fortran routine are in column-major format but are written into
11 // the output file in row-major format.
12 package main
13
14 import (
15         "compress/gzip"
16         "encoding/json"
17         "log"
18         "os"
19         "path/filepath"
20
21         "golang.org/x/exp/rand"
22
23         "gonum.org/v1/gonum/lapack/internal/testdata/netlib"
24 )
25
26 type Dlahr2Test struct {
27         N, K, NB int
28         A        []float64
29
30         AWant   []float64
31         TWant   []float64
32         YWant   []float64
33         TauWant []float64
34 }
35
36 func main() {
37         file, err := os.Create(filepath.FromSlash("../../../testlapack/testdata/dlahr2data.json.gz"))
38         if err != nil {
39                 log.Fatal(err)
40         }
41         defer file.Close()
42         w := gzip.NewWriter(file)
43
44         rnd := rand.New(rand.NewSource(1))
45
46         var tests []Dlahr2Test
47         for _, n := range []int{4, 5, 6, 7, 11} {
48                 for k := 0; k <= n/2; k++ {
49                         for nb := 1; nb <= k; nb++ {
50                                 ain := genrand(n, n-k+1, rnd)
51                                 a := make([]float64, len(ain))
52                                 copy(a, ain)
53
54                                 t := genrand(nb, nb, rnd)
55                                 y := genrand(n, nb, rnd)
56                                 tau := genrand(nb, 1, rnd)
57
58                                 netlib.Dlahr2(n, k, nb, a, n, tau, t, nb, y, n)
59
60                                 tests = append(tests, Dlahr2Test{
61                                         N:       n,
62                                         K:       k,
63                                         NB:      nb,
64                                         A:       rowMajor(n, n-k+1, ain),
65                                         AWant:   rowMajor(n, n-k+1, a),
66                                         TWant:   rowMajor(nb, nb, t),
67                                         YWant:   rowMajor(n, nb, y),
68                                         TauWant: tau,
69                                 })
70                         }
71                 }
72         }
73         json.NewEncoder(w).Encode(tests)
74
75         err = w.Close()
76         if err != nil {
77                 log.Fatal(err)
78         }
79 }
80
81 // genrand returns a general r×c matrix with random entries.
82 func genrand(r, c int, rnd *rand.Rand) []float64 {
83         m := make([]float64, r*c)
84         for i := range m {
85                 m[i] = rnd.NormFloat64()
86         }
87         return m
88 }
89
90 // rowMajor returns the given r×c column-major matrix a in row-major format.
91 func rowMajor(r, c int, a []float64) []float64 {
92         if len(a) != r*c {
93                 panic("testdata: slice length mismatch")
94         }
95         m := make([]float64, len(a))
96         for i := 0; i < r; i++ {
97                 for j := 0; j < c; j++ {
98                         m[i*c+j] = a[i+j*r]
99                 }
100         }
101         return m
102 }