class Kwyjibo::Matrix

Attributes

cols[R]
rows[R]

Public Class Methods

new(rows,cols) click to toggle source
# File lib/kwyjibo.rb, line 7
def initialize(rows,cols)
    @rows, @cols = rows, cols
end

Public Instance Methods

*(other) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
max() click to toggle source
# 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
min() click to toggle source
# 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