Class: MathExpansion::Matriz_Densa

Inherits:
Matriz
  • Object
show all
Defined in:
lib/math_expansion/matriz_densa.rb

Instance Attribute Summary

Attributes inherited from Matriz

#M, #N, #contenido

Instance Method Summary collapse

Constructor Details

#initialize(n, m) ⇒ Matriz_Densa



5
6
7
8
9
10
11
12
13
14
# File 'lib/math_expansion/matriz_densa.rb', line 5

def initialize(n, m)
  super
  
  @contenido = Array.new(@N,0)
  i = 0
  while i < @N
    @contenido[i] = Array.new(@M,0)
    i += 1  
  end
end

Instance Method Details

#*(other) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/math_expansion/matriz_densa.rb', line 117

def *(other)
		raise ArgumentError , 'Parametro invalido' unless other.is_a? Numeric or other.is_a? Matriz

		if(other.is_a? Numeric) # Matriz * numero
  			c = Matriz_Densa.new(@N, @M)
	i = 0
  			while(i < @N)
    			j = 0
    			while(j < @M)
     				c.set(i, j, get(i,j)*other)
     				j += 1
 			end # while j
 			i += 1
  			end # while i
		else # Matriz * Matriz
  			raise ArgumentError , 'Matriz no compatible (A.N == B.M)' unless @M == other.N
  			c = Matriz_Densa.new(@N, other.M)
  			i = 0
  			while(i < @N)
    			j = 0
    			while(j < other.M)
   				k = 0
	#if (get(i,j).is_a? Fraccion)	      				
	#	c.set(i, j, Fraccion.null
	#else
	#	c.set(i, j, 0)
	#end
   				while(k < @M)
     				c.set(i, j, get(i, k) * other.get(k,j) + c.get(i,j))
     				k += 1
   				end # while k
      				j += 1
    			end # while j
 			i += 1
  			end # while i
		end # while else
if (c.null_percent > 0.6)
			c = MathExpansion::Matriz_Dispersa.copy(c)
		end
		c
end

#+(other) ⇒ Object

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/math_expansion/matriz_densa.rb', line 77

def +(other)
  		raise ArgumentError , 'Tipo invalido' unless other.is_a? Matriz
  		raise ArgumentError , 'Matriz no compatible' unless @N == other.N and @M == other.M
  
  		c = Matriz_Densa.new(@N, @M)
  		i = 0
		while(i < @N)
    		j = 0
    		while(j < @M)
   			c.set(i, j, get(i,j) + other.get(i,j))
	  			j += 1
			end 
			i += 1
  		end
		if (c.null_percent > 0.6)
			c = MathExpansion::Matriz_Dispersa.copy(c)
		end
  		c
end

#-(other) ⇒ Object

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/math_expansion/matriz_densa.rb', line 97

def -(other)
  		raise ArgumentError , 'Tipo invalido' unless other.is_a? Matriz
  		raise ArgumentError , 'Matriz no compatible' unless @N == other.N and @M == other.M
  
  		c = Matriz_Densa.new(@N, @M)
  		i = 0
  		while(i < @N)
    		j = 0
    		while(j < @M)
   			c.set(i, j, get(i,j) - other.get(i,j))
	  			j += 1
			end
			i += 1
  		end
		if (c.null_percent > 0.6)
			c = MathExpansion::Matriz_Dispersa.copy(c)
		end
  		c
end

#get(i, j) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/math_expansion/matriz_densa.rb', line 16

def get(i, j)
  if( i < 0 or i >=@N or j < 0 or j >= @M)
    return nil
  end

  @contenido[i][j]
end

#maxObject

*(other)



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/math_expansion/matriz_densa.rb', line 159

def max
	m = get(0,0)
    i = 0
	while(i < @N)
		j = 0
		while(j < @M)
			if (get(i,j) > m)
m = get(i,j)
			end
			j += 1
		end
		i += 1
	end
	m
end

#minObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/math_expansion/matriz_densa.rb', line 175

def min
	m = get(0,0)
    i = 0
	while(i < @N)
		j = 0
		while(j < @M)
			if (get(i,j) < m)
m = get(i,j)
			end
			j += 1
		end
		i += 1
	end
	m
end

#null_percentObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/math_expansion/matriz_densa.rb', line 24

def null_percent
  total = @N*@M
  no_nulos = 0
  
  i = 0
  while(i < @N)
    j = 0
    while(j < @M)
      if(@contenido[i][j] != @contenido[i][j].class.null)
        no_nulos += 1
	  end
      j += 1
    end
    i += 1
  end
  
  nulos = total - no_nulos
  nulos.to_f/total.to_f
end

#set(i, j, value) ⇒ Object

endmethod null_percent



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/math_expansion/matriz_densa.rb', line 44

def set(i, j, value)
  if( i < 0 or i >=@N or j < 0 or j >= @M)
   return nil
  end
    
  if(!(value.class.respond_to? :null))
    puts "Se debe definir el metodo \"null\" que devuelva un elemento nulo para la clase #{value.class}"
    return nil
  end

  # Contar elementos nulos y comprobar si se hace una matriz dispersa
  # De momento, no dejamos añadir elementos nulos
  # ¿o si?
  #if(value != nil and value != value.class.null) # Y se puede comprobar para todos los tipos si es necesario. (con un método zero, por ejemplo)
    @contenido[i][j] = value
  #end
end

#to_sObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/math_expansion/matriz_densa.rb', line 62

def to_s
  s = ""
  i = 0
  while(i < @N)
    j = 0
    while(j < @M)
      s += "#{@contenido[i][j].to_s}\t"
      j += 1
    end
    s += "\n"
    i += 1
  end
  s
end