Class: Statistical::Domain

Inherits:
Range
  • Object
show all
Includes:
Comparable
Defined in:
lib/statistical/helpers.rb

Overview

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

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.

Author:

  • Vaibhav Yenamandra

Constant Summary collapse

TYPES =
[
  :left_open,
  :right_open,
  :full_open,
  :closed
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, finish, domain_type, *exclusions) ⇒ Domain

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

Parameters:

  • start (Fixnum, Bignum)

    number where the range starts

  • finish (Fixnum, Bignum)

    number where the range ends

  • domain_type (Symbol)

    kind of domain to represent

  • exclusions

    homogenous list of exclusions



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

Instance Attribute Details

#:exclusionsObject (readonly)

The exclusion list of points and ranges to not be included in the domain. The list must be homogenous

Returns:

  • (Object)

    the current value of :exclusions



16
17
18
# File 'lib/statistical/helpers.rb', line 16

def :exclusions
  @:exclusions
end

#domain_typeObject (readonly)

Returns the value of attribute domain_type.



19
20
21
# File 'lib/statistical/helpers.rb', line 19

def domain_type
  @domain_type
end

#exclusionsObject (readonly)

Returns the value of attribute exclusions.



19
20
21
# File 'lib/statistical/helpers.rb', line 19

def exclusions
  @exclusions
end

#finishObject (readonly)

Returns the value of attribute finish.



19
20
21
# File 'lib/statistical/helpers.rb', line 19

def finish
  @finish
end

#startObject (readonly)

Returns the value of attribute start.



19
20
21
# File 'lib/statistical/helpers.rb', line 19

def start
  @start
end

Class Method Details

.[](*args) ⇒ Domain

Returns a frozen new instance

Returns:

  • (Domain)

    a new instance of the Domain class

See Also:



73
74
75
# File 'lib/statistical/helpers.rb', line 73

def self.[](*args)
  new(*args)
end

Instance Method Details

#<=>(other) ⇒ Object

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

Parameters:

  • other

    the point to compare

Returns:

  • -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



118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Boolean

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

Parameters:

  • val

    The numeric value to test for exclusion

Returns:

  • (Boolean)

    if the value is excluded from the current domain



81
82
83
84
85
86
87
88
89
90
91
92
# 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 ||= e.include?(val)
    end
  end
  return has_val
end

#include?(val) ⇒ Boolean

Find if a point value is part of the domain

Parameters:

  • val

    The value to test for inclusion

Returns:

  • (Boolean)

    if the value is included in the current domain



98
99
100
# File 'lib/statistical/helpers.rb', line 98

def include?(val)
  super(val) && !exclude?(val)
end

#inspectString Also known as: to_s

Serialize the instance

Returns:

  • (String)

    string representation of the Domain object

See Also:



106
107
108
109
# File 'lib/statistical/helpers.rb', line 106

def inspect
  return "[#{super}] - #{@exclusions}" unless @exclusions.empty?
  return super
end

#new(*args) ⇒ Domain

Returns a frozen new instance, overrides Range::new

Returns:

  • (Domain)

    a new instance of the Domain class



66
67
68
# File 'lib/statistical/helpers.rb', line 66

def new(*args)
  super(*args).freeze
end