Skip to content

Commit

Permalink
Fix Vector initialization when dtype is nmatrix, vector has nils and …
Browse files Browse the repository at this point in the history
…no nm_type is given (#153) (#369)
  • Loading branch information
baarkerlounger authored and zverok committed Jul 20, 2017
1 parent ab401ab commit 0708984
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/daru/accessors/nmatrix_wrapper.rb
Expand Up @@ -25,6 +25,8 @@ def inject(*args, &block)
attr_reader :size, :data, :nm_dtype

def initialize vector, context, nm_dtype=:int32
# To avoid arrays with nils throwing TypeError for nil nm_dtype
nm_dtype = :object if nm_dtype.nil? && vector.any?(&:nil?)
@size = vector.size
@data = NMatrix.new [@size*2], vector.to_a, dtype: nm_dtype
@context = context
Expand Down
2 changes: 1 addition & 1 deletion lib/daru/vector.rb
Expand Up @@ -1515,7 +1515,7 @@ def cast_vector_to dtype, source=nil, nm_dtype=nil
else raise ArgumentError, "Unknown dtype #{dtype}"
end

@dtype = dtype || :array
@dtype = dtype
new_vector
end

Expand Down
5 changes: 5 additions & 0 deletions spec/vector_spec.rb
Expand Up @@ -84,6 +84,11 @@
expect(dv.index.to_a).to eq(['a', 'b', :r, 0])
end

it "initializes array with nils with dtype NMatrix" do
dv = Daru::Vector.new [2, nil], dtype: :nmatrix
expect(dv.to_a).to eq([2, nil])
expect(dv.index.to_a).to eq([0, 1])
end
end

context "#reorder!" do
Expand Down

0 comments on commit 0708984

Please sign in to comment.