module Operatoria
Public Instance Methods
*(other)
click to toggle source
Dos matrices son multiplicables si el numero de columnas de A coincide con el numero de filas de B
# 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)
click to toggle source
Sobrecargado el + para poder sumar 2 matrices
# 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)
click to toggle source
Sobrecargado el - para poder restar 2 matrices
# 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
-@()
click to toggle source
Realiza el opuesto de una matriz
# 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)
click to toggle source
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.
# 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
Producto_escalar(other)
click to toggle source
# File lib/matriz.rb, line 35 def Producto_escalar (other) mul = Matriz.new(matriz) self.filas.times do |i| self.columnas.times do |j| mul.matriz[i][j] = self.matriz[i][j] * other end end return mul end
[]=(i, j, k)
click to toggle source
Funcion que asigna un valor k a una posicion i,j dentro de la matriz
# File lib/matriz.rb, line 6 def []=(i, j, k) matriz[i][j] = k end