math.real-set
Class to work with subsets of real numbers.
Version
0.4.0
Install
npm install math.real-set
Usage
const RealSet = require('math.real-set')const A = RealSet.parse('(0, 2] U {6, 7, 8}')const B = RealSet.parse('(5, 5)')const C = RealSet.parse('[1, 2)')const D = RealSet.parse('(2, 7)')const E = RealSet.parse('(8, 9]')// emptynessA.isEmpty() // falseB.isEmpty() // true// Containing numbersA.contains(4) // falseA.contains(6) // trueB.contains(3) // false// Containing setsA.contains(C) // true// joining setsA.union(D) // A U D is equivalent to RealSet.parse('(0, 7] U {8}')A.union(E) // A U E is equivalent to RealSet.parse('(0, 2] U {6, 7} U [8, 9]')// converting to stringA.toString() // '{6, 7, 8} U (0, 2]'B.toString() // '{}'const F = RealSet.parse('[3, 3]')F.toString() // '{3}'
API
Real subsets creation
parse :: String -> RealSet
Static method that takes and string as a representation of RealSet and returns a RealSet instance. It throws an error if string is not parsable to RealSet.
Example:
const RealSet = require('math.real-set')const set = RealSet.parse('(2, 5] U {6} U [8, 9)')
fromIntervals :: [Interval] -> RealSet
Static method that takes an array of Intervals and returns a RealSet instance.
Example:
const RealSet = require('math.real-set')const set = RealSet.fromIntervals([[{value: 2, limit: 1}, {value: 5, limit: 0}], // (2, 5][{value: 6, limit: 0}, {value: 6, limit: 0}], // {6}[{value: 8, limit: 0}, {value: 9, limit: -1}] // [8, 9)])
Predicates
contains :: RealSet ~> RealSet -> Boolean
Returns true or false depending on real set param is contained inside the set or not, respectively.
Example:
const RealSet = require('math.real-set')const A = RealSet.parse('[1, 3) U [4, 8]')const B = RealSet.parse('(1, 2) U [4, 7)')const C = RealSet.parse('{3, 6}')A.contains(B) // trueA.contains(C) // falseC.contains(A) // false
has :: RealSet ~> Number -> Boolean
Returns true
or false
if the set has a number or not, respectively.
Example:
const RealSet = require('math.real-set')const set = RealSet.parse('(1, 5] U {8}')set.has(1) // falseset.has(3) // trueset.has(5) // trueset.has(7) // falseset.has(8) // true
isEmpty :: RealSet ~> Boolean
Returns true or false if set is empty or not.
Example:
const RealSet = require('math.real-set')const A = RealSet.parse('[2, 4)')const B = RealSet.parse('[4, 2)')A.isEmpty() // falseB.isEmpty() // true
Real subset operations
union :: RealSet ~> RealSet -> RealSet
Instance method that returns the union of two sets.
const RealSet =const A = RealSetconst B = RealSetA // [1, 4] U {5} U (6, 7)
union :: [RealSet] -> RealSet
Static method that takes an array of RealSet instances and returns a RealSet instance that represents the union of the list of sets.
const RealSet = require('math.real-set')const A = RealSet.parse('[1, 3) U (3, 4]')const B = RealSet.parse('(2, 4) U {5}')const C = RealSet.parse('{5} U (6, 7)')RealSet.union([A, B, C]) // [1, 4] U {5} (6, 7)
RealSet converters
toString :: RealSet ~> String
Converts the real set into a string representation.
const RealSet = require('math.real-set')const A = RealSet.parse('(5, 2] U (3, 3)') // emptyconst B = RealSet.parse('[2, 2] U (5, 6)') // singleton intervalA.toString() // '{}'B.toString() // '{2} U (5, 6)'
toIntervals :: RealSet ~> [Interval]
Converts the real set into an array of disjoint Intervals ordered from lowest to highest.
const RealSet = require('math.real-set')const A = RealSet.parse('(5, 6) U [2, 2] U {4, 8}')A.toIntervals() /* [[{value: 2, limit: 0}, {value: 2, limit: 0}], // {2}[{value: 4, limit: 0}, {value: 4, limit: 0}], // {4}[{value: 5, limit: 1}, {value: 6, limit: -1}], // (5, 6)[{value: 8, limit: 0}, {value: 8, limit: 0}] // {8}]
LICENSE
MIT