From 629ac9d04b83a9f49f2b04dfed0eaf9cd32ac062 Mon Sep 17 00:00:00 2001 From: pporter2876 Date: Wed, 7 May 2014 22:48:35 -0400 Subject: [PATCH 1/4] Update svd_matrix.rb Column size no longer works --- lib/svd_matrix.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/svd_matrix.rb b/lib/svd_matrix.rb index 2e38d22..dd55c50 100644 --- a/lib/svd_matrix.rb +++ b/lib/svd_matrix.rb @@ -37,16 +37,21 @@ 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 + c_count += 1 + end + 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 +61,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 From 53ed4c0f04bae805c2271e3873f84230f2a62684 Mon Sep 17 00:00:00 2001 From: pporter2876 Date: Wed, 7 May 2014 23:02:06 -0400 Subject: [PATCH 2/4] Update svd.c Needed to return value. --- ext/svd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 */ From 5f479f730fb214ab7dd16839287e8c97ad1ac406 Mon Sep 17 00:00:00 2001 From: pporter2876 Date: Thu, 8 May 2014 18:30:36 -0400 Subject: [PATCH 3/4] Update svd_matrix.rb prints --- lib/svd_matrix.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/svd_matrix.rb b/lib/svd_matrix.rb index dd55c50..302d2d3 100644 --- a/lib/svd_matrix.rb +++ b/lib/svd_matrix.rb @@ -43,6 +43,12 @@ def decompose(reduce_dimensions_to = nil) input_array += row c_count += 1 end + 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 From f142ac04963ab2b1ea9ae74c23db49868f31dd86 Mon Sep 17 00:00:00 2001 From: pporter2876 Date: Thu, 8 May 2014 19:24:13 -0400 Subject: [PATCH 4/4] Update svd_matrix.rb --- lib/svd_matrix.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/svd_matrix.rb b/lib/svd_matrix.rb index 302d2d3..19bd522 100644 --- a/lib/svd_matrix.rb +++ b/lib/svd_matrix.rb @@ -41,8 +41,8 @@ def decompose(reduce_dimensions_to = nil) c_count = 0 @rows.each do |row| input_array += row - c_count += 1 end + c_count = input_array.length / row_size puts "input array" puts input_array.length puts "row size"