Removed references to the the graphics submodule due to the mysterious submodule error

This commit is contained in:
Jens Axel Søgaard 2012-06-20 23:02:24 +02:00
parent 0ee88d6310
commit 37bb90af0b
3 changed files with 253 additions and 260 deletions

View File

@ -1,5 +1,26 @@
#lang racket
#;(module bracket-graphics racket
(require "../graphics/graphics.rkt")
(define-syntax (declare/provide-vars stx)
(syntax-case stx ()
[(_ id ...)
#'(begin
(define id 'id) ...
(provide id) ...)]))
(provide Graphics)
(declare/provide-vars
Blend Darker Hue Lighter
Circle Disk Line Point Rectangle
Text Thickness
; Colors
Red Blue Green Black White Yellow
; Options
ImageSize PlotRange))
;;; An ATOMIC EXPRESSION is an
; - number (integer, real, complex)
; - reserved symbol (pi, e, i, inf, true, false)
@ -112,22 +133,22 @@
; This version prevents duplication of args.
; But uses apply in place of #%app
#;(define-syntax (sym-app stx)
(syntax-case stx ()
[(_ op arg ...)
(quasisyntax/loc stx
(let ([o op]
[as (λ () (list arg ...))])
; TODO: 1. If o is Flat, then flattened nested expression with o.
; TODO: 2. If o is Listable, then ...
; TODO: 3. If o is Orderless then ...
; TODO: 4. If o has associated rules ...
; TODO:
; REF: http://reference.wolfram.com/mathematica/tutorial/Evaluation.html
(if (procedure? o)
#,(syntax/loc stx (apply o (as)))
(if (holdable? o)
(cons o '(arg ...))
(cons o (as))))))])))
(syntax-case stx ()
[(_ op arg ...)
(quasisyntax/loc stx
(let ([o op]
[as (λ () (list arg ...))])
; TODO: 1. If o is Flat, then flattened nested expression with o.
; TODO: 2. If o is Listable, then ...
; TODO: 3. If o is Orderless then ...
; TODO: 4. If o has associated rules ...
; TODO:
; REF: http://reference.wolfram.com/mathematica/tutorial/Evaluation.html
(if (procedure? o)
#,(syntax/loc stx (apply o (as)))
(if (holdable? o)
(cons o '(arg ...))
(cons o (as))))))])))
(module expression-core racket
(require (submod ".." identifiers)
@ -204,7 +225,7 @@
(when compound?
(hash-set! recent-compound-expressions e #t))
compound?)))
(define (construct operator operands)
(cons operator operands))
@ -258,7 +279,7 @@
(submod ".." undefined)
(submod ".." identifiers))
(require (planet dherman/memoize:3:1))
(provide simplify
simplify-plus
simplify-minus
@ -323,7 +344,7 @@
[(1) (first vs)]
[(0) 0]
[else (construct 'Plus vs)]))])]))
(define (simplify-plus-rec us)
; a list of terms is received,
; a list of simplified terms is returned.
@ -369,9 +390,9 @@
(λ (u2i)
(let ([us (simplify-plus-rec (cons u1 (list u2i)))])
; If length(us)>=2 wrap with Plus
(if (empty? (rest us))
(first us)
(construct 'Plus us))))
(if (empty? (rest us))
(first us)
(construct 'Plus us))))
(operands u2)))]
[(before? u2 u1) (list u2 u1)]
[else (list u1 u2)])]
@ -407,7 +428,7 @@
(cons q1 (merge-sums p (rest q)))
(cons p1 (merge-sums (rest p) q)))]
[else (error)])]))
(define/memo (simplify-times ops)
;(displayln (list 'simplify-times ops))
; the operands os are simplified
@ -530,106 +551,106 @@
; See [Cohen] for the complete algorithm and explanation.
(define result
(cond
[(and (number? u) (number? v))
; straightforward for two real numbers,
; but complex numbers must be handled too.
(if (= u v)
#t
(if (real? u)
(if (real? v) (< u v) v)
(if (real? v)
v
(if (= (imag-part u) (imag-part v))
(< (real-part u) (real-part v))
(< (imag-part u) (imag-part v))))))]
; Number always come first
[(number? u) #t]
[(number? v) #f]
; Symbols are sorted in alphabetical order
[(and (symbol? u) (symbol? v))
(string<? (symbol->string u) (symbol->string v))]
; For products and sums order on the last different
; factor or term. Thus x+y < y+z.
[(or (and (times-expression? u) (times-expression? v))
(and (plus-expression? u) (plus-expression? v)))
(define first-non-equal
(for/first ([ui (in-list (reverse (operands u)))]
[vi (in-list (reverse (operands v)))]
#:unless (equal? ui vi))
(list ui vi)))
(if first-non-equal
(apply before? first-non-equal)
(< (length (operands u)) (length (operands v))))]
; When comparing a product with something else, use the last factor.
; Thus x*y < z and y < x*z.
; Note: This is consistent with comparisons of two products since
; x*y < 1*z and 1*y < x*z.
[(and (times-expression? u)
(or ;(power-expression? v)
;(plus-expression? v)
(symbolic-id? v)
(compound-expression? v)))
(let ([un (last-operand u)])
(or (equal? un v) (before? un v)))]
[(and (times-expression? v)
(or ;(power-expression? v)
;(plus-expression? v)
(symbolic-id? u)
(compound-expression? u)))
(define vn (last-operand v))
(or (equal? vn u) (before? u vn))]
; Powers with smallest base are first. 2^z < 3^y
[(and (power-expression? u) (power-expression? v))
(if (equal? (base u) (base v))
(before? (exponent u) (exponent v))
(before? (base u) (base v)))]
; When comparing a product with something else, pretend
; the else part is a power with exponent 1.
; Thus x^2 > x.
[(and (power-expression? u)
(or ; (plus-expression? v)
(symbolic-id? v)
(compound-expression? v)))
(before? u (construct 'Power (list v 1)))]
[(and (power-expression? v)
(or ; (plus-expression? v)
(symbolic-id? u)
(compound-expression? u)))
(before? (construct 'Power (list u 1)) v)]
; Same trick with sums. Thus x+(-1) < x (+0)
[(and (plus-expression? u)
(or (symbolic-id? v)
(compound-expression? v)))
(before? u (construct 'Plus (list v)))]
[(and (plus-expression? v)
(or (symbolic-id? u)
(compound-expression? u)))
(before? (construct 'Plus (list u)) v)]
; Here only function applications are left.
; Sort after name.
[(and (compound-expression? u) (compound-expression? v))
(if (not (equal? (kind u) (kind v)))
(before? (kind u) (kind v))
(let ()
; If the names are equal, sort after the first
; non-equal operand.
(define first-non-equal
(for/first ([ui (in-list (operands u))]
[vi (in-list (operands v))]
#:unless (equal? ui vi))
(list ui vi)))
(if first-non-equal
(apply before? first-non-equal)
(< (length (operands u)) (length (operands v))))))]
[(compound-expression? u)
#f]
[(compound-expression? v)
#t]
[else
(error 'before? "Internal error: A case is missing, got ~a and ~a" u v)]
; TODO : This isn't done
; some rules are missing ... functions????
))
[(and (number? u) (number? v))
; straightforward for two real numbers,
; but complex numbers must be handled too.
(if (= u v)
#t
(if (real? u)
(if (real? v) (< u v) v)
(if (real? v)
v
(if (= (imag-part u) (imag-part v))
(< (real-part u) (real-part v))
(< (imag-part u) (imag-part v))))))]
; Number always come first
[(number? u) #t]
[(number? v) #f]
; Symbols are sorted in alphabetical order
[(and (symbol? u) (symbol? v))
(string<? (symbol->string u) (symbol->string v))]
; For products and sums order on the last different
; factor or term. Thus x+y < y+z.
[(or (and (times-expression? u) (times-expression? v))
(and (plus-expression? u) (plus-expression? v)))
(define first-non-equal
(for/first ([ui (in-list (reverse (operands u)))]
[vi (in-list (reverse (operands v)))]
#:unless (equal? ui vi))
(list ui vi)))
(if first-non-equal
(apply before? first-non-equal)
(< (length (operands u)) (length (operands v))))]
; When comparing a product with something else, use the last factor.
; Thus x*y < z and y < x*z.
; Note: This is consistent with comparisons of two products since
; x*y < 1*z and 1*y < x*z.
[(and (times-expression? u)
(or ;(power-expression? v)
;(plus-expression? v)
(symbolic-id? v)
(compound-expression? v)))
(let ([un (last-operand u)])
(or (equal? un v) (before? un v)))]
[(and (times-expression? v)
(or ;(power-expression? v)
;(plus-expression? v)
(symbolic-id? u)
(compound-expression? u)))
(define vn (last-operand v))
(or (equal? vn u) (before? u vn))]
; Powers with smallest base are first. 2^z < 3^y
[(and (power-expression? u) (power-expression? v))
(if (equal? (base u) (base v))
(before? (exponent u) (exponent v))
(before? (base u) (base v)))]
; When comparing a product with something else, pretend
; the else part is a power with exponent 1.
; Thus x^2 > x.
[(and (power-expression? u)
(or ; (plus-expression? v)
(symbolic-id? v)
(compound-expression? v)))
(before? u (construct 'Power (list v 1)))]
[(and (power-expression? v)
(or ; (plus-expression? v)
(symbolic-id? u)
(compound-expression? u)))
(before? (construct 'Power (list u 1)) v)]
; Same trick with sums. Thus x+(-1) < x (+0)
[(and (plus-expression? u)
(or (symbolic-id? v)
(compound-expression? v)))
(before? u (construct 'Plus (list v)))]
[(and (plus-expression? v)
(or (symbolic-id? u)
(compound-expression? u)))
(before? (construct 'Plus (list u)) v)]
; Here only function applications are left.
; Sort after name.
[(and (compound-expression? u) (compound-expression? v))
(if (not (equal? (kind u) (kind v)))
(before? (kind u) (kind v))
(let ()
; If the names are equal, sort after the first
; non-equal operand.
(define first-non-equal
(for/first ([ui (in-list (operands u))]
[vi (in-list (operands v))]
#:unless (equal? ui vi))
(list ui vi)))
(if first-non-equal
(apply before? first-non-equal)
(< (length (operands u)) (length (operands v))))))]
[(compound-expression? u)
#f]
[(compound-expression? v)
#t]
[else
(error 'before? "Internal error: A case is missing, got ~a and ~a" u v)]
; TODO : This isn't done
; some rules are missing ... functions????
))
;(displayln (format " => ~a" result))
result
)
@ -743,35 +764,35 @@
(kind u)
(map (λ (ui) (concurrent-substitute ui ts rs))
(operands u))))])))
#;(module pattern-matching racket
(require (submod ".." expression))
(define (linear-form u x)
; u expression, x a symbol
(if (eq? u x)
(list 1 0)
(case (kind u)
[(symbol-id integer fraction real complex)
(list 0 u)]
[(Times)
(if (free-of u x)
(list 0 u)
(let ([u/x (Quotient u x)])
(if (Free-of u/x x)
(list u/x 0)
#f)))]
[(Plus)
(let ([f (linear-form (operand u 1) x)])
(and f
(let ([r (linear-form (Minus u (operand u 1)))])
(and r
(list (+ (operand f 0) (operand r 0))
(+ (operand f 1) (operand r 1)))))))]
[else
(and (free-of u x)
(list 0 u))]))))
(require (submod ".." expression))
(define (linear-form u x)
; u expression, x a symbol
(if (eq? u x)
(list 1 0)
(case (kind u)
[(symbol-id integer fraction real complex)
(list 0 u)]
[(Times)
(if (free-of u x)
(list 0 u)
(let ([u/x (Quotient u x)])
(if (Free-of u/x x)
(list u/x 0)
#f)))]
[(Plus)
(let ([f (linear-form (operand u 1) x)])
(and f
(let ([r (linear-form (Minus u (operand u 1)))])
(and r
(list (+ (operand f 0) (operand r 0))
(+ (operand f 1) (operand r 1)))))))]
[else
(and (free-of u x)
(list 0 u))]))))
(module equation-expression racket
(require (submod ".." expression))
@ -785,68 +806,50 @@
(values (map (curryr operand 0) (operands t=r-List))
(map (curryr operand 1) (operands t=r-List)))))
(module bracket-graphics racket
(require "../graphics/graphics.rkt")
(define-syntax (declare/provide-vars stx)
(syntax-case stx ()
[(_ id ...)
#'(begin
(define id 'id) ...
(provide id) ...)]))
(provide Graphics)
(declare/provide-vars
Blend Darker Hue Lighter
Circle Disk Line Point Rectangle
Text Thickness
; Colors
Red Blue Green Black White Yellow
; Options
ImageSize PlotRange
))
(module bracket racket
(require (submod ".." number-theory)
(submod ".." expression)
(submod ".." undefined)
(submod ".." equation-expression)
(submod ".." bracket-graphics))
;(submod ".." bracket-graphics)
)
(provide ; (all-from-out (submod ".." symbolic-application))
(rename-out [free-of Free-of]
[base Base]
[const Const]
[term Term]
[exponent Exponent]
[before? Before?]
[kind Kind])
(all-from-out (submod ".." bracket-graphics))
Operand
Operands
Hold
Complete-sub-expressions
Substitute
Sequential-substitute
Concurrent-substitute
Cons
List
List-ref
Plus Minus Times Quotient Power
Equal
Expand
Set Member?
Variables
Map
Apply
Append
AppendStar
Sin Cos Tan Sqrt
Solve-quadratic
Solve-linear
List->Set
Define
Range
Plot)
(rename-out [free-of Free-of]
[base Base]
[const Const]
[term Term]
[exponent Exponent]
[before? Before?]
[kind Kind])
;(all-from-out (submod ".." bracket-graphics))
Operand
Operands
Hold
Complete-sub-expressions
Substitute
Sequential-substitute
Concurrent-substitute
Cons
List
List-ref
Plus Minus Times Quotient Power
Equal
Expand
Set Member?
Variables
Map
Apply
Append
AppendStar
Sin Cos Tan Sqrt
Solve-quadratic
Solve-linear
List->Set
Define
Range
Plot)
;;;
;;; INVARIANT
@ -888,7 +891,7 @@
(define (Cons u1 u2)
(construct 'List (cons u1 (List->list u2))))
(define (Set . us)
(construct 'Set (set->list (list->set us))))
@ -1108,12 +1111,12 @@
(define (List? u)
(and (list? u)
(eq? (Kind u) 'List)))
(define-listable (Sqr u)
(cond
[(real? u) (sqr u u)]
[else (Power u 2)]))
(define-syntax (define-real-function stx)
(syntax-case stx ()
[(_ new old)
@ -1141,7 +1144,7 @@
(define-real-function Ceiling ceiling)
(define-real-function Truncate truncate)
(define-real-function Sgn sgn)
(define (Solve-quadratic a b c)
; return List of all solutions to ax^2+bx+c=0
(define d (Minus (Power b 2) (Times 4 a c)))
@ -1192,7 +1195,7 @@
(define (N u)
; TODO: Improve this
(eval u ns))
(define (Plot f range [options '(List)])
; TODO: Implement options
(displayln (list f range))
@ -1206,23 +1209,23 @@
(λ (x) (N (Substitute f (Equal var x)))))
x-min x-max y-min y-max excluded?)]
[else (error)]))
#;(and (real? x-min) (real? x-max) (real? y-min) (real? y-max)
(< x-min x-max) (< y-min y-max))
; (define (Monomial-gpe u v)
; (define s (if (eq? (Kind v) 'set) (list v) v))
; (cond
; [(Member? u (operands s)) #t]
; [else
; (if(power-expression? u)
; (define base (Operand u 0))
; (define exponent (Operand u 1))
; (if (and (Member? base s)
; (eq? (Kind exponent) 'integer)
; (> exponent 1))
; (define (Monomial-gpe u v)
; (define s (if (eq? (Kind v) 'set) (list v) v))
; (cond
; [(Member? u (operands s)) #t]
; [else
; (if(power-expression? u)
; (define base (Operand u 0))
; (define exponent (Operand u 1))
; (if (and (Member? base s)
; (eq? (Kind exponent) 'integer)
; (> exponent 1))
)
(module test racket
(require (submod ".." symbolic-application)

View File

@ -36,7 +36,8 @@
(strip-context
#'(module anything bracket-lang
(require (submod bracket.rkt bracket)
(submod bracket.rkt symbolic-application))
(submod bracket.rkt symbolic-application)
#;(submod bracket.rkt bracket-graphics))
(define-syntax (#%infix stx)
(syntax-case stx () [(_ expr) #'expr]))
; This lists the operators used by the parser.

View File

@ -50,7 +50,7 @@
1 0 32 #"(lib \"text-snipclass.ss\" \"xml\")\0"
1 0 15 #"test-case-box%\0"
2 0 1 6 #"wxloc\0"
0 0 63 0 1 #"\0"
0 0 65 0 1 #"\0"
0 75 1 #"\0"
0 12 90 -1 90 -1 3 -1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 255 255 255 1 -1 0 9
#"Standard\0"
@ -236,7 +236,13 @@
-1 -1 2 1 #"\0"
0 71 1 #"\0"
1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1.0 1.0 1.0 0 100 0 0 0 0 -1
-1 0 4352 0 26 3 12 #"#lang racket"
-1 4 1 #"\0"
0 -1 1 #"\0"
1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0
-1 -1 45 1 #"\0"
0 -1 1 #"\0"
1.0 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0
-1 -1 0 4335 0 26 3 12 #"#lang racket"
0 0 22 29 1 #"\n"
0 0 22 3 1 #"("
0 0 14 3 7 #"require"
@ -2361,8 +2367,7 @@
0 0 22 3 1 #" "
0 0 14 3 1 #"r"
0 0 22 3 3 #")) "
0 0 17 3 10 #"; relative"
0 0 17 3 24 #" to hor. range "
0 0 17 3 34 #"; relative to hor. range "
0 0 22 29 1 #"\n"
0 0 22 3 13 #" ("
0 0 15 3 12 #"parameterize"
@ -2394,8 +2399,7 @@
0 0 22 3 1 #" "
0 0 14 3 1 #"r"
0 0 22 3 3 #")) "
0 0 17 3 10 #"; relative"
0 0 17 3 24 #" to hor. range "
0 0 17 3 34 #"; relative to hor. range "
0 0 22 29 1 #"\n"
0 0 22 3 15 #" ("
0 0 15 3 12 #"parameterize"
@ -3218,8 +3222,7 @@
0 0 14 3 5 #"Style"
0 0 22 3 1 #" "
0 0 19 3 1 #"\""
0 0 19 3 7 #"Unknown"
0 0 19 3 9 #" spec ~a\""
0 0 19 3 16 #"Unknown spec ~a\""
0 0 22 3 2 #" ("
0 0 14 3 5 #"first"
0 0 22 3 1 #" "
@ -3242,8 +3245,7 @@
0 0 22 3 1 #" "
0 0 19 3 1 #"\""
0 0 19 3 8 #"Internal"
0 0 19 3 1 #" "
0 0 19 3 11 #"error: ~a \""
0 0 19 3 12 #" error: ~a \""
0 0 22 3 1 #" "
0 0 14 3 1 #"p"
0 0 22 3 7 #")])])))"
@ -4019,8 +4021,7 @@
0 0 17 3 2 #") "
0 0 17 3 1 #","
0 0 17 3 1 #"("
0 0 17 3 1 #"/"
0 0 17 3 13 #" x (* 2 pi)))"
0 0 17 3 14 #"/ x (* 2 pi)))"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
@ -4033,8 +4034,7 @@
0 0 17 3 1 #" "
0 0 17 3 1 #"1"
0 0 17 3 2 #" ("
0 0 17 3 1 #"/"
0 0 17 3 14 #" x (* 2 pi))))"
0 0 17 3 15 #"/ x (* 2 pi))))"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 50 #"; (Rotate (Rectangle) ,x)))"
@ -4047,16 +4047,14 @@
0 0 17 3 1 #"0"
0 0 17 3 2 #" ("
0 0 17 3 1 #"*"
0 0 17 3 1 #" "
0 0 17 3 19 #"4 pi) (* 2/24 pi)))"
0 0 17 3 20 #" 4 pi) (* 2/24 pi)))"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
0 0 17 3 3 #" "
0 0 17 3 1 #"'"
0 0 17 3 2 #"(("
0 0 17 3 5 #"Range"
0 0 17 3 19 #" ((-2 2) (-2 2)))))"
0 0 17 3 24 #"Range ((-2 2) (-2 2)))))"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
@ -4265,8 +4263,7 @@
0 0 17 3 3 #"0.0"
0 0 17 3 1 #","
0 0 17 3 1 #" "
0 0 17 3 3 #"1.0"
0 0 17 3 30 #"]>; given: -0.0416666666666663"
0 0 17 3 33 #"1.0]>; given: -0.0416666666666663"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 22 29 1 #"\n"
@ -4386,8 +4383,7 @@
0 0 17 3 1 #","
0 0 17 3 2 #" {"
0 0 17 3 1 #"x"
0 0 17 3 2 #", "
0 0 17 3 8 #"0, 10}]]"
0 0 17 3 10 #", 0, 10}]]"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 2 #"; "
@ -5086,8 +5082,7 @@
0 0 17 3 1 #";"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
0 0 17 3 15 #"Graphics[Table["
0 0 17 3 16 #";Graphics[Table["
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
@ -5099,8 +5094,7 @@
0 0 17 3 1 #"0"
0 0 17 3 1 #","
0 0 17 3 1 #" "
0 0 17 3 3 #"0.5"
0 0 17 3 25 #"]}], ; black, opacity 50%"
0 0 17 3 28 #"0.5]}], ; black, opacity 50%"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
@ -5117,8 +5111,7 @@
0 0 17 3 1 #" "
0 0 17 3 1 #"1"
0 0 17 3 1 #","
0 0 17 3 1 #" "
0 0 17 3 6 #"0.6], "
0 0 17 3 7 #" 0.6], "
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
@ -5143,8 +5136,7 @@
0 0 17 3 1 #","
0 0 17 3 2 #" ("
0 0 17 3 3 #"8-r"
0 0 17 3 1 #")"
0 0 17 3 6 #"/3]}, "
0 0 17 3 7 #")/3]}, "
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
@ -5154,8 +5146,7 @@
#" {"
) 0 0 17 3 1 #"r"
0 0 17 3 1 #","
0 0 17 3 1 #" "
0 0 17 3 4 #"6}, "
0 0 17 3 5 #" 6}, "
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 17 3 1 #";"
@ -5163,8 +5154,7 @@
(
#" "
#" {"
) 0 0 17 3 1 #"q"
0 0 17 3 7 #", 12}]]"
) 0 0 17 3 8 #"q, 12}]]"
0 0 22 29 1 #"\n"
0 0 22 3 2 #" "
0 0 22 29 1 #"\n"
@ -5443,7 +5433,6 @@
0 0 17 3 1 #" "
0 0 17 3 1 #"0"
0 0 17 3 1 #" "
0 0 17 3 1 #"0"
0 0 17 3 6 #") 5)))"
0 0 17 3 7 #"0) 5)))"
0 0 22 29 1 #"\n"
0 0