class SparseMatrix::DenseMatrix

Attributes

c[RW]
mat[RW]
r[RW]

Public Class Methods

new(r=0,c=0,matrix=[]) click to toggle source
Calls superclass method SparseMatrix::AbstractMatrix.new
# File lib/sparse_matrix.rb, line 195
def initialize(r=0,c=0,matrix=[])
    super(r,c)
    @mat = matrix
end

Public Instance Methods

*(b) click to toggle source
# File lib/sparse_matrix.rb, line 253
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) click to toggle source
# File lib/sparse_matrix.rb, line 234
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
print_matrix() click to toggle source
to_s() click to toggle source
# File lib/sparse_matrix.rb, line 202
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