diff --git a/ext/svd.c b/ext/svd.c index 8145e60..9ce5a45 100644 --- a/ext/svd.c +++ b/ext/svd.c @@ -21,7 +21,7 @@ VALUE decompose(VALUE module, VALUE matrix_ruby, VALUE m_ruby, VALUE n_ruby) { /* precondition */ if((m*n) != RARRAY_LEN(matrix_ruby)) { rb_raise(rb_eRangeError, "Size of the array is not equal to m * n"); - return; + return 0; } /* convert to u matrix */ diff --git a/lib/svd_matrix.rb b/lib/svd_matrix.rb index 2e38d22..19bd522 100644 --- a/lib/svd_matrix.rb +++ b/lib/svd_matrix.rb @@ -37,16 +37,27 @@ def inspect # [ 0, 0, 0, 0 ] def decompose(reduce_dimensions_to = nil) input_array = [] - @rows.each {|row| input_array += row} - u_array, w_array, v_array = SVD.decompose(input_array, row_size, column_size) + # @rows.each {|row| input_array += row} + c_count = 0 + @rows.each do |row| + input_array += row + end + c_count = input_array.length / row_size + puts "input array" + puts input_array.length + puts "row size" + puts row_size + puts "column size" + puts c_count + u_array, w_array, v_array = SVD.decompose(input_array, row_size, c_count) # recompose U matrix - u = SVDMatrix.new(row_size, reduce_dimensions_to || column_size) - row_size.times {|i| u.set_row(i, u_array.slice!(0, column_size)[0...(reduce_dimensions_to || column_size)])} + u = SVDMatrix.new(row_size, reduce_dimensions_to || c_count) + row_size.times {|i| u.set_row(i, u_array.slice!(0, c_count)[0...(reduce_dimensions_to || c_count)])} # recompose V matrix - v = SVDMatrix.new(column_size, reduce_dimensions_to || column_size) - column_size.times {|i| v.set_row(i, v_array.slice!(0, column_size)[0...(reduce_dimensions_to || column_size)])} + v = SVDMatrix.new(c_count, reduce_dimensions_to || c_count) + c_count.times {|i| v.set_row(i, v_array.slice!(0, c_count)[0...(reduce_dimensions_to || c_count)])} # diagonalise W array as a matrix if reduce_dimensions_to @@ -56,7 +67,7 @@ def decompose(reduce_dimensions_to = nil) [u, w, v] end - + # Reduce the number of dimensions of the data to dimensions. # Returns a back a recombined matrix (conceptually the original # matrix dimensionally reduced). For example Latent Semantic