Module: CsrMatrix::Helpers
- Included in:
- TwoDMatrix
- Defined in:
- lib/csrmatrix/helpers.rb
Instance Method Summary collapse
-
#count_nonzero(array) ⇒ Object
max_row.
-
#count_total(array) ⇒ Object
depth.
-
#depth(array) ⇒ Object
count_nonzero.
-
#max_col(array) ⇒ Object
ARRAY FUNCTIONS for pre-processing of matrix.
-
#max_row(array) ⇒ Object
max_col.
Instance Method Details
#count_nonzero(array) ⇒ Object
max_row
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/csrmatrix/helpers.rb', line 42 def count_nonzero(array) # Finds all nonzero values in an array. # pre array # post int nonzero count of array max_count = 0 array.each_index do |i| = array[i] .each_index do |x| if array[i][x] != 0 max_count += 1 end end end return max_count end |
#count_total(array) ⇒ Object
depth
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/csrmatrix/helpers.rb', line 74 def count_total(array) # Counts all elements in array - assumed 2d # pre array # post int count of all elements max_count = 0 array.each_index do |i| = array[i] .each_index do |x| max_count += 1 end end return max_count end |
#depth(array) ⇒ Object
count_nonzero
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/csrmatrix/helpers.rb', line 58 def depth(array) # Code from http://stackoverflow.com/questions/9545613/getting-dimension-of-multidimensional-array-in-ruby # Identifies the depth of an array. # pre array # post int depth of array return 0 if array.class != Array result = 1 array.each do |sub_a| if sub_a.class == Array dim = depth(sub_a) result = dim + 1 if dim + 1 > result end end return result end |
#max_col(array) ⇒ Object
ARRAY FUNCTIONS for pre-processing of matrix
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/csrmatrix/helpers.rb', line 9 def max_col(array) # Identifies the 'column' value of an array (eg. the number of entries in a column) # pre array # post column count of array values = array max_count = 0 # Loop over indexes. values.each_index do |i| counter = 0 # Get subarray and loop over its indexes also. = values[i] .each_index do |x| counter += 1 end if counter > max_count max_count = counter end end return max_count end |
#max_row(array) ⇒ Object
max_col
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/csrmatrix/helpers.rb', line 30 def max_row(array) # Identifies the 'row' value of an array (eg. the number of entries in a row) # pre array # post row count of array values = array max_count = 0 values.each_index do |i| max_count += 1 end return max_count end |