Class: SparseMatrix::DenseMatrix

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

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.



49
50
51
52
# File 'lib/sparse_matrix.rb', line 49

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

Instance Attribute Details

#matObject

Returns the value of attribute mat.



54
55
56
# File 'lib/sparse_matrix.rb', line 54

def mat
  @mat
end

Instance Method Details

#*(b) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sparse_matrix.rb', line 99

def *(b)
    c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
    c.read_matrix
    for i in(0...@mat.length)
        for j in(0...@mat.length)
            c.mat[i][j]=0
            for k in (0...@mat.length)
                c.mat[i][j] += @mat[i][k]*b.mat[k][j]
            end
        end
    end
    c
end

#+(b) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/sparse_matrix.rb', line 88

def +(b)
    c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
    c.read_matrix
    for i in (0...@mat.length)
        for j in(0...@mat.length)
            c.mat[i][j] = self.mat[i][j]+b.mat[i][j]
        end
    end
    c
end


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sparse_matrix.rb', line 72

def print_matrix()
    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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sparse_matrix.rb', line 56

def to_s()
    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