Class: SparseMatrix::DenseMatrix

Inherits:
AbstractMatrix show all
Defined in:
lib/sparse_matrix.rb

Overview

clase para matrices densas

Instance Attribute Summary collapse

Attributes inherited from AbstractMatrix

#column, #row

Instance Method Summary collapse

Methods inherited from AbstractMatrix

#read_matrix

Constructor Details

#initialize(r = 0, c = 0, matrix = []) ⇒ DenseMatrix

Returns a new instance of DenseMatrix.



185
186
187
188
# File 'lib/sparse_matrix.rb', line 185

def initialize(r=0,c=0,matrix=[])
    super(r,c)
    @mat = matrix
end

Instance Attribute Details

#cObject

Returns the value of attribute c.



190
191
192
# File 'lib/sparse_matrix.rb', line 190

def c
  @c
end

#matObject

Returns the value of attribute mat.



190
191
192
# File 'lib/sparse_matrix.rb', line 190

def mat
  @mat
end

#rObject

Returns the value of attribute r.



190
191
192
# File 'lib/sparse_matrix.rb', line 190

def r
  @r
end

Instance Method Details

#*(b) ⇒ Object

multiplicacion de matrices, dispersas y densas



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
# File 'lib/sparse_matrix.rb', line 243

def *(b)  #multiplicacion de matrices, dispersas y densas
if b.instance_of? SparseMatrix
    c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
    0.upto @mat.length-1 do |i|
        0.upto @mat.length-1 do |j|
            c.mat[i][j]=0
            0.upto @mat.length-1 do |k|
                c.mat[i][j] += @mat[i][k]*b.valor(k,j)
            end
        end
    end
    c
else
    c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
    0.upto @mat.length-1 do |i|
        0.upto @mat.length-1 do |j|
            c.mat[i][j]=0
            0.upto @mat.length-1 do |k|
                c.mat[i][j] += @mat[i][k]*b.mat[k][j]
            end
        end
    end
    c
end
end

#+(b) ⇒ Object

suma de matrices, tando densas como dispersas



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/sparse_matrix.rb', line 224

def +(b)  #suma de matrices, tando densas como dispersas
    c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
    if b.instance_of? SparseMatrix
        0.upto @mat.size-1 do |i|
            0.upto @mat.size-1 do |j|
                c.mat[i][j] = self.mat[i][j]+b.valor(i,j)
            end
        end
        c
    else
        0.upto @mat.size-1 do |i|
            0.upto @mat.size-1 do |j|
                c.mat[i][j] = self.mat[i][j]+b.mat[i][j]
            end
        end
        c
    end
end

metodo que imprime la matrix en pantalla



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/sparse_matrix.rb', line 208

def print_matrix()  #metodo que imprime la matrix en pantalla
    printf "| "
    for i in (0... @mat.length)
        for j in (0... @mat.length)
            if j==0
                printf "{ "
            end
            printf "#{@mat[i][j]}\t"
            if j == @mat.length-1
                printf " } ,"
            end
        end
    end
    printf "|"
end

#to_sObject

devuelve cadena string



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/sparse_matrix.rb', line 192

def to_s() #devuelve cadena string
    s="| "
    for i in (0... @mat.length)
        for j in (0... @mat.length)
            if j==0
                s += "{ "
            end
            s += "#{@mat[i][j]}\t"
            if j == @mat.length-1
                s += " } , "
            end
        end
    end
    s += "|"
end