Module: Operatoria
- Included in:
- Matriz
- Defined in:
- lib/matriz.rb
Instance Method Summary collapse
-
#*(other) ⇒ Object
Dos matrices son multiplicables si el numero de columnas de A coincide con el numero de filas de B.
-
#+(other) ⇒ Object
Sobrecargado el + para poder sumar 2 matrices.
-
#-(other) ⇒ Object
Sobrecargado el - para poder restar 2 matrices.
-
#-@ ⇒ Object
Realiza el opuesto de una matriz.
-
#==(other) ⇒ Object
Para comprobar que dos matrices son equivalentes,primero se comprueba sus dimensiones.
-
#[]=(i, j, k) ⇒ Object
Funcion que asigna un valor k a una posicion i,j dentro de la matriz.
- #Producto_escalar(other) ⇒ Object
Instance Method Details
#*(other) ⇒ Object
Dos matrices son multiplicables si el numero de columnas de A coincide con el numero de filas de B
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/matriz.rb', line 77 def * (other) raise ArgumentError, "La longitud de las matrices no coincide." unless @columnas == other.filas elemento = Array.new acumulado = 0 self.filas.times do |i| elemento_fila = Array.new other.columnas.times do |j| acumulado = 0 self.columnas.times do |k| suma = @matriz[i][k] * other.matriz[k][j] acumulado = suma + acumulado end elemento_fila << acumulado end elemento << elemento_fila end Matriz.new(elemento) end |
#+(other) ⇒ Object
Sobrecargado el + para poder sumar 2 matrices
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/matriz.rb', line 11 def +(other) raise ArgumentError, "La longitud de las matrices no coincide." unless @filas == other.filas && @columnas == other.columnas sum = Matriz.new(matriz) #inicializas el vector sum con el primer con el primer self.filas.times do |i| self.columnas.times do |j| sum.matriz[i][j] = self.matriz[i][j] + other.matriz[i][j] end end return sum #devuelve un tipo array modificando el objeto m1 si se hace m3=m1+m2 -> Se necesita q sea tipo Matriz end |
#-(other) ⇒ Object
Sobrecargado el - para poder restar 2 matrices
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/matriz.rb', line 23 def -(other) raise ArgumentError, "La longitud de las matrices no coincide." unless @filas == other.filas && @columnas == other.columnas resta = Matriz.new(matriz) self.filas.times do |i| self.columnas.times do |j| resta.matriz[i][j] = self.matriz[i][j] - other.matriz[i][j] end end return resta #devuelve un tipo matriz modificando el objeto m1 si se hace m3=m1+m2 end |
#-@ ⇒ Object
Realiza el opuesto de una matriz
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/matriz.rb', line 65 def -@ op = Matriz.new(matriz) self.filas.times do |i| self.columnas.times do |j| op.matriz[i][j] = -self.matriz[i][j] end end return op end |
#==(other) ⇒ Object
Para comprobar que dos matrices son equivalentes,primero se comprueba sus dimensiones. Si tienen las mismas dimensiones se comprueba que el valor de ambas matrices sean iguales en las mismas posiciones,si esto es asà se devuelve true,false en otro caso.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/matriz.rb', line 46 def ==(other) dev=true if ((self.filas.size==other.filas.size) && (self.columnas.size==other.columnas.size)) self.filas.times do |i| self.columnas.times do |j| if (self.matriz[i][j] != other.matriz[i][j]) dev=false else end end end else dev=false end return dev end |
#[]=(i, j, k) ⇒ Object
Funcion que asigna un valor k a una posicion i,j dentro de la matriz
6 7 8 |
# File 'lib/matriz.rb', line 6 def []=(i, j, k) matriz[i][j] = k end |