Skip to content
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
2 changes: 1 addition & 1 deletion ext/svd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
25 changes: 18 additions & 7 deletions lib/svd_matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down