Module: CsrMatrix::Operations

Includes:
Contracts::Core
Included in:
TwoDMatrix
Defined in:
lib/csrmatrix/operations.rb

Constant Summary collapse

C =
Contracts

Instance Method Summary collapse

Instance Method Details

#get_value(index) ⇒ Object



56
57
58
59
60
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/csrmatrix/operations.rb', line 93

def print_full()
  # 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
  puts ""
end


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/csrmatrix/operations.rb', line 107

def print_sparse()
    # 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 " #{val} "    
            end
        end
        puts ""
    end
end