Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with v2v bin operation and indexes (#501) #539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/daru/helpers/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,18 @@ def array_of?(array, match)
!array.empty? &&
array.all? { |el| match === el } # rubocop:disable Style/CaseEquality
end

def sort_composite_data(array)
array.sort
rescue ArgumentError, TypeError => msg
case msg.to_s
when /comparison of Symbol with String failed/,
/comparison of Symbol with \d+ failed/,
/comparison of String with :.* failed/,
/comparison of Integer with :.* failed/,
/no implicit conversion from nil to integer/
return array.sort_by(&:to_s)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/daru/maths/arithmetic/vector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def v2o_binary operation, other

def v2v_binary operation, other, opts={}
# FIXME: why the sorting?.. - zverok, 2016-05-18
index = (@index.to_a | other.index.to_a).sort
index = ArrayHelper.sort_composite_data(@index.to_a | other.index.to_a)

elements = index.map do |idx|
this = self.index.include?(idx) ? self[idx] : nil
Expand Down
2 changes: 1 addition & 1 deletion spec/maths/arithmetic/vector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end

it "appropriately adds vectors with numeric and non-numeric indexes" do
pending "Need an alternate index implementation?"
# pending "Need an alternate index implementation?"
v1 = Daru::Vector.new([1,2,3])
v2 = Daru::Vector.new([1,2,3], index: [:a,:b,:c])

Expand Down