Class: NumSeq
- Inherits:
-
Object
- Object
- NumSeq
- Includes:
- Enumerable
- Defined in:
- lib/num_seq.rb,
lib/num_seq/ops.rb,
lib/num_seq/internal_ops/div.rb,
lib/num_seq/internal_ops/exp.rb,
lib/num_seq/internal_ops/mod.rb,
lib/num_seq/internal_ops/mul.rb,
lib/num_seq/internal_ops/sub.rb,
lib/num_seq/internal_ops/sum.rb,
lib/num_seq/internal_ops/common.rb,
lib/num_seq/internal_ops/interpolation.rb
Defined Under Namespace
Classes: SequenceArray
Instance Attribute Summary collapse
-
#undef_value ⇒ Object
Returns the value of attribute undef_value.
Class Method Summary collapse
- .behave_strict? ⇒ Boolean
- .from_array(array, index_origin = 0) ⇒ Object
- .rough_behavior ⇒ Object
- .strict_behavior ⇒ Object
Instance Method Summary collapse
- #%(arg) ⇒ Object
- #*(arg) ⇒ Object
- #**(arg) ⇒ Object
- #+(arg) ⇒ Object
- #-(arg) ⇒ Object
- #/(arg) ⇒ Object
- #[](n) ⇒ Object
- #[]=(n, value) ⇒ Object
-
#compress(m) ⇒ Object
x.
-
#expand(l) ⇒ Object
x.
- #first ⇒ Object
-
#initialize(len, start_at = 0) ⇒ NumSeq
constructor
A new instance of NumSeq.
- #interpolate ⇒ Object
- #last ⇒ Object
- #length ⇒ Object (also: #size)
- #lshift(k) ⇒ Object
- #max_index ⇒ Object (also: #n_max)
- #min_index ⇒ Object (also: #n_min)
- #range ⇒ Object
- #rshift(k) ⇒ Object
- #shift(k) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(len, start_at = 0) ⇒ NumSeq
Returns a new instance of NumSeq.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/num_seq.rb', line 33 def initialize(len, start_at = 0) if len < 0 then raise ArgumentError, "Length of a num_seq must be positive" end @@strict = false @undef_value = nil if 0 == len then @undef_inf = @undef_sup = start_at else @undef_inf = start_at - 1 @undef_sup = start_at + len end @elems = SequenceArray.new(len, @undef_inf + 1) if block_given? then ((@undef_inf+1)...@undef_sup).each_with_index do |n,i| @elems[n] = yield(n,i) end end end |
Instance Attribute Details
#undef_value ⇒ Object
Returns the value of attribute undef_value.
59 60 61 |
# File 'lib/num_seq.rb', line 59 def undef_value @undef_value end |
Class Method Details
.behave_strict? ⇒ Boolean
61 62 63 |
# File 'lib/num_seq.rb', line 61 def self.behave_strict? return @@strict end |
.from_array(array, index_origin = 0) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/num_seq.rb', line 3 def self.from_array(array, index_origin = 0) len = array.length seq = Sequence.new(len, index_origin) do |n, i| array[i] end return seq end |
.rough_behavior ⇒ Object
69 70 71 |
# File 'lib/num_seq.rb', line 69 def self.rough_behavior @@strict = false end |
.strict_behavior ⇒ Object
65 66 67 |
# File 'lib/num_seq.rb', line 65 def self.strict_behavior @@strict = true end |
Instance Method Details
#%(arg) ⇒ Object
18 19 20 |
# File 'lib/num_seq/ops.rb', line 18 def %(arg) use_proper_operator(arg, :imod, :xmod) end |
#*(arg) ⇒ Object
10 11 12 |
# File 'lib/num_seq/ops.rb', line 10 def *(arg) use_proper_operator(arg, :imul, :xmul) end |
#**(arg) ⇒ Object
22 23 24 |
# File 'lib/num_seq/ops.rb', line 22 def **(arg) use_proper_operator(arg, :iexp, :xexp) end |
#+(arg) ⇒ Object
2 3 4 |
# File 'lib/num_seq/ops.rb', line 2 def +(arg) use_proper_operator(arg, :isum, :xsum) end |
#-(arg) ⇒ Object
6 7 8 |
# File 'lib/num_seq/ops.rb', line 6 def -(arg) use_proper_operator(arg, :isub, :xsub) end |
#/(arg) ⇒ Object
14 15 16 |
# File 'lib/num_seq/ops.rb', line 14 def /(arg) use_proper_operator(arg, :idiv, :xdiv) end |
#[](n) ⇒ Object
78 79 80 81 |
# File 'lib/num_seq.rb', line 78 def [](n) return @undef_value unless seq_defined_at? n return @elems[n] end |
#[]=(n, value) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/num_seq.rb', line 83 def []=(n, value) if self.class.behave_strict? and not seq_defined_at?(n) then raise IndexError, %Q%Attempt to assign at index #{n} % + %q%where the num_seq is not defined% end return @elems[n] = value if seq_defined_at? n if n <= @undef_inf then @undef_inf = n - 1 @elems.offset = n @elems[n] = value elsif n >= @undef_sup then @undef_sup = n + 1 @elems[n] = value else raise "Impossible point reached" end end |
#first ⇒ Object
122 123 124 |
# File 'lib/num_seq.rb', line 122 def first return self[n_min] end |
#interpolate ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/num_seq/internal_ops/interpolation.rb', line 2 def interpolate if block_given? then n_inf = n_sup = self.n_min while true # find the next defined element n = n_sup + 1 n_inf = n_sup while n <= self.n_max begin next if nil == self[n] n_sup = n break ensure n += 1 end end (n_inf..n_sup).each do |m| self[m] = yield(n_inf,n_sup, m) end break if self.n_max == n_sup end else # perform linear interpolation by default self.interpolate do |n0,n1,k;m| m = (self[n1] - self[n0]) # to avoid integer divisions m *= 1.0 if m.is_a? Numeric m /= n1 - n0 next (self[n0] + m * (k - n0)) end end return self end |
#last ⇒ Object
126 127 128 |
# File 'lib/num_seq.rb', line 126 def last return self[n_max] end |
#length ⇒ Object Also known as: size
107 108 109 |
# File 'lib/num_seq.rb', line 107 def length return @undef_sup - @undef_inf - 1 end |
#lshift(k) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/num_seq.rb', line 151 def lshift(k) if k < 0 then raise ArgumentError, "Argument should be non-negative" end return _lshift(k) end |
#max_index ⇒ Object Also known as: n_max
117 118 119 |
# File 'lib/num_seq.rb', line 117 def max_index return @undef_sup - 1 end |
#min_index ⇒ Object Also known as: n_min
112 113 114 |
# File 'lib/num_seq.rb', line 112 def min_index return @undef_inf + 1 end |
#range ⇒ Object
103 104 105 |
# File 'lib/num_seq.rb', line 103 def range return (@undef_inf+1..@undef_sup-1) end |
#rshift(k) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/num_seq.rb', line 144 def rshift(k) if k < 0 then raise ArgumentError, "Argument should be non-negative" end return _rshift(k) end |
#shift(k) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/num_seq.rb', line 131 def shift(k) if k < 0 then # left shift _lshift(-k) elsif k > 0 then # right shift _rshift(k) else # return copy -> CHANGE!! return nil end end |
#to_s ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/num_seq.rb', line 186 def to_s index = self.n_min @elems.each do |e| puts "#{index} #{e}" index += 1 end end |