class MatrizDensa
Attributes
mat[R]
Public Class Methods
new(nfil,ncol,mat)
click to toggle source
Calls superclass method
Matriz::new
# File lib/matriz.rb, line 21 def initialize(nfil,ncol,mat) super(nfil, ncol) @mat = Array.new(mat) #inicializo la matriz pasando como parametro un objeto de tipo matriz end
Public Instance Methods
*(other)
click to toggle source
metodo que multiplica dos matrices (Sobrecarga del operador *)
# File lib/matriz.rb, line 82 def *(other) m = Array.new(@nfil){Array.new(@ncol){0}} for i in 0...nfil do for j in 0...other.ncol do for k in 0...ncol do m[i][j] = m[i][j] + self.mat[i][k] * other.mat[k][j] end end end return MatrizDensa.new(self.nfil,other.ncol,m) end
+(other)
click to toggle source
metodo que suma dos matrices (Sobrecarga del operador +)
# File lib/matriz.rb, line 55 def +(other) raise ArgumentError, "Las matrices no son cuadradas." unless @nfil == other.nfil && @ncol == other.ncol m = Array.new(@nfil){Array.new(@ncol){0}} for i in 0...nfil for j in 0...ncol m[i][j] = self.mat[i][j]+ other.mat[i][j] end end return MatrizDensa.new(other.nfil,other.ncol,m) end
-(other)
click to toggle source
metodo que resta dos matrices (Sobrecarga del operador -)
# File lib/matriz.rb, line 68 def -(other) raise ArgumentError, "Las matrices no son cuadradas." unless @nfil == other.nfil && @ncol == other.ncol m = Array.new(@nfil){Array.new(@ncol){0}} for i in 0...nfil for j in 0...ncol m[i][j] = mat[i][j]- other.mat[i][j] end end return MatrizDensa.new(other.nfil,other.ncol,m) end
[](i)
click to toggle source
Funcion que devuelve una posicion i dentro de la matriz
# File lib/matriz.rb, line 28 def [](i) return mat[i] 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 35 def []=(i, j, k) return mat[i][j] = k end
max()
click to toggle source
# File lib/matriz.rb, line 94 def max max=0 for i in 0...nfil do for j in 0...ncol do if mat[i][j] > max max=mat[i][j] end end end return max end
min()
click to toggle source
# File lib/matriz.rb, line 106 def min min=9999; for i in 0...nfil do for j in 0...ncol do if mat[i][j] < min min=mat[i][j] end end end return min end
to_s()
click to toggle source
# File lib/matriz.rb, line 40 def to_s cad = " " for i in 0...nfil cad << " [ " for j in 0...ncol cad << "#{mat[i][j]} " end cad << "]" cad << "\n " end return cad end