Debian Codegraph @

test/math.scm

raw
1 ;; Testing math.* that depends on other extension modules.
2
3 (use gauche.test)
4 (use srfi.27)
5 (use srfi.42)
6
7 (test-start "math.* modules")
8
9 (test-section "math.prime")
10 (use math.prime)
11 (test-module 'math.prime)
12
13 (define *nth-primes*
14 '((1 . 3) (3 . 7) (5 . 13) (10 . 31) (100 . 547)
15 (1000 . 7927) (10000 . 104743) (100000 . 1299721) (200000 . 2750161)))
16
17 (test* "selected prime numbers" *nth-primes*
18 (map (^[i] (cons (car i) (list-ref *primes* (car i)))) *nth-primes*))
19
20 (let ([source (make-random-source)])
21 (random-source-pseudo-randomize! source 10 20)
22 (let1 samples (list-ec (: n 20)
23 ((random-source-make-integers source) (expt 10 10)))
24 (test* "naive-factorize" samples
25 (map (^[n] (let1 factors (naive-factorize n)
26 (if (every small-prime? factors)
27 (apply * factors)
28 `(composites in ,factors))))
29 samples))))
30
31 ;; we need more systematic approach to test this
32 (define *prime-test-samples*
33 '((#f . 736775510329)
34 (#t . 516119616549881)
35 (#f . 516119616549887)
36 ))
37
38 (test* "miller-rabin test" *prime-test-samples*
39 (map (^p (cons (miller-rabin-prime? (cdr p)) (cdr p)))
40 *prime-test-samples*))
41
42 (define *random-prime-test-count* 500)
43 ;(define *random-prime-test-count* 1000000) ;takes 5min on 2.4GHz Core2 machine
44
45 (let ([source (make-random-source)])
46 (random-source-pseudo-randomize! source 11 20)
47 (let ([rand (random-source-make-integers source)]
48 [r `(,*random-prime-test-count* samples tested)])
49 (test* "bpsw test vs miller-rabin test" r
50 (let loop ([n 0])
51 (if (= n *random-prime-test-count*)
52 r
53 ;; Avoid testing even numbers (which is waste of time), plus
54 ;; this makes density of smaller numbers slighly higher.
55 (let* ([x (rand (expt 2 48))]
56 [sample (ash x (- (twos-exponent-factor x)))])
57 (if (eq? (bpsw-prime? sample) (miller-rabin-prime? sample))
58 (loop (+ n 1))
59 `(disagreement at ,n with sample ,sample))))))))
60
61 (let1 results
62 ;;(a n jacobi)
63 '((0 1 1)
64 (0 3 0)
65 (4 5 1)
66 (333 1 1)
67 (78 13 0)
68 (1001 9907 -1))
69 (test* "jacobi symbol" results
70 (map (^[r] `(,(car r) ,(cadr r) ,(jacobi (car r) (cadr r))))
71 results))
72 )
73
74 (let1 ps (take *primes* 100)
75 (test* "totient (small primes)" (map (cut - <> 1) ps)
76 (map totient ps))
77 (let1 p2s (slices ps 3)
78 (test* "totient (some primes)"
79 (map (^[ps] (apply * (map (cut - <> 1) ps))) p2s)
80 (map (^[ps] (totient (apply * ps))) p2s))))
81
82 ;;;
83 ;;; math.simplex
84 ;;;
85
86 (test-section "math.simplex")
87 (use math.simplex)
88 (test-module 'math.simplex)
89
90 (define (test-simplex cnt A b goal c result)
91 (test* #"simplex test ~cnt"
92 result
93 (simplex-solve A b goal c)
94 (^[a b]
95 (or (eqv? a b)
96 (and (uvector? a) (uvector? b)
97 (every (^i (approx=? (~ a i) (~ b i) 1e-5))
98 (iota (uvector-length a))))))))
99
100 (test-simplex 1
101 #,(<f64array> (0 2 0 2) 1 0.5 3 2)
102 #f64(2 12)
103 :maximize #f64(1 1)
104 #f64(0 4))
105
106 (test-simplex 2
107 #,(<array> (0 3 0 3) 8 6 1 4 2 1.5 2 1.5 0.5)
108 #(48 20 8)
109 :maximize #(60 30 20)
110 #f64(2 0 8))
111
112 (test-simplex 3
113 #,(<array> (0 3 0 4) 3 2 1 2 1 1 1 1 4 3 3 4)
114 #u32(225 117 420)
115 :maximize #u32(19 13 12 17)
116 #f64(39 0 48 30))
117
118 (test-simplex 4
119 #,(<array> (0 3 0 4) 2 3 1 1 1 2 2 3 2 1 0 1)
120 #f64(18 20 16)
121 :maximize #f64(3 5 4 2)
122 #f64(5.333333 0 7.33333 0))
123
124 (test-simplex 5
125 #,(<array> (0 4 0 2) 6 4 1 2 -1 1 0 1)
126 #f64(24 6 1 2)
127 :minimize #f64(-5 -4)
128 #f64(3 1.5))
129
130 (test-simplex 6
131 #,(<array> (0 3 0 2) 0.25 0.5 0.4 0.2 0 0.8)
132 #f64(40 40 40)
133 :minimize #f64(-2 -3)
134 #f64(80 40))
135
136 (test-simplex 7
137 #,(<array> (0 4 0 3) 1 2 4 2 1 1 1 2 3 1 0 0)
138 #f64(2000 3600 2400 30)
139 :minimize #f64(-16 -17 -10)
140 #f64(30 985 0))
141
142 (test-simplex 8
143 #,(<array> (0 3 0 3) 2 3 0 2 5 0 3 2 4)
144 #f64(8 10 15)
145 :minimize #f64(-3 -5 -4)
146 #f64(0 2 2.75))
147
148 ;; Degenerative case
149 (test-simplex 9
150 #,(<array> (0 3 0 4) 1/2 -11/2 -5/2 9 1/2 -3/2 -1/2 1 1 1 1 1)
151 #f64(0 0 1)
152 :minimize #f64(-10 57 9 24)
153 #f64(0.5 0 0.5 0))
154
155 ;; Negative rhs
156 (test-simplex 10
157 #,(<array> (0 2 0 2) -2 -1 -1 -7)
158 #f64(-4 -7)
159 :minimize #f64(1 1)
160 #f64(21/13 10/13))
161
162 (test-simplex 11
163 #,(<array> (0 2 0 2) 2 3 -3 -1)
164 #f64(6 -3)
165 :maximize #f64(4 5)
166 #f64(3 0))
167
168 ;; Negative rhs, fails in phase 1
169 (test-simplex 12
170 #,(<array> (0 4 0 2) 1/2 1/4 -1 -3 1 1 -1 -1)
171 #f64(4 -36 10 -10)
172 :minimize #f64(2 3)
173 #f)
174
175 (test-simplex 13
176 #,(<array> (0 3 0 2)
177 -3 -2
178 -1 -4
179 1 1)
180 #f64(-3 -4 5)
181 :maximize #f64(5 8)
182 #f64(0 5))
183
184 (test-end)