lib/daru/vector.rb in daru-0.0.3 vs lib/daru/vector.rb in daru-0.0.3.1
- old
+ new
@@ -1,7 +1,12 @@
+require_relative 'math/arithmetic/vector.rb'
+require_relative 'math/statistics/vector.rb'
+
module Daru
class Vector
+ include Daru::Math::Arithmetic::Vector
+ include Daru::Math::Statistics::Vector
include Enumerable
def each(&block)
@vector.each(&block)
end
@@ -9,14 +14,14 @@
attr_reader :name
attr_reader :index
attr_reader :size
# Pass it name, source and index
- def initialize *args
- name = args.shift
- source = args.shift || []
- index = args.shift
+ def initialize source, opts={}
+ source = source || []
+ name = opts[:name]
+ index = opts[:index]
set_name name
@vector =
case source
@@ -48,16 +53,17 @@
if @index.include? index
@vector[@index[index]]
elsif index.is_a?(Numeric)
@vector[index]
else
- raise IndexError, "Specified index #{index} does not exist."
+ return nil
end
else
indexes.unshift index
- Daru::Vector.new @name, indexes.map { |index| @vector[@index[index]] }, indexes
+ Daru::Vector.new indexes.map { |index| @vector[@index[index]] },name: @name,
+ index: indexes
end
end
def []=(index, value)
if @index.include? index
@@ -128,10 +134,14 @@
hsh[index] = self[index]
hsh
end
end
+ def to_a
+ @vector.to_a
+ end
+
def to_json *args
self.to_hash.to_json
end
def to_html threshold=15
@@ -156,11 +166,12 @@
def to_s
to_html
end
def inspect spacing=10, threshold=15
- longest = [@index.to_a.map(&:to_s).map(&:size).max,
+ longest = [@name.to_s.size,
+ @index.to_a.map(&:to_s).map(&:size).max,
@vector .map(&:to_s).map(&:size).max].max
content = ""
longest = spacing if longest > spacing
name = @name || 'nil'
@@ -168,11 +179,11 @@
content += "\n#<" + self.class.to_s + ":" + self.object_id.to_s + " @name = " + name.to_s + " @size = " + size.to_s + " >"
content += sprintf formatter, "", name
@index.each_with_index do |index, num|
- content += sprintf formatter, index.to_s, self[index]
+ content += sprintf formatter, index.to_s, (self[index] || 'nil').to_s
if num > threshold
content += sprintf formatter, '...', '...'
break
end
@@ -192,10 +203,10 @@
def rename new_name
@name = new_name.to_sym
end
def dup
- Daru::Vector.new @name, @vector.dup, @index.dup
+ Daru::Vector.new @vector.dup, name: @name, index: @index.dup
end
def daru_vector *name
self
end
\ No newline at end of file