class NumSeq

Attributes

undef_value[RW]

Public Class Methods

behave_strict?() click to toggle source
# File lib/num_seq.rb, line 61
def self.behave_strict?
        return @@strict
end
from_array(array, index_origin = 0) click to toggle source
# 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
new(len, start_at = 0) { |n,i| ... } click to toggle source
# 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
rough_behavior() click to toggle source
# File lib/num_seq.rb, line 69
def self.rough_behavior
        @@strict = false
end
strict_behavior() click to toggle source
# File lib/num_seq.rb, line 65
def self.strict_behavior
        @@strict = true
end

Private Class Methods

maximal_index(seq_a, seq_b) click to toggle source
# File lib/num_seq/internal_ops/common.rb, line 11
def self.maximal_index(seq_a, seq_b)
        if seq_a.max_index > seq_b.max_index then
                seq_a.max_index
        else  
                seq_b.max_index
        end
end
minimal_index(seq_a, seq_b) click to toggle source
# File lib/num_seq/internal_ops/common.rb, line 2
def self.minimal_index(seq_a, seq_b)
        if seq_a.min_index < seq_b.min_index then
                seq_a.min_index
        else  
                seq_b.min_index
        end
end
operate(elem_a, elem_b, op) click to toggle source
# File lib/num_seq/internal_ops/common.rb, line 20
def self.operate(elem_a, elem_b, op)
        if nil != elem_a and nil != elem_b then
                elem_a.send(op, elem_b)
        elsif nil != elem_a then
                elem_a
        elsif nil != elem_b then
                elem_b
        else
                nil
        end           
end

Public Instance Methods

%(arg) click to toggle source
# File lib/num_seq/ops.rb, line 18
def %(arg)
        use_proper_operator(arg, :imod, :xmod)
end
*(arg) click to toggle source
# File lib/num_seq/ops.rb, line 10
def *(arg)
        use_proper_operator(arg, :imul, :xmul)
end
**(arg) click to toggle source
# File lib/num_seq/ops.rb, line 22
def **(arg)
        use_proper_operator(arg, :iexp, :xexp)
end
+(arg) click to toggle source
# File lib/num_seq/ops.rb, line 2
def +(arg)
        use_proper_operator(arg, :isum, :xsum)
end
-(arg) click to toggle source
# File lib/num_seq/ops.rb, line 6
def -(arg)
        use_proper_operator(arg, :isub, :xsub)
end
/(arg) click to toggle source
# File lib/num_seq/ops.rb, line 14
def /(arg)
        use_proper_operator(arg, :idiv, :xdiv)
end
[](n) click to toggle source
# File lib/num_seq.rb, line 78
def [](n)
        return @undef_value unless seq_defined_at? n 
        return @elems[n]
end
[]=(n, value) click to toggle source
# 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
compress(m) click to toggle source

x

# File lib/num_seq.rb, line 179
def compress(m)
end
expand(l) click to toggle source

x

# File lib/num_seq.rb, line 183
def expand(l)
end
first() click to toggle source
# File lib/num_seq.rb, line 122
def first
        return self[n_min]
end
interpolate() { |n_inf,n_sup, m| ... } click to toggle source
# 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() click to toggle source
# File lib/num_seq.rb, line 126
def last
        return self[n_max]
end
length() click to toggle source
# File lib/num_seq.rb, line 107
def length
        return @undef_sup - @undef_inf - 1
end
Also aliased as: size
lshift(k) click to toggle source
# 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() click to toggle source
# File lib/num_seq.rb, line 117
def max_index
        return @undef_sup - 1
end
Also aliased as: n_max
min_index() click to toggle source
# File lib/num_seq.rb, line 112
def min_index
        return @undef_inf + 1
end
Also aliased as: n_min
n_max()
Alias for: max_index
n_min()
Alias for: min_index
range() click to toggle source
# File lib/num_seq.rb, line 103
def range
        return (@undef_inf+1..@undef_sup-1)
end
rshift(k) click to toggle source
# 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) click to toggle source

y = x

# 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
size()
Alias for: length
to_s() click to toggle source
# 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

Private Instance Methods

_lshift(k) click to toggle source
# File lib/num_seq.rb, line 168
def _lshift(k)
        start_at = self.n_min
        length = self.length
        seq = Sequence.new(length, start_at - k) do |n|
                self[n+k]
        end
        return seq
end
_rshift(k) click to toggle source
# File lib/num_seq.rb, line 158
def _rshift(k)
        start_at = self.n_min
        length = self.length
        seq = Sequence.new(length, start_at + k) do |n|
                self[n-k]
        end
        return seq
end
idiv(seq) click to toggle source
# File lib/num_seq/internal_ops/div.rb, line 2
def idiv(seq)
        n_min = self.class.send(:minimal_index, self, seq)
        n_max = self.class.send(:maximal_index, self, seq)

        len = n_max - n_min + 1

        res = self.class.new(len, n_min) do |n|
                self.class.send(:operate, self[n], seq[n], :/)
        end
        return res
end
iexp(seq) click to toggle source
# File lib/num_seq/internal_ops/exp.rb, line 2
def iexp(seq)
        n_min = self.class.send(:minimal_index, self, seq)
        n_max = self.class.send(:maximal_index, self, seq)

        len = n_max - n_min + 1

        res = self.class.new(len, n_min) do |n|
                self.class.send(:operate, self[n], seq[n], :**)
        end
        return res
end
imod(seq) click to toggle source
# File lib/num_seq/internal_ops/mod.rb, line 2
def imod(seq)
        n_min = self.class.send(:minimal_index, self, seq)
        n_max = self.class.send(:maximal_index, self, seq)

        len = n_max - n_min + 1

        res = self.class.new(len, n_min) do |n|
                self.class.send(:operate, self[n], seq[n], :%)
        end
        return res
end
imul(seq) click to toggle source
# File lib/num_seq/internal_ops/mul.rb, line 2
def imul(seq)
        n_min = self.class.send(:minimal_index, self, seq)
        n_max = self.class.send(:maximal_index, self, seq)

        len = n_max - n_min + 1

        res = self.class.new(len, n_min) do |n|
                self.class.send(:operate, self[n], seq[n], :*)
        end
        return res
end
isub(seq) click to toggle source
# File lib/num_seq/internal_ops/sub.rb, line 2
def isub(seq)
        n_min = self.class.send(:minimal_index, self, seq)
        n_max = self.class.send(:maximal_index, self, seq)

        len = n_max - n_min + 1

        res = self.class.new(len, n_min) do |n|
                self.class.send(:operate, self[n], seq[n], :-)
        end
        return res
end
isum(seq) click to toggle source
# File lib/num_seq/internal_ops/sum.rb, line 2
def isum(seq)
        n_min = self.class.send(:minimal_index, self, seq)
        n_max = self.class.send(:maximal_index, self, seq)

        len = n_max - n_min + 1

        res = self.class.new(len, n_min) do |n|
                self.class.send(:operate, self[n], seq[n], :+)
        end
        return res
end
seq_defined_at?(n) click to toggle source
# File lib/num_seq.rb, line 73
def seq_defined_at?(n)
        return n > @undef_inf && n < @undef_sup
end
use_proper_operator(arg, internal_operator, external_operator) click to toggle source
# File lib/num_seq/ops.rb, line 26
def use_proper_operator(arg, internal_operator, external_operator)
        if arg.is_a? Numeric then
                return self.send(external_operator, arg)     
        elsif arg.is_a? NumSeq then
                return self.send(internal_operator, arg)
        else
                raise(ArgumentError, "Attempt to operate an object " + 
                "of the '#{self.class}' class with an object " +
                "of the '#{arg.class}' class using the " +
                "'#{caller[0][/`.*'/][1..-2]}' operator")
        end    
end