lib/csrmatrix/operations.rb in csrmatrix-1.0.0 vs lib/csrmatrix/operations.rb in csrmatrix-1.0.1

- old
+ new

@@ -1,33 +1,75 @@ +require "csrmatrix/exceptions" + module CsrMatrix module Operations + include Contracts::Core + C = Contracts + + Contract C::Nat => C::Num def get_value(index) + # gets the value off of the index of matrix + is_invariant? return @val[index] - end + end # get_value + Contract C::Nat, C::Or[C::Nat, nil] => C::Num + def index(row, col=nil) + # gets the index in the matrix at row, col + is_invariant? + + if col == nil + if @val.count < row + raise CsrMatrix::Exceptions::IndexOutOfRangeException.new, "Index out of Bounds" + return false + end + + return @val[row-1] + else + if !checkInputBounds(row, col) + raise CsrMatrix::Exceptions::IndexOutOfRangeException.new, "Index out of Bounds" + return false + end + + num_elm_in_prev = row_ptr[row-1] + num_elm_in_row = row_ptr[row] - num_elm_in_prev + + (0...num_elm_in_row).each do | x | + if ( col-1 == @col_ind[num_elm_in_prev+x] ) + return @val[num_elm_in_prev+x] + end + end + return 0 + end + end # index + + Contract C::None => C::Any def print_full() - (0...@col_ind.length).each do |iter| - (0...@col_ind.length).each do |col| - if (iter == @row_ptr[iter] and col == @col_ind[iter]) - print "#{get_value(iter)} " - else - print "0 " - end + # prints the full matrix for user + is_invariant? + full_matrix = self.decompose() + full_matrix.each do | row | + row.each do | val | + print "#{val} " end puts "" - end - end + end + end # print_full + Contract C::None => C::Any def print_sparse() - (0...@col_ind.length).each do |iter| - (0...@col_ind.length).each do |col| - if (iter == @row_ptr[iter] and col == @col_ind[iter]) - print "#{get_value(iter)} " + # prints all nonzero values of matrix for user + is_invariant? + full_matrix = self.decompose() + full_matrix.each do | row | + row.each do | val | + if val == 0 + print "---" else - print "0 " + print " #{val} " end end puts "" end - end - end -end \ No newline at end of file + end # print_sparse + end # Operations +end # CsrMatrix