-
Notifications
You must be signed in to change notification settings - Fork 1
/
ufrac.scm
256 lines (250 loc) · 8.29 KB
/
ufrac.scm
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
(guard (x (load "ufrac-math.so")
(load "ufrac-branch.so")
(load "ufrac-mt.so")
(load "ufrac-conjecture.so"))
(load "ufrac-math.scm")
(load "ufrac-branch.scm")
(load "ufrac-mt.scm")
(load "ufrac-conjecture.scm"))
(set! verbose? #f)
(set! print-frequency 1000)
(define (kill br)
(define (knapsack A aim sum-NUMs)
(define (for-each-insert e LISTs)
(map (lambda (l)
(append l (list e)))
LISTs))
(cond [(zero? aim) '(())] ; There is a solution
[(or (null? A) (negative? aim)) '()] ; There is no solution
[else
(let ([num (caar A)])
(cond [(< (- sum-NUMs num) aim)
(for-each-insert (cdar A)
(knapsack (cdr A)
(- aim num)
(- sum-NUMs num)))]
[else ; (>= (- sum-NUMs num) aim)
(append (for-each-insert (cdar A)
(knapsack (cdr A)
(- aim num)
(- sum-NUMs num)))
(knapsack (cdr A) aim (- sum-NUMs num)))]
))]
))
(define (generate-sets-aim-mod A aim-mod p)
(let ([sum-NUMs (sum (map car A))])
(apply append
(map (lambda (aim) (knapsack A aim sum-NUMs))
(range aim-mod sum-NUMs p)))))
(define (compute-p^highest-power p br)
(let ([M-p (filter (lambda (x) (divide? p x)) (br-denoms br))])
(if (null? M-p)
#f
(expt p (apply max (map (lambda (x) (highest-power p x)) M-p))))))
(define (compute-pre-aim p p^highest-power br)
(let* ([denom-diff (denominator (br-diff br))]
[p^highest-power-denom-diff (/ denom-diff (kill-p-factor p denom-diff))])
(cond [(not p^highest-power) #f] ; Either p^highest-power = #f
[(= p^highest-power-denom-diff p^highest-power)
(* (numerator (br-diff br))
(inverse-mod-p p
(kill-p-factor p
(denominator (br-diff br)))))]
[(< p^highest-power-denom-diff p^highest-power) 0]
[else #f] ;p^highest-power-denom-diff > p^highest-power
)))
(let ([r (br-r br)]
[diff (br-diff br)])
(cond [(br-denoms-sol br) (cons (list br) '())] ; br is actually a solution
[(or (negative? r) (negative? diff)) '(())]
[(integer? diff)
(let* ([d (car (br-denoms br))]
;; since the order of numbers in (br-denoms br) is preserved,
;; d is always the least number
[rec-d (/ 1 d)])
(cond [(< r rec-d) (cons '() (list (br-discard-first-d br)))]
[(= r rec-d) (cons (list (br-reserve-first-d br))
(list (br-discard-first-d br)))]
[else ; r > rec-d
(cons '() (list (br-reserve-first-d br)
(br-discard-first-d br)))]
))]
[else
(let*
([p (caar (factor (br-gpp br)))]
[p^highest-power (compute-p^highest-power p br)]
[pre-aim (compute-pre-aim p p^highest-power br)])
(if (not pre-aim) ; Consider the example (efrac '(2 2 4 4) 9/8).
'(())
(let*
([M (filter (lambda (x) (divide? p^highest-power x))
(br-denoms br))]
[NM (remp (lambda (x) (divide? p^highest-power x))
(br-denoms br))]
[rs-M (rec-sum M)]
[sum-NM (- (br-sum-denoms br) (sum M))]
[rs-NM (- (br-rs-denoms br) rs-M)]
[bound (- rs-NM (br-r br))]
[discard? (lambda (x) (or (negative? (+ bound (car x)))
(> (car x) (br-r br))))]
[solution? (lambda (x) (zero? (+ bound (car x))))]
[A (map (lambda (x)
(cons (inverse-mod-p p (/ x p^highest-power))
x))
M)]
[aim-mod (modulo (- (sum (map car A)) pre-aim) p)]
[reduced-SUBSETs-M
(remp discard?
(map (lambda (x) (cons (rec-sum x) x))
(generate-sets-aim-mod A aim-mod p)))])
(cons (map (lambda (x) ; solutions
(let ([subset-M (cdr x)])
(make-br sum-NM
rs-NM
rs-M
subset-M
NM
br)))
(filter solution? reduced-SUBSETs-M))
(remp (lambda (br) (or (negative? (br-r br))
(negative? (br-diff br))))
;; remove impossible branches produced by br-reduce
(map (lambda (x) ; new-BRs
(let ([subset-M (cdr x)])
(br-reduce (make-br sum-NM
rs-NM
rs-M
subset-M
NM
br))))
(remp solution? reduced-SUBSETs-M)))))))])))
(define (ufrac D r)
(define SOLUTIONs '())
(define (distinct-denoms) #f)
(define (recur BRs n)
(cond [(null? BRs) '()]
[else
(let* ([SOLs-BRs (kill (car BRs))]
[new-BRs (cdr SOLs-BRs)]
[next-BRs (append new-BRs (cdr BRs))] ; order is important
[n+1 (add1 n)])
(if distinct-denoms
(set! SOLUTIONs (append (car SOLs-BRs) SOLUTIONs))
(for-each ; for each new sol
(lambda (new-sol)
(if (null?
(filter
(lambda (old-sol) (br-equal-as-sol? new-sol old-sol))
SOLUTIONs)) ; consider (ufrac '(2 2 2 2) 1)
(set! SOLUTIONs (cons new-sol SOLUTIONs))))
(car SOLs-BRs)))
(cond [(and verbose? (divide? print-frequency n+1))
(printf "killed branches: ~12s branches: ~12s solutions: ~10s ~s \n"
n+1
(length BRs)
(length SOLUTIONs)
(time-utc->date (current-time)))
(cond [(not (null? next-BRs))
(br-display (car next-BRs))
(newline)])])
(recur next-BRs (add1 n)))]))
(let ([first-br (br-reduce (make-br D r))])
(set! distinct-denoms (set-distinct-numbers? (br-denoms first-br)))
(recur (list first-br) 0))
(if verbose?
(printf "Found ~s representations of ~s.\n"
(length SOLUTIONs)
r))
(map br-denoms-sol SOLUTIONs))
(define (ufrac-es D r)
(define (recur BRs n)
(cond [(null? BRs) '()]
[else
(let* ([SOLs-BRs (kill (car BRs))]
[SOLs (car SOLs-BRs)]
[new-BRs (cdr SOLs-BRs)])
(cond [(null? SOLs)
(let ([next-BRs (append new-BRs (cdr BRs))]
[n+1 (add1 n)])
(cond [(and verbose? (divide? print-frequency n+1))
(printf "killed branches: ~12s branches: ~12s ~s \n"
n+1
(length BRs)
(time-utc->date (current-time)))
(cond [(not (null? next-BRs))
(br-display (car next-BRs))
(newline)])])
(recur next-BRs (add1 n)))]
[else
(map br-denoms-sol (list (car SOLs)))]))]))
(let ([sol (recur (list (br-reduce (make-br D r))) 0)])
(if (null? sol)
#f
(car sol))))
(define (ufrac-es-progress D r)
(define sol '())
(define progress 0)
(define num-killed-brs 0)
(define (dig br num-BRs-above treasure-map)
(cond [(null? sol)
(let* ([SOLs-BRs (kill br)]
[SOLs (car SOLs-BRs)]
[new-BRs (cdr SOLs-BRs)]
[length-new-BRs (length new-BRs)])
(set! num-killed-brs (add1 num-killed-brs))
(let ([num-BRs (* num-BRs-above length-new-BRs)]
[new-treasure-map (if (> length-new-BRs 1)
(cons length-new-BRs treasure-map)
treasure-map)])
(cond [(null? SOLs)
(for-each (lambda (br) (dig br num-BRs new-treasure-map))
new-BRs)
(cond [(and verbose? (null? new-BRs))
(set! progress (+ progress
(reciprocal num-BRs-above)))
(printf "progress: ~14,10f% killed branches: ~12s ~s ~s\n"
(* 100 progress)
num-killed-brs
(time-utc->date (current-time))
new-treasure-map)])]
[else (set! sol (map br-denoms-sol (list (car SOLs))))]
)))]
))
(dig (br-reduce (make-br D r)) 1 '())
(if (null? sol)
#f
(car sol)))
(define (ufrac-bfs D r)
(define (distinct-denoms) #f)
(define (recur SOLUTIONs BRs)
(cond [(and verbose? (not (= (length BRs) 1)))
(printf "~10@s solutions ~10@s branches ~s\n"
(length SOLUTIONs)
(length BRs)
(time-utc->date (current-time)))])
(cond ;[(not (null? SOLUTIONs)) (map br-denoms-sol SOLUTIONs)]
[(null? BRs) (map br-denoms-sol SOLUTIONs)]
[else
(let ([new-BRs '()])
(for-each ; for each br in BRs
(lambda (br)
(let ([SOLs-BRs (kill br)])
(if distinct-denoms
(set! SOLUTIONs (append (car SOLs-BRs) SOLUTIONs))
(for-each ; for each new sol
(lambda (new-sol)
(if (null?
(filter
(lambda (old-sol) (br-equal-as-sol? new-sol old-sol))
SOLUTIONs)) ; consider (ufrac-bfs '(2 2 2 2) 1)
(set! SOLUTIONs (cons new-sol SOLUTIONs))))
(car SOLs-BRs)))
(for-each ; for each new br
(lambda (new-br)
(set! new-BRs (cons new-br new-BRs)))
(cdr SOLs-BRs))))
BRs)
(recur SOLUTIONs new-BRs))]))
(let ([first-br (br-reduce (make-br D r))])
(set! distinct-denoms (set-distinct-numbers? (br-denoms first-br)))
(recur '() (list first-br))))