Class: MathExpansion::Matriz_Dispersa

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

Instance Attribute Summary

Attributes inherited from Matriz

#M, #N, #contenido

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, m) ⇒ Matriz_Dispersa

Returns a new instance of Matriz_Dispersa.



15
16
17
18
# File 'lib/math_expansion/matriz_dispersa.rb', line 15

def initialize(n, m)
  super
  reset
end

Class Method Details

.copy(matriz) ⇒ Object

Metodo factoria

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/math_expansion/matriz_dispersa.rb', line 21

def self.copy(matriz)
  raise ArgumentError, 'Tipo invalido' unless matriz.is_a? MathExpansion::Matriz_Densa
  obj = new(matriz.N, matriz.M)

  i = 0
  while(i < matriz.N)
    j = 0
    while(j < matriz.M)
      value = matriz.get(i,j)
      raise RuntimeError , "No se ha definido \"null\" para la clase #{value.class}" unless value.class.respond_to? :null
      
      if( value != value.class.null)
        obj.contenido[i][j] = value
      end #endif

      j += 1
    end #endwhile j

    i += 1
  end #endwhile i

  obj
end

Instance Method Details

#*(other) ⇒ Object

Raises:

  • (ArgumentError)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/math_expansion/matriz_dispersa.rb', line 147

def *(other)
    raise ArgumentError , 'Parametro invalido' unless other.is_a? Numeric or other.is_a? Matriz

    if(other.is_a? Numeric) # Matriz * numero

        c = Matriz_Densa.new(@N, @M)
  i = 0
        while(i < @N)
          j = 0
          while(j < @M)
            c.set(i, j, get(i,j)*other)
            j += 1
      end # while j

      i += 1
        end # while i

    else # Matriz * Matriz

        raise ArgumentError , 'Matriz no compatible (A.N == B.M)' unless @M == other.N
        c = Matriz_Densa.new(@N, other.M)
        i = 0
        while(i < @N)
          j = 0
          while(j < other.M)
                k = 0
  #if (get(i,j).is_a? Fraccion)               

  #  c.set(i, j, Fraccion.null

  #else

  #  c.set(i, j, 0)

  #end

                    while(k < @M)
            c.set(i, j, get(i, k) * other.get(k,j) + c.get(i,j))
            k += 1
          end # while k

              j += 1
          end # while j

      i += 1
        end # while i

    end # while else

    if(c.null_percent > 0.6)
      c = Matriz_Dispersa.copy(c)
    end  
    c
end

#+(other) ⇒ Object

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/math_expansion/matriz_dispersa.rb', line 107

def +(other)
      raise ArgumentError , 'Tipo invalido' unless other.is_a? Matriz
      raise ArgumentError , 'Matriz no compatible' unless @N == other.N and @M == other.M
  
      c = Matriz_Densa.new(@N, @M)
      i = 0
    while(i < @N)
        j = 0
        while(j < @M)
        c.set(i, j, get(i,j) + other.get(i,j))
         j += 1
      end 
      i += 1
      end
    if(c.null_percent > 0.6)
      c = Matriz_Dispersa.copy(c)
    end
      c
end

#-(other) ⇒ Object

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/math_expansion/matriz_dispersa.rb', line 127

def -(other)
      raise ArgumentError , 'Tipo invalido' unless other.is_a? Matriz
      raise ArgumentError , 'Matriz no compatible' unless @N == other.N and @M == other.M
  
      c = Matriz_Densa.new(@N, @M)
      i = 0
      while(i < @N)
        j = 0
        while(j < @M)
        c.set(i, j, get(i,j) - other.get(i,j))
         j += 1
      end
      i += 1
      end
    if(c.null_percent > 0.6)
      c = Matriz_Dispersa.copy(c)
    end   
      c
end

#get(i, j) ⇒ Object

endmethod null_percent



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/math_expansion/matriz_dispersa.rb', line 56

def get(i, j)
  if( !(i.is_a? Fixnum) or i < 0 or i >=@N or !(j.is_a? Fixnum) or j < 0 or j >= @M)
    return nil
  end
    
  if(@contenido[i][j] != nil) # Elemento no nulo (esta en el hash)

    return @contenido[i][j]
  else # Elemento nulo (no esta en el Hash)

    return 0
  end
end

#maxObject

*(other)



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/math_expansion/matriz_dispersa.rb', line 189

def max
  if(null_percent == 1.0)
    return nil # o return 0

  end
  
  # Valor máximo: si todos los elementos son menores que el elemento nulo

  # Se devolverá el mayor elemento no nulo.

  max = nil
  
  # Asignar al primer valor no-nulo de la matriz

  i = 0
  while(max == nil)
    if(@contenido[i].size != 0)
    max = @contenido[i].values[0]
  end
  i += 1
  end
  
  # Iterar por todos los elementos no nulos para encontrar el maximo

  i = 0
  while(i < @contenido.size)
    if(@contenido[i].values.max != nil and @contenido[i].values.max > max)
    max = @contenido[i].values.max
  end
    i += 1
  end
  
  max
end

#minObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/math_expansion/matriz_dispersa.rb', line 219

def min
    if(null_percent == 1.0)
 return nil # o return 0

    end
    
    # Valor máximo: si todos los elementos son menores que el elemento nulo

    # Se devolverá el mayor elemento no nulo.

    min = nil
    
    # Asignar al primer valor no-nulo de la matriz

    i = 0
    while(min == nil)
 if(@contenido[i].size != 0)
min = @contenido[i].values[0]
    end
    i += 1
    end
    
    # Iterar por todos los elementos no nulos para encontrar el maximo

    i = 0
    while(i < @contenido.size)
 if(@contenido[i].values.min != nil and @contenido[i].values.min < min)
min = @contenido[i].values.min
    end
 i += 1
    end
    
    min
end

#null_percentObject

endmethod copy



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/math_expansion/matriz_dispersa.rb', line 42

def null_percent
  total = @N*@M
  no_nulos = 0
  
  i = 0
  while(i < @N)
    no_nulos += @contenido[i].size # Nunca habra elementos nulos en alguna fila

    i += 1
  end
  
  nulos = total - no_nulos
  nulos.to_f/total.to_f
end

#resetObject



6
7
8
9
10
11
12
13
# File 'lib/math_expansion/matriz_dispersa.rb', line 6

def reset
  @contenido = Array.new(@N) # Array con @N filas y ninguna columna (vacio)

  i = 0
  while(i < @N)
    @contenido[i] = {}
    i += 1
  end
end

#set(i, j, value) ⇒ Object

endmethod get



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/math_expansion/matriz_dispersa.rb', line 68

def set(i, j, value)
  if(!(value.class.respond_to? :null))
    puts "Se debe definir el metodo \"null\" que devuelva un elemento nulo para la clase #{value.class}"
    return nil
  end
  
  if( !(i.is_a? Fixnum) or i < 0 or i >=@N or !(j.is_a? Fixnum) or j < 0 or j >= @M)
    return nil
  end
  
  if(value == nil or value == value.class.null)
    @contenido[i].delete(j) # Borrar elemento (valor nulo)

  else
    @contenido[i][j] = value
  end
  
  if(null_percent < 0.6) # Si se ha sobrepasado el número de elementos nulos, borramos el último elemento modificado

      @contenido[i].delete(j)
      puts "Borrado el elemento #{i},#{j} por sobrepasar el numero de elementos no nulos (Porcentaje actual: #{null_percent}"
  end
  
end

#to_sObject

endmethod set



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/math_expansion/matriz_dispersa.rb', line 91

def to_s
  # Ejemplo: "Fila 0: \nFila 1: 0=>1 1=>3 \nFila 2: \n"

  # 0 0

  # 1 3

  # 0 0

  i = 0
  output = ""
  while(i < @N)
    output += "Fila #{i}: "
    @contenido[i].sort.each{|k, v| output += "#{k.to_s}=>#{v.to_s} "}
    output += "\n"
    i += 1
  end
  output
end