Class: Matriz

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

Direct Known Subclasses

Matrc::MatrizDensa, Matrc::MatrizDispersa

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, col) ⇒ Matriz

Returns a new instance of Matriz.



7
8
9
# File 'lib/matrc/matriz.rb', line 7

def initialize(row,col)
 @row,@col = row,col
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



3
4
5
# File 'lib/matrc/matriz.rb', line 3

def col
  @col
end

#rowObject (readonly)

Returns the value of attribute row.



3
4
5
# File 'lib/matrc/matriz.rb', line 3

def row
  @row
end

Instance Method Details

#*(other) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/matrc/matriz.rb', line 22

def *(other)
  if(self.col == other.row)
    matres = self.class.new(self.row,self.col)
    for i in 0...self.row
      for j in 0...other.col
        for k in 0...self.col
          matres[i,j] += self[i,k] * other[k,j]
        end
      end
    end
    matres
	end
end

#+(other) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/matrc/matriz.rb', line 36

def +(other)
  if(self.col == other.col && other.row == self.row)
    matres = self.class.new(self.row,self.col)
    for i in 0...self.row
      for j in 0...self.col
        matres[i,j] = self[i,j] + other[i,j]
      end
    end
    matres
	end  
end

#-(other) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/matrc/matriz.rb', line 48

def -(other)
  if(self.col == other.col && other.row == self.row)
    matres = self.class.new(self.row,self.col)
    for i in 0...self.row
      for j in 0...self.col
        matres[i,j] = self[i,j] - other[i,j]
      end
    end
    matres
	end      
end

#==(other) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/matrc/matriz.rb', line 60

def ==(other)
   if (self.row != other.row || self.col != other.col)
    return false
   end
   for i in 0...self.row
    for j in 0...self.col
      if(self[i, j] != other[i,j])
	return false
      end
    end
   end
  return true
end

#eachObject



74
75
76
77
78
79
80
# File 'lib/matrc/matriz.rb', line 74

def each
  for i in 0...self.row
 for j in 0...self.col
   yield self[i, j]
    end
	end
end

#maxObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/matrc/matriz.rb', line 82

def max
    max = -9999
    for i in 0...@row
      for j in 0...@col
        if(self[i,j] > max)
           max = self[i,j]
        end
       end
     end
 max
end

#minObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/matrc/matriz.rb', line 94

def min
    min = 9999
    for i in 0...@row
      for j in 0...@col
        if(self[i,j] < min)
           min = self[i,j]
        end
       end
     end
 min
end

#to_sObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/matrc/matriz.rb', line 11

def to_s
 aux = ""
 for i in 0...self.row
   for j in 0...self.col
     aux << "#{self[i,j]}"
   end
   aux << "\n"
  end
  aux
end