Class: Matriz

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

Direct Known Subclasses

MatrizDensa, MatrizDispersa

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filas, columnas) ⇒ Matriz

Returns a new instance of Matriz.



4
5
6
# File 'lib/prac09/matriz.rb', line 4

def initialize(filas, columnas)
    @filas, @columnas = filas , columnas
end

Class Method Details

.constructor(filas, columnas, velementos) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/prac09/matriz.rb', line 19

def Matriz.constructor(filas, columnas, velementos)

      longitud=filas*columnas
       if (((velementos.count(0)*100)/(longitud)) >= 60)

        MatrizDispersa.new(filas,columnas,velementos)
      else 

        MatrizDensa.new(filas,columnas,velementos)
      end
end

Instance Method Details

#*(object) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/prac09/matriz.rb', line 96

def *(object)

    if(object.is_a? Numeric)
  matrizresultado=Array.new
  i=0
      while (i<@filas)
        j=0
        while (j<@columnas)
          matrizresultado << (self[i,j]*object)
          j=j+1
      end
        i=i+1
      end
      return Matriz.constructor(@filas,@columnas,matrizresultado)
      

    elsif ((object.is_a?(Matriz)==true) && (@columnas==object.filas()))
      i=0
                matrizresultado = Array.new
                while (i < @filas)
                        j = 0
                        while (j < object.columnas())
                                k = 0
                                matrizresultado<<0
                                while (k < @columnas) #itera en las columnas de la primera matriz y las filas de la segunda
                                        matrizresultado[(matrizresultado.size)-1]= (matrizresultado.last + (self[i,k] * object[k,j]))
                                        k = k + 1
                                end
                                j = j + 1
                        end
                        i = i + 1
                end
                return Matriz.constructor(@filas,object.columnas,matrizresultado)
        end
end

#+(object) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/prac09/matriz.rb', line 32

def +(object)
    
    if ( (@filas == object.filas) && (@columnas == object.columnas))
   
matrizresultado=Array.new
i = 0
while (i < @filas) do
      
  j=0
  while (j < @columnas) do
          
    matrizresultado << (self[i,j] + object[i,j])
                j=j+1
          end
            i=i+1
        end
        Matriz.constructor(@filas, @columnas, matrizresultado)
    end
end

#-(object) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/prac09/matriz.rb', line 53

def -(object)
    
    if ( (@filas == object.filas) && (@columnas == object.columnas))
   
matrizresultado=Array.new
i = 0
while (i < @filas) do
      
  j=0
  while (j < @columnas) do
          
    matrizresultado << (self[i,j] - object[i,j])
                j=j+1
          end
            i=i+1
        end
        Matriz.constructor(@filas, @columnas, matrizresultado)
    end
end

#==(object) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/prac09/matriz.rb', line 75

def ==(object)
  if ((@filas == object.filas) && (@columnas == object.columnas))
    i = 0
    while (i < @filas) do

      j = 0
      while (j < @columnas) do

        if (self[i,j] == object[i,j]) #comparamos elemento a elemento
          es_igual = true
        else 
          return es_igual = false #si solo uno es distinto la funcion devuelve falso
        end
        j = j + 1
      end
      i = i + 1
    end
  end
  return es_igual #si compara todos los elementos y son iguales devuelve verdadero
end

#coerce(object) ⇒ Object



186
187
188
# File 'lib/prac09/matriz.rb', line 186

def coerce(object)
    [self,object]
end

#columnasObject

get columnas



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

def columnas()
  @columnas
end

#filasObject

get filas



9
10
11
# File 'lib/prac09/matriz.rb', line 9

def filas()
  @filas
end

#maxObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/prac09/matriz.rb', line 132

def max()
  maximo = 0 # maximo provisional
  i, j = 0,0
  while i<=@filas
    while j<=@columnas
      if (self[i,j]>maximo) #si el valor es mayor que el provisional almacena el nuevo
        maximo = self[i,j]
      end
      j += 1;
    end
    i += 1;
  end
  return maximo
end

#minObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/prac09/matriz.rb', line 147

def min()
  minimo = 0 # maximo provisional
  i, j = 0,0
  while i<=@filas
    while j<=@columnas
      if (self[i,j]<minimo) #si el valor es mayor que el provisional almacena el nuevo
        minimo = self[i,j]
      end
      j += 1;
    end
    i += 1;
  end
  return minimo
end

#to_sObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/prac09/matriz.rb', line 162

def to_s
  string= "[" ##corchete de matriz
  
  fil=0
  while (fil<@filas)
    string = string + "[" #corchete de fila
    col=0
    while (col<@columnas)
      string = string + "#{self[fil,col]}"
      col += 1
      if(col < @columnas)
        string = string + ","
      end
    end
    string = string + "]" #cerramos el corchete de fila
    fil += 1
    if (fil < @filas)
      string = string + ","
    end
  end
  string = string +"]" ##cerramos el corchete de matriz
end