Set of real numbers: Difference between revisions

Added FreeBASIC
m (syntax highlighting fixup automation)
(Added FreeBASIC)
 
(4 intermediate revisions by 3 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 needsneed not apply.
* 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 805:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
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();
Line 843:
set3(2.0r).assertTrue();
}</syntaxhighlight>
 
=={{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}}==
Line 1,570 ⟶ 1,719:
}
</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}}==
Line 3,487 ⟶ 3,758:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="ecmascriptwren">import "./dynamic" for Enum
 
var RangeType = Enum.create("RangeType", ["CLOSED", "BOTH_OPEN", "LEFT_OPEN", "RIGHT_OPEN"])
2,122

edits