-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseMatrixPrePost.rb
More file actions
249 lines (213 loc) · 8.83 KB
/
SparseMatrixPrePost.rb
File metadata and controls
249 lines (213 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# ensures that the the value added is a spare matrix and dimensions of the two matrices are the same
def pre_sparse_matrix_addition(other)
assert(other.respond_to?(:checkSum), 'not adding by a sparse matrix')
# assert_equal self.dimension.length, other.getDimension.length, "matrices need to be same dimension"
assert_equal self.dimension, other.getDimension, "dimension sizes are different"
invariants
end
# ensures that that the sparse matrix is added correctly
def post_sparse_matrix_addition(other, result)
assert(result.respond_to?(:checkSum), 'not returning a matrix')
expected = self.checkSum{|sum, value| sum + value} + other.checkSum{|sum, value| sum + value}
actual = result.checkSum{|sum, value| sum + value}
assert_equal expected, actual, 'matrices added wrong'
assert_equal self.getDimension, result.getDimension, 'returned matrix of different dimension'
invariants
end
# ensures that the value taken in is a sparse matrix and the dimensions of the two matrices are the same
def pre_sparse_matrix_subtraction(other)
assert(other.respond_to?(:checkSum), 'not adding by a sparse matrix')
# assert_equal self.dimension.length, m.getDimension.length, "matrices need to be same dimension"
assert_equal self.dimension, other.getDimension, "dimension sizes are different"
invariants
end
# ensures that the sparse matrix is subtracted correctly
def post_sparse_matrix_subtraction(other,result)
assert(result.respond_to?(:checkSum), 'not returning a matrix')
expected = self.checkSum{|sum, value| sum + value} - other.checkSum{|sum, value| sum + value}
actual = result.checkSum{|sum, value| sum + value}
assert_equal expected, actual, 'matrices subtracted wrong'
assert_equal self.getDimension, result.getDimension, 'returned matrix of different dimension'
invariants
end
# ensures that the value inputted is a a Numeric
def pre_scalar_subtraction(other)
assert (other.respond_to? (:round)), "Not a number"
invariants
end
# ensures that the scalar subtraction is correct. Also makes sure that if the value to be substracted
# is zero, then the result matrix is the same as the original matrix
def post_scalar_subtraction(other,result)
assert(result.respond_to?(:checkSum), 'not returning a matrix')
expected = yield other,self
actual = result.checkSum{|sum, value| sum + value}
assert_equal expected , actual, 'subtracted wrong by scalar'
assert_equal self.getDimension, result.getDimension, 'returned matrix of different dimension'
invariants
end
# ensures that the value inputted is a Numeric value
def pre_scalar_addition(other)
assert (other.respond_to? (:round)), "Not a number"
invariants
end
# ensures that the scalar addition is correct. If the value is zero then the matrix should be the same
def post_scalar_addition(other,result)
assert(result.respond_to?(:checkSum), 'not returning a matrix')
expected = yield other,self
actual = result.checkSum{|sum, value| sum + value}
assert_equal expected , actual, 'added wrong by scalar'
assert_equal self.getDimension, result.getDimension, 'returned matrix of different dimension'
invariants
end
# ensures that the value inputted is a sparse matrix and the dimensions is 2 (for now)
def pre_sparse_matrix_multiplication(other)
assert(other.respond_to?(:getDimension), 'not adding by a sparse matrix')
# assert_equal self.dimension.length, m.getDimension.length, "matrices need to be same dimension"
assert_equal self.getDimension[1], other.getDimension[0], "dimension sizes are incorrect"
invariants
end
# ensures that the multiplication of a sparse matrix is multiplied correctly
def post_sparse_matrix_multiplication(other,result)
assert(result.respond_to?(:getDimension), 'not returning a matrix')
assert_equal self.getDimension[0], result.getDimension[0], 'returned matrix of different x dimension'
assert_equal other.getDimension[1], result.getDimension[1], 'returned matrix of different y dimension'
invariants
end
# ensures that the value inputted is a numeric
def pre_scalar_multiplication(other)
assert (other.respond_to? (:round)), "Not a number"
invariants
end
# ensures that the multiplication of a scalar is correct and is always a sparse matrix
def post_scalar_multiplication(other, result)
assert(result.respond_to?(:checkSum), 'not returning a matrix')
expected = yield other,self
actual = result.checkSum{|sum, value| sum + value}
assert_equal expected , actual, 'multiplied wrong by scalar'
assert_equal self.getDimension, result.getDimension, 'returned matrix of different dimension'
invariants
end
# ensures that the value inputted is a sparse matrix
def pre_sparse_matrix_division(other)
assert (other.respond_to? (:getDimension)), "Not a SparseMatrix"
assert_block ('matrix is not square') do
other.getDimension.all? {|dimensionSize| dimensionSize == other.getDimension[0]}
end
assert_equal self.getDimension[1], other.getDimension[0], "dimension sizes are incorrect"
assert(other.det != 0, 'second matrix has det of 0!')
invariants
end
# ensures that the division of the matrix is implemented correctly by taking the inverse of the matrix
def post_sparse_matrix_division(other,result)
assert(result.respond_to?(:getDimension), 'not returning a matrix')
assert_equal self.getDimension[0], result.getDimension[0], 'returned matrix of different x dimension'
assert_equal other.getDimension[1], result.getDimension[1], 'returned matrix of different y dimension'
invariants
end
# ensures that the number inputted is a numeric
def pre_scalar_division(other)
assert (other.respond_to? (:round)), "Not a number"
assert (other!=0), "Division by a zero"
invariants
end
# ensures that the division of the scalar is done successfully
def post_scalar_division(other, result)
assert(result.respond_to?(:getDimension), 'not returning a matrix')
assert_equal self.getDimension, result.getDimension, 'returned matrix of different dimension'
invariants
end
# checks the preconditions of the determinant.
def preDeterminant()
assert_block ('matrix is not square') do
self.getDimension.all? {|dimensionSize| dimensionSize == self.getDimension[0]}
end
invariants
end
# ensures that the determinant is correct by taking the transpose
def postDeterminant(result)
assert(result.respond_to? (:round), 'result is not an Integer')
# assert_equal(result, (self.transpose).determinant, 'det didnt work')
invariants
end
# ensures that it is a sparse matrix
def preTranspose()
assert(self.respond_to? (:getDimension), 'not a sparse matrix')
invariants
end
# ensures that the result of the transpose is correct by retransposing it
def postTranspose(result)
assert(result.respond_to? (:getDimension), 'not a matrix')
assert_equal(self.getDimension, result.getDimension.reverse, 'dimensions are not correct')
assert_equal(self.checkSum{|sum, value| sum + value}, result.checkSum{|sum, value| sum + value}, 'transpose failed')
# assert_equal(self, result.transpose, 'transpose not correct')
invariants
end
# ensures that the matrix is a sparse matrix
def preInverse(m)
assert_block ("#{m} matrix is not square") do
m.getDimension.all? {|dimensionSize| dimensionSize == m.getDimension[0]}
end
assert(self.det != 0)
invariants
end
# ensures that the inverse is done correctly
def postInverse(m,result)
assert(result.respond_to? (:getDimension), 'not a matrix')
assert_equal(m.getDimension, result.getDimension, 'dimension are not the same')
# assert_equal(identityMatrix, self * result, 'inverse did not work')
invariants
end
# ensures that it is a sparse matrix
def pre_power(other)
assert(other.respond_to? (:round), "Not a number")
invariants
end
# ensures that the result of the power operator is correct
def post_power(result)
assert(result.respond_to? (:getDimension), 'not a matrix')
assert_equal(self.getDimension, result.getDimension, 'dimensions not the same')
invariants
end
def pre_insert_at(position,value)
invariants
assert_equal position.length, @dimension.length,"Invalid position."
for i in 0..@dimension.length-1 do
assert(@dimension[i]>position[i], "Invalid position in matrix")
end
assert (value!=0), "Inserting a zero"
end
def post_insert_at(position,value)
invariants
assert_equal value,@values_hash[position],"Value is not inserted. FAILED."
end
def preCheckSum(matrix)
assert(matrix.respond_to? (:get_sparse_matrix_hash))
invariants
end
def postCheckSum(sum)
assert sum.respond_to? (:round)
invariants
end
def pre_init_dim(*args)
assert(args.length>1,"not right length")
end
def pre_init_array(arg)
assert_respond_to(arg,:to_a,"Not an array")
end
def pre_init_matrix(*args)
assert_equal 1,args.length,"Not the right size"
m=args[0]
assert_respond_to(m,:to_a)
end
def pre_init_sparse_matrix(*args)
assert_equal 1,args.length,"Not the right size"
sm=args[0]
assert_respond_to(sm,:get_sparse_matrix_hash)
end
def pre_init_hash(*rest_of_args,input_hash)
assert_respond_to(input_hash, :length)
assert_respond_to(input_hash, :hash)
rest_of_args.each do |arg|
assert_respond_to(arg,:to_i)
end
end