National Poetry Month

# npm

## math.interval-utils

0.3.0 • Public • Published

# math.interval-utils

This library provides a data structure and functions to do operations with intervals.

0.3.0

## Interval

### Data structure and valid values

Real interval can be represented with an pair of real numbers and two flags to indicate if these numbers are included or not in interval. This library defines the `Interval` type as the set of arrays of two objects with `value` and `limit` number properties:

`value` properties correspond to the values of interval and `limit` properties correspond to if values of interval are included or not.

• If limit is `0`, it indicates that value is included.
• If the `limit` of first item is `1` , it indicates that first `value` is not included in interval.
• If the `limit` of second item is `-1`, it indicates that second `value` is not included in interval.

For example:

• `(1, 5]` is represented with:
• `[-1, 3]` is represented with:
• `(10, 12)` is represented with:

### Empty interval

An interval is empty in these cases:

• `value` of first element is greater than `value` of second element.
• `value` of first and second elements are equal but `limit` of first element is greater than `limit` of second element.

## Functions

### areDisjoint :: (Interval, Interval) -> Boolean

Given two interval inputs, it returns `true` or `false` depending on intervals are disjoint or not, respectively.

Example:

### areEqual :: (Interval, Interval) -> Boolean

Given two interval inputs, it returns `true` or `false` depending on intervals are equal or not, respectively.

Example:

### contains :: (Interval, Interval) -> Boolean

Given two interval inputs, it returns `true` or `false` if first interval contains the second interval or not, respectively.

Example:

### intersection :: (Interval, Interval) -> Interval

Given two interval inputs, it returns the intersection of these intervals.

Example:

### isEmpty :: Interval -> Boolean

Given an `interval`, it returns `true` or `false` if `interval` is empty or not, respectively.

Example:

### isInterval :: Interval -> Boolean

It returns `true` or `false` if `interval` is an Interval.

Example:

### multiIntersection :: ([Interval], [Interval]) -> [Interval]

Given two lists of disjoint sorted intervals, it returns a new list of disjoint sorted intervals that represent the intersection of these sets.

Example:

This method has better perfomance than recolecting the intersections of each interval of each lists and intersecting one by one.

### numToInterval :: Number -> Interval

Given a number input, it returns an singleton interval that contains this number.

Example:

### parser :: string -> Either Interval String

Given a string that represents an interval, it returns an Either.Right value that saves an interval. If the string does not represent an interval it returns and Either.Left value that saves an error.

Example:

### relativeComplement :: (Interval, Interval) -> [Interval]

Given two interval inputs, it returns a list of intevals that represents the relative complement. It means, the set of numbers that belongs to the first interval but not the second.

Example:

// returns empty array relativeComplement(I['(2, 3)'], I['[1, 5)']) // []

### union :: [Interval] -> [Interval]

Given an array of intervals, it returns an array of sorted disjoint intervals that represents the union of these intervals.

Example:

MIT

## Keywords

### Install

`npm i math.interval-utils`

### Repository

github.com/xgbuils/math.interval-utils

134

0.3.0

MIT

21.3 kB

21