Class: StairCar::UMatrix
Instance Attribute Summary collapse
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
Methods included from Methods
#abs, #sqrt
#array_dimensions, #from_array, included
Methods included from Indicies
#convert_indicies
Methods included from Inspect
#inspect, #max_char_width
Methods included from Iteration
#each, #each_column, #each_row, #each_with_index
#==
#map, #map!, #map_non_zero, #map_non_zero!, #to_a, #transpose, #~
#*, #**, #+, #-, #/, #call_data_method, #dim_convert, #find_params, #max, #mean, #min, #perform, #std, #sum, #variance
#sparse?, #type, #type_class
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)
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)
@data = klass.call(rows_or_data, cols)
else
@data = klass.new(rows_or_data, cols)
end
setup_default_values(initialize_values)
else
@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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/stair_car/umatrix/umatrix.rb', line 90
def [](rows, cols)
rows = convert_indicies(rows, self.rows)
cols = convert_indicies(cols, self.cols)
if rows && cols && rows.size == 1 && cols.size == 1 && rows.first.is_a?(Fixnum) && cols.first.is_a?(Fixnum)
if @data.is_a?(Java::org.ujmp.core.objectmatrix.impl.ObjectCalculationMatrix)
@data.getObject(rows.first.to_java(:int), cols.first.to_java(:int)) || 0.0
else
@data.java_send(:getObject, [Java::long, Java::long], rows.first, cols.first) || 0.0
end
else
if !rows && !cols
return self.class.new(@data)
else
if rows
return self.class.new(@data.select(Java::OrgUjmpCoreCalculation::Calculation.LINK, rows, cols))
else
return self.class.new(@data.select_columns(Java::OrgUjmpCoreCalculation::Calculation.LINK, cols))
end
end
end
end
|
#[]=(rows, cols, value) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/stair_car/umatrix/umatrix.rb', line 117
def []=(rows, cols, value)
rows = convert_indicies(rows, self.rows)
cols = convert_indicies(cols, self.cols)
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]
if value.is_a?(Array)
value_rows, value_cols = array_dimensions(value)
if value_rows == 1 && subview.cols == 1
subview = subview.transpose
end
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
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
164
165
166
|
# File 'lib/stair_car/umatrix/umatrix.rb', line 164
def dup
UMatrix.new(@data.clone)
end
|
#each_non_zero(&block) ⇒ Object
Loop through each non-zero value, pass in the value, row, column
157
158
159
160
161
162
|
# File 'lib/stair_car/umatrix/umatrix.rb', line 157
def each_non_zero(&block)
@data.available_coordinates.each do |row, col|
val = self[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
|