Class: MDMatrix

Inherits:
Object
  • Object
show all
Includes:
Colt::Property
Defined in:
lib/colt/matrix/property.rb,
lib/colt/matrix/creation.rb,
lib/colt/matrix/colt_matrix.rb

Direct Known Subclasses

FixPointMDMatrix, FloatingMDMatrix

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colt::Property

#check_rectangular, #check_square, #density, #diagonal?, #diagonally_dominant_by_column?, #diagonally_dominant_by_row?, #equals?, #generate_non_singular!, #identity?, #lower_bandwidth, #lower_bidiagonal?, #lower_triangular?, #non_negative?, #orthogonal?, #positive?, #properties, #semi_bandwidth, #singular?, #skew_symmetric?, #square?, #strictly_lower_triangular?, #strictly_triangular?, #strictly_upper_triangular?, #symmetric?, #tolerance, #tolerance=, #triangular?, #tridiagonal?, #unit_triangular?, #upper_bandwidth, #upper_bidiagonal?, #upper_triangular?, #zero?

Instance Attribute Details

#coercedObject

Returns the value of attribute coerced.



43
44
45
# File 'lib/colt/matrix/colt_matrix.rb', line 43

def coerced
  @coerced
end

#colt_algebraObject (readonly)

Returns the value of attribute colt_algebra.



40
41
42
# File 'lib/colt/matrix/colt_matrix.rb', line 40

def colt_algebra
  @colt_algebra
end

#colt_matrixObject (readonly)

Returns the value of attribute colt_matrix.



39
40
41
# File 'lib/colt/matrix/colt_matrix.rb', line 39

def colt_matrix
  @colt_matrix
end

#colt_propertyObject (readonly)

Returns the value of attribute colt_property.



41
42
43
# File 'lib/colt/matrix/colt_matrix.rb', line 41

def colt_property
  @colt_property
end

#mdarrayObject (readonly)

Returns the value of attribute mdarray.



42
43
44
# File 'lib/colt/matrix/colt_matrix.rb', line 42

def mdarray
  @mdarray
end

Class Method Details

.arange(*args) ⇒ Object





174
175
176
177
# File 'lib/colt/matrix/creation.rb', line 174

def self.arange(*args)
  mdarray = MDArray.arange(*args)
  MDMatrix.from_mdarray(mdarray)
end

.build(type, shape, storage = nil) ⇒ Object





31
32
33
34
35
36
# File 'lib/colt/matrix/creation.rb', line 31

def self.build(type, shape, storage = nil)
  if (shape.size > 3)
    raise "Cannot create MDMatrix of size greater than 3"
  end
  self.from_mdarray(MDArray.build(type, shape, storage))
end

.double(shape, storage = nil) ⇒ Object





42
43
44
# File 'lib/colt/matrix/creation.rb', line 42

def self.double(shape, storage = nil)
  self.build("double", shape, storage)
end

.float(shape, storage = nil) ⇒ Object





50
51
52
# File 'lib/colt/matrix/creation.rb', line 50

def self.float(shape, storage = nil)
  self.build("float", shape, storage)
end

.from_colt_matrix(colt_matrix) ⇒ Object


Creates a new MDMatrix from a given colt_matrix




96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
# File 'lib/colt/matrix/creation.rb', line 96

def self.from_colt_matrix(colt_matrix)

  if (colt_matrix.is_a? DenseDoubleMatrix3D)
    mdarray = MDArray.from_jstorage("double", 
                                    [colt_matrix.slices, colt_matrix.rows, 
                                     colt_matrix.columns], colt_matrix.elements)
    return DoubleMDMatrix3D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseFloatMatrix3D)
    mdarray = MDArray.from_jstorage("float", 
                                    [colt_matrix.slices, colt_matrix.rows, 
                                     colt_matrix.columns], colt_matrix.elements)
    return FloatMDMatrix3D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseLongMatrix3D)
    mdarray = MDArray.from_jstorage("long", 
                                    [colt_matrix.slices, colt_matrix.rows, 
                                     colt_matrix.columns], colt_matrix.elements)
    return LongMDMatrix3D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseIntMatrix3D)
    mdarray = MDArray.from_jstorage("int", 
                                    [colt_matrix.slices, colt_matrix.rows, 
                                     colt_matrix.columns], colt_matrix.elements)
    return IntMDMatrix3D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseDoubleMatrix2D)
    mdarray = MDArray.from_jstorage("double", [colt_matrix.rows, colt_matrix.columns], 
                                    colt_matrix.elements)
    return DoubleMDMatrix2D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseFloatMatrix2D)
    mdarray = MDArray.from_jstorage("float", [colt_matrix.rows, colt_matrix.columns], 
                                    colt_matrix.elements)
    return FloatMDMatrix2D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseLongMatrix2D)
    mdarray = MDArray.from_jstorage("long", [colt_matrix.rows, colt_matrix.columns], 
                                    colt_matrix.elements)
    return LongMDMatrix2D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseIntMatrix2D)
    mdarray = MDArray.from_jstorage("int", [colt_matrix.rows, colt_matrix.columns], 
                                    colt_matrix.elements)
    return IntMDMatrix2D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseDoubleMatrix1D)
    mdarray = MDArray.from_jstorage("double", [colt_matrix.size], colt_matrix.elements)
    return DoubleMDMatrix1D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseFloatMatrix1D)
    mdarray = MDArray.from_jstorage("float", [colt_matrix.size], colt_matrix.elements)
    return FloatMDMatrix1D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseLongMatrix1D)
    mdarray = MDArray.from_jstorage("long", [colt_matrix.size], colt_matrix.elements)
    return LongMDMatrix1D.from_mdarray(mdarray)
  elsif (colt_matrix.is_a? DenseIntMatrix1D)
    mdarray = MDArray.from_jstorage("int", [colt_matrix.size], colt_matrix.elements)
    return IntMDMatrix1D.from_mdarray(mdarray)
  end

end

.from_mdarray(mdarray) ⇒ Object


Creates a MDMatrix from an MDArray. (int rows, int columns, double[] elements, int rowZero, int columnZero, int rowStride, int columnStride, boolean isView)




76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/colt/matrix/creation.rb', line 76

def self.from_mdarray(mdarray)

  case mdarray.rank

  when 1
    dense1D(mdarray)
  when 2
    dense2D(mdarray)
  when 3
    dense3D(mdarray)
  else
    raise "Cannot create MDMatrix of rank greater than 3"
  end

end

.fromfunction(type, shape, &block) ⇒ Object





154
155
156
157
158
159
# File 'lib/colt/matrix/creation.rb', line 154

def self.fromfunction(type, shape, &block)

  mdarray = MDArray.fromfunction(type, shape, &block)
  MDMatrix.from_mdarray(mdarray)

end

.init_with(type, shape, value) ⇒ Object





165
166
167
168
# File 'lib/colt/matrix/creation.rb', line 165

def self.init_with(type, shape, value)
  mdarray = MDArray.init_with(type, shape, value)
  MDMatrix.from_mdarray(mdarray)
end

.int(shape, storage = nil) ⇒ Object





66
67
68
# File 'lib/colt/matrix/creation.rb', line 66

def self.int(shape, storage = nil)
  self.build("int", shape, storage)
end

.linspace(type, start, stop, number) ⇒ Object





192
193
194
195
# File 'lib/colt/matrix/creation.rb', line 192

def self.linspace(type, start, stop, number)
  mdarray = MDArray.linspace(type, start, stop, number)
  MDMatrix.from_mdarray(mdarray)
end

.long(shape, storage = nil) ⇒ Object





58
59
60
# File 'lib/colt/matrix/creation.rb', line 58

def self.long(shape, storage = nil)
  self.build("long", shape, storage)
end

.ones(type, shape) ⇒ Object





201
202
203
204
# File 'lib/colt/matrix/creation.rb', line 201

def self.ones(type, shape)
  mdarray = MDArray.ones(type, shape)
  MDMatrix.from_mdarray(mdarray)
end

.typed_arange(type, *args) ⇒ Object





183
184
185
186
# File 'lib/colt/matrix/creation.rb', line 183

def self.typed_arange(type, *args)
  mdarray = MDArray.typed_arange(type, *args)
  MDMatrix.from_mdarray(mdarray)
end

Instance Method Details

#add(other_val) ⇒ Object Also known as: +





61
62
63
64
65
66
67
68
69
# File 'lib/colt/matrix/colt_matrix.rb', line 61

def add(other_val)
  if (other_val.is_a? Numeric)
    MDMatrix.from_mdarray(@mdarray + other_val)
  elsif (other_val.is_a? MDMatrix)
    MDMatrix.from_mdarray(@mdarray + other_val.mdarray)
  else
    raise "Cannot add a matrix to the given value"
  end
end

#coerce(num) ⇒ Object





49
50
51
52
53
54
55
# File 'lib/colt/matrix/colt_matrix.rb', line 49

def coerce(num)

  matrix = MDMatrix.from_mdarray(@mdarray)
  matrix.coerced = true
  [matrix, num]

end

#copyObject





210
211
212
# File 'lib/colt/matrix/creation.rb', line 210

def copy
  MDMatrix.from_mdarray(self.mdarray.copy)
end

#div(other_val) ⇒ Object Also known as: /





77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/colt/matrix/colt_matrix.rb', line 77

def div(other_val)
  
  if (other_val.is_a? Numeric)
    val1, val2 = (@coerced)? [other_val, @mdarray] : [@mdarray, other_val] 
    MDMatrix.from_mdarray(val1 / val2)
  elsif (other_val.is_a? MDMatrix)
    begin
      self * other_val.inverse
    rescue Exception => e
      puts e.message
      raise "Dividing by singular matrix is not possible"
    end
  else
    raise "Cannot divide the given value from matrix"
  end

end

#each(&block) ⇒ Object





118
119
120
# File 'lib/colt/matrix/colt_matrix.rb', line 118

def each(&block)
  @mdarray.each(&block)
end

#each_with_counter(&block) ⇒ Object





126
127
128
# File 'lib/colt/matrix/colt_matrix.rb', line 126

def each_with_counter(&block)
  @mdarray.each_with_counter(&block)
end

#fill(val, func = nil) ⇒ Object


Fills the array with the given value




150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/colt/matrix/colt_matrix.rb', line 150

def fill(val, func = nil)

  if (func)
    return MDMatrix.from_colt_matrix(@colt_matrix.assign(val.colt_matrix, func))
  end

  if ((val.is_a? Numeric) || (val.is_a? Proc) || (val.is_a? Class))
    MDMatrix.from_colt_matrix(@colt_matrix.assign(val))
  elsif (val.is_a? MDMatrix)
    MDMatrix.from_colt_matrix(@colt_matrix.assign(val.colt_matrix))
  else
    raise "Cannot fill a Matrix with the given value"
  end
end

#fill_cond(cond, val) ⇒ Object


Fills the matrix based on a given condition




169
170
171
# File 'lib/colt/matrix/colt_matrix.rb', line 169

def fill_cond(cond, val)
  return MDMatrix.from_colt_matrix(@colt_matrix.assign(cond, val))
end

#flip(dim) ⇒ Object


Create a new Array using same backing store as this Array, by flipping the index so that it runs from shape-1 to 0.




219
220
221
# File 'lib/colt/matrix/colt_matrix.rb', line 219

def flip(dim)
  MDMatrix.from_mdarray(@mdarray.flip(dim))
end

#get(*index) ⇒ Object Also known as: []





208
209
210
# File 'lib/colt/matrix/colt_matrix.rb', line 208

def get(*index)
  @colt_matrix.get(*index)
end

#nextObject





142
143
144
# File 'lib/colt/matrix/colt_matrix.rb', line 142

def next
  @mdarray.next
end

#normalize!Object





227
228
229
# File 'lib/colt/matrix/colt_matrix.rb', line 227

def normalize!
  @colt_matrix.normalize
end




282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/colt/matrix/colt_matrix.rb', line 282

def print

  case mdarray.type

  when "double"
    formatter = DoubleFormatter.new
  when "float"
    formatter = FloatFormatter.new
  when "long"
    formatter = LongFormatter.new
  when "int"
    formatter = IntFormatter.new

  end

  printf(formatter.toString(@colt_matrix))

end

#rankObject





235
236
237
# File 'lib/colt/matrix/colt_matrix.rb', line 235

def rank
  @mdarray.rank
end

#reduce(aggr, func, cond = nil) ⇒ Object


Applies a function to each cell and aggregates the results. Returns a value v such that v==a(size()) where a(i) == aggr( a(i-1), f(get(row,column)) ) and terminators are a(1) == f(get(0,0)), a(0)==Double.NaN.




179
180
181
182
# File 'lib/colt/matrix/colt_matrix.rb', line 179

def reduce(aggr, func, cond = nil)
  (cond)? @colt_matrix.aggregate(aggr, func, cond) : 
    @colt_matrix.aggregate(aggr, func)
end

#region(*args) ⇒ Object


Makes a view of this array based on the given parameters shape origin size stride range section spec




266
267
268
# File 'lib/colt/matrix/colt_matrix.rb', line 266

def region(*args)
  MDMatrix.from_mdarray(@mdarray.region(*args))
end

#reset_traversalObject





134
135
136
# File 'lib/colt/matrix/colt_matrix.rb', line 134

def reset_traversal
  @mdarray.reset_traversal
end

#reshape!(shape) ⇒ Object


Reshapes the Matrix.




188
189
190
191
192
# File 'lib/colt/matrix/colt_matrix.rb', line 188

def reshape!(shape)
  @mdarray.reshape!(shape)
  @colt_matrix = MDMatrix.from_mdarray(@mdarray).colt_matrix
  self
end

#set(row, column, val) ⇒ Object Also known as: []=





198
199
200
# File 'lib/colt/matrix/colt_matrix.rb', line 198

def set(row, column, val)
  @colt_matrix.set(row, column, val)
end

#shapeObject





243
244
245
# File 'lib/colt/matrix/colt_matrix.rb', line 243

def shape
  @mdarray.shape
end

#sizeObject





251
252
253
# File 'lib/colt/matrix/colt_matrix.rb', line 251

def size
  @mdarray.size
end

#sub(other_val) ⇒ Object Also known as: -





101
102
103
104
105
106
107
108
109
110
# File 'lib/colt/matrix/colt_matrix.rb', line 101

def sub(other_val)
  if (other_val.is_a? Numeric)
    val1, val2 = (@coerced)? [other_val, @mdarray] : [@mdarray, other_val] 
    MDMatrix.from_mdarray(val1 - val2)
  elsif (other_val.is_a? MDMatrix)
    MDMatrix.from_mdarray(@mdarray - matrix.mdarray)
  else
    raise "Cannot subtract the given value from matrix"
  end
end

#sumObject





274
275
276
# File 'lib/colt/matrix/colt_matrix.rb', line 274

def sum
  @colt_matrix.zSum
end