github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/internal/testdata/dlahr2test/main.go (about) 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 "math/rand" 19 "os" 20 "path/filepath" 21 22 "github.com/gonum/lapack/internal/testdata/netlib" 23 ) 24 25 type Dlahr2Test struct { 26 N, K, NB int 27 A []float64 28 29 AWant []float64 30 TWant []float64 31 YWant []float64 32 TauWant []float64 33 } 34 35 func main() { 36 file, err := os.Create(filepath.FromSlash("../../../testlapack/testdata/dlahr2data.json.gz")) 37 if err != nil { 38 log.Fatal(err) 39 } 40 defer file.Close() 41 w := gzip.NewWriter(file) 42 43 rnd := rand.New(rand.NewSource(1)) 44 45 var tests []Dlahr2Test 46 for _, n := range []int{4, 5, 6, 7, 11} { 47 for k := 0; k <= n/2; k++ { 48 for nb := 1; nb <= k; nb++ { 49 ain := genrand(n, n-k+1, rnd) 50 a := make([]float64, len(ain)) 51 copy(a, ain) 52 53 t := genrand(nb, nb, rnd) 54 y := genrand(n, nb, rnd) 55 tau := genrand(nb, 1, rnd) 56 57 netlib.Dlahr2(n, k, nb, a, n, tau, t, nb, y, n) 58 59 tests = append(tests, Dlahr2Test{ 60 N: n, 61 K: k, 62 NB: nb, 63 A: rowMajor(n, n-k+1, ain), 64 AWant: rowMajor(n, n-k+1, a), 65 TWant: rowMajor(nb, nb, t), 66 YWant: rowMajor(n, nb, y), 67 TauWant: tau, 68 }) 69 } 70 } 71 } 72 json.NewEncoder(w).Encode(tests) 73 74 err = w.Close() 75 if err != nil { 76 log.Fatal(err) 77 } 78 } 79 80 // genrand returns a general r×c matrix with random entries. 81 func genrand(r, c int, rnd *rand.Rand) []float64 { 82 m := make([]float64, r*c) 83 for i := range m { 84 m[i] = rnd.NormFloat64() 85 } 86 return m 87 } 88 89 // rowMajor returns the given r×c column-major matrix a in row-major format. 90 func rowMajor(r, c int, a []float64) []float64 { 91 if len(a) != r*c { 92 panic("testdata: slice length mismatch") 93 } 94 m := make([]float64, len(a)) 95 for i := 0; i < r; i++ { 96 for j := 0; j < c; j++ { 97 m[i*c+j] = a[i+j*r] 98 } 99 } 100 return m 101 }