Class: Matriz
- Inherits:
-
Object
- Object
- Matriz
- Defined in:
- lib/p10lppt13.rb
Overview
Clase base Matriz, que guarda el numero de filas y columnas
Instance Attribute Summary collapse
-
#ncol ⇒ Object
readonly
Variable que almacena el numero de columnas de la matriz.
-
#nfil ⇒ Object
readonly
Variable que almacena el numero de filas de la matriz.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Metodo para multiplicar una matriz por un escalar.
-
#+(other) ⇒ Object
Metodo para sumar dos matrices (si tienen la misma dimension).
-
#-(other) ⇒ Object
Metodo para restar dos matrices (si tienen la misma dimension).
-
#colum ⇒ Object
Getter de ncol (devuelve el numero de columnas de la matriz).
-
#filas ⇒ Object
Getter de nfil (devuelve el numero de filas de la matriz).
-
#initialize(nfil, ncol) ⇒ Matriz
constructor
Metodo initilize de la clase base.
-
#max ⇒ Object
Metodo para hallar el valor maximo de los elementos de una matriz.
-
#min ⇒ Object
Metodo para hallar el valor minimo de los elementos de una matriz.
-
#por(other) ⇒ Object
Metodo para multiplicacion dos matrices.
-
#porf(other) ⇒ Object
Metodo para multiplicacion dos matrices.
-
#primervalor ⇒ Object
Metodo para hallar el primer valor no nulo de una matriz.
-
#to_s ⇒ Object
Metodo para convertir la matriz a string (a una cadena).
-
#trasponer ⇒ Object
Metodo para hallar la traspuesta de una matriz.
-
#vectorizar ⇒ Object
Metodo para convertir la matriz en un vector.
Constructor Details
#initialize(nfil, ncol) ⇒ Matriz
Metodo initilize de la clase base
12 13 14 15 |
# File 'lib/p10lppt13.rb', line 12 def initialize(nfil, ncol) @nfil = nfil @ncol = ncol end |
Instance Attribute Details
#ncol ⇒ Object (readonly)
Variable que almacena el numero de columnas de la matriz
9 10 11 |
# File 'lib/p10lppt13.rb', line 9 def ncol @ncol end |
#nfil ⇒ Object (readonly)
Variable que almacena el numero de filas de la matriz
7 8 9 |
# File 'lib/p10lppt13.rb', line 7 def nfil @nfil end |
Instance Method Details
#*(other) ⇒ Object
Metodo para multiplicar una matriz por un escalar
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/p10lppt13.rb', line 184 def *(other) elemento = Array.new(0) @nfil.times do |i| fila = Array.new(0) @ncol.times do |j| if (pos[i][j] != nil) fila << pos[i][j]*other else fila << pos[i][j] end end elemento << fila end Densa.new(@nfil, @ncol, elemento) end |
#+(other) ⇒ Object
Metodo para sumar dos matrices (si tienen la misma dimension)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/p10lppt13.rb', line 40 def +(other) # Primero se comprueba si se pueden sumar (si tienen la misma dimension) if ((@nfil != other.nfil) || (@ncol != other.ncol)) puts "No se pueden sumar las matrices" else elemento = Array.new(0) @nfil.times do |i| fila = Array.new(0) @ncol.times do |j| if self.pos[i][j] == nil && other.pos[i][j] != nil fila << other.pos[i][j] elsif self.pos[i][j] != nil && other.pos[i][j] == nil fila << pos[i][j] elsif self.pos[i][j] == nil && other.pos[i][j] == nil fila << 0 else fila << pos[i][j] + other.pos[i][j] end end elemento << fila end Densa.new(@nfil, @ncol, elemento) end end |
#-(other) ⇒ Object
Metodo para restar dos matrices (si tienen la misma dimension)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/p10lppt13.rb', line 66 def -(other) # Primero se comprueba si se pueden restar (si tienen la misma dimension) if ((@nfil != other.nfil) || (@ncol != other.ncol)) puts "No se pueden restar las matrices" else elemento = Array.new(0) @nfil.times do |i| fila = Array.new(0) @ncol.times do |j| if self.pos[i][j] == nil && other.pos[i][j] != nil fila << -other.pos[i][j] elsif self.pos[i][j] != nil && other.pos[i][j] == nil fila << pos[i][j] elsif self.pos[i][j] == nil && other.pos[i][j] == nil fila << 0 else fila << pos[i][j] - other.pos[i][j] end end elemento << fila end Densa.new(@nfil, @ncol, elemento) end end |
#colum ⇒ Object
Getter de ncol (devuelve el numero de columnas de la matriz)
23 24 25 |
# File 'lib/p10lppt13.rb', line 23 def colum @ncol end |
#filas ⇒ Object
Getter de nfil (devuelve el numero de filas de la matriz)
18 19 20 |
# File 'lib/p10lppt13.rb', line 18 def filas @nfil end |
#max ⇒ Object
Metodo para hallar el valor maximo de los elementos de una matriz
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/p10lppt13.rb', line 103 def max acum = pos[0][0] for i in 0...filas for j in 0...colum if self.pos[i][j] != nil if pos[i][j] > acum acum = pos[i][j] end end end end acum end |
#min ⇒ Object
Metodo para hallar el valor minimo de los elementos de una matriz
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/p10lppt13.rb', line 118 def min acum = pos[0][0] for i in 0...filas for j in 0...colum if self.pos[i][j] != nil if pos[i][j] < acum acum = pos[i][j] end end end end acum end |
#por(other) ⇒ Object
Metodo para multiplicacion dos matrices
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/p10lppt13.rb', line 133 def por(other) # Primero se comprueba si se pueden multiplicar (las dimensiones deben ser nxm y mxn) if ((@nfil != other.ncol) || (@ncol != other.nfil)) puts "No se pueden multiplicarr las matrices" else elemento = Array.new(0) @nfil.times do |i| fila = Array.new(0) other.ncol.times do |j| aux = 0 @ncol.times do |k| if self.pos[i][k] != nil && other.pos[i][k] != nil aux += pos[i][k] * other.pos[k][j] end end fila << aux end elemento << fila end end Densa.new(@nfil, other.ncol, elemento) end |
#porf(other) ⇒ Object
Metodo para multiplicacion dos matrices
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 |
# File 'lib/p10lppt13.rb', line 157 def porf(other) # Primero se comprueba si se pueden multiplicar (las dimensiones deben ser nxm y mxn) if ((@nfil != other.ncol) || (@ncol != other.nfil)) puts "No se pueden multiplicarr las matrices" else elemento = Array.new(0) @nfil.times do |i| fila = Array.new(0) other.ncol.times do |j| aux = Fraccion.new(1,1) aux = aux - aux @ncol.times do |k| if (self.pos[i][k] != nil && other.pos[i][k] != nil) aux += pos[i][k] * other.pos[k][j] else aux << pos[i][j] end fila << aux end end elemento << fila end end Densa.new(@nfil, other.ncol, elemento) end |
#primervalor ⇒ Object
Metodo para hallar el primer valor no nulo de una matriz
92 93 94 95 96 97 98 99 100 |
# File 'lib/p10lppt13.rb', line 92 def primervalor for i in 0...@nfil for j in 0...@nncol if (self.pos[i][j] != nil) return self.pos[i][j] end end end end |
#to_s ⇒ Object
Metodo para convertir la matriz a string (a una cadena)
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/p10lppt13.rb', line 28 def to_s aux = "" @nfil.times do |i| @ncol.times do |j| aux << "#{pos[i][j]}\t" end aux << "\n" end aux end |
#trasponer ⇒ Object
Metodo para hallar la traspuesta de una matriz
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/p10lppt13.rb', line 201 def trasponer elemento = Array.new(0) @ncol.times do |i| fila = Array.new(0) @nfil.times do |j| fila << pos[j][i] end elemento << fila end Densa.new(@ncol, @nfil, elemento) end |
#vectorizar ⇒ Object
Metodo para convertir la matriz en un vector
215 216 217 218 219 220 221 222 223 |
# File 'lib/p10lppt13.rb', line 215 def vectorizar aux = Array.new(0) @ncol.times do |i| @nfil.times do |j| aux << pos[i][j] end end Array.new(aux) end |