Class: MatrixExpansion::Matriz_Densa
- Defined in:
- lib/matrix_expansion/matrix_densa.rb
Instance Attribute Summary
Attributes inherited from Matriz
Instance Method Summary collapse
-
#*(other) ⇒ Object
Producto de matrices densas.
-
#+(other) ⇒ Object
Suma de matrices densas.
-
#-(other) ⇒ Object
Resta de matrices densas.
-
#get(i, j) ⇒ Object
Comprueba que se accede dentro de la matriz y devuelve el valor.
-
#initialize(n, m) ⇒ Matriz_Densa
constructor
Crea una matriz anidando array e inicializa los valores a 0.
-
#max ⇒ Object
Calcula el maximo elemento de la matriz.
-
#min ⇒ Object
Calcula el minimo elemento de la matriz.
-
#porcentaje_nulos ⇒ Object
Calcula el porcentaje de valores nulos.
-
#set_valores_fracc ⇒ Object
Da valores fraccionales a la matriz densa.
-
#set_valores_num ⇒ Object
Da valores numericos a la matriz densa.
-
#to_s ⇒ Object
Muestra la matriz densa.
Constructor Details
#initialize(n, m) ⇒ Matriz_Densa
Crea una matriz anidando array e inicializa los valores a 0
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 23 def initialize(n, m) super @matrix = Array.new(@fil,0) i = 0 while i < @fil @matrix[i] = Array.new(@col,0) i += 1 end end |
Instance Method Details
#*(other) ⇒ Object
Producto de matrices densas
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/matrix_expansion/matrix_densa.rb', line 150 def *(other) raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Numeric or other.is_a? Matriz #Si el argumento es un numero if(other.is_a? Numeric) c = Matriz_Densa.new(@fil, @col) i = 0 while(i < @N) j = 0 while(j < @M) c.matrix = @matrix[i][j] * other j += 1 end i += 1 end #Si el arguumento es una matriz else raise ArgumentError , 'Matriz no compatible (A.fil == B.col)' unless @col == other.fil c = Matriz_Densa.new(@fil, other.col) i = 0 while(i < @fil) j = 0 while(j < other.col) k = 0 c.matrix[i][j] = 0 while(k < @col) c.matrix[i][j] += @matrix[i][k] * other.matrix[k][j] k += 1 end j += 1 end i += 1 end end c end |
#+(other) ⇒ Object
Suma de matrices densas
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 113 def +(other) raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Matriz raise ArgumentError , 'Las matrices deben ser del mismo tamano' unless @fil == other.fil and @col == other.col c = Matriz_Densa.new(@fil, @col) i = 0 while(i < @fil) j = 0 while(j < @col) c.matrix[i][j] = @matrix[i][j] + other.matrix[i][j] j += 1 end i += 1 end c end |
#-(other) ⇒ Object
Resta de matrices densas
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 131 def -(other) raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Matriz raise ArgumentError , 'Las matrices deben ser del mismo tamano' unless @fil == other.fil and @col == other.col c = Matriz_Densa.new(@fil, @col) i = 0 while(i < @fil) j = 0 while(j < @col) c.matrix[i][j] = @matrix[i][j] - other.matrix[i][j] j += 1 end i += 1 end c end |
#get(i, j) ⇒ Object
Comprueba que se accede dentro de la matriz y devuelve el valor
67 68 69 70 71 72 73 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 67 def get(i,j) if( !(i.is_a? Fixnum) or i < 0 or i >=@fil or !(j.is_a? Fixnum) or j < 0 or j >= @col) return nil end return @matrix[i][j] end |
#max ⇒ Object
Calcula el maximo elemento de la matriz
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 211 def max # Establecemos valor del primer elemento max = @matrix[0][0] i = 0 #Fila a fila actualizando el valor maximo while (i < @fil) j = 0 while (j < @col) if (@matrix[i][j] > max) max = @matrix[i][j] end j += 1 end i += 1 end max end |
#min ⇒ Object
Calcula el minimo elemento de la matriz
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 191 def min # Establecemos valor del primer elemento min = @matrix[0][0] i = 0 # Fila a fila actualizando el valor minimo while (i < @fil) j = 0 while (j < @col) if (@matrix[i][j] < min) min = @matrix[i][j] end j += 1 end i += 1 end min end |
#porcentaje_nulos ⇒ Object
Calcula el porcentaje de valores nulos
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 76 def porcentaje_nulos total = @fil*@col no_nulos = 0 i = 0 while(i < @fil) j = 0 while(j < @col) if(@matrix[i][j] != 0) no_nulos += 1 end j += 1 end i += 1 end nulos = total - no_nulos nulos.to_f/total.to_f end |
#set_valores_fracc ⇒ Object
Da valores fraccionales a la matriz densa
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 50 def set_valores_fracc a = 1 b = 2 i = 0 while(i < @col) j = 0 while(j < @fil) @matrix[i][j] = Fraccion.new(a,b) a += 1 b += 1 j += 1 end i += 1 end end |
#set_valores_num ⇒ Object
Da valores numericos a la matriz densa
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 35 def set_valores_num valor = 1 i = 0 while(i < @col) j = 0 while(j < @fil) @matrix[i][j] = valor valor += 1 j += 1 end i += 1 end end |
#to_s ⇒ Object
Muestra la matriz densa
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/matrix_expansion/matrix_densa.rb', line 97 def to_s s = "" i = 0 while(i < @col) j = 0 while(j < @fil) s += "#{@matrix[i][j].to_s}\t" j += 1 end s += "\n" i += 1 end s end |