Permalink
Browse files

Changed matrix creation so that dense Ruby object matrices start with…

… nils instead of undefined. This eliminates some of the segfaults, but not enough.
  • Loading branch information...
1 parent ce6bbf6 commit f9fa6a4e40dd1be8c3a7186dd623e8d4eafc98c9 @mohawkjohn mohawkjohn committed Mar 26, 2012
Showing with 15 additions and 6 deletions.
  1. +0 −1 ext/nmatrix/dense.c
  2. +9 −4 ext/nmatrix/nmatrix.c
  3. +1 −1 lib/nmatrix.rb
  4. +5 −0 spec/nmatrix_spec.rb
View
@@ -130,7 +130,6 @@ DENSE_STORAGE* create_dense_storage(int8_t dtype, size_t* shape, size_t rank, vo
if (elements_length == count) s->elements = elements;
else {
s->elements = ALLOC_N(char, nm_sizeof[dtype]*count);
- // allocation succeeded
if (elements_length > 0) {
// repeat elements over and over again until the end of the matrix
View
@@ -585,6 +585,7 @@ size_t* nm_interpret_initial_capacity(VALUE arg) {
static VALUE nm_init(int argc, VALUE* argv, VALUE nm) {
char ZERO = 0;
+ VALUE QNIL = Qnil;
int8_t dtype, stype, offset = 0;
size_t rank;
size_t* shape;
@@ -616,10 +617,14 @@ static VALUE nm_init(int argc, VALUE* argv, VALUE nm) {
if (TYPE(argv[1+offset]) == T_ARRAY) init_val_len = RARRAY_LEN(argv[1+offset]);
else init_val_len = 1;
}
- } else if (stype == S_DENSE) {
- init_val = NULL; // no need to initialize dense with any kind of default value.
- } else { // if it's a list or compressed, we want to assume default of 0 even if none provided
- if (stype == S_YALE) {
+ } else {
+ if (stype == S_DENSE) { // no need to initialize dense with any kind of default value unless it's an NM_ROBJ matrix
+ if (dtype == NM_ROBJ) { // pretend [nil] was passed for ROBJ.
+ init_val = ALLOC(VALUE);
+ SetFuncs[NM_ROBJ][NM_ROBJ](1, init_val, 0, &QNIL, 0);
+ init_val_len = 1;
+ } else init_val = NULL;
+ } else if (stype == S_YALE) { // if it's a list or compressed, we want to assume default of 0 even if none provided
init_val = ALLOC(size_t);
*(size_t*)init_val = 0;
} else {
View
@@ -44,7 +44,7 @@ def pretty_print
(0...shape[0]).each do |i|
arr = []
(0...shape[1]).each do |j|
- arr << self[i,j]
+ arr << (self[i,j].nil? ? "nil" : self[i,j])
end
puts arr.join(" ")
end
View
@@ -36,6 +36,11 @@
RATIONAL_MATRIX43A_ARRAY = MATRIX43A_ARRAY.collect { |x| x.to_r }
RATIONAL_MATRIX32A_ARRAY = MATRIX32A_ARRAY.collect { |x| x.to_r }
+ it "correctly fills dense Ruby object matrix with nil" do
+ n = NMatrix.new([4,3], :object)
+ n[0,0].should == nil
+ end
+
it "correctly fills dense with individual assignments" do
n = NMatrix.new([4,3], :float64)
n[0,0] = 14.0

0 comments on commit f9fa6a4

Please sign in to comment.