Class: Dispersa

Inherits:
Matriz show all
Defined in:
lib/dispersa.rb

Instance Attribute Summary collapse

Attributes inherited from Matriz

#mCol, #nFil

Instance Method Summary collapse

Methods inherited from Matriz

#deter, #igual, #t, #x

Constructor Details

#initialize(n, m, mat) ⇒ Dispersa

Returns a new instance of Dispersa.



7
8
9
10
11
# File 'lib/dispersa.rb', line 7

def initialize (n,m,mat)
  @f = n
  @c = m
  @matriz = mat
end

Instance Attribute Details

#cObject

Returns the value of attribute c.



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

def c
  @c
end

#fObject

Returns the value of attribute f.



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

def f
  @f
end

#matrizObject

Returns the value of attribute matriz.



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

def matriz
  @matriz
end

Instance Method Details

#*(mat) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dispersa.rb', line 111

def *(mat)
  if (@c == mat.f)
    result = Array.new
    for i in 0...@f do
      result[i] = Array.new
      for j in 0...@c do
        result[i][j] = 0
      end
    end
    aux = Densa.new(result)
    nElementos = 0
    for i in 0...@f do
      for j in 0...mat.c do
        for z in 0...@c do
          if ((!@matriz[i].nil?) && (!@matriz[i][z].nil?) && (!mat.matriz[z].nil?) && (!mat.matriz[z][j].nil?))
            aux.matriz[i][j] += @matriz[i][z] * mat.matriz[z][j]
            nElementos += 1
          end
        end
      end
    end
    if ((@f * @c) * 0.4 > nElementos)
      aux = to_dispersa(aux)
    end
  else
    aux = "No se pueden multiplicar"
  end
  aux
end

#+(mat) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dispersa.rb', line 28

def +(mat)
    if (mat.f == @f && mat.c == @c)
    resultado = Array.new
    for i in 0...@f do
      resultado[i] = Array.new
      for j in 0...@c do
        resultado[i][j] = 0
      end
    end
    aux = Densa.new(resultado)
    nElementos = 0
    for i in 0...@f do
      for j in 0...@c do
 if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && (!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
          aux.matriz[i][j] = @matriz[i][j] + mat.matriz[i][j]
          nElementos += 1
 elsif ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && ((!mat.matriz[i].nil?) || (!mat.matriz[i].nil? && !mat.matriz[i][j].nil?)))
   aux.matriz[i][j] = @matriz[i][j]
          nElementos += 1
 elsif ((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && ((!@matriz[i].nil?) || (!@matriz[i].nil? && !@matriz[i][j].nil?)))
   aux.matriz[i][j] = mat.matriz[i][j]
          nElementos += 1
 end
      end
    end
    if ((@f * @c) * 0.4 > nElementos)
      aux = to_dispersa(aux)
    end
  else
    aux = "No se pueden sumar"
  end
  aux
end

#-(mat) ⇒ Object



62
63
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
92
93
94
# File 'lib/dispersa.rb', line 62

def -(mat)
  if (mat.f == @f && mat.c == @c)
    result = Array.new
    for i in 0...@f do
      result[i] = Array.new
      for j in 0...@c do
        result[i][j] = 0
      end
    end
    aux = Densa.new(result)
    nElementos = 0
    for i in 0...@f do
      for j in 0...@c do
        if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && (!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
          aux.matriz[i][j] = @matriz[i][j] - mat.matriz[i][j]
          nElementos += 1
        elsif ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && ((!mat.matriz[i].nil?) || (!mat.matriz[i].nil? && !mat.matriz[i][j].nil?)))
          aux.matriz[i][j] = @matriz[i][j]
          nElementos += 1
        elsif ((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && ((!@matriz[i].nil?) || (!@matriz[i].nil? && !@matriz[i][j].nil?)))
          aux.matriz[i][j] = - mat.matriz[i][j]
          nElementos += 1
        end
      end
    end
    if ((@f * @c) * 0.4 > nElementos)
      aux = to_dispersa(aux)
    end
  else
    aux = "No se pueden sumar"
  end
  aux
end

#mayorObject



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dispersa.rb', line 141

def mayor
  max = 0
  for i in 0...@f do
    for j in 0...@c do
	if((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
 if (max < @matriz[i][j])
   max = @matriz[i][j]
 end
	end
    end
  end
end

#menorObject



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dispersa.rb', line 155

def menor
  min = 0
  for i in 0...@f do
    for j in 0...@c do
	if((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
 if (min > @matriz[i][j])
   min = @matriz[i][j]
 end
	end
    end
  end
end

#to_dispersa(mat) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dispersa.rb', line 96

def to_dispersa(mat)
  resultado = {}
  for i in 0...mat.f do
    for j in 0...mat.c do
      if (mat.matriz[i][j] != 0)
        if (resultado[i].nil?)
          resultado[i] = {}
        end
        resultado[i][j] = mat.matriz[i][j]
      end
    end 
  end
  aux = Dispersa.new(mat.f, mat.c, resultado)
end

#to_sObject

Funcion para mostrar una matriz



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dispersa.rb', line 13

def to_s
  aux = ""
  for i in 0...@f
    for j in 0...@c
	if((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
 aux = aux + @matriz[i][j].to_s + "\t"
	else
 aux = aux + "0\t"
	end
    end
    aux = aux + "\n"
  end
  aux
end