-
Notifications
You must be signed in to change notification settings - Fork 10
/
format.rkt
1219 lines (1137 loc) · 64.3 KB
/
format.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket/base
(provide (all-defined-out))
(require racket/format racket/list racket/match racket/math racket/string
math/bigfloat
(for-syntax racket/base racket/syntax syntax/parse)
"core.rkt" "math-match.rkt" "normalize.rkt" "up-ref.rkt" "compose-app.rkt"
"logical-operators.rkt" "relational-operators.rkt" "expand.rkt" "trig.rkt"
"parameters.rkt"
(prefix-in % "bfracket.rkt"))
(module+ test
(require rackunit math/bigfloat)
(define x 'x) (define y 'y) (define z 'z))
;;;
;;; FORMATTING
;;;
;;; Variables and Constants
; The parameter output-variable-name holds the formatter
; for variables and constants.
; Input Default TeX MMA
; @e e \mathrm{e} E
; @pi pi π Pi
; @i i o I
; x x x x
; TeX handles various other symbols in symbol->tex.
(define (default-output-variable-name x)
(match x ['@pi "pi"] ['@i "i"] ['@e "e"] [_ (~a x)]))
(define (mma-output-variable-name x)
(match x ['@pi "Pi"] ['@i "I"] ['@e "E"] [_ (~a x)]))
(define (tex-output-variable-name x)
(match x ['@pi "π"] ['@i "i"] ['@e "\\mathrm{e}"]
[(? symbol? x)
(define s (symbol->string x))
(cond
; single letter variables are italic
[(= (string-length s) 1) (symbol->tex x)]
; identifiers with subscripts
[(string-contains? s "_")
(define parts (string-split s "_"))
(define formatted-parts (map tex-output-variable-name (map string->symbol parts)))
(string-append* (add-between (map ~a formatted-parts) "_"))]
[(string-contains? s "-")
; An dash in an identifer is formatted as a space
(~a "\\mathrm{" (symbol->tex (string->symbol (string-replace s "-" "\\;"))) "}")]
[else
(~a "\\mathrm{" (symbol->tex x) "}")])]))
;;; Fractions
(define (default-output-fraction α) (~a α))
(define (mma-output-fraction α) (~a α))
(define (tex-output-fraction α)
(if (> (denominator α) 1)
(~a "\\frac{" (numerator α) "}{" (denominator α) "}")
(~a α)))
;;; Roots
; If output-root? is true, the formatter uses output-root to output
; powers of the form (expt u 1/n).
(define (default-output-root u n)
; note: KAS can't parse root(u,n) so we need to output u^(1/n)
#f) ; this makes verbose! output u^(1/n) with correct parens
;; (define (mma-output-root formatted-u)
;; (match u
;; [(Expt u α) #:when (= (numerator? α) 1) (def n (/ 1 α)) (~a "Power[" formatted-u "," α ")")]
;; [_ (error 'mma-output-root (~a "Expected expression of the form (expt u 1/n), got: " u))]))
;; (define (tex-output-root formatted-u)
;; (match u
;; [(Expt u α) #:when (= (numerator? α) 1) (def n (/ 1 α)) (~a "\\sqrt[" n " ]{" formatted-u "}")]
;; [_ (error 'tex-output-root (~a "Expected expression of the form (expt u 1/n), got: " u))]))
;;; Logarithms
; Input Default Tex MMA
; (log x) log(x) \log(x) log(x)
; (log 2 x) log_2(x) \log_{2}(x) log_2(x)
(define (default-output-log u [v #f])
(match-define (list l r) (output-application-brackets))
(cond [v (~a "log_" (verbose~ u) l (verbose~ v) r)]
[else (~a "log" l (verbose~ u) r)]))
(define (default-output-up u v)
(~a "(" (verbose~ u) "," (verbose~ v) ")"))
(define mma-output-log default-output-log)
(define (tex-output-log u [v #f])
(parameterize ([output-wrapper values])
(cond [v (~a "\\log_{" (verbose~ u) "}(" (verbose~ v) ")")]
[else (~a "\\log(" (verbose~ u) ")")])))
(define (tex-output-up u v)
(parameterize ([output-wrapper values])
(define x (verbose~ u))
(define y (verbose~ v))
(when (and (= (string-length x) (+ (string-length y) 1))
(eqv? (string-ref x 0) #\-))
(set! y (~a "\\phantom{-}" y)))
(when (and (= (string-length y) (+ (string-length x) 1))
(eqv? (string-ref y 0) #\-))
(set! x (~a "\\phantom{-}" x)))
(if (output-brackets-for-up?)
(~a "\\begin{bmatrix} " x "\\\\" y "\\end{bmatrix}")
(~a "\\begin{pmatrix} " x "\\\\" y "\\end{pmatrix}"))))
;;; Intervals
(define (default-output-interval u)
(define v~ verbose~)
(parameterize ([output-wrapper values]) ; avoid $ around sub parts
; (displayln (list 'default-output-interval u))
(match u
[(list 'ccinterval -inf.0 +inf.0) (~a "]" "-∞" "," "∞" "[")]
[(list 'ocinterval -inf.0 v) (~a "]" "-∞" "," (v~ v) "]")]
[(list 'cointerval u +inf.0) (~a "[" (v~ u) "," "∞" "[")]
[(list 'ccinterval u v) (~a "[" (v~ u) "," (v~ v) "]")]
[(list 'ocinterval u v) (~a "]" (v~ u) "," (v~ v) "]")]
[(list 'cointerval u v) (~a "[" (v~ u) "," (v~ v) "[")]
[(list 'oointerval u v) (~a "]" (v~ u) "," (v~ v) "[")]
[_ (error 'default-output-interval (~a "unknown interval type, got: " u))])))
(define (tex-output-interval u)
; (displayln (list 'tex-output-interval u))
(define v~ verbose~)
(parameterize ([output-wrapper values]) ; avoid $ around sub parts
(match u
[(list 'oointerval -inf.0 +inf.0) (~a "]" "-∞" "," "∞" "[")]
[(list 'oointerval u +inf.0) (~a "]" (v~ u) "," "∞" "[")] ; for wrong options
[(list 'oointerval -inf.0 v) (~a "]" "-∞" "," (v~ v) "[")]
[(list 'ccinterval -inf.0 +inf.0) (~a "[" "-∞" "," "∞" "]")] ; for wrong options
[(list 'ccinterval -inf.0 v) (~a "[" "-∞" "," (v~ v) "]")] ; for wrong options
[(list 'ccinterval u +inf.0) (~a "[" (v~ u) "," "∞" "]")] ; for wrong options
[(list 'ocinterval -inf.0 +inf.0) (~a "]" "-∞" "," "∞" "]")] ; for wrong options
[(list 'ocinterval -inf.0 v) (~a "]" "-∞" "," (v~ v) "]")]
[(list 'ocinterval u +inf.0) (~a "]" (v~ u) "," "∞" "]")] ; for wrong options
[(list 'cointerval -inf.0 +inf.0) (~a "[" "-∞" "," "∞" "[")] ; for wrong options
[(list 'cointerval u +inf.0) (~a "[" (v~ u) "," "∞" "[")]
[(list 'cointerval -inf.0 v) (~a "[" "-∞" "," (v~ v) "[")] ; for wrong options
[(list 'ccinterval u v) (~a "[" (v~ u) "," (v~ v) "]")]
[(list 'ocinterval u v) (~a "]" (v~ u) "," (v~ v) "]")]
[(list 'cointerval u v) (~a "[" (v~ u) "," (v~ v) "[")]
[(list 'oointerval u v) (~a "]" (v~ u) "," (v~ v) "[")]
[_ (error 'tex-output-interval (~a "unknown interval type, got: " u))])))
;;; Formatting Parameters
(define output-mode (make-parameter #f)) ; #f=default, 'default, 'mma or 'tex
(define output-application-brackets (make-parameter (list "(" ")")))
(define output-format-function-symbol (make-parameter ~a))
(define output-format-quotient (make-parameter #f)) ; #f means default u/v
(define output-format-quotient-parens (make-parameter (list "(" ")")))
(define output-sub-expression-parens (make-parameter (list "(" ")")))
(define output-wrapper (make-parameter values))
(define output-use-quotients? (make-parameter #t))
(define output-sqrt? (make-parameter #t)) ; use sqrt to output (expt u 1/2) ; otherwise as expt
(define output-root? (make-parameter #t)) ; use root to output (expt u 1/n) ; otherwise as expt
(define output-format-abs (make-parameter (λ(u) (~a "abs(" (verbose~ u) ")"))))
(define output-format-sqrt (make-parameter (λ(u) (~a "sqrt(" (verbose~ u) ")"))))
(define output-format-root (make-parameter default-output-root))
(define output-format-log (make-parameter default-output-log))
(define output-format-up (make-parameter default-output-up))
(define output-sub-exponent-parens (make-parameter (list "(" ")"))) ; for Tex it is { }
(define output-sub-exponent-wrapper (make-parameter values)) ; TeX needs extra {}
(define output-format-negative-exponent (make-parameter #f)) ; use 1/2 or 2^-1 to output negative exponents
(define output-terms-descending? (make-parameter #f)) ; reverse terms before outputting?
(define output-implicit-product? (make-parameter #f)) ; useful for TeX
(define output-relational-operator (make-parameter ~a)) ; useful for TeX
(define output-set-operator (make-parameter ~a)) ; useful for TeX
(define output-floating-point-precision (make-parameter 4)) ;
(define output-variable-name (make-parameter default-output-variable-name)) ; also handles @e, @pi and @i
(define output-differentiation-mark (make-parameter '(x))) ; use (u)' rather than d/dx(u) for variables in this list
(define output-fraction (make-parameter default-output-fraction))
(define output-interval (make-parameter default-output-interval))
(define output-brackets-for-up? (make-parameter #f)) ; NSpire uses brackets
(define (use-mma-output-style)
(output-mode 'mma)
(output-application-brackets (list "[" "]"))
(output-format-function-symbol (λ(s) (string-titlecase (~a s))))
(output-format-quotient #f)
(output-format-quotient-parens (list "(" ")"))
(output-sub-expression-parens (list "(" ")"))
(output-wrapper values)
(output-sqrt? #t)
(output-format-abs (λ(u) (~a "Abs[" (verbose~ u) "]")))
(output-format-sqrt (λ(u) (~a "Sqrt[" (verbose~ u) "]")))
(output-format-root (λ(u n) (~a "Root[" (verbose~ u) "," (verbose~ n) "]")))
(output-format-log mma-output-log)
(output-format-up default-output-up)
(output-sub-exponent-parens (list "(" ")"))
(output-sub-exponent-wrapper values)
(output-format-negative-exponent #f)
(output-implicit-product? #f)
(output-relational-operator ~a)
(output-set-operator ~a)
(output-variable-name mma-output-variable-name)
(output-fraction mma-output-fraction))
(define (use-default-output-style)
(output-mode 'default)
(output-application-brackets (list "(" ")"))
(output-format-function-symbol ~a)
(output-format-quotient #f)
(output-format-quotient-parens (list "(" ")"))
(output-sub-expression-parens (list "(" ")"))
(output-sub-exponent-parens (list "(" ")"))
(output-sub-exponent-wrapper values)
(output-format-negative-exponent #f)
(output-wrapper values)
(output-sqrt? #t)
(output-root? #f)
(output-format-abs (λ(u) (~a "abs(" (verbose~ u) ")")))
(output-format-sqrt (λ(u) (~a "sqrt(" (verbose~ u) ")")))
(output-format-root (λ(u n) (~a "root(" (verbose~ u) "," (verbose~ n) ")")))
(output-format-log default-output-log)
(output-format-up default-output-up)
(output-implicit-product? #f)
(output-relational-operator ~a)
(output-set-operator ~a)
(output-variable-name default-output-variable-name)
(output-fraction default-output-fraction))
(define (use-tex-output-style)
(output-mode 'tex)
(define operators '(sin cos tan log ln sqrt det))
(define (~relop u)
(match u
['<= "≤ "]
['>= "≥ "]
['≤ "≤ "]
['≥ "≥ "]
['~ "\\approx "]
['Less "< "]
['LessEqual "≤ "]
['Greater "> "]
['GreaterEqual "≥ "]
[_ (~a u)]))
(define (~setop u)
(match u
['Union "\\cup "]
['Intersection "\\cap "]
['Difference "\\setminus "]
[_ (~a u)]))
(define (~symbol s)
(match s
['acos "\\cos^{-1}"]
['asin "\\sin^{-1}"]
['atan "\\tan^{-1}"]
[_ #:when (member s operators) (~a "\\" s)]
['<= "\\leq "]
['>= "\\geq "]
['≤ "≤ "]
['≥ "≥ "]
['~ "\\approx "]
['* "\\cdot "]
['or "\\vee "]
['and "\\wedge "]
[_ (define t (~a s))
(if (= (string-length t) 1) t (~a "\\text{" t "}"))]))
(output-application-brackets (list "(" ")"))
(output-format-function-symbol ~symbol)
(output-format-quotient (λ (u v) (~a "\\frac{" u "}{" v "}")))
(output-format-quotient-parens (list "" "")) ; not needed due to {} from \frac
; (output-use-quotients? #t)
(output-sub-expression-parens (list "{" "}"))
(output-wrapper (λ (s) (~a "$" s "$")))
(output-format-abs (λ(u) (parameterize ([output-wrapper values])
(~a "\\left|" (verbose~ u) "\\right|"))))
(output-sqrt? #t)
(output-root? #f)
(output-format-sqrt (λ(u) (parameterize ([output-wrapper values])
(~a "\\sqrt{" (verbose~ u) "}"))))
(output-format-root (λ(u n) (parameterize ([output-wrapper values])
(if (equal? n 2)
(~a "\\sqrt{" (verbose~ u) "}")
(~a "\\sqrt[" (verbose~ n) "]{" (verbose~ u) "}")))))
(output-format-log tex-output-log)
(output-format-up tex-output-up)
(output-sub-exponent-parens (list "{" "}"))
(output-sub-exponent-wrapper (λ (s) (~a "{" s "}")))
(output-format-negative-exponent #f)
(output-implicit-product? #t)
(output-relational-operator ~relop)
(output-set-operator ~setop)
(output-variable-name tex-output-variable-name)
(output-fraction tex-output-fraction)
(output-interval tex-output-interval))
(define (tex u)
(define operators '(sin cos tan log ln sqrt
det
sinh cosh tanh )) ; needs \\ in output
(define relational-operators '(= < <= > >=))
(define (~relop u)
(match u
['<= "≤ "]
['>= "≥ "]
['~ "\\approx "]
['Less "< "]
['LessEqual "≤ "]
['Greater "> "]
['GreaterEqual "≥ "]
[_ (~a u)]))
(define (~setop u)
(match u
['Union "\\cup "]
['Intersection "\\cap "]
['Difference "\\setminus "]
[_ (~a u)]))
(define (~symbol s)
(match s
['acos "\\cos^{-1}"]
['asin "\\sin^{-1}"]
['atan "\\tan^{-1}"]
[_ #:when (member s operators) (~a "\\" s)]
['<= "\\leq "]
['>= "\\geq "]
['~ "\\approx "]
['* "\\cdot "] ; multiplication
['or "\\vee "] ; logical or
['and "\\wedge "] ; logical and
['|%| "\\%"]
[_ (define t (~a s))
(if (= (string-length t) 1) t (~a "\\text{" t "}"))]))
(parameterize ((output-mode 'tex)
(output-application-brackets (list "(" ")"))
(output-format-function-symbol ~symbol)
(output-format-quotient (λ (u v) (~a "\\frac{" u "}{" v "}")))
(output-format-quotient-parens (list "" ""))
; (output-use-quotients? #t)
(output-sub-expression-parens (list "{" "}"))
(output-wrapper (λ (s) (~a "$" s "$")))
; (output-sqrt? #t) ; uncommented!! otherwise the user can't control it
(output-format-sqrt (λ(u) (parameterize ([output-wrapper values])
(~a "\\sqrt{" (verbose~ u) "}"))))
(output-format-root (λ(u n) (parameterize ([output-wrapper values])
(if (equal? n 2)
(~a "\\sqrt{" (verbose~ u) "}")
(~a "\\sqrt[" (verbose~ n) "]{" (verbose~ u) "}")))))
(output-sub-exponent-parens (list "{" "}"))
(output-sub-exponent-wrapper (λ (s) (~a "{" s "}")))
; (output-format-negative-exponent #f) ; omitted on purpose
(output-implicit-product? (default-output-implicit-product?)) ; #t
(output-relational-operator ~relop)
(output-set-operator ~setop)
(output-variable-name tex-output-variable-name)
(output-format-log tex-output-log)
(output-format-up tex-output-up)
(output-fraction tex-output-fraction)
(output-interval tex-output-interval))
(verbose~ u)))
(define char->tex
(let ()
(define dict
( hash
; symbolic constants
'α "\\alpha" 'β "\\beta" 'γ "\\gamma" 'Γ "\\Gamma" 'δ "\\delta" 'Δ "\\Delta"
'ε "\\epsilon" 'ζ "\\zeta" 'η "\\eta" 'θ "\\theta" 'Θ "\\Theta" 'ι "\\iota"
'κ "\\kappa" 'λ "\\lambda" 'Λ "\\Lambda" 'μ "\\mu" 'ν "\\nu" 'ξ "\\xi"
'Ξ "\\Xi" 'π "\\pi" 'Π "\\Pi" 'ρ "\\rho" 'σ "\\sigma" 'Σ "\\Sigma"
'τ "\\Tau" 'υ "\\upsilon" 'Υ "\\Upsilon" 'φ "\\phi" 'Φ "\\Phi" 'χ "\\chi"
'ψ "\\psi" 'Ψ "\\Psi" 'ω "\\omega" 'Ω "\\Omega"
'|%| "\\%"))
(λ (c)
(define s (string->symbol (string c)))
(match (hash-ref dict s #f)
[#f (string c)]
[s (~a s " ")]))))
(define (string->tex s)
(define s1 (string-append* (map char->tex (string->list s))))
(if (equal? s s1) s s1))
(define (symbol->tex s)
(define t (string->symbol (string->tex (symbol->string s))))
(match t
['@e "\\mathrm{e}"] ; Euler's constant
['@pi "\\pi"] ; pi
['@i "i"] ; the imaginary unit
['@n "@n"] ; an arbitrary natural number
['@p "@p"] ; an arbitrary integer
['|%| "\\%"] ; an arbitrary integer
[_ t]))
(define (prepare-unnormalized-for-formatting
u
#:zero-term [zero-term #f] ; #t means: remove 0 in sums
#:one-factor [one-factor #f] ; #t means: rewrite (* 1 u) to u
#:zero-factor [zero-factor #f] ; #t means: rewrite (* 0 u) to 0
#:all [all #t])
; the purpose of this function is to reuse the formatter for normalized
; expressions, for formatting unnormalized expressions.
(when all
(set! zero-term #t)
(set! one-factor #t)
(set! zero-factor #t))
;; Note: Differences and quotients does not appear in normalized expressions.
;; Therefore we need to handle these with care.
;; The pattern ⊖ matches various differences
;; (⊖ x y) matches (- a b) and binds x->a, y->b
;; (⊖ x y) matches (- a b c ...) and binds x->a, y->(+ b c ...)
(define-match-expander ⊖
(λ (stx)
(syntax-parse stx
[(_ u v) #'(or (list '- u v)
(list-rest '- u (app (λ(ys) (cons '+ ys)) v)))]
[(_ u) #'(list '- u)])))
;; The pattern ⊘ matches quotient
;; (⊘ x y) matches (/ a b) and binds x->a, y->b
(define-match-expander ⊘
(λ (stx)
(syntax-parse stx
[(_ u v) #'(list '/ u v)])))
(define (argcons op u v)
(match v
[(list* (== op) vs) (list* op u vs)]
[v (list op u v)]))
(define (p u)
; (displayln (list 'p u))
(define (non-zero? u) (not (equal? 0 u)))
(math-match u
; keep formatting declaration unchanged
[(list 'formatting options u) `(formatting ,options ,(p u))]
; rewrites
[(⊗ 1 v) #:when one-factor (p v)]
[(⊘ u 1) #:when one-factor (p u)]
[(⊗ 0 v) #:when zero-factor 0]
[(⊗ v 0) #:when zero-factor 0]
[(⊕ 0 v) #:when zero-term (p v)]
[(⊕ v 0) #:when zero-term (p v)]
[(⊕ (⊗ 0 u) v) #:when zero-term (p v)]
[(⊕ (⊗ u 0) v) #:when zero-term (p v)]
; note: the above special cases a 0 as the second factor
; a zero as third fact results in a zero term
[(⊖ u 0) #:when zero-term (p u)]
; no rewrites
[r u]
[r.bf u]
[x u]
; rewrite sub expressions
[(⊖ u) (list '- (p u) )]
[(⊖ u v) (argcons '- (p u) (p v))]
[(⊘ u v) (list '/ (p u) (p v))] ; binary only
[(⊗ u v) (argcons '* (p u) (p v))]
[(⊕ u v) (match (list (p u) (p v))
[(list 0 0) #:when zero-term 0]
[(list 0 u) #:when zero-term u]
[(list u 0) #:when zero-term u]
[(list u v) (argcons '+ u v)])]
[(⊕ u) (p u)]
; other
[(And u v) (argcons 'and (p u) (p v))]
[(Or u v) (argcons 'or (p u) (p v))]
[(Equal u v) (list '= (p u) (p v))]
[(Expt u v) (list 'expt (p u) (p v))]
[(Log u) (list 'log (p u))]
[(Log u v) (list 'log (p u) (p v))]
[(Piecewise us vs) (Piecewise: (map p us) (map p vs))]
[(app: f us) (cons f (map p us))]
[_ ; (display u)
(error 'prepare-unnormalized-for-formatting
(~a "internal error, got: " u))]))
(if (string? u)
u
(p u)))
(define prepare prepare-unnormalized-for-formatting)
; ~ converts an expression into a string
; Originally it only formatted normalized expressions, but
; now unnormalized expressions are supported too.
; The output format is configured using parameters.
; The three builtin styles are default, mma and tex.
(define (verbose~ u)
; (displayln (list 'verbose~ u))
(match-define (list app-left app-right) (output-application-brackets))
(match-define (list sub-left sub-right) (output-sub-expression-parens))
(match-define (list expt-left expt-right) (output-sub-exponent-parens))
(match-define (list quot-left quot-right) (output-format-quotient-parens))
;(define use-quotients? (output-use-quotients?))
(define ~sym (let ([sym (output-format-function-symbol)]) (λ (x) (sym x)))) ; function names
(define ~var (let ([out (output-variable-name)]) (λ(x) (out x)))) ; variable names
(define (~relop x) ((output-relational-operator) x))
(define (~setop x) ((output-set-operator) x))
(define (~red str) (~a "{\\color{red}" str "\\color{black}}"))
(define (~blue str) (~a "{\\color{blue}" str "\\color{black}}"))
(define (~green str) (~a "{\\color{green}" str "\\color{black}}"))
(define (~purple str) (~a "{\\color{purple}" str "\\color{black}}"))
(define (~explicit-paren strs)
(case (output-mode)
[(tex) (~a "{\\left(" (string-append* (add-between strs ",")) "\\right)}")]
[else (~a "(" (string-append* (add-between strs ",")) ")")]))
(define (v~ u [original? #f])
; (displayln (list 'v~ u 'orig original?))
(define ~frac (output-fraction))
(define (~num r)
(define precision (output-floating-point-precision))
(cond [(eqv? r -inf.0) "-∞"]
[(eqv? r +inf.0) "∞"]
[(and (exact? r) (> (denominator r) 1)) (~frac r)]
[(exact? r) (~a r)]
[(nan? r) (~a r)]
[precision (~r r #:precision precision)]
[else (~a r)]))
(define (paren u) ; always wrap in ( )
(~a "(" (v~ u #t) ")"))
(define (exponent-wrap s)
(~a expt-left s expt-right))
(define (sub u) ; always wrap in sub-left and sub-right parentheses
(~a sub-left (v~ u #t) sub-right))
(define (exponent-sub u) ; wraps the exponent of an expt-expression
(exponent-wrap (v~ u #t)))
(define (base-sub u) ; wraps the base of an expt-expression
(define (non-integer-fraction? u)
(and (number? u) (exact? u) (> (denominator u) 1)))
(if (and (number? u) (or (negative? u) (non-integer-fraction? u)))
; we only need to add real parens, if expt-left aren't (
(if (equal? expt-left "(")
(~a expt-left (v~ u) expt-right)
(~a expt-left (paren u) expt-right))
(if (equal? expt-left "(")
(~a expt-left (v~ u) expt-right)
(~a expt-left (paren u) expt-right))))
(define (quotient-sub u) ; wraps numerator or denominator of quotient
(~a quot-left (v~ u) quot-right))
(define implicit-mult (if (output-implicit-product?) "" (~sym '*)))
(define (argcons op x xs)
(match xs
[(list* (== op) args) (list* op x args)]
[args (list* op x (list args))]))
(define (implicit* u v) ; returns either (~sym '*) or implicit-mult
; (displayln (list 'implicit* u v))
(cond
[(output-implicit-product?)
(math-match u
; first factor is a number
[r (math-match v
[s (~sym '*)]
[x implicit-mult]
[(⊗ u1 u2) (implicit* r u1)]
[(Expt u1 u2) (implicit* r u1)]
[(list '+ u1 u2 ...) implicit-mult]
[(list 'vec u1 u2 ...) implicit-mult]
[(list 'sqrt u1) implicit-mult]
[(list 'sqr u1) implicit-mult]
[(list 'up _ ...) implicit-mult]
[_ (~sym '*)])]
; first factor is a symbol
[x (define s (~a x))
(if (or (= (string-length s) 1)
(and (string-contains? s "_")
(>= (string-length s) 2)
(eqv? (string-ref s 1) #\_)))
; only single letter variables (possibly with an index) uses implicit
(math-match v
[s (~sym '*)]
[y implicit-mult]
[(⊗ u1 u2) (implicit* x u1)]
[(Expt u1 u2) (implicit* x u1)]
[(list '+ u1 u2 ...) implicit-mult]
[(list 'vec u1 u2 ...) implicit-mult]
[(list 'sqrt u1) implicit-mult]
[(list 'sqr u1) implicit-mult]
[(list 'up _ ...) implicit-mult]
[_ (~sym '*)])
; other variables uses explicit
(~sym '*))]
; anything else is explicit
[_ (~sym '*)])]
; if implicit products are off, always use *
[else (~sym '*)]))
(define (prefix-minus s)
(if (eqv? (string-ref s 0) #\-)
(~a "-(" s ")")
(~a "-" s)))
(define (par u
#:use [wrap paren]
#:wrap-fractions? [wrap-fractions? #f]
#:exponent-base? [exponent-base? #f]
#:exponent-exponent? [exponent-exponent? #f]
) ; wrap if (locally) necessary
(when debugging? (displayln (list 'par u 'orig original? 'exponent-base exponent-base?)))
(math-match u
[(list 'red u) (~red (par u))] ; red color
[(list 'blue u) (~blue (par u))] ; blue color
[(list 'green u) (~green (par u))] ; green color
[(list 'purple u) (~purple (par u))] ; purpe color
[(list 'paren u ...) (~explicit-paren (map v~ u))] ; explicit parens (tex)
[α #:when (and wrap-fractions? (not (integer? α))) (wrap (~frac α))] ; XXX
[α #:when (and (not (integer? α)) exponent-base?) (wrap (~frac α))] ; XXX
[α #:when (not (integer? α)) (~frac α)] ; XXX
[r #:when (>= r 0) (~num r)]
[r.bf #:when (bf>= r.bf (bf 0)) (~a r.bf)]
[x (~a (~var x))]
; infix operators and relations
; [(⊗ 1 v) (exponent-wrap (par v))] ; xxx
[(⊗ 1 v) (exponent-wrap (~a (v~ v original?)))]
[(⊗ -1 v) #:when exponent-base? (exponent-wrap (~a "(-" (v~ v #t) ")"))]
[(⊗ -1 v) #:when original? (let ([s (prefix-minus (v~ v))])
(if (eqv? (string-ref s 0) #\-) (wrap s) (exponent-wrap s)))] ; XX
[(⊗ -1 v) (exponent-wrap (~a "(-" (v~ v #t) ")"))]
[(⊗ u v) #:when exponent-base? (exponent-wrap (paren (~a (par u) (~sym '*) (par v))))] ; TODO XXX ~ two layers
[(⊗ r u) #:when (positive? r) (let ([s (~a (~num (abs r)) (implicit* r u) (par u))])
(if exponent-exponent? (exponent-wrap s) s))] ; XXX YY
[(⊗ u v) #:when original? (let ([s (~a (par u) (implicit* u v) (par v))])
(if exponent-exponent? (wrap s) s))] ; XXX
[(⊗ u v) (let ([s (~a (par u) (implicit* u v) (par v))])
(if exponent-exponent? (wrap s) s))] ; YYY
[(⊕ _ __) (wrap u)]
[(list* '- _ __) (wrap u)]
[(And u v) (~a (par u) " " (~sym 'and) " " (par v))]
[(Or u v) (~a (par u) " " (~sym 'or) " " (par v))]
[(Equal u v) (~a (par u) " " (~sym '=) " " (par v))]
; powers
[(Expt u 1/2) #:when (output-sqrt?) ((output-format-sqrt) u)]
[(Expt u -1) (define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(cond [(output-format-negative-exponent) (~a (par u) "^" (exponent-wrap -1))]
[else (format/ 1 (par u #:use quotient-sub))])]
; unnormalized power of a power
[(Expt (and (Expt u v) w) w1) (~a ((output-sub-exponent-wrapper) ; braces for tex otherwise nothing
(v~ w))
(~sym '^) ((output-sub-exponent-wrapper)
(fluid-let ([original? #t])
(par v #:use exponent-sub
#:wrap-fractions? #t))))]
[(Expt u p) (if (and (negative? p) (output-format-negative-exponent))
; note: LaTeX needs to wrap the base in {} if u is an exponent
(~a ((output-sub-exponent-wrapper) (par u)) "^" (exponent-wrap p))
(~a (par u #:use base-sub #:exponent-base? #t)
(~sym '^) ((output-sub-exponent-wrapper)
(fluid-let ([original? #t])
(par p #:use exponent-sub)))))]
[(Expt u α) #:when (= (numerator α) -1) ; -1/p
(define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(format/ 1 (par (Root u (/ 1 (- α))) #:use quotient-sub))]
[(Expt u v) (~a (par u #:use base-sub #:exponent-base? #t)
(~sym '^) ((output-sub-exponent-wrapper)
(fluid-let ([original? #t])
(par v #:use exponent-sub #:wrap-fractions? #t))))]
[(Log u) ((output-format-log) u)]
[(Log u v) ((output-format-log) u v)]
[(Up u v) ((output-format-up) u v)]
[(app: f us) #:when (memq f '(< > <= >=))
(match us [(list u v) (~a (v~ u) (~relop f) (v~ v))])]
; unnormalized quotient
[(list '/ u v) (define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(define out (format/ (par u #:use quotient-sub) (par v #:use quotient-sub)))
(if exponent-base? (base-sub out) out)]
; unormalized sqr
[(list 'sqr u) (v~ `(expt ,u 2))]
; unormalized sqrt
[(list 'sqrt u) ((output-format-sqrt) u)]
; unnormalized root
[(list 'root u v) ((output-format-root) u v)]
; unnormalized diff
[(list 'diff (list 'sqrt u) x)
#:when (member x (output-differentiation-mark))
(~a "(" ((output-format-sqrt) u) ")'")]
[(list 'diff (list 'vecfun u) x)
#:when (member x (output-differentiation-mark))
(~a "\\vec{" (v~ u) "}^{\\prime}" "(" (v~ x) ")" )]
[(list 'diff f)
#:when (symbol? f) (~a (~sym f) "'")]
[(list 'diff (list f x) x)
#:when (and (symbol? f) (symbol? x)) (~a (~sym f) "'(" (~var x) ")")]
[(list 'diff (list f x) x u) ; f'(x)|x=x0 i.e. f'(x0)
#:when (and (symbol? f) (member x (output-differentiation-mark))) (~a (~sym f) "'(" (v~ u) ")")]
[(list 'diff u x)
#:when (and (symbol? u) (member x (output-differentiation-mark))) (~a (v~ u #t) "' ")]
[(list 'diff u x)
#:when (member x (output-differentiation-mark)) (~a "(" (v~ u #t) ")' ")]
[(list 'diff u x) (~a "\\dv{" (~var x) "}(" (v~ u #t) ") ")]
[(list 'percent u) (~a (v~ u) (~sym '|%|))]
[(list 'abs u) ((output-format-abs) u)]
[(list 'vec u) (let ([s (v~ u)])
(if (= (string-length s) 1)
(~a "\\vec{" s "}")
(~a "\\overrightarrow{" s "}")))] ; TODO: only for TeX
[(list 'vecfun u v) (~a "\\overrightarrow{" (v~ u) "}" "(" (v~ v) ")" )] ; TODO: only for TeX
[(list 'deg u) (~a (v~ u) "° ")] ; TODO: only for TeX
[(list 'hat u) (~a "\\widehat{" (v~ u) "}")] ; TODO: only for TeX
[(list 'bar u) (~a "\\bar{" (v~ u) "}")] ; TODO: only for TeX
[(list* 'braces us) (apply ~a (append (list "\\{") (add-between (map v~ us) ",") (list "\\}")))] ; TODO: only for TeX
[(list* 'bracket us) (apply ~a (append (list "[") (add-between (map v~ us) ",") (list "]")))] ; TODO: only for TeX
;; Set operations
[(list 'Union u v) (~a "{{" (v~ u) "}" (~setop 'Union) "{" (v~ v) "}}")]
[(list 'Intersection u v) (~a "{{" (v~ u) "}" (~setop 'Intersection) "{" (v~ v) "}}")]
[(list 'Difference u v) (~a "{{" (v~ u) "}" (~setop 'Difference) "{" (v~ v) "}}")]
[(list 'Conditional u v) (~a "{{" (v~ u) "}|{" (v~ v) "}}")] ; conditional prob. as in P(A|B)
[(list 'int u v) (cond
[(or (and (number? u) (negative? u))
(match u
[(list '* u0 ...) (and (number? u0) (negative? u0))]
[(list (or '+ '-) _ ...) #t]
[_ #f]))
(~a "\\int " (v~ `(paren ,u)) "\\ \\textrm{d}" (v~ v))]
[else
(~a "\\int " (v~ u) "\\ \\textrm{d}" (v~ v))])] ; TODO: only for TeX
; applications
[(app: f us) (match us
[(list (list 'paren us ...))
(define arguments (apply string-append (add-between (map v~ us) ",")))
(define head ((output-format-function-symbol) f))
(~a head "\\left"app-left arguments "\\right" app-right)]
[_
(define arguments (apply string-append (add-between (map v~ us) ",")))
(define head ((output-format-function-symbol) f))
(~a head app-left arguments app-right)])]
[_ (wrap u)]))
(define (t1~ u) ; term 1 aka first term in a sum
(when debugging? (displayln (list 't1 u)))
(math-match u
[(list 'red u) (~red (t1~ u))]
[(list 'blue u) (~blue (t1~ u))] ; blue color
[(list 'green u) (~green (t1~ u))]
[(list 'purple u) (~purple (t1~ u))]
[(list 'paren u ...) (~explicit-paren (map t1~ u))] ; explicit parens (tex)
; unnormalized and normalized quotients
[(list '/ u v) (define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(format/ (par u #:use quotient-sub) (par v #:use quotient-sub))]
[(Quotient u v) #:when (and (output-use-quotients?) (not (rational? v)))
(define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(format/ (par u #:use quotient-sub) (par v #:use quotient-sub))]
[(⊗ 1 u) (~a (v~ u))]
[(⊗ -1 u) (prefix-minus (par u))]
; integer
; Explicit multiplication between integers
[(⊗ p q) (~a (~num p) (~sym '*) (par q))]
; [(⊗ p u) #:when (negative? p) (~a (~sym '-) (~num (abs p)) (v~ u))] ;
; [(⊗ p u) #:when (positive? p) (~a (~num (abs p)) (v~ u))]
; rationals (non-integer)
; Explicit multiplication between rationals
[(⊗ α β) (~a (~num α) (~sym '*) (par β))]
; problem: if u is a number we need an explicit *
; [(⊗ α u) #:when (negative? α) (~a (~sym '-) (~num (abs α)) (v~ u))]
; [(⊗ α u) #:when (positive? α) (~a (~num (abs α)) (v~ u))]
; other reals
[(⊗ r s) (~a (~num r) (~sym '*) (par s))]
; explicit multiplication for powers with numbers as base
[(⊗ r (and (Expt (num: s) u) v)) #:when (negative? r) (~a "-" (~num (abs r)) (~sym '*) (v~ v))] ; XXX
[(⊗ r (and (Expt (num: s) u) v)) #:when (positive? r) (~a (~num (abs r)) (~sym '*) (v~ v))]
[(⊗ r u) #:when (negative? r) (~a (~sym '-) (~num (abs r)) (implicit* r u) (par u))] ; XXX
[(⊗ r u) #:when (positive? r) (~a (~num (abs r)) (implicit* r u) (par u))] ; XXX
;; Set operations
[(list 'Union u v) (~a "{{" (v~ u) "}" (~setop 'Union) "{" (v~ v) "}}")]
[(list 'Intersection u v) (~a "{{" (v~ u) "}" (~setop 'Intersection) "{" (v~ v) "}}")]
[(list 'Difference u v) (~a "{{" (v~ u) "}" (~setop 'Difference) "{" (v~ v) "}}")]
[(list 'Conditional u v) (~a "{{" (v~ u) "}|{" (v~ v) "}}")] ; conditional prob. as in P(A|B)
[u (v~ u) ]))
(when debugging? (write (list 'v~ u 'orig original?)) (newline))
(math-match u
[(? string? u) u]
[(list 'red u) (~red (v~ u))]
[(list 'blue u) (~blue (v~ u))] ; blue color
[(list 'green u) (~green (v~ u))]
[(list 'purple u) (~purple (v~ u))]
[(list 'paren u ...) (~explicit-paren (map v~ u))] ; explicit parens (tex)
[(list 'formatting options u)
(let loop ([os options])
(match os
['() (v~ u)]
[(list (list 'use-quotients? v) os ...) (parameterize ([output-use-quotients? v]) (loop os))]
[_ (error 'verbose-formatting (~a "unknown option" os))]))]
[α (~frac α)]
[r (~num r)]
[r.bf (bigfloat->string r.bf)]
[x (~a (~var x))]
; unnormalized and normalized quotients
[(list '/ u v) (define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(format/ (par u #:use quotient-sub) (par v #:use quotient-sub))]
[(Quotient u v) #:when (and (output-use-quotients?) (not (rational? v)))
(define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(format/ (par u #:use quotient-sub) (par v #:use quotient-sub))]
[(Expt u -1) (define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(cond [(output-format-negative-exponent)
(~a ((output-sub-exponent-wrapper) (par u)) "^" (exponent-wrap -1))]
[else (format/ 1 (par u #:use quotient-sub))])]
[(Expt u p) #:when (negative? p)
(define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(cond [(and (output-format-negative-exponent) (output-use-quotients?))
; note: the base needs to be wrapped - TeX requires {} around the base to avoid
; doubles superscripts as in a^2^3
(~a ((output-sub-exponent-wrapper) (par u)) "^" (exponent-wrap p))]
[(output-use-quotients?)
(format/ 1 (par (Expt u (- p)) #:use quotient-sub #:exponent-base? #t))]
[else
(~a ((output-sub-exponent-wrapper) (par u #:exponent-base? #t)) "^" (exponent-wrap p))])]
[(Expt u α) #:when (and (output-root?) (= (numerator α) 1) (> (abs (denominator α)) 1)
((output-format-root) u (/ 1 α))) ; α=1/n
((output-format-root) u (/ 1 α))] ; only used, if (output-format-root) returns non-#f
[(Expt u α) #:when (= (numerator α) -1) ; -1/p
(define format/ (or (output-format-quotient) (λ (u v) (~a u "/" v))))
(format/ 1 (par (Root u (/ 1 (- α))) #:use quotient-sub #:exponent-base? #t))]
; mult
[(⊗ 1 v) (~a (v~ v))]
[(⊗ -1 α) #:when (negative? α) (~a "-" (paren (v~ α)))]
[(⊗ -1 α) (~a "-" (v~ α))]
[(⊗ -1 x) (~a "-" (v~ x))]
[(⊗ -1 (Expt u v)) (~a "-" (v~ `(expt ,u ,v)))]
[(⊗ -1 (⊗ u v)) #:when (not (equal? v '(*))) (~a "-" (v~ `(* ,u ,v)))] ; ex: (verbose~ '(* -1 4))
[(⊗ -1 v) (~a "-" (paren (v~ v)))]
[(⊗ -1 p v) #:when (and original? (negative? p)) ; (displayln (list "A" p v (⊗ p v)))
(~a "-" (paren (v~ (⊗ p v) #f)))] ; wrong
[(⊗ -1 v) #:when original? (~a "-" (v~ v))]
; [(⊗ -1 p v) #:when (negative? p) (~a "-" (paren (v~ (⊗ p v) #f)))] ; wrong
[(⊗ -1 v) (paren (~a "-" (v~ v)))]
; Explicit multiplication between integers
[(⊗ p q) #:when original? (~a (~num p) (~sym '*) (par q))]
[(⊗ p q) #:when (not (negative? p)) (~a (~num p) (~sym '*) (par q))]
[(⊗ p q) #:when (negative? p) (~a "(" (~num p) ")" (~sym '*) (par q))]
; An implicit multiplication can not be used for fractions
;[(⊗ p v) #:when (negative? p) (~a "-" (~num (abs p)) implicit-mult (par v #:use paren))]
;[(⊗ p v) #:when (positive? p) (~a (~num (abs p)) implicit-mult (par v #:use paren))]
;[(⊗ α u) #:when (= (numerator α) 1) (~a "\\frac{" (v~ u) "}{" (~num (/ α)) "}")]
;[(⊗ α u) #:when (= (numerator α) -1) (~a "\\frac{" (v~ u) "}{" "-" (~num (/ (abs α))) "}")]
; Implicit multiplication only if we have a symbols as base
[(⊗ r (and (Expt (var: x) u) v)) #:when (negative? r) (if original?
(~a "-" (~num (abs r)) implicit-mult (v~ v #t))
(~a (paren (~a "-" (~num (abs r)))) implicit-mult (v~ v #t)))] ; XXXXX *
[(⊗ r (and (Expt (var: x) u) v)) #:when (positive? r) (~a (~num (abs r)) implicit-mult (v~ v #t))]
; Implicit multiplication between numbers and variables
[(⊗ r x) (~a (~num r) (~var x))] ; XXXX
; Use explicit multiplication for fractions
[(⊗ r (⊗ u v)) #:when (and (negative? r) (not (equal? '(*) v)))
;(displayln 'X1)
(~a "-" (~num (abs r)) (implicit* r u) (v~ (argcons '* u v)))]
[(⊗ r (⊗ u v)) #:when (and (positive? r) (not (equal? '(*) v)))
;(displayln (list 'X2 r u v (argcons '* u v) (fluid-let ([original? #t]) (v~ (argcons '* u v) #t))))
(~a (~num (abs r)) (implicit* r u) (fluid-let ([original? #t]) (v~ (argcons '* u v) #t)))] ; XXX
[(⊗ r v) #:when (negative? r)
; (displayln 'X3)
(define w (if original? values paren))
(~a (w (~a "-" (~num (abs r)))) (implicit* r v) (par v #:use paren))] ; XXX
[(⊗ r v) #:when (positive? r)
;(displayln 'X4)
(~a (~num r) (implicit* r v) (par v #:use paren))] ; XXX
[(⊗ u v) #:when (not (equal? '(*) v))
; (displayln (list 'X5 u v (par u) (fluid-let ([original? #t]) (par v))))
(~a (par u) (implicit* u v) (fluid-let ([original? #t]) (par v)))]
; plus
[(⊕ u r) (if (negative? r)
(~a (t1~ u) (~sym '-) (~num (abs r)))
(~a (t1~ u) (~sym '+) (~num (abs r))))]
[(⊕ u (⊗ -1 v)) (~a (t1~ u) (~sym '-) (par v))] ; YYY
; Unnormalized (in a normalized expression only the first factor can be a number)
[(⊕ u (⊗ r s)) #:when (negative? r) (~a (t1~ u) (~sym '-) (~num (abs r)) (~sym '*) (par s))]
[(⊕ u (⊗ r s)) #:when (positive? r) (~a (t1~ u) (~sym '+) (~num (abs r)) (~sym '*) (par s))]
; previous two rules ensure that v is non-empty
[(⊕ u (⊗ r (⊗ s v))) #:when (negative? r)
(~a (t1~ u) (~sym '-) (~num (abs r)) (~sym '*) (par s) (~sym '*) (v~ v))]
[(⊕ u (⊗ r (⊗ s v))) #:when (positive? r)
(~a (t1~ u) (~sym '+) (~num (abs r)) (~sym '*) (par s) (~sym '*) (v~ v))]
;
[(⊕ u (⊗ r v)) #:when (negative? r)
(~a (t1~ u) (~sym '-) (v~ (⊗ (abs r) v)))]
[(⊕ u (⊗ r v)) #:when (positive? r)
(~a (t1~ u) (~sym '+) (v~ (⊗ r v)))]
[(⊕ u (⊕ (⊗ -1 v) w)) (~a (t1~ u) (~sym '-) (v~ (argcons '+ (par v) w)))] ; YYY
; [(⊕ u (⊕ (⊗ r v) w)) #:when (negative? r) (displayln (list 'EEE r v))
; (~a (t1~ u) (~sym '-) (v~ (argcons '+ (list '* (abs r) v) w)))]
; [(⊕ u (⊕ (⊗ r v) w)) #:when (positive? r) (displayln (list 'FFF r v))
; (~a (t1~ u) (~sym '+) (v~ (argcons '+ (list '* (abs r) v) w)))]
; TODO: Problem: If v is a negative number, we need a paren around v.
;; [(⊕ u (⊕ (⊗ r v) w)) #:when (negative? r) (displayln (list 'EEE r v))
;; (~a (t1~ u) (~sym '-) (~num (abs r)) (implicit* r v) (v~ (argcons '+ v w)))]
;; ; TODO: Problem: If v is a negative number, we need a paren around v.
;; [(⊕ u (⊕ (⊗ r v) w)) #:when (positive? r) (displayln (list 'FFF r v))
;; (~a (t1~ u) (~sym '+) (~num (abs r)) (implicit* r v) (v~ (argcons '+ v w)))]
[(⊕ u v) (match v
[(? number? r) #:when (negative? r) (~a (t1~ u) (v~ v))]
[(list* '* (? number? r) _) #:when (negative? r) (~a (t1~ u) (v~ v))]
[(list* '+ (? number? r) _) #:when (negative? r) (~a (t1~ u) (v~ v))]
[(list* '+ (list* '* (? number? r) _) _) #:when (negative? r) (~a (t1~ u) (v~ v))]
[_ (~a (t1~ u) (~sym '+) (v~ v))])]
; minus (doesn't appear in normalized expressions)
[(list '- u) (~a (~sym '-) (par u #:use paren))]
[(list* '- u v) (~a (t1~ u) (~sym '-)
(par (match v
[(list v) v]
[(list* vs) (cons '+ vs)])
#:use paren))]
; other
[(And (Less u v) (Less u1 v1)) #:when (equal? v u1)
(~a (v~ u) " " (~relop '<) " " (v~ v) " " (~relop '<) " " (v~ v1))]
[(And (LessEqual u v) (Less u1 v1)) #:when (equal? v u1)
(~a (v~ u ) " " (~relop '<=) " " (v~ v) " " (~relop '<) " " (v~ v1))]
[(And (LessEqual u v) (LessEqual u1 v1)) #:when (equal? v u1)
(~a (v~ u) " " (~relop '<=) " " (v~ v) " " (~relop '<=) " " (v~ v1))]
[(And (Less u v) (LessEqual u1 v1)) #:when (equal? v u1)
(~a (v~ u) " " (~relop '<) " " (v~ v) " " (~relop '<=) " " (v~ v1))]
[(And u v) (~a (par u) " " (~sym 'and) " " (par v))]
; todo: if u or v contains And or Or in u or v then we need parentheses as in the And line
[(Or u v) (~a (v~ u) " " (~sym 'or) " " (v~ v))]
[(list '= v) (~a (~sym '=) (v~ v))]
[(list* '= us) ; handle illegal = with multiple terms
(string-append* (add-between (map (λ (u) (v~ u #t)) us) (~a " " (~relop '=) " ")))]
[(list '⇔ u) ; handle ⇔ with single term (i.e. line that begins with ⇔
(~a "\\ \\ \\Leftrightarrow \\ \\ " (v~ u #t))]
[(list* '⇔ us) ; handle ⇔ with multiple terms
(string-append* (add-between (map (λ (u) (v~ u #t)) us) "\\ \\ \\Leftrightarrow \\ \\ "))]
[(list '~ v) (~a (~sym '~) (v~ v))]
[(list* '~ us)
(string-append* (add-between (map (λ (u) (v~ u #t)) us) (~a " " (~relop '~) " ")))]
[(Equal u v) (~a (v~ u #t) " " (~relop '=) " " (v~ v #t))] ; never reached!!
; [(⊖ u v) (~a (par u) "-" (v~ v))]
; [(⊘ u v) (~a (par u) (~sym '/) (par v))]
[(Expt u 1/2) #:when (output-sqrt?) ((output-format-sqrt) u)]
; unnormalized power of a power
[(Expt (and (Expt u v) w) w1) (~a ((output-sub-exponent-wrapper)
(v~ w))
(~sym '^) (fluid-let ([original? #t])
((output-sub-exponent-wrapper)
(par w1 #:use exponent-sub
#:wrap-fractions? #t))))]
[(Expt u v) (~a (par u #:exponent-base? #t #:wrap-fractions? #t) (~sym '^)
(fluid-let ([original? #t])
((output-sub-exponent-wrapper)
(par v #:use exponent-sub #:exponent-exponent? #t
#:wrap-fractions? #t))))]
;; Set operations
[(list 'Union u v) (~a "{{" (v~ u) "}" (~setop 'Union) "{" (v~ v) "}}")]
[(list 'Intersection u v) (~a "{{" (v~ u) "}" (~setop 'Intersection) "{" (v~ v) "}}")]
[(list 'Difference u v) (~a "{{" (v~ u) "}" (~setop 'Difference) "{" (v~ v) "}}")]
[(list 'Conditional u v) (~a "{{" (v~ u) "}|{" (v~ v) "}}")] ; conditional prob. as in P(A|B)
[(App u v) (match u
[(? symbol? f) (~a f "(" (v~ v) ")")]
[(list 'vec (? symbol? f)) (~a "\\vec{" f "}" "(" (v~ v) ")")]
[_ (error '~v
"the first subform of App is restricted to f and (vec f) in format")])]
; Unnormalized
[(list 'sqr u) (v~ `(expt ,u 2))]
[(list 'in u v) (~a (v~ u) "\\in " (v~ v))]
; handle sqrt first
[(list 'diff (list 'sqrt u) x)
#:when (member x (output-differentiation-mark))
(~a "(" ((output-format-sqrt) u) ")'")]
[(list 'diff f)
#:when (symbol? f) (~a (~sym f) "'")]
[(list 'diff (list 'vecfun u x))
#:when (member x (output-differentiation-mark))
(~a "\\vec{" (v~ u) "}^{\\prime}" "(" (v~ x) ")" )]
[(list 'diff (list f x) x)
#:when (and (symbol? f) (symbol? x)) (~a (~sym f) "'(" (~var x) ")")]
[(list 'diff (list f x) x u) ; f'(x)|x=u i.e. f'(u)
#:when (and (symbol? f) (member x (output-differentiation-mark))) (~a (~sym f) "'(" (v~ u) ")")]
[(list 'diff u x)
#:when (and (symbol? u) (member x (output-differentiation-mark))) (~a (v~ u #t) "' ")]
[(list 'diff u x)
#:when (member x (output-differentiation-mark)) (~a "(" (v~ u #t) ")' ")]
[(list 'diff u x) (~a "\\dv{" (~var x) "}(" (v~ u #t) ") ")]
[(Equal u v) (~a (v~ u #t) (~sym '=) (v~ v #t))]
[(Log u) ((output-format-log) u)]
[(Log u v) ((output-format-log) u v)]
[(Up u v) ((output-format-up) u v)]