### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

181 lines (127 sloc) 4.351 kb

# Borel

Borelian sets are formed by enumerable union, intersection or complement, of intervals.

Borel enables performing regular operations on intervals of any comparable class.

Borel borrows many of the ideas (and code) from the Intervals gem. However it differs from Intervals in which it's aim is not on numerical precision and calculation, but on ease of use and solving some general interval related problems.

## Install

You may install it traditionally, tipically for interactive sessions:

``````\$ gem install borel
``````

Or just put this somewhere on your application's `Gemfile`

`gem 'borel'`

## Usage

### Initializing

An Interval can be initialized with an empty, one or two sized array (respectively for an empty, degenerate or `Simple` interval), or an array of one or two sized arrays (for a `Multiple` interval).

```  Interval[]
Interval[1]
Interval[0,1]
Interval[[0,1],[2,3],[5]]```

Another way to initialize an Interval is by using the `#to_interval` method extension.

```  nil.to_interval     # -> Interval[]
1.to_interval       # -> Interval[1]
(1..2).to_interval  # -> Interval[1,2]
(1...3).to_interval # -> Interval[1,2]
[1,2].to_interval   # -> Interval[1,2]```

The `Infinity` constant is available for specifying intervals with no upper or lower boundary.

```  Interval[-Infinity, 0]
Interval[1, Infinity]
Interval[-Infinity, Infinity]```

### Properties

Some natural properties of intervals:

```  Interval[1,2].inf             # -> 1
Interval[1,2].sup             # -> 2
Interval[1].degenerate?       # -> true
Interval[[0,1],[2,3]].simple? # -> false
Interval[].empty?             # -> true
Interval[1,5].include?(3.4)   # -> true```

### Operations

• Complement

`#complement`, alias: `#~`

`    ~Interval[0,5]             # -> Interval[[-Infinity, 0], [5, Infinity]]`
• Union

`#union`, aliases: `#|`,`#+`

`Interval[0,5] | Interval[-1,3] # -> Interval[-1,5]`
• Intersection

`#intersect`, aliases: `#&`,`#^`

`Interval[0,5] ^ Interval[-1,3] # -> Interval[0,3]`
• Subtraction

`#minus`, alias: `#-`

`Interval[0,5] - Interval[-1,3] # -> Interval[3,5]`

## Generic Intervals

You may use any Comparable class:

• String
```Interval['a','c'] ^ Interval['b','d'] # -> Interval['b','c']
Interval['a','c'] | Interval['b','d'] # -> Interval['a','d']```
• Time
```def t(seconds)
Time.now + seconds
end

Interval[t(1),t(5)] ^ Interval[t(3),t(7)] # -> Interval[t(3),t(5)]
Interval[t(1),t(2)] | Interval[t(3),t(4)] # -> Interval[[t(1),t(2)],[t(3),t(4)]]```

## Math Extensions

By requiring `borel/math_extensions` you are provided with some natural math-related interval methods:

```require 'borel/math_extensions'

Interval[1,5].rand  # -> Random.new.rand 1..5
Interval[1,5].width # -> 5-1, only for simple intervals```

It's supported only for `Numeric`, `Comparable` and arithmetic supported classes

## Remarks

• There is no distinction between open and closed intervals
• `complement` and `minus` operations, and also Math Extensions have limited support for non numeric-comparable classes