Class: Densa

Inherits:
Matriz show all
Defined in:
lib/densa.rb

Instance Attribute Summary collapse

Attributes inherited from Matriz

#mCol, #nFil

Instance Method Summary collapse

Constructor Details

#initialize(mat) ⇒ Densa

constructor



7
8
9
10
11
# File 'lib/densa.rb', line 7

def initialize(mat) #constructor
  @matriz = mat #array de arrays con la matriz
  @f = mat.size #filas de la matriz
  @c = mat[0].size #columnas de la matriz
end

Instance Attribute Details

#cObject

Returns the value of attribute c.



5
6
7
# File 'lib/densa.rb', line 5

def c
  @c
end

#fObject

Returns the value of attribute f.



5
6
7
# File 'lib/densa.rb', line 5

def f
  @f
end

#matrizObject

Returns the value of attribute matriz.



5
6
7
# File 'lib/densa.rb', line 5

def matriz
  @matriz
end

Instance Method Details

#*(mat) ⇒ Object

Metodo para multiplicacion dos matrices



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/densa.rb', line 59

def *(mat)
     if(mat.class == Densa)
      if (@c == mat.f)
	aux = Array.new
	for i in 0...@f do
	  aux[i] = Array.new
	  for j in 0...mat.c do
	    if(mat.matriz[0][0].class == Fraccion)
	      aux[i][j] = Fraccion.new(0,1)
	    else
	      aux[i][j] = 0
	    end
	  end
	end
	resultado = Densa.new(aux)
	for i in 0...@f do
	  for j in 0...mat.c do
	    for k in 0...@c do
	      resultado.matriz[i][j] += @matriz[i][k] * mat.matriz[k][j]
	    end
	  end
	end
    else
      puts "error"
    end
   else
     if(@c == mat.f)
       aux = Array.new
       for i in 0...@f do
	 aux[i] = Array.new
	 for j in 0...mat.c do
	   if(((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && (mat.matriz[i][j].class == Fraccion)) || (@matriz[0][0].class == Fraccion))
	     aux[i][j] = Fraccion.new(0,1)
	   else
	     aux[i][j] = 0
	   end
	 end
       end
     resultado = Densa.new(aux)
     for i in 0...@f do
       for j in 0...mat.c do
	 for k in 0...@c do
	   if((!mat.matriz[k].nil?)  && (!mat.matriz[k][j].nil?))
	     resultado.matriz[i][j] += @matriz[i][k] * mat.matriz[k][j]
	    end
	 end
       end
     end
     else
       puts "error"
     end
    end
    return resultado
end

#+(mat) ⇒ Object

Metodo para sumar dos matrices



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/densa.rb', line 17

def +(mat)
	if(mat.class == Densa)
 resultado = Densa.new(@matriz)
 for i in 0...@f do
  for j in 0...@c do
   resultado.matriz[i][j] =@matriz[i][j] + mat.matriz[i][j]
  end
 end
	else
	  resultado = Densa.new(@matriz)
	  for i in 0...@f
 for j in 0...@c
   if((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
		resultado.matriz = resultado.matriz[i][j] + mat.matriz[i][j]
   end
 end
	  end
	end
	resultado
end

#-(mat) ⇒ Object

Metodo para restar dos matrices



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/densa.rb', line 38

def -(mat)
  if(mat.class == Densa)
	resultado = Densa.new(@matriz)
	for i in 0...@f do
		for j in 0...@c do
			resultado.matriz[i][j] =@matriz[i][j] - mat.matriz[i][j]
		end
	end
  else
	  resultado = Densa.new(@matriz)
	  for i in 0...@f
 for j in 0...@c
   if((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
		resultado.matriz = resultado.matriz[i][j] - mat.matriz[i][j]
   end
 end
	  end
	end
  resultado
end

#coerce(dato) ⇒ Object



13
14
15
# File 'lib/densa.rb', line 13

def coerce (dato)
  [self, dato]
end

#deterObject

Metodo para calcular el determinante de una matriz



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/densa.rb', line 139

def deter
   det = @matriz[0][0]
   aux = Densa.new(@matriz)
   for k in 0...@f do
     l = k+1
     for i in l...@c do
       for j in l...@c do
         aux.matriz[i][j] = (aux.matriz[k][k] * aux.matriz[i][j] - aux.matriz[k][j] * aux.matriz[i][k])/ aux.matriz[k][k]
       end
     end
     det = det * aux.matriz[k][k]
   end
   det
end

#igual(mat) ⇒ Object

Metodo para comprobar si dos matrices son iguales



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/densa.rb', line 126

def igual (mat)
  if(@f == mat.f && @c == mat.c)
	 for i in 0...@f
	   for j in 0...@c
 if(@matriz[i][j] != mat.matriz[i][j])
   return -1
 end
	   end
	 end
  end
  return 1
end

#mayorObject



178
179
180
181
182
183
184
185
186
187
# File 'lib/densa.rb', line 178

def mayor
  max = @matriz[0][0]
  for i in 0...@f
	for j in 0...@c
	  if(max < @matriz[i][j])
 max = @matriz[i][j]
	  end
	end
  end
end

#menorObject



189
190
191
192
193
194
195
196
197
198
# File 'lib/densa.rb', line 189

def menor
  min = @matriz[0][0]
  for i in 0...@f
	for j in 0...@c
	  if(min > @matriz[i][j])
 min = @matriz[i][j]
	  end
	end
  end
end

#tObject

Metodo para calcular la traspuesta de una matriz



155
156
157
158
159
160
161
162
163
164
# File 'lib/densa.rb', line 155

def t
  resultado = Array.new
  for i in 0...@c
	 resultado[i] = Array.new
	 for j in 0...@f
	   resultado[i][j] = matriz[j][i]
	 end
  end
  resultado
end

#to_densa(mat) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/densa.rb', line 200

def to_densa (mat)
  aux = Array.new
  for i in 0...@f
	aux[i] = Array.new
	for j in 0...@c
	  if(!mat[i].nil? && !mat[i][j].nil?)
 aux[i][j] = mat[i][j]
	  else
 aux[i][j] = 0
	  end
	end
  end
  Densa.new(aux)
end

#to_sObject

Metodo para convertir la matriz a string



167
168
169
170
171
172
173
174
175
176
# File 'lib/densa.rb', line 167

def to_s
 aux = ""
 @f.times do |i|
  @c.times do |j|
   aux << "#{@matriz[i][j]}\t"
  end
  aux << "\n"
 end
 aux
end

#x(n) ⇒ Object

Metodo para multiplicar una matriz por un escalar



115
116
117
118
119
120
121
122
123
# File 'lib/densa.rb', line 115

def x(n)
  	resultado = Densa.new(@matriz)
	for i in 0...@f do
		for j in 0...@c do
			resultado.matriz[i][j] =@matriz[i][j] *n
		end
	end
	resultado
end