Unparse now display products with negative powers as fractions.
This commit is contained in:
parent
034cd321f6
commit
0538280d65
|
@ -861,6 +861,7 @@
|
||||||
(concurrent-substitute u ts rs))
|
(concurrent-substitute u ts rs))
|
||||||
|
|
||||||
(define (Equal u1 u2)
|
(define (Equal u1 u2)
|
||||||
|
(displayln (list 'Equal u1 u2))
|
||||||
(cond
|
(cond
|
||||||
[(and (number? u1) (number? u2))
|
[(and (number? u1) (number? u2))
|
||||||
(= u1 u2)]
|
(= u1 u2)]
|
||||||
|
@ -868,9 +869,12 @@
|
||||||
(string=? u1 u2)]
|
(string=? u1 u2)]
|
||||||
[(equal? u1 u2)
|
[(equal? u1 u2)
|
||||||
true]
|
true]
|
||||||
|
#;[(equal? (Expand (Minus u1 u2)) 0)
|
||||||
|
true]
|
||||||
[else (construct 'Equal (list u1 u2))]))
|
[else (construct 'Equal (list u1 u2))]))
|
||||||
|
|
||||||
(define (Expand u)
|
(define (Expand u)
|
||||||
|
(displayln u)
|
||||||
; [Cohen, Elem, p.253]
|
; [Cohen, Elem, p.253]
|
||||||
(case (kind u)
|
(case (kind u)
|
||||||
[(Plus)
|
[(Plus)
|
||||||
|
@ -887,7 +891,9 @@
|
||||||
[(power-expression? u) (Expand-power u)]
|
[(power-expression? u) (Expand-power u)]
|
||||||
[else u]))
|
[else u]))
|
||||||
(Expand-product (Expand v)
|
(Expand-product (Expand v)
|
||||||
(Expand (Quotient u v))))]
|
(Expand (Quotient u v))))
|
||||||
|
#;(Expand-product (Expand v)
|
||||||
|
(Expand (Quotient u v)))]
|
||||||
[(Power)
|
[(Power)
|
||||||
(define base (Operand u 0))
|
(define base (Operand u 0))
|
||||||
(define exponent (Operand u 1))
|
(define exponent (Operand u 1))
|
||||||
|
@ -1424,6 +1430,7 @@
|
||||||
(check-equal? (Match-linear-form (Plus 3 x y) x) (List 1 (Plus 3 y)))
|
(check-equal? (Match-linear-form (Plus 3 x y) x) (List 1 (Plus 3 y)))
|
||||||
; List:
|
; List:
|
||||||
; Equal:
|
; Equal:
|
||||||
|
;(check-equal? (Equal (Power (Minus x 1) 2) (Plus (Power x 2) (Times -2 x) 1)) true)
|
||||||
|
|
||||||
;;; Solve
|
;;; Solve
|
||||||
(require (submod ".." solve))
|
(require (submod ".." solve))
|
||||||
|
|
|
@ -6,7 +6,8 @@
|
||||||
(provide unparse)
|
(provide unparse)
|
||||||
|
|
||||||
(require (submod "bracket.rkt" expression-core)
|
(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)
|
(define (map/first? base f xs)
|
||||||
|
@ -55,10 +56,23 @@
|
||||||
[(eqv? (first ops) -1)
|
[(eqv? (first ops) -1)
|
||||||
(string-append "-" (unparse-product (cons 'Times (rest ops))))]
|
(string-append "-" (unparse-product (cons 'Times (rest ops))))]
|
||||||
[else
|
[else
|
||||||
|
(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
|
(define unwrapped
|
||||||
(string-append* (add-between (map unparse-factor ops) "*")))
|
(string-append* (add-between (map unparse-factor ops) "*")))
|
||||||
(wrap-if (and level-below-times? (>= (length ops) 2))
|
(wrap-if (and level-below-times? (>= (length ops) 2))
|
||||||
unwrapped)])]
|
unwrapped))
|
||||||
|
(if (empty? den)
|
||||||
|
(format-factors num)
|
||||||
|
(format "~a/~a"
|
||||||
|
(format-factors num)
|
||||||
|
(wrap-if (>= (length den) 2)
|
||||||
|
(unparse (apply Times den)))))])]
|
||||||
[else
|
[else
|
||||||
(unparse-factor form first?)]))
|
(unparse-factor form first?)]))
|
||||||
|
|
||||||
|
@ -148,6 +162,9 @@
|
||||||
(check-equal? (unparse '(Times -2 x)) "-2*x")
|
(check-equal? (unparse '(Times -2 x)) "-2*x")
|
||||||
; Products of products (unsimplified)
|
; Products of products (unsimplified)
|
||||||
(check-equal? (unparse '(Times 2 (Times 3 x) (Times -5 y))) "2*(3*x)*(-5*y)")
|
(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
|
; Powers
|
||||||
(check-equal? (unparse '(Power 2 3)) "2^3")
|
(check-equal? (unparse '(Power 2 3)) "2^3")
|
||||||
(check-equal? (unparse '(Power -2 3)) "(-2)^3")
|
(check-equal? (unparse '(Power -2 3)) "(-2)^3")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user