Skip to content

Commit b4ec476

Browse files
committed
Repair issue #32
1 parent a236b42 commit b4ec476

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

racket-cas/core.rkt

+24-18
Original file line numberDiff line numberDiff line change
@@ -480,21 +480,21 @@
480480
[(v 1) `(+ @i ,v)]
481481
[(v u) `(+ ,(⊗ @i u) ,v)])]
482482
; other
483-
[(u s) (plus2 s u)] ; ok since u can not be a number nor @i, we have that s <<= u
484-
[(u u) (times2 2 u)]
483+
[(u s) (plus2 s u)] ; ok since u can not be a number nor @i, we have that s <<= u
484+
[(u u) (times2 2 u)]
485485
[((k⊗ r u) (k⊗ s u)) (times2 (+ r s) u)]
486-
[((k⊗ r u) (k⊗ s v)) #:when (<<= v u) (plus2 s2 s1)]
487-
[((⊕ u v) (⊕ _ _)) (plus2 u (plus2 v s2))]
488-
[((⊕ u v) _) (plus2 u (plus2 v s2))]
489-
[(u (⊕ v w))
490-
(if (<<= u v)
491-
(match (plus2 u v)
492-
[(cons '+ _) (match w
493-
[(cons '+ ws) (list* '+ u v ws)]
494-
[_ (list '+ u v w)])]
495-
[u+v (plus2 u+v w)])
496-
(plus2 v (plus2 u w)))]
497-
[(_ _) (list '+ s1 s2)]))
486+
[((k⊗ r u) (k⊗ s v))
487+
#:when (<<= v u) (plus2 s2 s1)]
488+
[((⊕ u v) (⊕ _ _)) (plus2 u (plus2 v s2))]
489+
[((⊕ u v) _) (plus2 u (plus2 v s2))]
490+
[(u (⊕ v w)) (if (<<= u v)
491+
(match (plus2 u v)
492+
[(cons '+ _) (match w
493+
[(cons '+ ws) (list* '+ u v ws)]
494+
[_ (list '+ u v w)])]
495+
[u+v (plus2 u+v w)])
496+
(plus2 v (plus2 u w)))]
497+
[(_ _) (list '+ s1 s2)]))
498498

499499
(module+ test
500500
(displayln "TEST - Plus")
@@ -565,8 +565,11 @@
565565
(define (times2 s1 s2)
566566
(when verbose-debugging? (displayln (list 'times2 s1 s2)))
567567
(math-match* (s1 s2)
568-
[(0 u) 0] [(u 0) 0]
569-
[(1 u) u] [(u 1) u]
568+
[(0 u) 0]
569+
[(u 0) 0]
570+
[(1 u) u]
571+
[(u 1) u]
572+
570573
[(r s) (* r s)]
571574

572575
[(@i @i) -1]
@@ -587,9 +590,10 @@
587590
[((Expt u v) u) #:when (not (integer? u)) (Expt u (⊕ 1 v))]
588591
[((Expt u v) (Expt u w)) (Expt u (⊕ v w))]
589592
[(x y) (if (symbol<<? x y) (list '* x y) (list '* y x))]
593+
[(-1 (⊕ u v)) (⊕ (times2 -1 u) (times2 -1 v))] ; Issue #32
590594
; all recursive calls must reduce size of s1 wrt <<=
591595
[((⊗ u v) (⊗ _ __)) (times2 u (times2 v s2))]
592-
[((⊗ u v) w) (times2 s2 s1)]
596+
[((⊗ u v) w) (times2 s2 s1)]
593597
[(u (⊗ v w))
594598
(if (<<= u v)
595599
(match (times2 u v)
@@ -626,7 +630,9 @@
626630
(check-equal? (⊗ x '(cos x)) '(* x (cos x)))
627631
(check-equal? (⊗ (⊗ x y) (Sqr (⊗ x y))) (⊗ (Expt x 3) (Expt y 3)))
628632
(check-equal? (⊗ 2 (Expt 2 1/2)) '(* 2 (expt 2 1/2)))
629-
(check-equal? (⊗ (Expt 2 1/2) 2) '(* 2 (expt 2 1/2))))
633+
(check-equal? (⊗ (Expt 2 1/2) 2) '(* 2 (expt 2 1/2)))
634+
(check-equal? (⊗ -1 (⊕ 1 x)) '(+ -1 (* -1 x)))
635+
(check-equal? (⊕ 1 (⊕ x -1)) 'x))
630636

631637

632638

racket-cas/math-match.rkt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
; For completeness, code that uses math-match will often use
2323
; u v to indicate normalized expressions
24-
; a b to indeicate general (maybe unnormalized) expressions
24+
; a b to indicate general (maybe unnormalized) expressions
2525

2626

2727
(require (for-syntax racket/string racket/match racket/syntax)

racket-cas/new-format.rkt

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(provide use-minus-in-sums?
55
implicit-product?
66
implicit-minus-one-as-first-factor?
7+
explicit-one-as-first-factor?
78
output-root?
89
output-root-as-power?
910
use-sqrt-for-two-as-root-exponent?
@@ -55,6 +56,10 @@
5556
; If #t: (* -1 x) -> -x
5657
; If #f: (* -1 x) -> -1*x (or -1x if implicit-prodcut? is #t
5758

59+
(define explicit-one-as-first-factor? (make-parameter #f))
60+
; If #t: (* 1 x) -> 1*x
61+
; If #f: (* 1 x) -> x (or 1x if implicit-prodcut? is #t)
62+
5863
(define output-root? (make-parameter #f))
5964
; If #t: (expt u 1/n) -> root(u,n)
6065
; If #t: (expt u 1/n) -> u^(1/n)
@@ -595,7 +600,7 @@
595600
(define (format-product ctx x)
596601
; Note: implicit-minus-one-as-first-factor affects the output
597602
; If #t: (* -1 x) -> -x
598-
; If #f: (* -1 x) -> -1*x (or -1x if implicit-prodcut? is #t
603+
; If #f: (* -1 x) -> -1*x (or -1x if implicit-product? is #t
599604
(define explicit (case (mode) [(latex) "\\cdot "] [else "*"]))
600605
(define implicit (if (implicit-product?) "" explicit))
601606
(define (implicit* u v) ; returns either explicit or implicit
@@ -648,7 +653,9 @@
648653
[(list '* first-factor)
649654
(format-factor (list* 'first-factor ctx) first-factor)]
650655
[(list '* 1 last-factor)
651-
(~a (format-factor (list* 'last-factor ctx) last-factor))]
656+
(if (explicit-one-as-first-factor?)
657+
(~a 1 implicit (format-factor (list* 'last-factor ctx) last-factor))
658+
(~a (format-factor (list* 'last-factor ctx) last-factor)))]
652659
[(list '* -1 last-factor)
653660
(cond
654661
[(implicit-minus-one-as-first-factor?)
@@ -1958,6 +1965,19 @@
19581965
(check-equal? (~ `(expt 2 -2)) "$2^{-2}$")
19591966
(check-equal? (~ '(expt y -4)) "$y^{-4}$"))
19601967
(check-equal? (~ '(expt 2 1)) "$2^1$")
1968+
1969+
1970+
(parameterize ([explicit-one-as-first-factor? #t]
1971+
[implicit-product? #t])
1972+
(check-equal? (~ `(* 1 x)) "$1x$")
1973+
(check-equal? (~ `(sin (* 1 x))) "$\\sin(1x)$")
1974+
(check-equal? (~ `(* 1 (sin (* 1 x)))) "$1\\sin(1x)$"))
1975+
(parameterize ([explicit-one-as-first-factor? #t]
1976+
[implicit-product? #f])
1977+
(check-equal? (~ `(* 1 x)) "$1\\cdot x$")
1978+
(check-equal? (~ `(sin (* 1 x))) "$\\sin(1\\cdot x)$")
1979+
(check-equal? (~ `(* 1 (sin (* 1 x)))) "$1\\cdot \\sin(1\\cdot x)$"))
1980+
19611981
;;; END LATEX
19621982
)
19631983

racket-cas/normalize.rkt

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
[(app: f us) (match u
7979
[(list '/ u v) (⊘ (n u) (n v))]
8080
[(list '- u) (⊖ (n u))]
81-
[(list '- u v) ( (n u) (n v))]
81+
[(list '- u v) ( (n u) (-1 (n v)))]
8282
[(list 'tan v) (Tan (n v))]
8383
[(list 'sqr u) (Sqr (n u))]
8484
[(list 'sqrt u) (Sqrt (n u))]

0 commit comments

Comments
 (0)