Class: Matriz

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

Direct Known Subclasses

Matriz_densa, Matriz_dispersa

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, cols) ⇒ Matriz

Returns a new instance of Matriz.



6
7
8
# File 'lib/matrices/matriz.rb', line 6

def initialize(rows, cols)
  @rows, @cols = rows , cols
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



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

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

Class Method Details

.nula(rows = 1, cols = 1) ⇒ Object



19
20
21
22
# File 'lib/matrices/matriz.rb', line 19

def Matriz.nula(rows=1, cols=1)
  aux=Array.new(rows*cols, 0)
  Matriz_dispersa.new(rows, cols, aux)
end

.vector(rows, cols, ele) ⇒ Object

Raises:

  • (TypeError)


10
11
12
13
14
15
16
17
# File 'lib/matrices/matriz.rb', line 10

def Matriz.vector(rows, cols, ele)
  raise TypeError, 'No se han introducido suficientes valores' unless (ele.length==rows*cols) 
    if (((ele.count{|e| e == e.elemento_nulo}*100)/(rows*cols)) >= 60) # Matriz dispersa
      Matriz_dispersa.new(rows,cols,ele)
    else # Matriz densa
      Matriz_densa.new(rows,cols,ele)
    end
end

Instance Method Details

#*(other) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/matrices/matriz.rb', line 64

def *(other)
  if(other.is_a? Numeric)
    result=Array.new
    for i in 0...@rows
      for j in 0...@cols
        result << (self[i,j]*other)
      end
    end
    return Matriz.vector(@rows,@cols,result)
    
  elsif(other.is_a? Matriz)
    raise TypeError, 'Las matrices dadas no se pueden multiplicar entre si' unless (@cols == other.rows)
    result=Array.new
    for i in 0...@rows
      for j in 0...other.cols
        result << 0
        for k in 0...@cols
          result[(result.length)-1]= (result.last + (self[i,k] * other[k,j]))
        end
      end
    end
  end
  Matriz.vector(@rows,other.cols,result)
end

#+(other) ⇒ Object

Raises:

  • (TypeError)


42
43
44
45
46
47
48
49
50
51
# File 'lib/matrices/matriz.rb', line 42

def +(other) 
  raise TypeError, 'Las matrices no son del mismo tamanyo' unless (@rows==other.rows && @cols==other.cols)
    result=Array.new
    for i in 0...@rows
      for j in 0...@cols
        result << (self[i,j] + other[i,j])
      end 
    end
    Matriz.vector(@rows, @cols, result)
end

#-(other) ⇒ Object

Raises:

  • (TypeError)


53
54
55
56
57
58
59
60
61
62
# File 'lib/matrices/matriz.rb', line 53

def -(other) 
  raise TypeError, 'Las matrices no son del mismo tamanyo' unless (@rows==other.rows && @cols==other.cols)
    result=Array.new
    for i in 0...@rows
      for j in 0...@cols
        result << (self[i,j] - other[i,j])
      end 
    end
    Matriz.vector(@rows, @cols, result)
end

#coerce(other) ⇒ Object



119
120
121
# File 'lib/matrices/matriz.rb', line 119

def coerce(other)
  [self,other]
end

#maxObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/matrices/matriz.rb', line 89

def max
  aux=self[0,0]
  for i in 0...@rows
    for j in 0...@cols
      r = aux.coerce(self[i,j])
      if (r[0] > r[1])
        aux = r[0]
      else
        aux = r[1]
      end
    end
  end
  aux
end

#minObject



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

def min
  aux=self[0,0]
  for i in 0...@rows
    for j in 0...@cols
      r = aux.coerce(self[i,j])
      if (r[0] < r[1])
        aux = r[0]
      else
        aux = r[1]
      end
    end
  end
  aux
end

#to_sObject



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

def to_s  
  cadena = "["
  for i in (0..(@rows-1))
    cadena += "["
    for j in (0..(@cols-1))
      cadena += "#{self[i,j]}"
      if (j < (@cols-1)) 
        cadena += "," 
      end
    end
    cadena += "]"
    if (i < (@rows-1)) 
      cadena += "," 
    end
  end
  cadena += "]"
end