Class: TwoDMatrix

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core, Contracts::Invariants, CsrMatrix::Arithmetic, CsrMatrix::Decompositions, CsrMatrix::Exceptions, CsrMatrix::Functions, CsrMatrix::Helpers, CsrMatrix::MContracts, CsrMatrix::Operations, CsrMatrix::Properties
Defined in:
lib/csrmatrix.rb

Overview

General code convention in this manner - generate documentation via ‘rdoc lib’.

Constant Summary collapse

C =
Contracts
Url =

The current website ref. Used for verification of rb systems.

"https://github.com/Team-Aqua/Matrix-Library/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CsrMatrix::Helpers

#count_nonzero, #count_total, #depth, #max_col, #max_row

Methods included from CsrMatrix::Decompositions

#eigen, #eigenvalue

Methods included from CsrMatrix::Functions

#det, #determinant, included, #rank, #round, #tr, #trace

Methods included from CsrMatrix::Arithmetic

#count_in_dim, included, #inverse, #inverse_multiply, #is_same_dim, #matrix_add, #matrix_division, #matrix_multiply, #matrix_subtract, #matrix_vector, #max_row, #multiply_csr, #multiply_inverse, #scalar_add, #scalar_division, #scalar_exp, #scalar_multiply, #scalar_subtract, #t, #transpose

Methods included from CsrMatrix::Properties

#diagonal?, #empty?, included, #lower_triangular?, #nonsingular?, #normal?, #not_null?, #orthogonal?, #permutation?, #real?, #singular?, #symmetric?, #unitary?, #upper_triangular?, #zero?

Methods included from CsrMatrix::Operations

#get_value, #index, #insert, #print_full, #print_sparse

Constructor Details

#initializeTwoDMatrix

Blank setup; setup module.



76
77
78
79
80
81
82
83
84
# File 'lib/csrmatrix.rb', line 76

def initialize()	
  @nonzero_count = nil
  @row_ptr = nil
  @col_ind = nil
  @val = nil
  @rows = 0
  @columns = 0
  @ndim = 2
end

Instance Attribute Details

#col_indObject (readonly)

Returns the value of attribute col_ind.



73
74
75
# File 'lib/csrmatrix.rb', line 73

def col_ind
  @col_ind
end

#columnsObject (readonly)

Returns the value of attribute columns.



73
74
75
# File 'lib/csrmatrix.rb', line 73

def columns
  @columns
end

#ndimObject (readonly)

Returns the value of attribute ndim.



73
74
75
# File 'lib/csrmatrix.rb', line 73

def ndim
  @ndim
end

#row_ptrObject (readonly)

Returns the value of attribute row_ptr.



73
74
75
# File 'lib/csrmatrix.rb', line 73

def row_ptr
  @row_ptr
end

#rowsObject (readonly)

Returns the value of attribute rows.



73
74
75
# File 'lib/csrmatrix.rb', line 73

def rows
  @rows
end

#valObject (readonly)

Returns the value of attribute val.



73
74
75
# File 'lib/csrmatrix.rb', line 73

def val
  @val
end

Instance Method Details

#==(o) ⇒ Object

equals override for matrix operations



92
93
94
95
96
97
# File 'lib/csrmatrix.rb', line 92

def ==(o)
  # equals overide to check if object o equals self
  # pre   o, self
  # post  true if o is_a csrmatrix and o == self
  o.class == self.class && o.state == state
end

#build(type, data, extra = nil) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/csrmatrix.rb', line 243

def build(type, data, extra = nil)
  # builds matrix dependent on input
  # pre   string type
  #       data, dependent type
  # post  generated matrix
  #       boolean depending on build
  case type 
    when "matrix"
      self.build_from_matrix(data)
    when "row", "rows"
      self.build_from_rows(data)
    when "array"
      self.build_from_array(data)
    when "column", "columns"
      self.build_from_columns(data)
    when "identity", "i", "unit"
      if extra != nil 
        self.build_identity_matrix(data, extra)
      else
        self.build_identity_matrix(data)
      end
    when "zero"
      self.build_zero_matrix(data)
    when "csr"
      self.build_from_csr(data[0], data[1], data[2], data[3], data[4])
    else 
      raise MatrixTypeException.new, "Bad build type, no build response."
      return false;
    end 
    return true;
end

#build_from_array(array) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/csrmatrix.rb', line 178

def build_from_array(array)
#Contracts: Pre
  if !same_sublength(array)
    raise MatrixDimException.new, "Invalid row/column pairs imported."
    return false
  end
  #END Contracts: Pre
  @val = [] #Allows Invariant Check to Pass for following method call
  @columns, @rows, nonzero_count, @val, @row_ptr, @col_ind = convert_to_csr(array)
  return true
end

#build_from_columns(array) ⇒ Object



294
295
296
297
298
# File 'lib/csrmatrix.rb', line 294

def build_from_columns(array)
  # builds a matrix given its columns ;; redirect to array build
# build a matrix given columns. same implimentation as array build
  self.build_from_array(array)
end

#build_from_csr(row_ptr, col_ind, val, col_siz, row_siz) ⇒ Object



319
320
321
322
323
324
325
326
327
# File 'lib/csrmatrix.rb', line 319

def build_from_csr(row_ptr, col_ind, val, col_siz, row_siz)
  # generate an array from user generated csr values
  @val = val
  @row_ptr = row_ptr
  @col_ind = col_ind
  @rows = row_siz
  @columns = col_siz
  self
end

#build_from_matrix(matrix) ⇒ Object



277
278
279
280
281
# File 'lib/csrmatrix.rb', line 277

def build_from_matrix(matrix)
# builds a csr matrix a ruby matrix
  build_from_array(matrix.to_a())
  return true
end

#build_from_rows(array) ⇒ Object



285
286
287
288
289
# File 'lib/csrmatrix.rb', line 285

def build_from_rows(array)
# builds a csr matrix from rows
  build_from_array(array)
  self.transpose()
end

#build_identity_matrix(size) ⇒ Object



302
303
304
305
306
# File 'lib/csrmatrix.rb', line 302

def build_identity_matrix(size)
  # FIXME: test code: replace with CSR identity gen
# generate identity matrix of a given size
  self.build_from_array(Matrix.identity(size).to_a())
end

#build_zero_matrix(rows, columns = rows) ⇒ Object



310
311
312
313
314
315
# File 'lib/csrmatrix.rb', line 310

def build_zero_matrix(rows, columns = rows)
  # FIXME: test code: replace with CSR zero gen
# generate a matrix with all values equaling zero for a given number of rows and columns
  self.build_from_array(Matrix.zero(rows, columns).to_a())
  return true
end

#checkInputBounds(row, col) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/csrmatrix.rb', line 124

def checkInputBounds(row, col)
  # checks whether or not the index searched is within bounds	
  if row > @rows
    raise IndexOutOfRangeException.new, "Row index too large"
    return false
  elsif col > @columns
    raise IndexOutOfRangeException.new, "Column index too large"
    return false
  elsif row < 0
    raise IndexOutOfRangeException.new, "Row index too small"
    return false
  elsif col < 0
    raise IndexOutOfRangeException.new, "Column index too small"
    return false
  else
    return true
  end
end

#convert_to_csr(array) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/csrmatrix.rb', line 192

def convert_to_csr(array)
  # converts a given array to csr format
  # pre  array


  # post csrmatrix from array
  row_count = 0
  col_count = 0
  nonzero_count = 0

  row_val = 0
  row_prev_sum = 0

  col_val = 0

  value_array = Array.new
  row_ptr = Array.new
  col_ind = Array.new
  
  array.each_index do |i| # each row
    col_val = 0 # eg. for pos [0, 1, 2, 3] it goes 0, 1, 2, 3
    col_tmp = 0
    row_count += 1
    row_prev_sum = row_val
    row_ptr << row_prev_sum # ref: http://op2.github.io/PyOP2/linear_algebra.html
    subarray = array[i]
    subarray.each_index do |x| # each column entry in row
      col_tmp += 1
      if array[i][x] != 0
        # use nonzero value in CSR
        if array[i][x] == nil
          return false
        end
        nonzero_count += 1
        value_array << array[i][x]
        col_ind << col_val
        row_val += 1
      end
      col_val += 1 # eg. col_val add at the end
    end
    if col_tmp >= col_count
      col_count = col_tmp
    end
  end
  row_prev_sum = row_val
  row_ptr << row_prev_sum
  return [col_count, row_count, nonzero_count, value_array, row_ptr, col_ind]
end

#decomp_to_matrixObject



166
167
168
169
# File 'lib/csrmatrix.rb', line 166

def decomp_to_matrix()
  @matrix = Matrix.rows(self.decompose())
  return @matrix
end

#decomposeObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/csrmatrix.rb', line 148

def decompose()
# decompose the matrix into an array
# pre  csrmatrix
# post array from the csrmartix
  res = Array.new(@rows) { Array.new(@columns, 0) }
  row_counter = 0
  row_idx = 0
  @row_ptr.drop(1).each do |i| #eg. 2 4 7 10
    while row_counter < i 
      res[row_idx][@col_ind[row_counter]] = @val[row_counter]
      row_counter += 1
    end 
    row_idx += 1
  end
  return res
end

#dimensionsObject



110
111
112
113
114
# File 'lib/csrmatrix.rb', line 110

def dimensions()
  is_invariant?
  # returns the dimensions of the csrmatrix
  return [@rows, @columns]
end

#is_invariant?Boolean

@nonzero_count.is_a?(array) FIXME????????? and @nonzero_count.count() >= 0 invariant(@nonzero_count) { @nonzero_count >= 0} invariant(@row_ptr) { @row_ptr.count() >= 0 and @row_ptr.is_a?(Array)} @row_pointer.is_a?(array) and @row_pointer.count() >= 0 @col_ind.is_a?(array) and @col_ind.count() >= 0 @val.is_a?(array) and @val.count() >= 0

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/csrmatrix.rb', line 45

def is_invariant?
  if @val == nil
    raise InvariantError.new, "Empty Matrix"
    return false
  end
  if @columns < 0
    raise InvariantError.new, "Invalid Column Dimension"
    return false
  end
  if @rows < 0
    raise InvariantError.new, "Invalid Row Dimension"
    return false
  end
  #!@nonzero_count.is_a?(Array) or IS THIS AN ARRAY?
  # if @nonzero_count == nil
  #   raise InvariantError.new, "Invalid Non-Zero Count"
  #   return false
  # end
  # @nonzero_count.is_a?(array) and @nonzero_count.count() >= 0
# @row_pointer.is_a?(array) and @row_pointer.count() >= 0
# @col_ind.is_a?(array) and @col_ind.count() >= 0
# @val.is_a?(array) and @val.count() >= 0
# @dimension == 2
  return true
end

#same_sublength(array) ⇒ Object

FIXME: Could be a contract in itself Contract :ArrayOf[C::ArrayOf] => C::Bool #Causes Invariant issues



333
334
335
336
337
338
339
340
341
342
# File 'lib/csrmatrix.rb', line 333

def same_sublength(array)
# ensures that all sub arrays have the same length
  testLength = array[0].length
  array.each do |subarray|
    if(subarray.length != testLength)
        return false
    end
  end
  return true
end

#square?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
# File 'lib/csrmatrix.rb', line 117

def square?
  # returns whether or not the system is square
  is_invariant?
  return self.rows == self.columns
end

#stateObject

FIXME: convert to protected value Contract C::None => :ArrayOf[ArrayOf,ArrayOf,ArrayOf,C::Nat,C::Nat,C::Nat]



101
102
103
104
105
106
# File 'lib/csrmatrix.rb', line 101

def state
  # returns the current state of the csrmatrix
  # pre self
  # post [@value, @row_pointer, @column_index, @rows, @columns, @dimension]
  [@val, @row_ptr, @col_ind, @rows, @columns, @ndim]
end