Class: Matriz

Inherits:
Object
  • Object
show all
Defined in:
lib/p10lppt13.rb

Direct Known Subclasses

Densa, Dispersa

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nfil, ncol) ⇒ Matriz

Returns a new instance of Matriz.



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

def initialize(nfil, ncol)
  @nfil = nfil
  @ncol = ncol
end

Instance Attribute Details

#ncolObject (readonly)

Returns the value of attribute ncol.



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

def ncol
  @ncol
end

#nfilObject (readonly)

Returns the value of attribute nfil.



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

def nfil
  @nfil
end

Instance Method Details

#*(other) ⇒ Object

Metodo para multiplicar una matriz por un escalar



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/p10lppt13.rb', line 169

def *(other)
  elemento = Array.new(0)
  for i in 0...filas
    fila = Array.new(0)
    for j in 0...colum
      fila << pos[i][j]*other
    end
    elemento << fila
  end
  Matriz.new(@nfil, @ncol, elemento)
end

#+(other) ⇒ Object

Metodo para sumar dos matrices



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

def +(other)
  if ((@nfil != other.nfil) || (@ncol != other.ncol))
    puts "No se pueden sumar las matrices"
  else 
    elemento = Array.new(0)
    for i in 0...filas
      fila = Array.new(0)
      for j in 0...colum
        if self.pos[i][j] == nil && other.pos[i][j] != nil
          fila << other.pos[i][j]
        elsif self.pos[i][j] != nil && other.pos[i][j] == nil
          fila << pos[i][j]
        elsif self.pos[i][j] == nil && other.pos[i][j] == nil
          fila << 0
        else
          fila << pos[i][j] + other.pos[i][j]
        end              
      end
      elemento << fila
    end
  Densa.new(@nfil, @ncol, elemento)
  end
end

#-(other) ⇒ Object

Metodo para restar dos matrices



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/p10lppt13.rb', line 61

def -(other)
  if ((@nfil != other.nfil) || (@ncol != other.ncol))
    puts "No se pueden restar las matrices"
  else 
    elemento = Array.new(0)
    for i in 0...filas
      fila = Array.new(0)
      for j in 0...colum
        if self.pos[i][j] == nil && other.pos[i][j] != nil
          fila << -other.pos[i][j]
        elsif self.pos[i][j] != nil && other.pos[i][j] == nil
          fila << pos[i][j]
        elsif self.pos[i][j] == nil && other.pos[i][j] == nil
          fila << 0
        else
          fila << pos[i][j] - other.pos[i][j]
        end              
      end
      elemento << fila
    end
  Densa.new(@nfil, @ncol, elemento)
  end
end

#columObject

Getter de ncol



19
20
21
# File 'lib/p10lppt13.rb', line 19

def colum
  @ncol
end

#filasObject

Getter de nfil



14
15
16
# File 'lib/p10lppt13.rb', line 14

def filas
  @nfil
end

#maxObject

Metodo para hallar el maximo de una matriz



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/p10lppt13.rb', line 96

def max
  acum = pos[0][0]
  for i in 0...filas
    for j in 0...colum
      if self.pos[i][j] != nil
        if pos[i][j] > acum
          acum = pos[i][j]
        end
      end             
    end
  end
  acum
end

#minObject

Metodo para hallar el minimo de una matriz



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/p10lppt13.rb', line 111

def min
  acum = pos[0][0]
  for i in 0...filas
    for j in 0...colum
      if self.pos[i][j] != nil
        if pos[i][j] < acum
          acum = pos[i][j]
        end
      end             
    end
  end
  acum
end

#por(other) ⇒ Object

Metodo para multiplicacion dos matrices



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/p10lppt13.rb', line 126

def por(other)
  if ((@nfil != other.ncol) || (@ncol != other.nfil))
    puts "No se pueden multiplicarr las matrices"
  else
    elemento = Array.new(0)
    for i in 0...nfil
      fila = Array.new(0)
      for j in 0...other.ncol
        aux = 0
        for k in 0...ncol
          aux += pos[i][k] * other.pos[k][j]
        end
        fila << aux
      end
      elemento << fila
    end
  end
  Matriz.new(@nfil, other.ncol, elemento)
end

#porf(other) ⇒ Object

Metodo para multiplicacion dos matrices



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/p10lppt13.rb', line 147

def porf(other)
  if ((@nfil != other.ncol) || (@ncol != other.nfil))
    puts "No se pueden multiplicarr las matrices"
  else
    elemento = Array.new(0)
    for i in 0...nfil
      fila = Array.new(0)
      for j in 0...other.ncol
        aux = Fraccion.new(1,1)
        aux = aux - aux
        for k in 0...ncol
          aux += pos[i][k] * other.pos[k][j]
        end
        fila << aux
      end
      elemento << fila
    end
  end
  Densa.new(@nfil, other.ncol, elemento)
end

#primervalorObject



85
86
87
88
89
90
91
92
93
# File 'lib/p10lppt13.rb', line 85

def primervalor
  for i in 0...@nfil
    for j in 0...@nncol
      if (self.pos[i][j] != nil) 
          return self.pos[i][j]
      end
    end
  end
end

#to_sObject

Metodo para convertir la matriz a string



24
25
26
27
28
29
30
31
32
33
# File 'lib/p10lppt13.rb', line 24

def to_s
  aux = ""
  @nfil.times do |i|
    @ncol.times do |j|
      aux << "#{pos[i][j]}\t"
    end
    aux << "\n"
  end
  aux
end

#trasponerObject

Metodo para hallar la traspuesta de una matriz



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/p10lppt13.rb', line 182

def trasponer
  elemento = Array.new(0)
  for i in 0...colum
    fila = Array.new(0)
    for j in 0...filas
      fila << pos[j][i]
    end
    elemento << fila
  end
  Densa.new(@ncol, @nfil, elemento)
end

#vectorizarObject

Metodo para convertir la matriz en un vector



196
197
198
199
200
201
202
203
204
# File 'lib/p10lppt13.rb', line 196

def vectorizar
  aux = Array.new(0)
  for i in 0...colum
    for j in 0...filas
      aux << pos[i][j]
    end
  end
  Array.new(aux)
end