diff --git a/bracket/bracket.rkt b/bracket/bracket.rkt index f99542c5d..f08215f0a 100644 --- a/bracket/bracket.rkt +++ b/bracket/bracket.rkt @@ -618,7 +618,7 @@ - (define (simplify-minus ops) + (define (simplify-minus ops) ; - unary and binary does not occur in simplified expressions ; - (Minus x) => (Times -1 x) ; - (Minus a b) => (Plus a (Times -1 b)) @@ -861,6 +861,7 @@ (concurrent-substitute u ts rs)) (define (Equal u1 u2) + (displayln (list 'Equal u1 u2)) (cond [(and (number? u1) (number? u2)) (= u1 u2)] @@ -868,9 +869,12 @@ (string=? u1 u2)] [(equal? u1 u2) true] + #;[(equal? (Expand (Minus u1 u2)) 0) + true] [else (construct 'Equal (list u1 u2))])) (define (Expand u) + (displayln u) ; [Cohen, Elem, p.253] (case (kind u) [(Plus) @@ -887,7 +891,9 @@ [(power-expression? u) (Expand-power u)] [else u])) (Expand-product (Expand v) - (Expand (Quotient u v))))] + (Expand (Quotient u v)))) + #;(Expand-product (Expand v) + (Expand (Quotient u v)))] [(Power) (define base (Operand u 0)) (define exponent (Operand u 1)) @@ -1424,6 +1430,7 @@ (check-equal? (Match-linear-form (Plus 3 x y) x) (List 1 (Plus 3 y))) ; List: ; Equal: + ;(check-equal? (Equal (Power (Minus x 1) 2) (Plus (Power x 2) (Times -2 x) 1)) true) ;;; Solve (require (submod ".." solve)) diff --git a/bracket/unparse.rkt b/bracket/unparse.rkt index 8fb5470a7..1ee0f6382 100644 --- a/bracket/unparse.rkt +++ b/bracket/unparse.rkt @@ -6,7 +6,8 @@ (provide unparse) (require (submod "bracket.rkt" expression-core) - (submod "bracket.rkt" equation-expression)) + (submod "bracket.rkt" equation-expression) + (submod "bracket.rkt" bracket)) (define (map/first? base f xs) @@ -55,10 +56,23 @@ [(eqv? (first ops) -1) (string-append "-" (unparse-product (cons 'Times (rest ops))))] [else - (define unwrapped - (string-append* (add-between (map unparse-factor ops) "*"))) - (wrap-if (and level-below-times? (>= (length ops) 2)) - unwrapped)])] + (define-values (den num) + (partition (λ (f) (and (power-expression? f) + (number? (exponent f)) + (negative? (exponent f)))) + ops)) + (set! den (map (λ (f) (Power (base f) (- (exponent f)))) den)) + (define (format-factors ops) + (define unwrapped + (string-append* (add-between (map unparse-factor ops) "*"))) + (wrap-if (and level-below-times? (>= (length ops) 2)) + unwrapped)) + (if (empty? den) + (format-factors num) + (format "~a/~a" + (format-factors num) + (wrap-if (>= (length den) 2) + (unparse (apply Times den)))))])] [else (unparse-factor form first?)])) @@ -148,6 +162,9 @@ (check-equal? (unparse '(Times -2 x)) "-2*x") ; Products of products (unsimplified) (check-equal? (unparse '(Times 2 (Times 3 x) (Times -5 y))) "2*(3*x)*(-5*y)") + ; Products with factors with negative exponents + (check-equal? (unparse (Quotient x y)) "x/y") + (check-equal? (unparse (Quotient x (Times y z))) "x/(y*z)") ; Powers (check-equal? (unparse '(Power 2 3)) "2^3") (check-equal? (unparse '(Power -2 3)) "(-2)^3")