-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmath-tools.rkt
186 lines (161 loc) · 5.71 KB
/
math-tools.rkt
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
#lang racket/base
(require racket/contract
racket/match
pict
racket/class
pict/tree-layout
racket/draw)
(provide
(contract-out
[$ (->* () (#:size exact-nonnegative-integer?)
#:rest (listof (or/c pict? string?))
pict?)]
[msub (->* (string?) (exact-nonnegative-integer? real?) pict?)]
[msup (->* (string?) (exact-nonnegative-integer? real?) pict?)]
[roman (->* (string?) (exact-nonnegative-integer? real?) pict?)]
[sans (->* (string?) (exact-nonnegative-integer? real?) pict?)]
[mono (->* (string?) (exact-nonnegative-integer? real?) pict?)]
[mcaps (->* (string?) (exact-nonnegative-integer? real?) pict?)]
[rcaps (->* (string?) (exact-nonnegative-integer? real?) pict?)]
[big-wedge (->* () (#:size exact-nonnegative-integer? #:below pict?)
#:rest (listof pict?)
pict?)]
[big-vee (->* () (#:size exact-nonnegative-integer? #:below pict?)
#:rest (listof pict?)
pict?)]
[big-cap (->* () (#:size exact-nonnegative-integer? #:below pict?)
#:rest (listof pict?)
pict?)]
[big-cup (->* () (#:size exact-nonnegative-integer? #:below pict?)
#:rest (listof pict?)
pict?)]
[parens (->* () (#:offset exact-integer?
#:style (or/c 'round 'square 'curly 'angle))
#:rest (listof pict?)
pict?)]
[inference-rule (->* (pict? pict?)
(#:thickness exact-nonnegative-integer?
#:scale (and/c real? positive?))
pict?)]
[draw-bdd (-> bdd/c pict?)]
[draw-lbdd (-> lbdd/c pict?)]))
(define ($ #:size [size 20]
. bodies)
(define (->pict x)
(match x
[(? string?) (math x)]
[(? pict?) x]))
(apply hbl-append (map ->pict bodies)))
(define (msub str [size 20] [angle 0])
(text str (cons 'subscript "Latin Modern Math") size angle))
(define (msup str [size 20] [angle 0])
(text str (cons 'superscript "Latin Modern Math") size angle))
(define (math str [size 20] [angle 0])
(text str "Latin Modern Math" size angle))
(define (roman str [size 20] [angle 0])
(text str "Latin Modern Roman" size angle))
(define (sans str [size 20] [angle 0])
(text str "Latin Modern Sans" size angle))
(define (mono str [size 20] [angle 0])
(text str "Latin Modern Mono" size angle))
(define (mcaps str [size 20] [angle 0])
(text str "Latin Modern Mono Caps" size angle))
(define (rcaps str [size 20] [angle 0])
(text str "Latin Modern Roman Caps" size angle))
(define ((big-op op) #:size [size 20] #:below [below (blank 0 0)]
. rhss)
(define the-op (math op (* 3 size)))
(define rhs (apply hbl-append rhss))
(define big-op-app (if (< (pict-height rhs) (pict-height the-op))
hc-append
hbl-append))
(vl-append
(big-op-app the-op rhs)
(scale below 3/4)))
(define big-wedge (big-op "∧"))
(define big-vee (big-op "∨"))
(define big-cap (big-op "∩"))
(define big-cup (big-op "∪"))
(define (parens #:style [style 'round]
#:offset [offset 0] . bodies)
(define body (apply hbl-append bodies))
(define body-height (pict-height body))
(define-values (l-str r-str)
(match style
['round (values "(" ")")]
['square (values "[" "]")]
['curly (values "{" "}")]
['angle (values "⟨" "⟩")]))
(let loop ([size 2]
[scalar 1])
(define l (scale (math l-str size) 1 scalar))
(cond
[(>= (pict-height l) body-height)
(hc-append offset l body (scale (math r-str size) 1 scalar))]
[(< scalar 6/4) (loop size (+ scalar 1/4))]
[else
(loop (+ 2 size) 1)])))
(define (inference-rule above below
#:thickness [thickness 1]
#:scale [scalar 1])
(define w (* scalar (max (pict-width above) (pict-width below))))
(vc-append (scale above scalar)
(hline w thickness)
(scale below scalar)))
(define blue (make-object color% 51 153 255))
(define red (make-object color% 255 80 80))
(define grey (make-object color% 200 200 200))
(define bdd/c
(or/c pict?
(list/c pict?
(recursive-contract bdd/c #:flat)
(recursive-contract bdd/c #:flat))))
(define (node p)
(cc-superimpose
(filled-rounded-rectangle
(* 1.5 (pict-width p))
(* 1.5 (pict-height p))
#:color "white")
p))
(define edge-width 6)
(define (draw-bdd b)
(inset
(naive-layered
(let loop ([b b])
(match b
[(? pict?) (tree-layout #:pict (node b) #f #f)]
[(list x l r)
(tree-layout
#:pict (node x)
(tree-edge #:edge-color blue
#:edge-width edge-width
(loop l))
(tree-edge #:edge-color red
#:edge-width edge-width
(loop r)))])))
5))
(define lbdd/c
(or/c pict?
(list/c pict?
(recursive-contract lbdd/c #:flat)
(recursive-contract lbdd/c #:flat)
(recursive-contract lbdd/c #:flat))))
(define (draw-lbdd b)
(inset
(naive-layered
(let loop ([b b])
(match b
[(? pict?) (tree-layout #:pict (node b))]
[(list x l m r)
(tree-layout
#:pict (node x)
(tree-edge #:edge-color blue
#:edge-width edge-width
(loop l))
(tree-edge #:edge-color grey
#:edge-width edge-width
(loop m))
(tree-edge #:edge-color red
#:edge-width edge-width
(loop r)))])))
5))