Class: Matriz

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

Overview

Clase Base que contiene el metodo initilize y los getters. Además contiene el to_s y el método []

Direct Known Subclasses

MatrizDensa, MatrizDispersa

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Operatoria

#*, #+, #-, #-@, #==, #Producto_escalar, #[]=

Constructor Details

#initialize(matriz) ⇒ Matriz

Returns a new instance of Matriz.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/matriz.rb', line 104

def initialize(matriz)
   @matriz = Array.new(matriz)
   @filas = matriz.size
   @columnas = matriz[0].size
   @n_elementos= (matriz.size * matriz[0].size)*0.6 
    n_ceros=0
   filas.times do |i|
      columnas.times do |j|
  if (matriz[i][j]==0)  
     n_ceros=n_ceros+1
end
   end
   end
   if n_ceros > @n_elementos
      raise RuntimeError, 'La Matriz no es densa'
   end
  
end

Instance Attribute Details

#columnasObject

Returns the value of attribute columnas.



102
103
104
# File 'lib/matriz.rb', line 102

def columnas
  @columnas
end

#filasObject

Returns the value of attribute filas.



102
103
104
# File 'lib/matriz.rb', line 102

def filas
  @filas
end

#matrizObject

Returns the value of attribute matriz.



102
103
104
# File 'lib/matriz.rb', line 102

def matriz
  @matriz
end

Instance Method Details

#[](i, j) ⇒ Object



150
151
152
# File 'lib/matriz.rb', line 150

def [](i,j)
   matriz[i][j]
end

#comprobar(matriz) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/matriz.rb', line 123

def comprobar (matriz)

 n_ceros=0
   filas.times do |i|
      columnas.times do |j|
  if (matriz[i][j]==0)  
     n_ceros=n_ceros+1
end
   end
   end
   if n_ceros > @n_elementos
      raise RuntimeError, 'La Matriz no es densa'
   end
end

#to_sObject

Imprime la matrices



139
140
141
142
143
144
145
146
147
# File 'lib/matriz.rb', line 139

def to_s
   filas.times do |i|   
      columnas.times do |j|
         print "#{matriz[i][j]}  "
      end
	puts
   end
   puts 
end