Class: Matriz_densa
Constant Summary
Constants included from Matriz
Instance Attribute Summary collapse
-
#col ⇒ Object
readonly
Returns the value of attribute col.
-
#fil ⇒ Object
readonly
Returns the value of attribute fil.
-
#mat ⇒ Object
readonly
Returns the value of attribute mat.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Multiplicacion: realiza el producto escalar o por otra matriz segun la clase de other.
-
#+(other) ⇒ Object
Devuelve la suma de dos matrices.
-
#-(other) ⇒ Object
Devuelve la resta de dos matrices.
-
#==(other) ⇒ Object
Compara si dos matrices son iguales o no.
-
#coerce(other) ⇒ Object
Metodo para hacer la conversion de datos para poder operar con la clase matriz densa.
-
#generar(o) ⇒ Object
Segun la opcion elegida (1 o 2) crear la matriz densa con valores aleatorios (1)enteros o (2)racionales.
-
#initialize(m, n) ⇒ Matriz_densa
constructor
Estructura de datos de la matriz densa.
-
#introducir_datos(o) ⇒ Object
El usuario puede elegir los tipos de datos (1.enteros y 2.racionales) y datos que contendra la matriz.
-
#llenar(other) ⇒ Object
Recoge por parametro el array que formara la nueva matriz.
-
#max ⇒ Object
Devuelve el elemento mayor de la matriz.
- #min ⇒ Object
-
#t ⇒ Object
Calcula la traspuesta de una matriz.
-
#to_s ⇒ Object
Metodo para mostrar una matriz densa.
Constructor Details
#initialize(m, n) ⇒ Matriz_densa
Estructura de datos de la matriz densa
8 9 10 11 12 13 |
# File 'lib/matriz_densa.rb', line 8 def initialize (m,n) #Estructura de datos de la matriz densa raise TypeError, "Error. Tipo de dimensiones incorrectas" if ((m < 0) or (n < 0)) super # @fil, @col = m, n @mat = Array.new(@fil) {Array.new(@col)} end |
Instance Attribute Details
#col ⇒ Object (readonly)
Returns the value of attribute col.
6 7 8 |
# File 'lib/matriz_densa.rb', line 6 def col @col end |
#fil ⇒ Object (readonly)
Returns the value of attribute fil.
6 7 8 |
# File 'lib/matriz_densa.rb', line 6 def fil @fil end |
#mat ⇒ Object (readonly)
Returns the value of attribute mat.
6 7 8 |
# File 'lib/matriz_densa.rb', line 6 def mat @mat end |
Instance Method Details
#*(other) ⇒ Object
Multiplicacion: realiza el producto escalar o por otra matriz segun la clase de other
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/matriz_densa.rb', line 157 def *(other) #Multiplicacion: realiza el producto escalar o por otra matriz segun la clase de other if other.is_a?(Numeric) nueva = Matriz_densa.new(self.fil,self.col) if (self.mat[0][0]).is_a?(NumerosRacionales) n=NumerosRacionales.new(other,1) else n=other end @fil.times do |i| # 0.upto(@fil-1) do |i| # for i in (0...self.fil) @col.times do |j| # 0.upto(@col-1) do |j| # for j in (0...self.col) nueva.mat[i][j] = n*self.mat[i][j] end end nueva elsif other.is_a?(Matriz) if (self.col == other.fil) if (other.is_a?(Matriz_dispersa)) other=self.coerce(other) other=other[0] end nueva = Matriz_densa.new(self.fil,other.col) @fil.times do |i| # for i in (0...self.fil) @col.times do |j| # for j in (0...other.col) if (self.mat[0][0]).is_a?(NumerosRacionales) nueva.mat[i][j] = NumerosRacionales.new(0,1) else nueva.mat[i][j] = 0 end @col.times do |k| # for k in (0...self.col) nueva.mat[i][j] += self.mat[i][k]*other.mat[k][j] end end end nueva else puts "Error. No se pueden multiplicar estas matrices. La col de la M1 debe coincidir con la fil de M2" end end end |
#+(other) ⇒ Object
Devuelve la suma de dos matrices
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/matriz_densa.rb', line 118 def +(other) #Devuelve la suma de dos matrices raise unless other.is_a?(Matriz) if (other.is_a?(Matriz_dispersa)) other=self.coerce(other) other=other[0] end if (self.fil == other.fil) and (self.col == other.col) nueva = Matriz_densa.new(self.fil,self.col) @fil.times do |i| # 0.upto(@fil-1) do |i| # for i in (0...self.fil) @col.times do |j| # 0.upto(@col-1) do |j| # for j in (0...self.col) nueva.mat[i][j]= self.mat[i][j] + other.mat[i][j] end end nueva else puts "Error. No se pueden sumar matrices de distintas dimensiones." end end |
#-(other) ⇒ Object
Devuelve la resta de dos matrices
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/matriz_densa.rb', line 137 def -(other) #Devuelve la resta de dos matrices raise unless other.is_a?(Matriz) if (other.is_a?(Matriz_dispersa)) other=self.coerce(other) other=other[0] end if (self.fil == other.fil) and (self.col == other.col) nueva = Matriz_densa.new(self.fil,self.col) @fil.times do |i| # 0.upto(@fil-1) do |i| # for i in (0...self.fil) @col.times do |j| # 0.upto(@col-1) do |j| # for j in (0...self.col) nueva.mat[i][j] = self.mat[i][j] - other.mat[i][j] end end nueva else puts "Error. No se pueden restar matrices de distintas dimensiones." end end |
#==(other) ⇒ Object
Compara si dos matrices son iguales o no
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/matriz_densa.rb', line 100 def ==(other) #Compara si dos matrices son iguales o no raise unless other.is_a?(Matriz) if (other.is_a?(Matriz_dispersa)) other=self.coerce(other) other=other[0] end if (self.fil == other.fil) and (self.col == other.col) for i in (0...other.fil) for j in (0...other.col) if self.mat[i][j] != other.mat[i][j] return false end end end true end #if end |
#coerce(other) ⇒ Object
Metodo para hacer la conversion de datos para poder operar con la clase matriz densa
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/matriz_densa.rb', line 75 def coerce (other) #Metodo para hacer la conversion de datos para poder operar con la clase matriz densa if (other.is_a?(Matriz_dispersa)) a=Matriz_densa.new(other.fil,other.col) for i in (0...a.fil) for j in (0...a.col) a.mat[i][j]=0 end end for i in (0...other.pos.size) a.mat[other.pos[i][0]][other.pos[i][1]]= other.dato[i] end return [a,self] end end |
#generar(o) ⇒ Object
Segun la opcion elegida (1 o 2) crear la matriz densa con valores aleatorios (1)enteros o (2)racionales
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/matriz_densa.rb', line 26 def generar (o) #Segun la opcion elegida (1 o 2) crear la matriz densa con valores aleatorios (1)enteros o (2)racionales if (o==1) @mat = Array.new(@fil) {Array.new(@col) {rand(-10..10)}} elsif (o==2) for i in (0...self.fil) for j in (0...self.col) self.mat[i][j]=NumerosRacionales.new(rand(-10..10),rand(1..10)) end end end end |
#introducir_datos(o) ⇒ Object
El usuario puede elegir los tipos de datos (1.enteros y 2.racionales) y datos que contendra la matriz
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/matriz_densa.rb', line 48 def introducir_datos (o) #El usuario puede elegir los tipos de datos (1.enteros y 2.racionales) y datos que contendra la matriz puts "Rellene la matriz..." if (o==1) for i in (0...self.fil) for j in (0...self.col) puts "casilla (#{i},#{j}): " STDOUT.flush dato=gets.chomp self.mat[i][j]= dato.to_i end end elsif for i in (0...self.fil) for j in (0...self.col) puts "casilla (#{i},#{j}): " puts "numerador: " STDOUT.flush num=gets.chomp puts "denominador: " STDOUT.flush den=gets.chomp self.mat[i][j]=NumerosRacionales.new(num.to_i,den.to_i) end end end end |
#llenar(other) ⇒ Object
Recoge por parametro el array que formara la nueva matriz
38 39 40 41 42 43 44 45 46 |
# File 'lib/matriz_densa.rb', line 38 def llenar (other) #Recoge por parametro el array que formara la nueva matriz if other.is_a?(Array) for i in (0...self.fil) for j in (0...self.col) self.mat[i][j] = other[i][j] end end end end |
#max ⇒ Object
Devuelve el elemento mayor de la matriz
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/matriz_densa.rb', line 197 def max #Devuelve el elemento mayor de la matriz r=-9999999 @mat.each do |i| i.each do |j| if (j.to_f > r.to_f) r = j end end end r end |
#min ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/matriz_densa.rb', line 209 def min r= 9999999 #Devuelve el elemento menor de la matriz @mat.each do |i| i.each do |j| if (j.to_f < r.to_f) r = j end end end r end |
#t ⇒ Object
Calcula la traspuesta de una matriz
90 91 92 93 94 95 96 97 98 |
# File 'lib/matriz_densa.rb', line 90 def t #Calcula la traspuesta de una matriz nueva = Matriz_densa.new(self.col,self.fil) for i in (0...nueva.fil) for j in (0...nueva.col) nueva.mat[i][j] = self.mat[j][i] end end nueva end |
#to_s ⇒ Object
Metodo para mostrar una matriz densa
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/matriz_densa.rb', line 15 def to_s #Metodo para mostrar una matriz densa for i in (0...self.fil) print "(" for j in (0...self.col) print " #{self.mat[i][j]}\t" end puts ")\n" end puts "\n" end |