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.
@@ -1,5 +1,169 @@
1
- require "matrizdispersascp/version"
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
+ # Clases MatrizDispersa y MatrizDensa
6
+
7
+ require 'matrizdispersascp/version'
8
+ require 'matriz'
9
+ require 'fraccion'
2
10
 
3
11
  module Matrizdispersascp
4
- # Your code goes here...
12
+
13
+ class MatrizDensa < Matriz
14
+
15
+ @matrix
16
+ @n #filas
17
+ @m #columnas
18
+
19
+ def initialize(m)
20
+ @filas = m.size
21
+ @columnas = m[0].size
22
+ @matriz = m;
23
+ end
24
+
25
+ end
26
+
27
+ class MatrizDispersa < Matriz
28
+
29
+ attr_reader :matriz, :filas, :columnas
30
+
31
+ def initialize(matriz)
32
+ @matriz = matriz
33
+ @filas = matriz.size
34
+ @columnas = matriz.size
35
+
36
+ end
37
+
38
+ def to_s
39
+ fil = 0
40
+ st = "{"
41
+ while fil < filas
42
+ col = 0
43
+ st += "{"
44
+ while col < columnas
45
+ if @matriz[fil] != nil # Hay datos en la fila
46
+ if @matriz[fil].has_key?(col)
47
+ st += "#{@matriz[fil][col].to_s}"
48
+ else
49
+ st += "0"
50
+ end
51
+ else
52
+ st += "0"
53
+ end
54
+ if (col + 1) < columnas then
55
+ st += ", "
56
+ end
57
+ col += 1
58
+ end
59
+ if (fil + 1) < filas then
60
+ st += "}"
61
+ end
62
+ st += "}"
63
+ fil += 1
64
+ end
65
+ st += "}"
66
+ st
67
+ end
68
+
69
+ def to_f
70
+ flotante = Array.new(matriz.size - 1)
71
+ for i in 0...matriz.size
72
+ # Hay datos en la fila
73
+ if matriz[i] != nil
74
+ flotante[i] = Hash.new()
75
+ matriz[i].each do |key, value|
76
+ flotante[i][key] = matriz[i][key].to_f
77
+ end
78
+ end
79
+ end
80
+ MatrizDispersa.new(flotante)
81
+ end
82
+
83
+ # Suma de matrices
84
+ def +(o)
85
+ suma = Array.new(matriz.size - 1)
86
+ for i in 0...matriz.size
87
+ if (matriz[i] != nil or o.matriz[i] != nil)
88
+ suma[i] = Hash.new()
89
+ case true
90
+ when (matriz[i] != nil and o.matriz[i] != nil)
91
+ suma[i] = matriz[i]
92
+ o.matriz[i].each do |key, value|
93
+ if suma[i].has_key?(key)
94
+ suma[i][key] = suma[i][key] + o.matriz[i][key]
95
+ else
96
+ suma[i][key] = o.matriz[i][key]
97
+ end
98
+ end
99
+ when matriz[i] != nil
100
+ suma[i] = matriz[i]
101
+ when o.matriz[i] != nil
102
+ suma[i] = o.matriz[i]
103
+ end
104
+ end
105
+ end
106
+ MatrizDispersa.new(suma)
107
+ end
108
+
109
+ # Resta de matrices
110
+ def -(o)
111
+ resta = Array.new(matriz.size - 1)
112
+ for i in 0...matriz.size
113
+ if (matriz[i] != nil or o.matriz[i] != nil)
114
+ resta[i] = Hash.new()
115
+ case true
116
+ when (matriz[i] != nil and o.matriz[i] != nil)
117
+ resta[i] = matriz[i]
118
+ o.matriz[i].each do |key, value|
119
+ if resta[i].has_key?(key)
120
+ resta[i][key] = resta[i][key] - o.matriz[i][key]
121
+ else
122
+ resta[i][key] = o.matriz[i][key] * -1
123
+ end
124
+ end
125
+ when matriz[i] != nil
126
+ resta[i] = matriz[i]
127
+ when o.matriz[i] != nil
128
+ resta[i] = o.matriz[i]
129
+ resta[i].each do |key, value|
130
+ resta[i][key] = resta[i][key] * -1
131
+ end
132
+ end
133
+ end
134
+ end
135
+ MatrizDispersa.new(resta)
136
+ end
137
+
138
+ # Meodo que calcula el maximo de una matriz dispersa
139
+ def max
140
+ maximo = 0.to_f
141
+ for i in 0...matriz.size
142
+ if matriz[i] != nil
143
+ matriz[i].each do |key, value|
144
+ if matriz[i][key].to_f > maximo
145
+ maximo = matriz[i][key].to_f
146
+ end
147
+ end
148
+ end
149
+ end
150
+ maximo
151
+ end
152
+
153
+ # Metodo que calcula el minimo de una mariz dispersa
154
+ def min
155
+
156
+ minimo = 0.to_f
157
+ for i in 0...matriz.size
158
+ if matriz[i] != nil
159
+ matriz[i].each do |key, value|
160
+ if matriz[i][key].to_f < minimo
161
+ minimo = matriz[i][key].to_f
162
+ end
163
+ end
164
+ end
165
+ end
166
+ minimo
167
+ end
168
+ end
5
169
  end
@@ -0,0 +1,169 @@
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
+ # Clases MatrizDispersa y MatrizDensa
6
+
7
+ require 'matrizdispersascp/version'
8
+ require 'matriz'
9
+ require 'fraccion'
10
+
11
+ module Matrizdispersascp
12
+
13
+ class MatrizDensa < Matriz
14
+
15
+ @matrix
16
+ @n #filas
17
+ @m #columnas
18
+
19
+ def initialize(m)
20
+ @filas = m.size
21
+ @columnas = m[0].size
22
+ @matriz = m;
23
+ end
24
+
25
+ end
26
+
27
+ class MatrizDispersa < Matriz
28
+
29
+ attr_reader :matriz, :filas, :columnas
30
+
31
+ def initialize(matriz)
32
+ @matriz = matriz
33
+ @filas = matriz.size
34
+ @columnas = matriz.size
35
+
36
+ end
37
+
38
+ def to_s
39
+ fil = 0
40
+ st = "{"
41
+ while fil < filas
42
+ col = 0
43
+ st += "{"
44
+ while col < columnas
45
+ if @matriz[fil] != nil # Hay datos en la fila
46
+ if @matriz[fil].has_key?(col)
47
+ st += "#{@matriz[fil][col].to_s}"
48
+ else
49
+ st += "0"
50
+ end
51
+ else
52
+ st += "0"
53
+ end
54
+ if (col + 1) < columnas then
55
+ st += ", "
56
+ end
57
+ col += 1
58
+ end
59
+ if (fil + 1) < filas then
60
+ st += "}"
61
+ end
62
+ st += "}"
63
+ fil += 1
64
+ end
65
+ st += "}"
66
+ st
67
+ end
68
+
69
+ def to_f
70
+ flotante = Array.new(matriz.size - 1)
71
+ for i in 0...matriz.size
72
+ # Hay datos en la fila
73
+ if matriz[i] != nil
74
+ flotante[i] = Hash.new()
75
+ matriz[i].each do |key, value|
76
+ flotante[i][key] = matriz[i][key].to_f
77
+ end
78
+ end
79
+ end
80
+ MatrizDispersa.new(flotante)
81
+ end
82
+
83
+ # Suma de matrices
84
+ def +(o)
85
+ suma = Array.new(matriz.size - 1)
86
+ for i in 0...matriz.size
87
+ if (matriz[i] != nil or o.matriz[i] != nil)
88
+ suma[i] = Hash.new()
89
+ case true
90
+ when (matriz[i] != nil and o.matriz[i] != nil)
91
+ suma[i] = matriz[i]
92
+ o.matriz[i].each do |key, value|
93
+ if suma[i].has_key?(key)
94
+ suma[i][key] = suma[i][key] + o.matriz[i][key]
95
+ else
96
+ suma[i][key] = o.matriz[i][key]
97
+ end
98
+ end
99
+ when matriz[i] != nil
100
+ suma[i] = matriz[i]
101
+ when o.matriz[i] != nil
102
+ suma[i] = o.matriz[i]
103
+ end
104
+ end
105
+ end
106
+ MatrizDispersa.new(suma)
107
+ end
108
+
109
+ # Resta de matrices
110
+ def -(o)
111
+ resta = Array.new(matriz.size - 1)
112
+ for i in 0...matriz.size
113
+ if (matriz[i] != nil or o.matriz[i] != nil)
114
+ resta[i] = Hash.new()
115
+ case true
116
+ when (matriz[i] != nil and o.matriz[i] != nil)
117
+ resta[i] = matriz[i]
118
+ o.matriz[i].each do |key, value|
119
+ if resta[i].has_key?(key)
120
+ resta[i][key] = resta[i][key] - o.matriz[i][key]
121
+ else
122
+ resta[i][key] = o.matriz[i][key] * -1
123
+ end
124
+ end
125
+ when matriz[i] != nil
126
+ resta[i] = matriz[i]
127
+ when o.matriz[i] != nil
128
+ resta[i] = o.matriz[i]
129
+ resta[i].each do |key, value|
130
+ resta[i][key] = resta[i][key] * -1
131
+ end
132
+ end
133
+ end
134
+ end
135
+ MatrizDispersa.new(resta)
136
+ end
137
+
138
+ # Meodo que calcula el maximo de una matriz dispersa
139
+ def max
140
+ maximo = 0.to_f
141
+ for i in 0...matriz.size
142
+ if matriz[i] != nil
143
+ matriz[i].each do |key, value|
144
+ if matriz[i][key].to_f > maximo
145
+ maximo = matriz[i][key].to_f
146
+ end
147
+ end
148
+ end
149
+ end
150
+ maximo
151
+ end
152
+
153
+ # Minimo de una matriz dispersa
154
+ def min
155
+
156
+ minimo = 0.to_f
157
+ for i in 0...matriz.size
158
+ if matriz[i] != nil
159
+ matriz[i].each do |key, value|
160
+ if matriz[i][key].to_f < minimo
161
+ minimo = matriz[i][key].to_f
162
+ end
163
+ end
164
+ end
165
+ end
166
+ minimo
167
+ end
168
+ end
169
+ end
@@ -1,3 +1,3 @@
1
1
  module Matrizdispersascp
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,140 @@
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
+ # Fichero de expectativas para las clases MatrizDensa y MatrizDispersa
6
+
7
+ require 'spec_helper'
8
+ require 'matrizdispersascp'
9
+ include Matrizdispersascp
10
+
11
+ describe "Pruebas Matrices" do
12
+ #############################################################################
13
+ describe "Matrices densas" do # Expectativas para la clase matrices densas
14
+ #############################################################################
15
+ @matrizA
16
+ @matrizB
17
+ before :each do
18
+
19
+ # Matrices densas
20
+ @matrizA = MatrizDensa.new([[1, 1], [2, 2]])
21
+ @matrizB = MatrizDensa.new([[1, 1], [2, 2]])
22
+
23
+ end
24
+
25
+ describe "Metodos para la comprobacion de la inicialización y asignacion de valores" do
26
+
27
+ it "Se inicializa correctamente la matriz " do # Comprobamos que todas las casillas estan a cero cuando se crea el objeto
28
+ @matrizA.matriz[0][0].should == 1
29
+ @matrizA.matriz[1][0].should == 2
30
+ end
31
+
32
+ it "Asignamos valores a posiciones de la matriz" do
33
+ @matrizA[1,1] = 4
34
+ @matrizA[0,1] = 2
35
+ @matrizA.matriz[1][1].should == 4
36
+ @matrizA.matriz[0][1].should == 2
37
+ end
38
+ end
39
+
40
+ describe "Metodos para el cambio de formato a string y a flotante" do
41
+
42
+ it "La matriz se muestra en formato string" do
43
+ @matrizA.to_s.should == "{{1,1}{2,2}}"
44
+ end
45
+
46
+ it "La matriz se muestra en formato flotante" do
47
+ @matrizA.to_f.should == "{{1.0,1.0}{2.0,2.0}}"
48
+ end
49
+ end
50
+
51
+ describe "Metodo para la negacion" do
52
+
53
+ it "La matriz negada con el simbolo -" do
54
+ @matrizC = -@matrizA
55
+ @matrizC.to_s.should == "{{-1,-1}{-2,-2}}"
56
+ end
57
+ end
58
+
59
+ describe "Metodos para la comprobacion de operaciones entre matrices" do
60
+
61
+ it "Se suman las matrices" do
62
+ @matrizC = (@matrizA + @matrizB)
63
+ @matrizC.to_s.should == "{{2,2}{4,4}}"
64
+ end
65
+
66
+ it "Se restan las matrices" do
67
+ @matrizC = (@matrizA - @matrizB)
68
+ @matrizC.to_s.should == "{{0,0}{0,0}}"
69
+ end
70
+
71
+ it "Se multiplican las mastrices" do
72
+ @matrizC = (@matrizA * @matrizB)
73
+ @matrizC.to_s.should == "{{3,3}{6,6}}"
74
+ end
75
+ end
76
+
77
+ describe "Metodos para calcular maximos y minimos" do
78
+
79
+ it "Maximo de una matriz" do
80
+ @matrizA.max.should == 2.0
81
+ end
82
+
83
+ it "Minimo de una matriz" do
84
+ @matrizA.min.should == 1.0
85
+ end
86
+ end
87
+ end
88
+
89
+ ###############################################################################
90
+ describe "Matrices dispersas" do # Expectativas para la clase matrices dispersas
91
+ ###############################################################################
92
+ @matrizA
93
+ @matrizB
94
+ before :each do
95
+
96
+ # Matrices dispersas
97
+ mat1 = [nil, {1 =>3}]
98
+ mat2 = [nil, {1 =>4}]
99
+ @matrizA = MatrizDispersa.new(mat1)
100
+ @matrizB = MatrizDispersa.new(mat2)
101
+ end
102
+
103
+ describe "Metodos para el cambio de formato a string y a flotante" do
104
+
105
+ it "La matriz se muestra en formato string" do
106
+ @matrizA.to_s.should == "{{0, 0}}{0, 3}}"
107
+ @matrizB.to_s.should == "{{0, 0}}{0, 4}}"
108
+ end
109
+
110
+ it "La matriz se muestra en formato flotante" do
111
+ @matrizA.to_f.should.to_s == MatrizDispersa.new([nil,{1 =>3.0}]).to_f.to_s
112
+ end
113
+
114
+ end
115
+
116
+ describe "Metodo para la comprobacion de operaciones aritmeticas" do
117
+ it "se suman dos matrices dispersas" do
118
+ (@matrizA + @matrizA).to_s.should == "{{0, 0}}{0, 6}}"
119
+
120
+ end
121
+
122
+ it "Se restan dos matrices dispersas" do
123
+ (@matrizA - @matrizB).to_s.should == "{{0, 0}}{0, -1}}"
124
+ end
125
+ end
126
+
127
+ describe "Metodo para la comprobacion de maximos y minimos" do
128
+ it "se calcula el maximo de una matriz" do
129
+ @matrizA.max == 3
130
+ end
131
+
132
+ it "Se calcula el minimo de una matriz" do
133
+ @matrizB.min == 0
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+
140
+