class Statistical::Domain

This class models a mathematical domain by basing it on Ruby's Range Does not allow for enumeration of the domain unless. The primary addition here is the addtion of an exclusion list which allows us to exclude specific points and ranges from within the domain.

@note If the exclusion list contains points outside of the base range these

points are not validated. The user is expect to supply valid input, since
this is a helper class

@note All instances of this class are returned frozen

@author Vaibhav Yenamandra

@attr_reader :exclusions The exclusion list of points and ranges to not

be included in the domain. The list must be homogenous

Constants

TYPES

Attributes

domain_type[R]
exclusions[R]
finish[R]
start[R]

Public Class Methods

[](*args) click to toggle source

Returns a frozen new instance @see new @return [Domain] a new instance of the Domain class

# File lib/statistical/helpers.rb, line 73
def self.[](*args)
  new(*args)
end
new(start, finish, domain_type, *exclusions) click to toggle source

Creates a new domain instance which can be one of the following types

:left_open, :right_open, :full_open, :closed
An exclusion list is also maintained to capture undesired points, ranges

@param [Fixnum, Bignum] start number where the range starts @param [Fixnum, Bignum] finish number where the range ends @param [Symbol] domain_type kind of domain to represent @param exclusions homogenous list of exclusions

Calls superclass method
# File lib/statistical/helpers.rb, line 36
def initialize(start, finish, domain_type, *exclusions)
  exclusions ||= []
  exclude_end = false

  case domain_type
  when :left_open
    @exclusions = [start, exclusions].flatten
    exclude_end = false
  when :right_open
    @exclusions = [exclusions, finish].flatten
    exclude_end = true
  when :full_open
    @exclusions = [start, exclusions, finish].flatten
    exclude_end = true
  when :closed
    @exclusions = [exclusions].flatten
    exclude_end = false
  else
    raise ArgumentError,
          "Invalid domain type, must be one of #{DOMAIN_TYPES}"
  end
  @start = start
  @finish = finish
  @domain_type = domain_type

  super(@start, @finish, exclude_end)
end

Public Instance Methods

<=>(other) click to toggle source

Compares Domain objects with Real numeric types (Fixnum, Bignum, Float)

@param other the point to compare @return -1 if the value lies to the left of the domain

1 if the value lies to the right of domain
0 if the value is included in the range
nil if the two are not deemed comparable
# File lib/statistical/helpers.rb, line 118
def <=>(other)
  case other
  when Fixnum, Bignum, Float
    return -1 if other <= @start && !include?(other)
    return  1 if other >= @finish && !include?(other)
    return  0 if include?(other)
  else
    # Not comparable
    return nil
  end
end
exclude?(val) click to toggle source

Find if a point value is part of the instance's exclusion list

@param val The numeric value to test for exclusion @return [Boolean] if the value is excluded from the current domain

# File lib/statistical/helpers.rb, line 81
def exclude?(val)
  has_val = false
  @exclusions.each do |e|
    case e
    when Fixnum, Bignum, Float
      has_val = (has_val || (e == val))
    when Range
      has_val = (has_val || e.include?(val))
    end
  end
  return has_val
end
include?(val) click to toggle source

Find if a point value is part of the domain

@param val The value to test for inclusion @return [Boolean] if the value is included in the current domain

Calls superclass method
# File lib/statistical/helpers.rb, line 98
def include?(val)
  super(val) && !exclude?(val)
end
inspect() click to toggle source

Serialize the instance

@see to_s @return [String] string representation of the Domain object

Calls superclass method
# File lib/statistical/helpers.rb, line 106
def inspect
  return "[#{super}] - #{@exclusions}" unless @exclusions.empty?
  return super
end
Also aliased as: to_s
new(*args) click to toggle source

Returns a frozen new instance, overrides Range::new @return [Domain] a new instance of the Domain class

Calls superclass method
# File lib/statistical/helpers.rb, line 66
def new(*args)
  super(*args).freeze
end
to_s()
Alias for: inspect