module CsrMatrix::Operations

Constants

C

Public Instance Methods

get_value(index) click to toggle source
# File lib/csrmatrix/operations.rb, line 56
def get_value(index)
    # gets the value off of the index of matrix
    is_invariant?
    return @val[index]
end
index(row, col=nil) click to toggle source
# File lib/csrmatrix/operations.rb, line 63
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
insert(value, row, column) click to toggle source
# File lib/csrmatrix/operations.rb, line 9
def insert(value, row, column)
  # gets the value off of the index of matrix
  is_invariant?
  # post value inserted or updated in the given row and column

  # insert if the value exists
  for i in self.row_ptr[row-1]..self.row_ptr[row]-1
    if column-1 == self.col_ind[i]
      self.val[i] = value
      return true
    end
  end

  # add value if it does not exist
  for i in self.columns-1..0
    index = self.row_ptr[row]-1 + i
    if self.col_ind[index] < column-1
      #add value
      self.col_ind.insert(index+1, column-1)
      self.val.insert(index+1, value)
      
      #increment row pointers
      for j in row..self.row_ptr.count()-1
        self.row_ptr[j] += 1
      end

      return true
    end
  end

  #add value
  self.col_ind.insert(self.row_ptr[row]-1, column-1)
  self.val.insert(self.row_ptr[row]-1, value)

  #increment row pointers
  for j in row..self.row_ptr.count()-1
    self.row_ptr[j] += 1
  end             
  return true

  #post
  if self.index(row, column) != value
    raise ContractReturnError.new "index not as expected"
  end
end
print_full() click to toggle source
print_sparse() click to toggle source