Set of real numbers: Difference between revisions

Content added Content deleted
(Added Kotlin)
Line 728: Line 728:


(Note that this result is not exactly the same as the previous result. Determining why would be an interesting exercise in numerical analysis.)
(Note that this result is not exactly the same as the previous result. Determining why would be an interesting exercise in numerical analysis.)

=={{header|Kotlin}}==
The RealSet class has two constructors - a primary one which creates an object for an arbitrary predicate and a secondary one which creates an object for a simple range by generating the appropriate predicate and then invoking the primary one.

As far as the optional work is concerned, I decided to add a length property which gives only an approximate result. Basically, it works by keeping track of the low and high values of the set and then counting points at successive small intervals between these limits which satisfy the predicate. An isEmpty() function has also been added but as this depends, to some extent, on the length property it is not 100% reliable.

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.
<lang scala>// version 1.1.4-3

typealias RealPredicate = (Double) -> Boolean

enum class RangeType { CLOSED, BOTH_OPEN, LEFT_OPEN, RIGHT_OPEN }

class RealSet(val low: Double, val high: Double, val predicate: RealPredicate) {

constructor (start: Double, end: Double, rangeType: RangeType): this(start, end,
when (rangeType) {
RangeType.CLOSED -> fun(d: Double) = d in start..end
RangeType.BOTH_OPEN -> fun(d: Double) = start < d && d < end
RangeType.LEFT_OPEN -> fun(d: Double) = start < d && d <= end
RangeType.RIGHT_OPEN -> fun(d: Double) = start <= d && d < end
}
)

fun contains(d: Double) = predicate(d)

infix fun union(other: RealSet): RealSet {
val low2 = minOf(low, other.low)
val high2 = maxOf(high, other.high)
return RealSet(low2, high2) { predicate(it) || other.predicate(it) }
}
infix fun intersect(other: RealSet): RealSet {
val low2 = maxOf(low, other.low)
val high2 = minOf(high, other.high)
return RealSet(low2, high2) { predicate(it) && other.predicate(it) }
}

infix fun subtract(other: RealSet) = RealSet(low, high) { predicate(it) && !other.predicate(it) }

var interval = 0.00001

val length: Double get() {
if (!low.isFinite() || !high.isFinite()) return -1.0 // error value
if (high <= low) return 0.0
var p = low
var count = 0
do {
if (predicate(p)) count++
p += interval
}
while (p < high)
return count * interval
}

fun isEmpty() = if (high == low) !predicate(low) else length == 0.0
}

fun main(args: Array<String>) {
val a = RealSet(0.0, 1.0, RangeType.LEFT_OPEN)
val b = RealSet(0.0, 2.0, RangeType.RIGHT_OPEN)
val c = RealSet(1.0, 2.0, RangeType.LEFT_OPEN)
val d = RealSet(0.0, 3.0, RangeType.RIGHT_OPEN)
val e = RealSet(0.0, 1.0, RangeType.BOTH_OPEN)
val f = RealSet(0.0, 1.0, RangeType.CLOSED)
val g = RealSet(0.0, 0.0, RangeType.CLOSED)

for (i in 0..2) {
val dd = i.toDouble()
println("(0, 1] ∪ [0, 2) contains $i is ${(a union b).contains(dd)}")
println("[0, 2) ∩ (1, 2] contains $i is ${(b intersect c).contains(dd)}")
println("[0, 3) − (0, 1) contains $i is ${(d subtract e).contains(dd)}")
println("[0, 3) − [0, 1] contains $i is ${(d subtract f).contains(dd)}\n")
}

println("[0, 0] is empty is ${g.isEmpty()}\n")

val aa = RealSet(0.0, 10.0) { x -> (0.0 < x && x < 10.0) &&
Math.abs(Math.sin(Math.PI * x * x)) > 0.5 }
val bb = RealSet(0.0, 10.0) { x -> (0.0 < x && x < 10.0) &&
Math.abs(Math.sin(Math.PI * x)) > 0.5 }
val cc = aa subtract bb
println("Approx length of A - B is ${cc.length}")
}</lang>

{{out}}
<pre>
(0, 1] ∪ [0, 2) contains 0 is true
[0, 2) ∩ (1, 2] contains 0 is false
[0, 3) − (0, 1) contains 0 is true
[0, 3) − [0, 1] contains 0 is false

(0, 1] ∪ [0, 2) contains 1 is true
[0, 2) ∩ (1, 2] contains 1 is false
[0, 3) − (0, 1) contains 1 is true
[0, 3) − [0, 1] contains 1 is false

(0, 1] ∪ [0, 2) contains 2 is false
[0, 2) ∩ (1, 2] contains 2 is false
[0, 3) − (0, 1) contains 2 is true
[0, 3) − [0, 1] contains 2 is true

[0, 0] is empty is false

Approx length of A - B is 2.07587
</pre>


=={{header|Mathematica}}==
=={{header|Mathematica}}==