Module: Operatoria

Included in:
Matriz
Defined in:
lib/matriz.rb

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

Dos matrices son multiplicables si el numero de columnas de A coincide con el numero de filas de B

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

#Producto_escalar(other) ⇒ Object



35
36
37
38
39
40
41
42
43
# 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