Class: Matriz
Overview
Clase abstracta de la que heredan matriz densa y dispersa. No se puede crear un objeto de esta clase.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#col ⇒ Object
Numero de columnas.
-
#fil ⇒ Object
Numero de filas.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Sobrecarga del operador de suma, recibe como parametros dos matrices y devuelve una matriz con el resultado de la suma de forma A+B= (Aij+Bij).
-
#-(other) ⇒ Object
Sobrecarga del operador de resta, recibe como parametros dos matrices y devuelve una matriz con el resultado de la suma de forma A-B= (Aij+Bij).
-
#initialize(f, c) ⇒ Matriz
constructor
Se almacena el valor de las filas y las columnas segun se le pasen por parametros.
Constructor Details
#initialize(f, c) ⇒ Matriz
Se almacena el valor de las filas y las columnas segun se le pasen por parametros.
16 17 18 19 |
# File 'lib/matrices_p9.rb', line 16 def initialize(f, c) @fil=f.to_i; @col=c.to_i; end |
Instance Attribute Details
#col ⇒ Object
Numero de columnas
14 15 16 |
# File 'lib/matrices_p9.rb', line 14 def col @col end |
#fil ⇒ Object
Numero de filas
11 12 13 |
# File 'lib/matrices_p9.rb', line 11 def fil @fil end |
Instance Method Details
#+(other) ⇒ Object
Sobrecarga del operador de suma, recibe como parametros dos matrices y devuelve una matriz con el resultado de la suma de forma A+B= (Aij+Bij)
23 24 25 26 27 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 61 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/matrices_p9.rb', line 23 def +(other) if(self.fil == other.fil and self.col == other.col) # SELF Matrices densas if self.instance_of?MatrizDensa temp = MatrizDensa.new(self.fil, self.col, nil) if other.instance_of?MatrizDensa #for i in ([email protected]_i) @fil.to_i.times do |i| #for j in ([email protected]_i) @col.to_i.times do |j| temp.mat[i][j] = (@mat[i][j]) + (other.mat[i][j]) end end end if other.instance_of?MatrizDispersa #for i in ([email protected]_i) @fil.to_i.times do |i| #for j in ([email protected]_i) @col.to_i.times do |j| encontrado = 0 #for k in (0...other.posx.size) other.posx.size.times do |k| if (i==other.posx[k] and j==other.posy[k] and encontrado==0) temp.mat[i][j] = (self.mat[i][j]) + (other.valor[k]) encontrado = 1 end end if (encontrado == 0) temp.mat[i][j] = self.mat[i][j] end end end end end # SELF Matriz Dispersa if self.instance_of?MatrizDispersa if other.instance_of?MatrizDensa temp = MatrizDensa.new(self.fil, self.col, nil) #for i in ([email protected]_i) @fil.to_i.times do |i| for j in (0...@col.to_i) encontrado = 0 #for k in (0...self.posx.size.to_i) self.posx.size.times do |k| if (i==self.posx[k] and j==self.posy[k] and encontrado==0) temp.mat[i][j] = (other.mat[i][j]) + (self.valor[k]) encontrado = 1 end end if (encontrado == 0) temp.mat[i][j] = other.mat[i][j] end end end end if other.instance_of?MatrizDispersa temp = MatrizDispersa.new(self.fil,self.col,[],[],[]) temp.valor = self.valor temp.posx = self.posx temp.posy = self.posy #for j in (0...other.posx.size.to_i) other.posx.size.to_i.times do |j| encontrado = false #for k in (0...self.posx.size.to_i) self.posx.size.times do |k| if(other.posx[j] == temp.posx[k] and other.posy[j] == temp.posy[k]) temp.valor[k] = temp.valor[k] + other.valor[j] encontrado = true end end if (encontrado == false) temp.posx << other.posx[j] temp.posy << other.posy[j] temp.valor << other.valor[j] end end end end return temp else return nil end end |
#-(other) ⇒ Object
Sobrecarga del operador de resta, recibe como parametros dos matrices y devuelve una matriz con el resultado de la suma de forma A-B= (Aij+Bij)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 188 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 218 219 220 221 222 223 224 |
# File 'lib/matrices_p9.rb', line 124 def -(other) if(self.fil == other.fil and self.col == other.col) # SELF Matrices densas if self.instance_of?MatrizDensa temp = MatrizDensa.new(self.fil, self.col, nil) if other.instance_of?MatrizDensa #for i in ([email protected]_i) @fil.to_i.times do |i| #for j in ([email protected]_i) @col.to_i.times do |j| temp.mat[i][j] = (self.mat[i][j]) - (other.mat[i][j]) end end end if other.instance_of?MatrizDispersa #for i in ([email protected]_i) @fil.to_i.times do |i| #for j in ([email protected]_i) @col.to_i.times do |j| encontrado = 0 #for k in (0...other.posx.size) other.posx.size.times do |k| if (i==other.posx[k] and j==other.posy[k] and encontrado==0) temp.mat[i][j] = (self.mat[i][j]) - (other.valor[k]) encontrado = 1 end end if (encontrado == 0) temp.mat[i][j] = self.mat[i][j] end end end end end # SELF Matriz Dispersa if self.instance_of?MatrizDispersa if other.instance_of?MatrizDensa temp = MatrizDensa.new(self.fil, self.col, nil) #for i in ([email protected]_i) @fil.to_i.times do |i| #for j in ([email protected]_i) @col.to_i.times do |j| encontrado = 0 #for k in (0...self.posx.size.to_i) self.posx.size.times do |k| if (i==self.posx[k] and j==self.posy[k] and encontrado==0) temp.mat[i][j] = (other.mat[i][j]) - (self.valor[k]) encontrado = 1 end end if (encontrado == 0) temp.mat[i][j] = other.mat[i][j] end end end end if other.instance_of?MatrizDispersa temp = MatrizDispersa.new(self.fil,self.col,[],[],[]) temp.valor = self.valor temp.posx = self.posx temp.posy = self.posy #for j in (0...other.posx.size.to_i) other.posx.size.times do |j| encontrado = false #for k in (0...self.posx.size.to_i) self.posx.size.times do |k| if(other.posx[j] == temp.posx[k] and other.posy[j] == temp.posy[k]) temp.valor[k] = temp.valor[k] - other.valor[j] encontrado = true end end if (encontrado == false) temp.posx << other.posx[j] temp.posy << other.posy[j] temp.valor << other.valor[j] end end end end return temp else return nil end end |