Set of real numbers: Difference between revisions
Added FreeBASIC
(→{{header|Wren}}: Now uses new core library method.) |
(Added FreeBASIC) |
||
(6 intermediate revisions by 5 users not shown) | |||
Line 25:
'''Implementation notes'''
* 'Any' real set means 'sets that can be expressed as the union of a finite number of convex real sets'. Cantor's set
* Infinities should be handled gracefully; indeterminate numbers (NaN) can be ignored.
* You can use your machine's native real number representation, which is probably IEEE floating point, and assume it's good enough (it usually is).
Line 36:
=={{header|C}}==
Providing an implementation of lambdas would be better, but this should do for now.
<
#include <stdbool.h>
#include <stdio.h>
Line 208:
return 0;
}</
{{out}}
<pre>(0, 1] union [0, 2) contains 0 is 1
Line 228:
=={{header|C sharp}}==
<
namespace RosettaCode.SetOfRealNumbers
Line 260:
}
}
}</
Test:
<
using RosettaCode.SetOfRealNumbers;
Line 310:
}
}
}</
=={{header|C++}}==
{{trans|Java}}
<
#include <functional>
#include <iostream>
Line 447:
return 0;
}</
{{out}}
<pre>(0, 1] ? [0, 2) contains 0 is true
Line 470:
=={{header|Clojure}}==
{{trans|Racket}}
<
(defn >=|<= [lo hi] #(<= lo % hi))
Line 503:
(def Q ratio?)
(def I #(∖ R Z Q))
(def N #(∖ Z neg?))</
=={{header|Common Lisp}}==
Common Lisp has a standard way to represent intervals.
<
(deftype set<> (a b) `(real (,a) (,b)))
(deftype set=> (a b) `(real ,a (,b)))
Line 535:
(assert (not (in-set-p 0 set)))
(assert (not (in-set-p 1 set)))
(assert (in-set-p 2 set))))</
=={{header|D}}==
{{trans|C sharp}}
<
const pure nothrow bool delegate(in T) contains;
Line 599:
}
void main() {}</
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Set_of_real_numbers;
Line 696:
end;
readln;
end.</
=={{header|EchoLisp}}==
Implementation of sets operations, which apply to '''any''' subsets of ℜ defined by a predicate.
===Sets operations===
<
(lib 'match) ;; reader-infix macros
Line 726:
(define (⟧...⟧ a b)(lambda(x) (and (> x a) (<= x b))))
(define (⟧...⟦ a b)(lambda(x) (and (> x a) (< x b))))
</syntaxhighlight>
{{out}}
<pre>
Line 761:
</pre>
=== Optional : measuring sets===
<
;; The following applies to convex sets ⟧...⟦ Cx,
;; and families F of disjoint convex sets.
Line 802:
→ 2.075864841184666
</syntaxhighlight>
=={{header|Elena}}==
ELENA
<
extension setOp
Line 823:
{
// union
var set := (x => x >= 0.0r && x <= 1.0r ).union::(x => x >= 0.0r && x < 2.0r );
set(0.0r).assertTrue();
Line 830:
// intersection
var set2 := (x => x >= 0.0r && x < 2.0r ).intersection::(x => x >= 1.0r && x <= 2.0r );
set2(0.0r).assertFalse();
Line 837:
// difference
var set3 := (x => x >= 0.0r && x < 3.0r ).difference::(x => x >= 0.0r && x <= 1.0r );
set3(0.0r).assertFalse();
set3(1.0r).assertFalse();
set3(2.0r).assertTrue();
}</
=={{header|FreeBASIC}}==
{{incomplete|FreeBASIC|Despite my efforts, the set difference results are erroneous.
I would appreciate help.}}
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Type Func
As Integer ID
As Double ARGS(2)
End Type
Declare Function cf(f As Func, x As Double) As Boolean
Declare Function Union_(a As Func, b As Func, x As Double) As Boolean
Declare Function Inters(a As Func, b As Func, x As Double) As Boolean
Declare Function Differ(a As Func, b As Func, x As Double) As Boolean
Declare Function OpOp(a As Double, b As Double, x As Double) As Boolean
Declare Function ClCl(a As Double, b As Double, x As Double) As Boolean
Declare Function OpCl(a As Double, b As Double, x As Double) As Boolean
Declare Function ClOp(a As Double, b As Double, x As Double) As Boolean
Declare Function aspxx(a As Double) As Boolean
Declare Function aspx(a As Double) As Boolean
Function cf(f As Func, x As Double) As Boolean
Select Case f.ID
Case 1: Return OpOp(f.ARGS(0), f.ARGS(1), x)
Case 2: Return ClCl(f.ARGS(0), f.ARGS(1), x)
Case 3: Return OpCl(f.ARGS(0), f.ARGS(1), x)
Case 4: Return ClOp(f.ARGS(0), f.ARGS(1), x)
'Extra credit
Case 5: Return OpOp(f.ARGS(0), f.ARGS(1), x) And aspxx(x)
Case 6: Return OpOp(f.ARGS(0), f.ARGS(1), x) And aspx(x)
End Select
End Function
Function Union_(a As Func, b As Func, x As Double) As Boolean
Return cf(a, x) Or cf(b, x)
End Function
Function Inters(a As Func, b As Func, x As Double) As Boolean
Return cf(a, x) And cf(b, x)
End Function
Function Differ(a As Func, b As Func, x As Double) As Boolean
Return cf(a, x) And (Not cf(b, x))
End Function
Function OpOp(a As Double, b As Double, x As Double) As Boolean
Return a < x And x < b
End Function
Function ClCl(a As Double, b As Double, x As Double) As Boolean
Return a <= x And x <= b
End Function
Function OpCl(a As Double, b As Double, x As Double) As Boolean
Return a < x And x <= b
End Function
Function ClOp(a As Double, b As Double, x As Double) As Boolean
Return a <= x And x < b
End Function
'Extra credit
Function aspxx(a As Double) As Boolean
Return Abs(Sin(3.14159 * a * a)) > 0.5
End Function
Function aspx(a As Double) As Boolean
Return Abs(Sin(3.14159 * a)) > 0.5
End Function
' Set definitions and test methods
Dim As Func s(6, 2)
s(1, 0).ID = 3: s(1, 0).ARGS(0) = 0: s(1, 0).ARGS(1) = 1
s(1, 1).ID = 4: s(1, 1).ARGS(0) = 0: s(1, 1).ARGS(1) = 2
s(2, 0).ID = 4: s(2, 0).ARGS(0) = 0: s(2, 0).ARGS(1) = 2
s(2, 1).ID = 3: s(2, 1).ARGS(0) = 1: s(2, 1).ARGS(1) = 2
s(3, 0).ID = 4: s(3, 0).ARGS(0) = 0: s(3, 0).ARGS(1) = 3
s(3, 1).ID = 1: s(3, 1).ARGS(0) = 0: s(3, 1).ARGS(1) = 1
s(4, 0).ID = 4: s(4, 0).ARGS(0) = 0: s(4, 0).ARGS(1) = 3
s(4, 1).ID = 2: s(4, 1).ARGS(0) = 0: s(4, 1).ARGS(1) = 1
s(5, 0).ID = 2: s(5, 0).ARGS(0) = 0: s(5, 0).ARGS(1) = 0
'Extra credit
s(6, 1).ID = 5: s(6, 1).ARGS(0) = 0: s(6, 1).ARGS(1) = 10
s(6, 2).ID = 6: s(6, 2).ARGS(0) = 0: s(6, 2).ARGS(1) = 10
Dim As Integer i, x, r
For x = 0 To 2
i = 1
r = Union_(s(i, 1), s(i, 2), x)
Print Using "# in (#_,#] u [#_,#) : &"; x; s(i, 0).ARGS(0); s(i, 0).ARGS(1); s(i, 1).ARGS(0); s(1, 1).ARGS(1); Cbool(r)
Next x
Print
For x = 0 To 2
i = 2
r = Inters(s(i, 1), s(i, 2), x)
Print Using "# in (#_,#] u [#_,#) : &"; x; s(i, 0).ARGS(0); s(i, 0).ARGS(1); s(i, 1).ARGS(0); s(1, 1).ARGS(1); Cbool(r)
Next x
Print
For x = 0 To 2
i = 3
r = Differ(s(i, 1), s(i, 2), x)
Print Using "# in (#_,#] u [#_,#) : &"; x; s(i, 0).ARGS(0); s(i, 0).ARGS(1); s(i, 1).ARGS(0); s(1, 1).ARGS(1); Cbool(r)
Next x
Print
For x = 0 To 2
i = 4
r = Differ(s(i, 1), s(i, 2), x)
Print Using "# in (#_,#] u [#_,#) : &"; x; s(i, 0).ARGS(0); s(i, 0).ARGS(1); s(i, 1).ARGS(0); s(1, 1).ARGS(1); Cbool(r)
Next x
Print
x = 0
i = 5
r = Differ(s(i, 1), s(i, 2), x)
Print Using "[#_,#] is empty : &"; s(i, 0).ARGS(0); s(i, 0).ARGS(1); Cbool(r)
Print
'Extra credit
Dim As Double z = 0, paso = 0.00001
Dim As Integer count = 0
While z <= 10
If Differ(s(6, 1), s(6, 2), z) Then count += 1
z += paso
Wend
Print "Approximate length of A-B: "; count * paso
Sleep</syntaxhighlight>
{{out}}
<pre>0 in (0,1] u [0,2) : true
1 in (0,1] u [0,2) : true
2 in (0,1] u [0,2) : false
0 in (0,2] u [1,2) : false
1 in (0,2] u [1,2) : false
2 in (0,2] u [1,2) : false
0 in (0,3] u [0,2) : false
1 in (0,3] u [0,2) : false
2 in (0,3] u [0,2) : false
0 in (0,3] u [0,2) : true
1 in (0,3] u [0,2) : true
2 in (0,3] u [0,2) : false
[0,0] is empty : false
Approximate length of A-B: 2.07586</pre>
=={{header|F#|F sharp}}==
<
let union s1 s2 =
Line 887 ⟶ 1,036:
assert (not (i1 2.0))
0 // return an integer exit code</
=={{header|Go}}==
Just the non-optional part:
<
import "fmt"
Line 918 ⟶ 1,067:
fmt.Println()
}
}</
[http://play.golang.org/p/YQ2GRBM4af Run in Go Playground].
{{out}}
Line 937 ⟶ 1,086:
2 ∈ s3: true</pre>
This simple implementation doesn't support lengths so the although the A, B, and A−B sets can be defined and tested (see below), they can't be used to implement the optional part.
<
return math.Abs(math.Sin(math.Pi*x*x)) > .5
})
Line 947 ⟶ 1,096:
for x := float64(5.98); x < 6.025; x += 0.01 {
fmt.Printf("%.2f ∈ A−B: %t\n", x, C(x))
}</
=={{header|Haskell}}==
<
{- Not so functional representation of R sets (with IEEE Double), in a strange way -}
Line 1,096 ⟶ 1,245:
mapM_ isintest restest >>
putStrLn ("measure: " ++ (show (set_measure (set_difference testA testB))))
</syntaxhighlight>
{{out}}
<pre>
Line 1,123 ⟶ 1,272:
simplifications of some representations, but more could be done.
<
s1 := RealSet("(0,1]").union(RealSet("[0,2)"))
s2 := RealSet("[0,2)").intersect(RealSet("(1,2)"))
Line 1,222 ⟶ 1,371:
initially(s)
put(ranges := [],Range(\s).notEmpty())
end</
Sample run:
Line 1,250 ⟶ 1,399:
In essence, this looks like building a restricted set of statements. So we build a specialized parser and expression builder:
<
ing=: `''
Line 1,266 ⟶ 1,415:
union=: 4 :'(x has +. y has)ing'
intersect=: 4 :'(x has *. y has)ing'
without=: 4 :'(x has *. [: -. y has)ing'</
With this in place, the required examples look like this:
<
1 1 0
('[0,2)' intersect '(1,2]')has 0 1 2
Line 1,279 ⟶ 1,428:
1 1 1
('[0,3)' without '[0,1]')has 0 1 2
0 0 1</
Note that without the arguments these wind up being expressions. For example:
<
(0&< *. 1&>:) +. 0&<: *. 2&></
In other words, this is a statement built up from inequality terminals (where each inequality is bound to a constant) and the terminals are combined with logical operations.
Line 1,294 ⟶ 1,443:
Here is an alternate formulation which allows detection of empty sets:
<
ing=: `''
Line 1,316 ⟶ 1,465:
without=: 4 :'(x has *. [: -. y has)ing; x edges y'
in=: 4 :'y has x'
isEmpty=: 1 -.@e. contour in ]</
The above examples work identically with this version, but also:
<
0
isEmpty '[0,2)' intersect '(1,2]'
Line 1,329 ⟶ 1,478:
1
isEmpty '[0,2]' intersect '[2,3]'
0</
Note that the the set operations no longer return a simple verb -- instead, they return a pair, where the first element represents the verb and the second element is a list of interval boundaries. We can tell if two adjacent bounds, from this list, bound a valid interval by checking any point between them.
Line 1,337 ⟶ 1,486:
The optional work centers around expressions where the absolute value of sin pi * n is 0.5. It would be nice if J had an arcsine which gave all values within a range, but it does not have that. So:
<
0.166667</
(Note on notation: 1 o. is sine in J, and 2 o. is cosine -- the mnemonic is that sine is an odd function and cosine is an even function, the practical value is that sine, cosine and sine/cosine pairs can all be generated from the same "real" valued function.
Similarly, _1 o. is arcsine and _2 o. is arcsine. Also 1p_1 is the reciprocal of pi. So the above tells us that the principal value for arc sine 0.5 is one sixth.)
<
1 5 7 11 13 17 19 23 25 29
2 -~/\ (#~ 0.5 = 1 |@o. 1r6p1&*) i. 30
4 2 4 2 4 2 4 2 4</
Here we see the integers which when multiplied by pi/6 give 0.5 for the absolute value of the sine, and their first difference. Thus:
<
is a function to generate the values which correspond to the boundaries of the intervals we want:
<
zA=: zeros0toN&.*: 10
Line 1,364 ⟶ 1,513:
200
#zB
20</
And, here are the edges of the sets of intervals we need to consider.
Line 1,370 ⟶ 1,519:
To find the length of the the set A-B we can find the length of set A and subtract the length of the set A-B:
<
2.07586</
Here, we have paired adjacent elements from the zero bounding list (non-overlapping infixes of length 2). For set A's length we sum the results of subtracting the smaller number of the pair from the larger. For set A-B's length we consider each combination of pairs from A and B and subtract the larger of the beginning values from the smaller of the ending values (and ignore any negative results).
Line 1,377 ⟶ 1,526:
Alternatively, if we use the set implementation with empty set detection, and the following definitions:
<
A=: union/_2 intervalSet/\ zA
B=: union/_2 intervalSet/\ zB
diff=: A without B</
We can replace the above sentence to compute the length of the difference with:
<
2.07588</
(Note that this result is not exactly the same as the previous result. Determining why would be an interesting exercise in numerical analysis.)
=={{header|Java}}==
<
import java.util.function.Predicate;
Line 1,502 ⟶ 1,651:
System.out.printf("Approx length of A - B is %f\n", cc.length());
}
}</
{{out}}
<pre>(0, 1] ∪ [0, 2) contains 0 is true
Line 1,524 ⟶ 1,673:
=={{header|Javascript}}==
<
function realSet(set1, set2, op, values) {
const makeSet=(set0)=>{
Line 1,569 ⟶ 1,718:
return res;
}
</syntaxhighlight>
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq provided `keys_unsorted` is replaced with `keys`'''
This entry focuses on functions that operate on "real sets" and not
just intervals of the real number line, it being understood that a
"real set" in the present context is a finite union of such intervals,
in accordance with the problem description.
Since every "real set" in this sense can be represented in canonical form as
a finite disjoint union of intervals, we will use the term RealSet
to denote a canonical representation in jq of a "real set" as follows:
A RealSet is a jq array consisting of numbers and/or two-element arrays
[a,b], where a and b are numbers with a < b, where:
* each number represents the closed interval containing that number;
* each array [a,b] represents the open interval from a to b exclusive;
* the items in the outer array are sorted in ascending order in the obvious way.
The jq values `infinite` and `-infinite` are also allowed, thus allowing infinite
intervals to be represented.
Examples:
* [] representes the empty RealSet.
* [[1,2]] represents the RealSet consisting of the open interval from 1 to 2 exclusive.
* [1, [1,2], 2] represents the closed interval from 1 to 2 inclusive.
* [1,2] represents the union of the two closed intervals containing respectively 1 and 2.
* [-infinite, 0] represents the open interval consisting of the finite negative numbers.
* [infinite] represents the closed interval whose only element is positive inifinity.
For clarity and to facilitate reuse, the RealSet function definitions are bundled
together in a jq module, RealSet, available at
[[:Category:Jq/RealSet.jq]]. Here we summarize the key functions and illustrate their use.
1) To convert an arbitrary union of real intervals to a RealSet,
use `RealSet/0`, e.g.
[ [1,5], [2,6] ] | RealSet #=> [[1,6]]
2) Testing whether a RealSet is empty
Since the empty RealSet is just [], there is no real need to define
a function for testing whether a RealSet is empty. To test
whether an arbitrary union of real intervals is empty,
use the idiom:
RealSet == []
For example:
[ [1,3], [0,1] ] | RealSet == [] #=> false
3) To check whether a specific number, $r, is in a RealSet,
one can use `containsNumber($r)`, and similarly to check
whether an open interval is in a RealSet, one can
use `containsOpenInterval($a; $b)` where $a < $b defines
the open interval.
4) The basic binary operations on RealSets are:
add/1
intersection/1
minus/1
5) To compute the length of a RealSet: `RealSetLength/0`
This returns `infinite` if any component interval is infinite.
===Tasks===
<syntaxhighlight lang="jq">
include "realset" {search: "."};
def test_cases:
{ "(0, 1] ∪ [0, 2)": ( [ [0,1], 1] | add( [0, [0,2]] )),
"[0, 2) ∩ (1, 2]": ( [ 0, [0,2]] | intersection( [[1,2],2] ) ),
"[0, 3) − (0, 1)": ( [ 0, [0,3]] | minus( [[0,1]] ) ),
"[0, 3) − [0, 1]": ( [ 0, [0,3]] | minus( [0, [0,1], 1] ))
} ;
def keys_unsorted: keys; # for gojq
def tests($values):
"Checking containment of: \($values | join(" "))",
(keys_unsorted[] as $name
| "\($name) has length \(.[$name]|RealSetLength) and contains: \( [$values[] as $i | select(.[$name] | containsNumber($i) ) | $i] | join(" ") )" )
;
# A and B
def pi: 1 | atan * 4;
# For positive integers $n,
# we define B($n) to correspond to {x | 0 < x < $n and |sin(π x)| > 1/2}
def B($upper):
def x: 0.5 | asin / pi;
x as $x
| reduce range(0; $upper) as $i ([];
. + [ [$i + $x, $i + 1 - $x]]);
# |sin(π x²)| > 1/2
def A($upper):
B($upper * $upper) | map( map(sqrt) );
# The simple tests:
test_cases | tests([0,1,2]),
# A - B
"|A - B| = \(A(10) | minus( B(10) ) | RealSetLength)"
</syntaxhighlight>
{{output}}
<pre>
Checking containment of: 0 1 2
(0, 1] ∪ [0, 2) has length 2 and contains: 0 1
[0, 2) ∩ (1, 2] has length 1 and contains:
[0, 3) − (0, 1) has length 2 and contains: 0 1 2
[0, 3) − [0, 1] has length 2 and contains: 2
|A - B| = 2.075864841184667
</pre>
=={{header|Julia}}==
<syntaxhighlight lang="julia">
"""
struct ConvexRealSet
Line 1,649 ⟶ 1,920:
testconvexrealset()
</syntaxhighlight>
{{output}}<pre>
Testing with x = 0.
Line 1,681 ⟶ 1,952:
Clearly, the above approach is only suitable for sets with narrow ranges (as we have here) but does have the merit of not over-complicating the basic class.
<
typealias RealPredicate = (Double) -> Boolean
Line 1,757 ⟶ 2,028:
val cc = aa subtract bb
println("Approx length of A - B is ${cc.length}")
}</
{{out}}
Line 1,782 ⟶ 2,053:
=={{header|Lua}}==
<
local l,h = tonumber(low), tonumber(high)
if l and h then
Line 1,879 ⟶ 2,150:
)
local cc = aa.subtract(bb)
print("Approx length of A - B is "..cc.length())</
{{out}}
<pre>(0, 1] union [0, 2) contains 0 is true
Line 1,901 ⟶ 2,172:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
setcc[a_, b_] := a <= x <= b
setoo[a_, b_] := a < x < b
Line 1,927 ⟶ 2,198:
Print[set4]
Print["Fourth trial set, [0,3)\[Minus][0,1], testing for {0,1,2}:"]
Print[inSetQ[#, set4] & /@ {0, 1, 2}]</
{{Output}}
<pre>0<x<=1||0<=x<2
Line 1,947 ⟶ 2,218:
=={{header|Nim}}==
{{trans|Kotlin}}
<
type
Line 2,033 ⟶ 2,304:
cc = aa - bb
echo &"Approximative length of A - B is {cc.length}."</
{{out}}
Line 2,057 ⟶ 2,328:
=={{header|PARI/GP}}==
Define some sets and use built-in functions:
<
set01(x,a,b)=select(x -> a < x && x <= b, x);
set10(x,a,b)=select(x -> a <= x && x < b, x);
Line 2,067 ⟶ 2,338:
setintersect(set10(V, 0, 2), set01(V, 1, 2))
setminus(set10(V, 0, 3), set00(V, 0, 1))
setminus(set10(V, 0, 3), set11(V, 0, 1))</
Output:<pre>
Line 2,076 ⟶ 2,347:
=={{header|Perl}}==
<
# numbers used as boundaries to real sets. Each has 3 components:
Line 2,254 ⟶ 2,525:
my $z = $x - $y;
print "A - B\t= ", brev($z), "\n\tlength = ", $z->len, "\n";
print $z ? "not empty\n" : "empty\n";</
Set 1 = (0.00, 2.00): has 1;
Set 2 = [0.00, 0.00] ∪ [1.00, 3.00): has 0; has 1; has 2;
Line 2,266 ⟶ 2,537:
A - B = [0.83, 0.91) ∪ (1.08, 1.17] ∪ ... ∪ (9.91, 9.94) ∪ (9.96, 9.99)
length = 2.07586484118467
not empty</
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">ID</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ARGS</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">call_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ID</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ARGS</span><span style="color: #0000FF;">])&</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Union</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Inter</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Diffr</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">OpOp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">x</span> <span style="color: #008080;">and</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">b</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">ClCl</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">x</span> <span style="color: #008080;">and</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">b</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">OpCl</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">x</span> <span style="color: #008080;">and</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">b</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">ClOp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">x</span> <span style="color: #008080;">and</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">b</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #000080;font-style:italic;">-- expected
-- ---- desc ----, 0 1 2, --------------- set method ---------------</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"(0,1] u [0,2)"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">Union</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">OpCl</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">ClOp</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}}}}},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"[0,2) n (1,2]"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">Inter</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">ClOp</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">OpCl</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}}}}},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"[0,3) - (0,1)"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">Diffr</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">ClOp</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">OpOp</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}}}},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"[0,3) - [0,1]"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">Diffr</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">ClOp</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">ClCl</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}}}}}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">expect</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">method</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">method</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">error</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">expect</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]?</span><span style="color: #008000;">"error"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d in %s : %t %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">desc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">error</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,315 ⟶ 2,589:
</pre>
Extra credit - also translated from Go, but with an extended loop and crude summation, inspired by Java/Kotlin.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">aspxx</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x</span><span style="color: #0000FF;">))></span><span style="color: #000000;">0.5</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">aspx</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x</span><span style="color: #0000FF;">))</span> <span style="color: #0000FF;">></span><span style="color: #000000;">0.5</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">A</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">Inter</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">OpOp</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">aspxx</span><span style="color: #0000FF;">,{}}}},</span>
<span style="color: #000000;">B</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">Inter</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">OpOp</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">aspx</span><span style="color: #0000FF;">,{}}}},</span>
<span style="color: #000000;">C</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">Diffr</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">A</span><span style="color: #0000FF;">,</span><span style="color: #000000;">B</span><span style="color: #0000FF;">}}</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">step</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.00001</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">x</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">C</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">step</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Approximate length of A-B: %.5f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">*</span><span style="color: #000000;">step</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,333 ⟶ 2,609:
=={{header|Python}}==
<
def __init__(self, lo, hi, includelo=True, includehi=False):
self.eqn = "(%i<%sX<%s%i)" % (lo,
Line 2,378 ⟶ 2,654:
% ('in' if v in s else 'ex', v)
for v in range(3)),
s.eqn))</
;Output:
Line 2,389 ⟶ 2,665:
=={{header|Racket}}==
This is a simple representation of sets as functions (so obviously no good way to the the extra set length).
<syntaxhighlight lang="racket">
#lang racket
Line 2,432 ⟶ 2,708:
(try ((0 +-- 3) ∖ (0 --- 1)))
(try ((0 +-- 3) ∖ (0 +-+ 1)))
</syntaxhighlight>
Output:
Line 2,445 ⟶ 2,721:
(formerly Perl 6)
{{works with|Rakudo|2018.10}}
<syntaxhighlight lang="raku"
has $.range handles <min max excludes-min excludes-max minmax ACCEPTS>;
method empty {
Line 2,591 ⟶ 2,867:
say "A − B =";
say " ",.gist for $C.intervals;
say "Length A − B = ", $C.length;</
{{out}}
<pre> 0 1 2
Line 2,648 ⟶ 2,924:
=={{header|REXX}}==
===no error checking, no ∞===
<
call quertySet 1, 3, '[1,2)'
call quertySet , , '[0,2) union (1,3)'
Line 2,693 ⟶ 2,969:
parse var q @.1 2 @.2 ',' @.3 (@.4)
if @.2>@.3 then parse var L . @.0 @.2 @.3
return space(@.1 @.2 @.0 @.3 @.4, 0)</
{{out|output|text= is the same as the next REXX version (below).}}
===has error checking, ∞ support===
<
call quertySet 1, 3, '[1,2)'
call quertySet , , '[0,2) union (1,3)'
Line 2,753 ⟶ 3,029:
end /*j*/
if @.2>@.3 then parse var L . @.0 @.2 @.3
return space(@.1 @.2 @.0 @.3 @.4, 0)</
{{out|output|text= when using the (internal) default inputs:}}
<pre>
Line 2,801 ⟶ 3,077:
=={{header|Ruby}}==
{{works with|Ruby|1.9.3}}
<
Set = Struct.new(:lo, :hi, :inc_lo, :inc_hi) do
def include?(x)
Line 2,971 ⟶ 3,247:
def Rset(lo, hi, inc_hi=false)
Rset.new(lo, hi, false, inc_hi)
end</
Test case:
<
[1,2,3].each{|x|puts "#{x} => #{a.include?(x)}"}
puts
Line 3,018 ⟶ 3,294:
puts "a = #{a = Rset(-inf,inf)}"
puts "b = #{b = Rset.parse('[1/3,11/7)')}"
puts "a - b -> #{a - b}"</
{{out}}
Line 3,073 ⟶ 3,349:
{{works with|Ruby|2.1+}}
(with Rational suffix.)
<
puts "#{str} -> #{eval(str)}\t\t# create empty set"
str = "e.empty?"
Line 3,103 ⟶ 3,379:
puts "b.length : #{b.length}"
puts "a - b : #{a - b}"
puts "(a-b).length : #{(a-b).length}"</
{{out}}
Line 3,122 ⟶ 3,398:
* A <code>CompositeSet</code> which represents a <code>SetOperation</code> (union, intersection, or difference) between two other <code>RealSet</code>s, which themselves can be composite or not. When the <code>contains()</code> method is called on a composite set for a given number, it will recursively call contains() on its component sets to check whether they contain the number. Depending on the operation, this will define whether the number is contained in this set.
Since we use Rust's [https://doc.rust-lang.org/std/primitive.f64.html f64], which is a standard [https://en.wikipedia.org/wiki/IEEE_754 IEEE 754] double-precision floating-point number, we get correct behavior at infinity for free.
<
enum SetOperation {
Union,
Line 3,256 ⟶ 3,532:
}
}
}</
<pre>Set (0, 1] ∪ [0, 2)
- contains 0
Line 3,276 ⟶ 3,552:
=={{header|Tcl}}==
This code represents each set of real numbers as a collection of ranges, where each range is quad of the two boundary values and whether each of those boundaries is a closed boundary. (Using expressions internally would make the code much shorter, at the cost of being much less tractable when it comes to deriving information like the length of the real line “covered” by the set.) A side-effect of the representation is that the length of the list that represents the set is, after normalization, the number of discrete ranges in the set.
<
proc inRange {x range} {
Line 3,421 ⟶ 3,697:
}
return $len
}</
Basic problems:
<
{(0, 1] ∪ [0, 2)} {
union [realset {(0,1]}] [realset {[0,2)}]
Line 3,441 ⟶ 3,717:
puts "$x : $str :\t[elementOf $x $Set]"
}
}</
Extra credit:
<
for {set i $from} {$i<=$to} {incr i} {
lappend result [list [expr {$i+1./6}] 0 [expr {$i+5./6}] 0]
Line 3,461 ⟶ 3,737:
set B [spi2 0 10]
set AB [difference $A $B]
puts "[llength $AB] contiguous subsets, total length [length $AB]"</
Output:
<pre>
Line 3,482 ⟶ 3,758:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<
var RangeType = Enum.create("RangeType", ["CLOSED", "BOTH_OPEN", "LEFT_OPEN", "RIGHT_OPEN"])
Line 3,558 ⟶ 3,834:
var bb = RealSet.new(0, 10) { |x| (0 < x && x < 10) && ((Num.pi * x).sin.abs > 0.5) }
var cc = aa.subtract(bb)
System.print("Approx length of A - B is %(cc.length)")</
{{out}}
Line 3,585 ⟶ 3,861:
{{trans|D}}
No ∞
<
fcn init(fx){ var [const] contains=fx; }
fcn holds(x){ contains(x) }
Line 3,591 ⟶ 3,867:
fcn __opSub(rs){ RealSet('wrap(x){ contains(x) and not rs.contains(x) }) }
fcn intersection(rs) { RealSet('wrap(x){ contains(x) and rs.contains(x) }) }
}</
The python method could used but the zkl compiler is slow when used in code to generate code.
The method used is a bit inefficient because it closes the contains function of the other set so you can build quite a long call chain as you create new sets.
<
// test union
Line 3,623 ⟶ 3,899:
tester.testRun(s.holds(0.0),Void,False,__LINE__);
tester.testRun(s.holds(1.0),Void,False,__LINE__);
tester.testRun(s.holds(2.0),Void,False,__LINE__);</
{{out}}
<pre>
|