Class: Matriz_dispersa

Inherits:
Matriz
  • Object
show all
Defined in:
lib/matrices/matriz_dispersa.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Matriz

#*, #+, #-, #coerce, #max, #min, nula, #to_s, vector

Constructor Details

#initialize(rows, cols, ele) ⇒ Matriz_dispersa

Returns a new instance of Matriz_dispersa.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/matrices/matriz_dispersa.rb', line 6

def initialize(rows, cols, ele)
  
  super(rows, cols)
  
  @val, @f, @c = Array.new, Array.new, Array.new

  for i in 0...(@rows*@cols)
    if(ele[i]!=0)
      @val << (ele[i])
      @f << (i/@cols)
      @c << (i%@cols)
    end
  end
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



4
5
6
# File 'lib/matrices/matriz_dispersa.rb', line 4

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



4
5
6
# File 'lib/matrices/matriz_dispersa.rb', line 4

def rows
  @rows
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/matrices/matriz_dispersa.rb', line 54

def ==(other)
  if (@val.length != other.length || @rows != other.rows || @cols != other.cols)
    return false
  end
  for pos in 0...@val.length
    if (@val[pos] != other(@f[pos],@c[pos]))
      return false
    end
  end
  true
end

#[](x, y) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/matrices/matriz_dispersa.rb', line 21

def [](x,y)
  for pos in 0...@val.length
    if (@f[pos] == x && @c[pos] == y)
      return @val[pos]
    end
  end
  0
end

#[]=(x, y, value) ⇒ Object



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

def []=(x,y,value)
  for pos in 0...@val.length
    if (@f[pos] == x && @c[pos] == y)
      if (value != 0)
        @val[pos] = value
      else
        @val.delete_at(pos)
        @f.delete_at(pos)
        @c.delete_at(pos)
      end
      return
    end
  end
  if (value != 0)
    @val << value
    @f << x
    @c << y
  end
end

#lengthObject



50
51
52
# File 'lib/matrices/matriz_dispersa.rb', line 50

def length
  @val.length
end