-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
132 lines (116 loc) · 3.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
/*
The original data of direction numbers is available at:
https://web.maths.unsw.edu.au/~fkuo/sobol/ (BSD License)
Please download it and run following command:
$ go run _tools/codegen_sobol/main.go ./tmp/new-joe-kuo-6.21201 ./sobol/direction_numbers.go
*/
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type DirectionNumber struct {
D uint32 // dimension
S uint32 // the degree of the primitive polynomial
A uint32 // the number representing the coefficients
M []uint32 // the list of initial direction numbers
}
// ParseJoeKuoDirectionNumbers parse the file which contains the direction numbers
// distributed at https://web.maths.unsw.edu.au/~fkuo/sobol/ (BSD License).
func ParseJoeKuoDirectionNumbers(filepath string) ([]DirectionNumber, error) {
f, err := os.Open(filepath)
defer f.Close()
if err != nil {
return nil, err
}
directionNumbers := make([]DirectionNumber, 0, 21201)
scanner := bufio.NewScanner(f)
scanner.Scan() // skip 1st line
for scanner.Scan() {
line := scanner.Text()
rows := strings.Fields(line)
d, err := strconv.ParseUint(rows[0], 10, 32)
if err != nil {
return nil, err
}
s, err := strconv.ParseUint(rows[1], 10, 32)
if err != nil {
return nil, err
}
a, err := strconv.ParseUint(rows[2], 10, 32)
if err != nil {
return nil, err
}
mi := make([]uint32, len(rows)-3)
for i, miItemStr := range rows[3:] {
x, err := strconv.ParseUint(miItemStr, 10, 32)
if err != nil {
return nil, err
}
mi[i] = uint32(x)
}
directionNumbers = append(directionNumbers, DirectionNumber{
D: uint32(d),
S: uint32(s),
A: uint32(a),
M: mi,
})
}
if err := scanner.Err(); err != nil {
return nil, err
}
return directionNumbers, nil
}
func main() {
flag.Parse()
inputFilePath := flag.Arg(0)
outputFilePath := flag.Arg(1)
if inputFilePath == "" || outputFilePath == "" {
log.Fatalln("Please specify an input and output file path. (ex: ./new-joe-kuo-6.21201)")
}
directionNumbers, err := ParseJoeKuoDirectionNumbers(inputFilePath)
if err != nil {
log.Fatalln(err)
}
fout, err := os.Create(outputFilePath)
defer fout.Close()
if err != nil {
log.Fatalln(err)
}
cmd := "go run _tools/sobol_direction_numbers/main.go " + strings.Join(os.Args[1:], " ")
fmt.Fprintf(fout, "// Code generated by \"%s\"; DO NOT EDIT.\n", cmd)
fmt.Fprintf(fout, "\n")
fmt.Fprintf(fout, "package sobol\n")
fmt.Fprintf(fout, "\n")
fmt.Fprintf(fout, "// The original data of direction numbers is distributed at https://web.maths.unsw.edu.au/~fkuo/sobol/ (BSD License).\n")
fmt.Fprintf(fout, "\n")
fmt.Fprintf(fout, "const maxDim = 21201 - 2\n")
fmt.Fprintf(fout, "const maxDeg = 18\n")
fmt.Fprintf(fout, "const maxBit = 30\n")
fmt.Fprintf(fout, "\n")
fmt.Fprintf(fout, "type DirectionNumber struct {\n")
fmt.Fprintf(fout, "\tD uint32\n")
fmt.Fprintf(fout, "\tS uint32\n")
fmt.Fprintf(fout, "\tA uint32\n")
fmt.Fprintf(fout, "\tM []uint32\n")
fmt.Fprintf(fout, "}\n")
fmt.Fprintf(fout, "\n")
fmt.Fprintf(fout, "var directionNumbers = []DirectionNumber{")
for _, dn := range directionNumbers {
mStr := ""
for _, m := range dn.M {
if mStr == "" {
mStr += fmt.Sprintf("%d", m)
} else {
mStr += fmt.Sprintf(", %d", m)
}
}
fmt.Fprintf(fout, "{D: %d, S: %d, A: %d, M: []uint32{%s}},", dn.D, dn.S, dn.A, mStr)
}
fmt.Fprintf(fout, "}\n")
}