Set of real numbers: Difference between revisions

Added EchoLisp
m (J: move the other instance of an (in) defn closer to where it's used)
(Added EchoLisp)
Line 219:
 
void main() {}</lang>
 
=={{header|EchoLisp}}==
Implementation of sets operations, which apply to '''any''' subsets of ℜ defined by a predicate.
===Sets operations===
<lang scheme>
(lib 'match) ;; reader-infix macros
 
(reader-infix '∈ )
(reader-infix '∩ )
(reader-infix '∪ )
(reader-infix '⊖ ) ;; set difference
(define-syntax-rule (∈ x a) (a x))
(define-syntax-rule (∩ a b) (lambda(x) (and ( a x) (b x))))
(define-syntax-rule (∪ a b) (lambda(x) (or ( a x) (b x))))
(define-syntax-rule (⊖ a b) (lambda(x) (and ( a x) (not (b x)))))
 
;; predicates to define common sets
(define (∅ x) #f) ;; the empty set predicate
(define (Z x) (integer? x))
(define (N x) (and (Z x) (>= x 0)))
(define (Q x) (rational? x))
(define (ℜ x) #t)
 
;; predicates to define convex sets
(define (⟦...⟧ a b)(lambda(x) (and (>= x a) (<= x b))))
(define (⟦...⟦ a b)(lambda(x) (and (>= x a) (< x b))))
(define (⟧...⟧ a b)(lambda(x) (and (> x a) (<= x b))))
(define (⟧...⟦ a b)(lambda(x) (and (> x a) (< x b))))
</lang>
{{out}}
<pre>
(3/7 ∈ ∅) → #f
(3/7 ∈ Q) → #t
(6.7 ∈ ℜ) → #t
 
(define A (⟦...⟧ 2 10)) ; closed interval
(define B (⟧...⟦ 5 15)) ; open interval
 
(8 ∈ A) → #t
(11 ∈ A)→ #f
(define AB (A ∩ B))
(8 ∈ AB) → #t
(3 ∈ AB) → #f
(5 ∈ AB) → #f ;; because B is ]5 .. 15]
(define A-B (A ⊖ B))
(5 ∈ A-B) → #t
(-666 ∈ (⟧...⟧ -Infinity 0 )) → #t
 
;; task
(0 ∈ ((⟧...⟧ 0 1) ∪ (⟦...⟦ 0 2))) → #t
(0 ∈ ((⟦...⟦ 0 2) ∩ (⟧...⟧ 1 2))) → #f
(0 ∈ ((⟦...⟦ 0 3) ⊖ (⟧...⟦ 0 1))) → #t
(0 ∈ ((⟦...⟦ 0 3) ⊖ (⟦...⟧ 0 1))) → #f
(1 ∈ ((⟧...⟧ 0 1) ∪ (⟦...⟦ 0 2))) → #t
(1 ∈ ((⟦...⟦ 0 2) ∩ (⟧...⟧ 1 2))) → #f
(1 ∈ ((⟦...⟦ 0 3) ⊖ (⟧...⟦ 0 1))) → #t
(1 ∈ ((⟦...⟦ 0 3) ⊖ (⟦...⟧ 0 1))) → #f
(2 ∈ ((⟧...⟧ 0 1) ∪ (⟦...⟦ 0 2))) → #f
(2 ∈ ((⟦...⟦ 0 2) ∩ (⟧...⟧ 1 2))) → #f
(2 ∈ ((⟦...⟦ 0 3) ⊖ (⟧...⟦ 0 1))) → #t
(2 ∈ ((⟦...⟦ 0 3) ⊖ (⟦...⟧ 0 1))) → #t
</pre>
=== Optional : measuring sets===
<lang scheme>
;; The following applies to convex sets ⟧...⟦ Cx,
;; and families F of disjoint convex sets.
;; Cx are implemented as vectors [lo, hi]
 
(define-syntax-id _.lo [_ 0])
(define-syntax-id _.hi [_ 1])
;; Cx-ops
(define (Cx-new lo hi) (if (< lo hi) (vector lo hi) Cx-empty))
(define (Cx-empty? A) (>= A.lo A.hi))
(define Cx-empty #(+Infinity -Infinity))
(define (Cx-inter A B) (Cx-new (max A.lo B.lo) (min A.hi B.hi)))
(define (Cx-measure A) (if (< A.lo A.hi) (- A.hi A.lo) 0))
;; Families ops
(define (CF-measure FA) (for/sum ((A FA)) (Cx-measure A))) ;; because disjoint
;; intersection of two families
(define (CF-inter FA FB) (for*/list ((A FA)(B FB)) (Cx-inter A B)))
;; measure of FA/FB = m(FA) - m (FA ∩ FB)
(define (CF-measure-FA/FB FA FB)
(- (CF-measure FA) (CF-measure (CF-inter FA FB))))
;; Application :
;; FA = {x | 0 < x < 10 and |sin(π x²)| > 1/2 }
(define FA
(for/list ((n 100))
(Cx-new (sqrt (+ n (// 6))) (sqrt (+ n (// 5 6))))))
 
;; FB = {x | 0 < x < 10 and |sin(π x)| > 1/2 }
(define FB
(for/list ((n 10))
(Cx-new (+ n (// 6)) (+ n (// 5 6)))))
 
→ (#(0.1667 0.8333) #(1.1667 1.8333) #(2.1667 2.8333)
#(3.1667 3.8333) #(4.1667 4.8333) #(5.1667 5.8333)
#(6.1667 6.8333) #(7.1667 7.8333) #(8.1667 8.8333) #(9.1667 9.8333))
(CF-measure-FA/FB FA FB)
→ 2.075864841184666
 
</lang>
 
=={{header|Elena}}==
<lang elena>#import system.