Class: Kwyjibo::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/kwyjibo.rb

Direct Known Subclasses

DenseMatrix, SparseMatrix

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, cols) ⇒ Matrix

Returns a new instance of Matrix.



7
8
9
# File 'lib/kwyjibo.rb', line 7

def initialize(rows,cols)
    @rows, @cols = rows, cols
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



5
6
7
# File 'lib/kwyjibo.rb', line 5

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



5
6
7
# File 'lib/kwyjibo.rb', line 5

def rows
  @rows
end

Instance Method Details

#*(other) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kwyjibo.rb', line 49

def *(other)
    raise ArgumentError, "Columns and Rows must be equal" unless (@cols == other.rows)
    c = DenseMatrix.new(@rows,other.cols)
    @rows.times do |i|
        other.cols.times do |j|
            ac = 0
            @cols.times do |k|
                ac += self[i][k] * other[k][j] if (self[i][k] != nil && other[k][j] != nil)
            end
            c[i][j] = ac
        end
    end
    c
end

#+(other) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kwyjibo.rb', line 11

def +(other)
    raise ArgumentError, "Matrix size must be equal" unless @rows == other.rows && @cols == other.cols
    c = DenseMatrix.new(@rows, @cols)
    @rows.times do |i|
        @cols.times do |j|
            if self[i][j] == nil && other[i][j] == nil
                c[i][j] = 0
            elsif self[i][j] == nil && other[i][j] != nil
                c[i][j] = other[i][j]
            elsif self[i][j] != nil && other[i][j] == nil
                c[i][j] = self[i][j]
            else
                c[i][j] = self[i][j] + other[i][j]
            end
        end
    end
    c
end

#-(other) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kwyjibo.rb', line 30

def -(other)
    raise ArgumentError, "Matrix size must be equal" unless @rows == other.rows && @cols == other.cols
    c = DenseMatrix.new(@rows, @cols)
    @rows.times do |i|
        @cols.times do |j|
            if self[i][j] == nil && other[i][j] == nil
                c[i][j] = 0
            elsif self[i][j] == nil && other[i][j] != nil
                c[i][j] = -other[i][j]
            elsif self[i][j] != nil && other[i][j] == nil
                c[i][j] = self[i][j]
            else
                c[i][j] = self[i][j] - other[i][j]
            end
        end
    end
    c
end

#maxObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kwyjibo.rb', line 64

def max
    encontrado = false
    value = 0
    i = -1

    while encontrado == false
        i += 1
        j = 0
        while j < self.cols
            if self[i][j] != nil and value == 0
                value = self[i][j]
                encontrado = true
                break
            else
                j += 1
            end
        end
    end

    @rows.times do |i|
        @cols.times do |j|
            if self[i][j] != nil && self[i][j] > value
                value = self[i][j]
            end
        end
    end
    value
end

#minObject



93
94
95
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
# File 'lib/kwyjibo.rb', line 93

def min
    encontrado = false
    value = 0
    i = -1

    while encontrado == false
        i += 1
        j = 0
        while j < self.cols
            if self[i][j] != nil and value == 0
                value = self[i][j]
                encontrado = true
                break
            else
                j += 1
            end
        end
    end

    @rows.times do |i|
        @cols.times do |j|
            if self[i][j] != nil && self[i][j] < value
                value = self[i][j]
            end
        end
    end
    value
end