Class: StairCar::UMatrix
- Inherits:
-
Object
- Object
- StairCar::UMatrix
- Includes:
- Indicies, InitMethods, Inspect, Iteration, Methods, UMatrixCompare, UMatrixMatrixMath, UMatrixTransforms, UMatrixTypes
- Defined in:
- lib/stair_car/umatrix/umatrix.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #[](rows, cols) ⇒ Object
- #[]=(rows, cols, value) ⇒ Object
- #cols ⇒ Object
- #dup ⇒ Object
-
#each_non_zero(&block) ⇒ Object
Loop through each non-zero value, pass in the value, row, column.
-
#initialize(rows_or_data = nil, cols = nil, type = :double, sparse = false, initialize_values = :zeros) ⇒ UMatrix
constructor
A new instance of UMatrix.
- #rows ⇒ Object
- #setup_default_values(initialize_values) ⇒ Object
- #shape ⇒ Object
- #size ⇒ Object
-
#to_i ⇒ Object
Takes a 1x1 matrix and converts it to an integer, raises an exception if the matrix is not 1x1.
-
#value_at(row, col) ⇒ Object
Gets the value at the row and column.
Methods included from Methods
Methods included from InitMethods
#array_dimensions, #from_array, included
Methods included from Indicies
Methods included from Inspect
Methods included from Iteration
#each, #each_column, #each_row, #each_with_index
Methods included from UMatrixCompare
Methods included from UMatrixTransforms
#map, #map!, #map_non_zero, #map_non_zero!, #to_a, #transpose, #~
Methods included from UMatrixMatrixMath
#*, #**, #+, #-, #/, #call_data_method, #dim_convert, #find_params, #max, #mean, #min, #perform, #std, #sum, #variance
Methods included from UMatrixTypes
Constructor Details
#initialize(rows_or_data = nil, cols = nil, type = :double, sparse = false, initialize_values = :zeros) ⇒ UMatrix
Returns a new instance of UMatrix.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 30 def initialize(rows_or_data=nil, cols=nil, type=:double, sparse=false, initialize_values=:zeros) if rows_or_data.is_a?(Array) klass = type_class(type, sparse, initialize_values) # Create the matrix from an array from_array(rows_or_data, klass) elsif rows_or_data.is_a?(Fixnum) raise MatrixDimensionsError, "Must specify columns and rows" unless rows_or_data && cols klass = type_class(type, sparse, initialize_values) if klass.is_a?(Method) || klass.is_a?(Proc) # A factory method was returned, call to build @data = klass.call(rows_or_data, cols) else # A class was returned, create new @data = klass.new(rows_or_data, cols) end setup_default_values(initialize_values) else # Passing in data directly @data = rows_or_data end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
28 29 30 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 28 def data @data end |
Instance Method Details
#[](rows, cols) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 114 def [](rows, cols) rows = convert_indicies(rows, self.rows) cols = convert_indicies(cols, self.cols) # Get subview, also convert rows/cols to java arrays if !rows && !cols return self.class.new(@data) else if rows return self.class.new(@data.select(Java::OrgUjmpCoreCalculation::Calculation.LINK, rows, cols)) else # If row is nil, we need to lookup by columns directly return self.class.new(@data.select_columns(Java::OrgUjmpCoreCalculation::Calculation.LINK, cols)) end end end |
#[]=(rows, cols, value) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 131 def []=(rows, cols, value) rows = convert_indicies(rows, self.rows) cols = convert_indicies(cols, self.cols) # Set either the value in a cell or a subview with a matrix if rows && cols && rows.size == 1 && cols.size == 1 && rows.first.is_a?(Fixnum) && cols.first.is_a?(Fixnum) @data.setObject(value.to_java(:double), rows.first, cols.first) else subview = self[rows, cols] # Assign a single array or a nested array if value.is_a?(Array) value_rows, value_cols = array_dimensions(value) # If one dimentional, and they want to set cols if value_rows == 1 && subview.cols == 1 # Transpose so we can place an array vertically subview = subview.transpose end # Check to make sure the sizes match if value_rows != subview.rows || value_cols != subview.cols raise MatrixDimensionsError, "the array you are trying to assign is not the correct size" end end # value = convert_value(value) unless value[0].is_a?(Array) value = [value] end value.each_with_index do |row,row_index| row.each_with_index do |cell,col_index| subview[row_index, col_index] = cell end end end end |
#cols ⇒ Object
86 87 88 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 86 def cols return shape[1] end |
#dup ⇒ Object
178 179 180 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 178 def dup UMatrix.new(@data.clone) end |
#each_non_zero(&block) ⇒ Object
Loop through each non-zero value, pass in the value, row, column
171 172 173 174 175 176 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 171 def each_non_zero(&block) @data.available_coordinates.each do |row, col| val = self.value_at(row,col) yield(val, row, col) end end |
#rows ⇒ Object
82 83 84 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 82 def rows return shape[0] end |
#setup_default_values(initialize_values) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 54 def setup_default_values(initialize_values) if initialize_values == :asc i = 0 self.map! do |val,row,col| self[row,col] = i i += 1 end elsif initialize_values == :desc i = size self.map! do |val,row,col| self[row,col] = i i -= 1 end end end |
#shape ⇒ Object
71 72 73 74 75 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 71 def shape rows, cols = data.size return [rows, cols] end |
#size ⇒ Object
77 78 79 80 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 77 def size rows, cols = shape return rows * cols end |
#to_i ⇒ Object
Takes a 1x1 matrix and converts it to an integer, raises an exception if the matrix is not 1x1
92 93 94 95 96 97 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 92 def to_i if rows != 1 || cols != 1 raise IncorrectMatrixDimensions, "to_i should only be called on 1x1 matricies" end return value_at(0, 0) end |
#value_at(row, col) ⇒ Object
Gets the value at the row and column
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/stair_car/umatrix/umatrix.rb', line 100 def value_at(row, col) row = convert_indicies(row, self.rows) col = convert_indicies(col, self.cols) # Returns either the value in a cell or a subview # From jruby we have to call this way for doubles if @data.is_a?(Java::org.ujmp.core.objectmatrix.impl.ObjectCalculationMatrix) @data.getObject(row.first.to_java(:int), col.first.to_java(:int)) || 0.0 else # From jruby we have to call this way for doubles @data.java_send(:getObject, [Java::long, Java::long], row.first, col.first) || 0.0 end end |