Set of real numbers: Difference between revisions

m (agreement)
Line 1,570:
}
</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}}==
2,484

edits