matrizdispersascp 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77f5cd7af5514eb3a69b2611dc48c81b51f0996a
4
- data.tar.gz: 487d4cd78c7e86d5b3983bae62bc02f6aa289daa
3
+ metadata.gz: 40ccc363c1f1befb5871ab5c7f96627a57362cb4
4
+ data.tar.gz: a585edb6410479b5ad69cfbe9eb7c4f422212609
5
5
  SHA512:
6
- metadata.gz: e1f4073f0a46a4b36b91a3cdf8da391e414823ffa95b05409216e48894ca6b864bf1f76bb896f60b8222229101c8139c18cb0fefc3a5e947968cfd72c1238d89
7
- data.tar.gz: 7572aa5dbf9403504ed6dbd4fa9153f7ae37d3c42408ae6a513a2e8ded5617444a8673422ed4519291106eaef99699cd5a87ef76527fe530267b1866ba46fb91
6
+ metadata.gz: 6493df2f96e3882f7cd72d4b49f273679b720a2acf69ba0a5ee185691282533c684e90beaf4b7a6c18572c8e4897a7e5222edb29cf66b92e93d4933ee3835d62
7
+ data.tar.gz: 1b1d3911fe0d74f60a6489cfa11b8ca9fd53b7be37edcad20030866660591690ee08787a6e302fad1fe29d7c568473cdbb60cb73e0a2fd23fcbd81bcbc148c8a
data/lib/fraccion.rb CHANGED
@@ -1,8 +1,8 @@
1
- # Practica 6 Lenguajes y Paradigmas de la Programación
2
- # Desarrollo Dirigido por el Comportamiento (Behavior Driven Development - BDD)
3
- # Autores: Pedro Javier Núñez Rodríguez
4
- # Constanza León Baritussio
5
- # Fichero que la implementación de la clase Fraccion
1
+ # Etsii ull grado informática
2
+ # Lenguajes y Paradigmas de la programacion
3
+ # Pedro Javier Núñez Rodríguez
4
+ # Constanza Leon Baritussio
5
+ # Clase matriz
6
6
 
7
7
  require 'mcd'
8
8
 
@@ -104,7 +104,9 @@ class Fraccion
104
104
  (@numerador.to_f / @denominador) >= (frac.numerador.to_f / frac.denominador)
105
105
  end
106
106
 
107
-
107
+ #def coerce(something)
108
+ # [self, Fraccion.new(something)]
109
+ #end
108
110
 
109
111
  end
110
112
 
data/lib/matriz.rb CHANGED
@@ -1,135 +1,151 @@
1
- # Practica 8 Lenguajes y Paradigmas de la programación usando #desarrollo dirigido por pruebas (Test Driven Development - TDD) con
2
- # la herramienta Rspec.
1
+ # Etsii ull grado informática
2
+ # Lenguajes y Paradigmas de la programacion
3
3
  # Pedro Javier Núñez Rodríguez
4
4
  # Constanza Leon Baritussio
5
- #
5
+ # Clase matriz
6
6
 
7
- class Matriz << class Vectordisperso
8
- class Matriz << class Matrixdispersa
7
+ require 'fraccion'
8
+
9
+ class Matriz
9
10
 
10
11
  include Comparable #Modulo comparable
11
- attr_accessor :matrix, :n, :m
12
- @matrix
13
- @n # filas
14
- @m # columnas
15
-
16
- def initialize(n,m) #Constructor de la clase que inicializa la matriz con los valores que se le pasan por parametros
17
- raise ArgumentError, 'Tamaño de matriz no valido' unless n.is_a? Integer and m.is_a? Integer and n > 0 and m > 0
18
- @n = n
19
- @m = m
20
- @matrix=[]
21
- for i in (0...@n)
22
- @matrix[i]=[]
23
- for j in (0...@m)
24
- @matrix[i][j] = 0
25
- end
26
- end
12
+
13
+ attr_accessor :filas, :columnas, :matriz
14
+
15
+ def initialize(m)
16
+ @filas = m.size
17
+ @columnas = m[0].size
18
+ @matriz = m;
27
19
  end
28
20
 
29
- def set(i, j, valor) #Método para aaceder a los valores i , j y valor de la matriz
30
- raise ArgumentError, 'Indice no valido' unless i.is_a? Integer and i >= 0 and i < @n
31
- raise ArgumentError, 'Indice no valido' unless j.is_a? Integer and j >= 0 and j < @m
32
- raise ArgumentError, 'Valor no numerico' unless valor.is_a? Integer
33
- @matrix[i][j] = valor
21
+ def [](fila, columna)
22
+ @matriz[fila][columna]
34
23
  end
35
-
36
- def get(i, j)
37
- return @matrix[i][j]
24
+
25
+ def []=(fila, columna, valor)
26
+ @matriz[fila][columna] = valor
38
27
  end
39
28
 
40
29
  def to_s #Método encargado de mostrar por consola la matriz de forma matriz[i][j]
41
- s = ""
42
- s += "{"
43
- for i in (0...n)
44
- s += "{"
45
- for j in (0...m)
46
- if j == 0
47
- s += "#{matrix[i][j]}"
48
- else
49
- s += ","
50
- s += "#{matrix[i][j]}"
51
- end
52
- end
53
- s += "}"
54
- end
55
- s += "}"
56
- s
30
+ s = ""
31
+ s += "{"
32
+ for i in (0...filas)
33
+ s += "{"
34
+ for j in (0...columnas)
35
+ if j == 0
36
+ s += "#{matriz[i][j]}"
37
+ else
38
+ s += ","
39
+ s += "#{matriz[i][j]}"
40
+ end
41
+ end
42
+ s += "}"
43
+ end
44
+ s += "}"
45
+ s
57
46
  end
58
47
 
59
48
  def to_f # Método encargado de mostrar por la consola la matriz en formato flotante
60
- s = ""
61
- s = "{"
62
- for i in (0...n)
63
- s += "{"
64
- for j in (0...m)
65
- if j == 0
66
- s += "#{matrix[i][j].to_f}"
67
- else
68
- s += ","
69
- s += "#{matrix[i][j].to_f}"
70
- end
71
- end
72
- s += "}"
73
- end
74
- s += "}"
75
- s
49
+ s = ""
50
+ s = "{"
51
+ for i in (0...filas)
52
+ s += "{"
53
+ for j in (0...columnas)
54
+ if j == 0
55
+ s += "#{matriz[i][j].to_f}"
56
+ else
57
+ s += ","
58
+ s += "#{matriz[i][j].to_f}"
59
+ end
60
+ end
61
+ s += "}"
62
+ end
63
+ s += "}"
64
+ s
76
65
  end
77
66
 
78
67
  def -@ # Sobrecarga del operador - para calcular el opuesto de una matriz
79
- mat = Matriz.new(n,m)
80
- for i in (0...n)
81
- for j in (0...m)
82
- if matrix[i][j] != 0
83
- mat.matrix[i][j] = (matrix[i][j] * -1)
84
- end
85
- end
86
- end
87
- mat
68
+ mat = Matriz.new(self.matriz)
69
+ for i in (0...filas)
70
+ for j in (0...columnas)
71
+ if matriz[i][j] != 0
72
+ mat.matriz[i][j] = (matriz[i][j] * -1)
73
+ end
74
+ end
75
+ end
76
+ mat
88
77
  end
89
78
 
90
79
  def * num #Método que multiplica un número por una matriz
91
- mat = Matriz.new(n,m)
92
- for i in (0...n)
93
- for j in (0...m)
94
- mat.set(i,j,matrix[i][j] * num)
95
- end
96
- end
97
- mat
80
+ mat = Matriz.new(self.matriz)
81
+ for i in (0...filas)
82
+ for j in (0...columnas)
83
+ mat.matriz[i][j] = (matriz[i][j] * num)
84
+ end
85
+ end
86
+ mat
98
87
  end
99
88
 
100
89
  def + matAux #Método para hacer la suma de de matrices
101
- mat = Matriz.new(n,m)
102
- for i in (0...n)
103
- for j in (0...m)
104
- mat.set(i,j,matrix[i][j] + matAux.matrix[i][j])
105
- end
90
+ mat = Matriz.new(matAux.matriz)
91
+ for i in (0...filas)
92
+ for j in (0...columnas)
93
+ mat.matriz[i][j] = (matriz[i][j] + matAux.matriz[i][j])
94
+ end
106
95
  end
107
96
  mat
108
97
  end
109
98
 
110
99
  def - matAux #Método para hacer la resta de matrices
111
- mat = Matriz.new(n,m)
112
- for i in (0...n)
113
- for j in (0...m)
114
- mat.set(i,j,matrix[i][j] - matAux.matrix[i][j])
115
- end
100
+ mat = Matriz.new(matAux.matriz)
101
+ for i in (0...filas)
102
+ for j in (0...columnas)
103
+ mat.matriz[i][j] = (matriz[i][j] - matAux.matriz[i][j])
104
+ end
116
105
  end
117
106
  mat
118
107
  end
119
108
 
120
109
  def * matAux #Método para hacer la multiplicacion de matrices
121
- mat = Matriz.new(n,m)
122
- for k in 0...n
123
- for i in (0...n)
124
- contador = 0
125
- for j in (0...m)
126
- contador = contador +(self.get(i,j) * matAux.get(i,j))
127
- end
128
- end
129
- mat.set(i,j, contador)
110
+ prod = Array.new(matriz.size - 1,0)
111
+ for i in 0...matriz[0].size
112
+ prod[i] = Array.new(matAux.matriz.size,0)
113
+ for j in 0...matAux.matriz.size
114
+ for pos in 0...matriz.size
115
+ prod[i][j] = prod[i][j] + (matriz[i][pos] * matAux.matriz[pos][j])
116
+ end
117
+ end
118
+ end
119
+ Matriz.new(prod)
120
+ end
121
+
122
+ def max
123
+ maximo = 0.to_f
124
+ for i in 0...matriz.size
125
+ for j in 0...matriz[i].size
126
+ if matriz[i][j].to_f > maximo
127
+ maximo = matriz[i][j].to_f
128
+ end
129
+ end
130
130
  end
131
- mat
131
+ maximo
132
132
  end
133
+
134
+ def min
135
+ minimo = 1000
136
+ for i in 0...matriz.size
137
+ for j in 0...matriz[i].size
138
+ if matriz[i][j].to_f < minimo
139
+ minimo = matriz[i][j].to_f
140
+ end
141
+ end
142
+ end
143
+ minimo
144
+ end
145
+
146
+ #def coerce(something)
147
+ # [self, Fraccion.new(something)]
148
+ #end
133
149
 
134
150
  end
135
151
 
data/lib/matriz.rb~ CHANGED
@@ -1,134 +1,151 @@
1
- # Practica 8 Lenguajes y Paradigmas de la programación usando #desarrollo dirigido por pruebas (Test Driven Development - TDD) con
2
- # la herramienta Rspec.
1
+ # Etsii ull grado informática
2
+ # Lenguajes y Paradigmas de la programacion
3
3
  # Pedro Javier Núñez Rodríguez
4
4
  # Constanza Leon Baritussio
5
- #
5
+ # Clase matriz
6
6
 
7
- class Matriz
8
-
7
+ require 'fraccion'
8
+
9
+ class Matriz
10
+
9
11
  include Comparable #Modulo comparable
10
- attr_accessor :matrix, :n, :m
11
- @matrix
12
- @n # filas
13
- @m # columnas
14
-
15
- def initialize(n,m) #Constructor de la clase que inicializa la matriz con los valores que se le pasan por parametros
16
- raise ArgumentError, 'Tamaño de matriz no valido' unless n.is_a? Integer and m.is_a? Integer and n > 0 and m > 0
17
- @n = n
18
- @m = m
19
- @matrix=[]
20
- for i in (0...@n)
21
- @matrix[i]=[]
22
- for j in (0...@m)
23
- @matrix[i][j] = 0
24
- end
25
- end
12
+
13
+ attr_accessor :filas, :columnas, :matriz
14
+
15
+ def initialize(m)
16
+ @filas = m.size
17
+ @columnas = m[0].size
18
+ @matriz = m;
26
19
  end
27
20
 
28
- def set(i, j, valor) #Método para aaceder a los valores i , j y valor de la matriz
29
- raise ArgumentError, 'Indice no valido' unless i.is_a? Integer and i >= 0 and i < @n
30
- raise ArgumentError, 'Indice no valido' unless j.is_a? Integer and j >= 0 and j < @m
31
- raise ArgumentError, 'Valor no numerico' unless valor.is_a? Integer
32
- @matrix[i][j] = valor
21
+ def [](fila, columna)
22
+ @matriz[fila][columna]
33
23
  end
34
-
35
- def get(i, j)
36
- return @matrix[i][j]
24
+
25
+ def []=(fila, columna, valor)
26
+ @matriz[fila][columna] = valor
37
27
  end
38
28
 
39
29
  def to_s #Método encargado de mostrar por consola la matriz de forma matriz[i][j]
40
- s = ""
41
- s += "{"
42
- for i in (0...n)
43
- s += "{"
44
- for j in (0...m)
45
- if j == 0
46
- s += "#{matrix[i][j]}"
47
- else
48
- s += ","
49
- s += "#{matrix[i][j]}"
50
- end
51
- end
52
- s += "}"
53
- end
54
- s += "}"
55
- s
30
+ s = ""
31
+ s += "{"
32
+ for i in (0...filas)
33
+ s += "{"
34
+ for j in (0...columnas)
35
+ if j == 0
36
+ s += "#{matriz[i][j]}"
37
+ else
38
+ s += ","
39
+ s += "#{matriz[i][j]}"
40
+ end
41
+ end
42
+ s += "}"
43
+ end
44
+ s += "}"
45
+ s
56
46
  end
57
47
 
58
48
  def to_f # Método encargado de mostrar por la consola la matriz en formato flotante
59
- s = ""
60
- s = "{"
61
- for i in (0...n)
62
- s += "{"
63
- for j in (0...m)
64
- if j == 0
65
- s += "#{matrix[i][j].to_f}"
66
- else
67
- s += ","
68
- s += "#{matrix[i][j].to_f}"
69
- end
70
- end
71
- s += "}"
72
- end
73
- s += "}"
74
- s
49
+ s = ""
50
+ s = "{"
51
+ for i in (0...filas)
52
+ s += "{"
53
+ for j in (0...columnas)
54
+ if j == 0
55
+ s += "#{matriz[i][j].to_f}"
56
+ else
57
+ s += ","
58
+ s += "#{matriz[i][j].to_f}"
59
+ end
60
+ end
61
+ s += "}"
62
+ end
63
+ s += "}"
64
+ s
75
65
  end
76
66
 
77
67
  def -@ # Sobrecarga del operador - para calcular el opuesto de una matriz
78
- mat = Matriz.new(n,m)
79
- for i in (0...n)
80
- for j in (0...m)
81
- if matrix[i][j] != 0
82
- mat.matrix[i][j] = (matrix[i][j] * -1)
83
- end
84
- end
85
- end
86
- mat
68
+ mat = Matriz.new(self.matriz)
69
+ for i in (0...filas)
70
+ for j in (0...columnas)
71
+ if matriz[i][j] != 0
72
+ mat.matriz[i][j] = (matriz[i][j] * -1)
73
+ end
74
+ end
75
+ end
76
+ mat
87
77
  end
88
78
 
89
79
  def * num #Método que multiplica un número por una matriz
90
- mat = Matriz.new(n,m)
91
- for i in (0...n)
92
- for j in (0...m)
93
- mat.set(i,j,matrix[i][j] * num)
94
- end
95
- end
96
- mat
80
+ mat = Matriz.new(self.matriz)
81
+ for i in (0...filas)
82
+ for j in (0...columnas)
83
+ mat.matriz[i][j] = (matriz[i][j] * num)
84
+ end
85
+ end
86
+ mat
97
87
  end
98
88
 
99
89
  def + matAux #Método para hacer la suma de de matrices
100
- mat = Matriz.new(n,m)
101
- for i in (0...n)
102
- for j in (0...m)
103
- mat.set(i,j,matrix[i][j] + matAux.matrix[i][j])
104
- end
90
+ mat = Matriz.new(matAux.matriz)
91
+ for i in (0...filas)
92
+ for j in (0...columnas)
93
+ mat.matriz[i][j] = (matriz[i][j] + matAux.matriz[i][j])
94
+ end
105
95
  end
106
96
  mat
107
97
  end
108
98
 
109
99
  def - matAux #Método para hacer la resta de matrices
110
- mat = Matriz.new(n,m)
111
- for i in (0...n)
112
- for j in (0...m)
113
- mat.set(i,j,matrix[i][j] - matAux.matrix[i][j])
114
- end
100
+ mat = Matriz.new(matAux.matriz)
101
+ for i in (0...filas)
102
+ for j in (0...columnas)
103
+ mat.matriz[i][j] = (matriz[i][j] - matAux.matriz[i][j])
104
+ end
115
105
  end
116
106
  mat
117
107
  end
118
108
 
119
109
  def * matAux #Método para hacer la multiplicacion de matrices
120
- mat = Matriz.new(n,m)
121
- for k in 0...n
122
- for i in (0...n)
123
- contador = 0
124
- for j in (0...m)
125
- contador = contador +(self.get(i,j) * matAux.get(i,j))
126
- end
127
- end
128
- mat.set(i,j, contador)
110
+ prod = Array.new(matriz.size - 1,0)
111
+ for i in 0...matriz[0].size
112
+ prod[i] = Array.new(matAux.matriz.size,0)
113
+ for j in 0...matAux.matriz.size
114
+ for pos in 0...matriz.size
115
+ prod[i][j] = prod[i][j] + (matriz[i][pos] * matAux.matriz[pos][j])
116
+ end
117
+ end
118
+ end
119
+ Matriz.new(prod)
120
+ end
121
+
122
+ def max
123
+ maximo = 0.to_f
124
+ for i in 0...matriz.size
125
+ for j in 0...matriz[i].size
126
+ if matriz[i][j].to_f > maximo
127
+ maximo = matriz[i][j].to_f
128
+ end
129
+ end
129
130
  end
130
- mat
131
+ maximo
131
132
  end
133
+
134
+ def min
135
+ minimo = 1000
136
+ for i in 0...matriz.size
137
+ for j in 0...matriz[i].size
138
+ if matriz[i][j].to_f < minimo
139
+ minimo = matriz[i][j].to_f
140
+ end
141
+ end
142
+ end
143
+ minimo
144
+ end
145
+
146
+ #def coerce(something)
147
+ # [self, Fraccion.new(something)]
148
+ #end
132
149
 
133
150
  end
134
151