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.
You may install it traditionally, tipically for interactive sessions:
$ gem install borel
Or just put this somewhere on your application's
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 Interval[0,1] Interval[[0,1],[2,3],]
Another way to initialize an Interval is by using the to_interval method on Ranges or Numbers.
1.to_interval (0..1).to_interval (0...2).to_interval
The Infinity constant is available for specifying intervals with no upper or lower boundary.
Interval[-Infinity, 0] Interval[1, Infinity] Interval[-Infinity, Infinity]
Some natural properties of intervals:
Interval.degenerate? # -> true Interval[[0,1],[2,3]].simple? # -> false Interval.empty? # -> true Interval[1,5].include?(3.4) # -> true
complement and ~
~Interval[0,5] # -> Interval[[-Infinity, 0], [5, Infinity]]
union, | and +
Interval[0,5] | Interval[-1,3] # -> Interval[-1,5]
intersect, &, ^
Interval[0,5] ^ Interval[-1,3] # -> Interval[0,3]
minus and -
Interval[0,5] - Interval[-1,3] # -> Interval[3,5]
You may use any Comparable class:
Interval['a','c'] ^ Interval['b','d'] # -> Interval['b','c'] Interval['a','c'] | Interval['b','d'] # -> Interval['a','d']
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)]]
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
- 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
(The MIT License)
Copyright (c) 2012 Amadeus Folego
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.