How to resolve the algorithm Set of real numbers step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Set of real numbers step by step in the Wren programming language

Table of Contents

Problem Statement

All real numbers form the uncountable set ℝ. Among its subsets, relatively simple are the convex sets, each expressed as a range between two real numbers a and b where a ≤ b. There are actually four cases for the meaning of "between", depending on open or closed boundary: Note that if a = b, of the four only [a, a] would be non-empty. Task Implementation notes Optional work |sin(π x)| > 1/2 is the same as n + 1/6 < x < n + 5/6 for all integers n; your program does not need to derive this by itself.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Set of real numbers step by step in the Wren programming language

Source code in the wren programming language

import "/dynamic" for Enum

var RangeType = Enum.create("RangeType", ["CLOSED", "BOTH_OPEN", "LEFT_OPEN", "RIGHT_OPEN"])

class RealSet {
    construct new(start, end, pred) {
        _low  = start
        _high = end
        _pred = (pred == RangeType.CLOSED)     ? Fn.new { |d| d >= _low && d <= _high } :
                (pred == RangeType.BOTH_OPEN)  ? Fn.new { |d| d >  _low && d <  _high } :
                (pred == RangeType.LEFT_OPEN)  ? Fn.new { |d| d >  _low && d <= _high } :
                (pred == RangeType.RIGHT_OPEN) ? Fn.new { |d| d >= _low && d <  _high } : pred
    }

    low  { _low  }
    high { _high }
    pred { _pred }

    contains(d) { _pred.call(d) }

    union(other) {
        if (!other.type == RealSet) Fiber.abort("Argument must be a RealSet")
        var low2 = _low.min(other.low)
        var high2 = _high.max(other.high)
        return RealSet.new(low2, high2) { |d| _pred.call(d) || other.pred.call(d) }
    }

    intersect(other) {
        if (!other.type == RealSet) Fiber.abort("Argument must be a RealSet")
        var low2 = _low.max(other.low)
        var high2 = _high.min(other.high)
        return RealSet.new(low2, high2) { |d| _pred.call(d) && other.pred.call(d) }
    }

    subtract(other) {
        if (!other.type == RealSet) Fiber.abort("Argument must be a RealSet")
        return RealSet.new(_low, _high) { |d| _pred.call(d) && !other.pred.call(d) }
    }

    length {
        if (_low.isInfinity || _high.isInfinity) return -1  // error value
        if (_high <= _low) return 0
        var p = _low
        var count = 0
        var interval = 0.00001
        while (true) {
            if (_pred.call(p)) count = count + 1
            p = p + interval
            if (p >= _high) break
        }
        return count * interval
    }

    isEmpty { (_high == _low) ? !_pred.call(_low) : length == 0 }
}

var a = RealSet.new(0, 1, RangeType.LEFT_OPEN)
var b = RealSet.new(0, 2, RangeType.RIGHT_OPEN)
var c = RealSet.new(1, 2, RangeType.LEFT_OPEN)
var d = RealSet.new(0, 3, RangeType.RIGHT_OPEN)
var e = RealSet.new(0, 1, RangeType.BOTH_OPEN)
var f = RealSet.new(0, 1, RangeType.CLOSED)
var g = RealSet.new(0, 0, RangeType.CLOSED)

for (i in 0..2) {
    System.print("(0, 1] ∪ [0, 2) contains %(i) is %(a.union(b).contains(i))")
    System.print("[0, 2) ∩ (1, 2] contains %(i) is %(b.intersect(c).contains(i))")
    System.print("[0, 3) − (0, 1) contains %(i) is %(d.subtract(e).contains(i))")
    System.print("[0, 3) − [0, 1] contains %(i) is %(d.subtract(f).contains(i))\n")
}

System.print("[0, 0] is empty is %(g.isEmpty)\n")

var aa = RealSet.new(0, 10) { |x| (0 < x && x < 10) && ((Num.pi * x * x).sin.abs > 0.5) }
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)")

  

You may also check:How to resolve the algorithm Literals/String step by step in the Picat programming language
You may also check:How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Python programming language
You may also check:How to resolve the algorithm Base64 decode data step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Substitution cipher step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the Tcl programming language