csrmatrix 1.0.0 → 1.0.1
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 +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +3 -1
- data/README.md +106 -1
- data/csrmatrix-1.0.0.gem +0 -0
- data/lib/csrmatrix/arithmetic.rb +213 -117
- data/lib/csrmatrix/decompositions.rb +22 -20
- data/lib/csrmatrix/exceptions.rb +18 -5
- data/lib/csrmatrix/functions.rb +52 -16
- data/lib/csrmatrix/helpers.rb +23 -11
- data/lib/csrmatrix/mcontracts.rb +9 -0
- data/lib/csrmatrix/operations.rb +60 -18
- data/lib/csrmatrix/properties.rb +219 -96
- data/lib/csrmatrix/version.rb +1 -1
- data/lib/csrmatrix.rb +204 -94
- metadata +4 -2
data/lib/csrmatrix.rb
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
require "csrmatrix/version"
|
2
|
-
require "csrmatrix/properties"
|
3
2
|
require "csrmatrix/arithmetic"
|
3
|
+
require "csrmatrix/properties"
|
4
4
|
require "csrmatrix/functions"
|
5
5
|
require "csrmatrix/decompositions"
|
6
6
|
require "csrmatrix/operations"
|
7
7
|
require "csrmatrix/helpers"
|
8
8
|
require "csrmatrix/exceptions"
|
9
|
+
require "contracts"
|
9
10
|
|
10
11
|
module CsrMatrix
|
11
12
|
# The current website ref. Used for verificationn of rb systems.
|
12
|
-
Url = "https://github.com/Team-Aqua/Matrix-Library/"
|
13
|
-
|
13
|
+
Url = "https://github.com/Team-Aqua/Matrix-Library/"
|
14
|
+
|
15
|
+
end# CsrMatrix
|
16
|
+
|
14
17
|
|
15
18
|
# General code convention in this manner - generate documentation via 'rdoc lib'.
|
16
19
|
class TwoDMatrix
|
@@ -22,13 +25,29 @@ class TwoDMatrix
|
|
22
25
|
include CsrMatrix::Decompositions
|
23
26
|
include CsrMatrix::Helpers
|
24
27
|
include CsrMatrix::Exceptions
|
28
|
+
include CsrMatrix::MContracts
|
29
|
+
include Contracts::Core
|
30
|
+
include Contracts::Invariants
|
31
|
+
|
32
|
+
C = Contracts
|
33
|
+
invariant(@rows) { @rows >= 0}
|
34
|
+
invariant(@columns) { @columns >= 0}
|
35
|
+
invariant(:val) {self.val != nil}
|
25
36
|
|
26
37
|
# The current website ref. Used for verification of rb systems.
|
27
38
|
Url = "https://github.com/Team-Aqua/Matrix-Library/"
|
28
39
|
attr_reader :row_ptr, :col_ind, :val, :rows, :columns, :ndim
|
29
40
|
|
30
41
|
# Blank setup; setup module.
|
31
|
-
def initialize()
|
42
|
+
def initialize()
|
43
|
+
# invariant
|
44
|
+
# @nonzero_count.is_a?(array) and @nonzero_count.count() >= 0
|
45
|
+
# @row_pointer.is_a?(array) and @row_pointer.count() >= 0
|
46
|
+
# @col_ind.is_a?(array) and @col_ind.count() >= 0
|
47
|
+
# @val.is_a?(array) and @val.count() >= 0
|
48
|
+
# @rows >= 0
|
49
|
+
# @columns >= 0
|
50
|
+
# @dimension == 2
|
32
51
|
@nonzero_count = nil
|
33
52
|
@row_ptr = nil
|
34
53
|
@col_ind = nil
|
@@ -36,37 +55,81 @@ class TwoDMatrix
|
|
36
55
|
@rows = 0
|
37
56
|
@columns = 0
|
38
57
|
@ndim = 2
|
39
|
-
end
|
58
|
+
end # initialize
|
40
59
|
|
41
60
|
##
|
42
61
|
# SPARSE MATRIX ATTRIBUTE OPERATORS
|
43
62
|
# matrix attributes and overloaded operators
|
44
63
|
#
|
64
|
+
def is_invariant?
|
65
|
+
if @val == nil
|
66
|
+
raise InvariantError.new, "Empty Matrix"
|
67
|
+
return false
|
68
|
+
end
|
69
|
+
return true
|
70
|
+
end
|
45
71
|
|
46
72
|
# equals override for matrix operations
|
47
73
|
def ==(o)
|
74
|
+
# equals overide to check if object o equals self
|
75
|
+
# pre o, self
|
76
|
+
# post true if o is_a csrmatrix and o == self
|
48
77
|
o.class == self.class && o.state == state
|
49
|
-
end
|
78
|
+
end # ==(o)
|
50
79
|
|
51
80
|
# FIXME: convert to protected value
|
81
|
+
# Contract C::None => C::ArrayOf[ArrayOf[C::Num],ArrayOf[C::Nat],ArrayOf[C::Nat],C::Nat,C::Nat,C::Nat]
|
52
82
|
def state
|
83
|
+
# returns the current state of the csrmatrix
|
84
|
+
# pre self
|
85
|
+
# post [@value, @row_pointer, @column_index, @rows, @columns, @dimension]
|
53
86
|
[@val, @row_ptr, @col_ind, @rows, @columns, @ndim]
|
54
|
-
end
|
87
|
+
end # state
|
55
88
|
|
56
89
|
# Finds column and row value of an array.
|
90
|
+
Contract C::None => C::ArrayOf[C::Nat]
|
57
91
|
def dimensions()
|
92
|
+
is_invariant?
|
93
|
+
# returns the dimensions of the csrmatrix
|
58
94
|
return [@rows, @columns]
|
59
|
-
end
|
95
|
+
end # dimensions
|
60
96
|
|
97
|
+
Contract C::None => C::Bool
|
61
98
|
def square?
|
99
|
+
# returns whether or not the system is square
|
100
|
+
is_invariant?
|
62
101
|
return self.rows == self.columns
|
63
|
-
end
|
102
|
+
end # square?
|
103
|
+
|
104
|
+
Contract C::Nat, C::Nat => C::Bool
|
105
|
+
def checkInputBounds(row, col)
|
106
|
+
# checks whether or not the index searched is within bounds
|
107
|
+
if row > @rows
|
108
|
+
raise IndexOutOfRangeException.new, "Row index too large"
|
109
|
+
return false
|
110
|
+
elsif col > @columns
|
111
|
+
raise IndexOutOfRangeException.new, "Column index too large"
|
112
|
+
return false
|
113
|
+
elsif row < 0
|
114
|
+
raise IndexOutOfRangeException.new, "Row index too small"
|
115
|
+
return false
|
116
|
+
elsif col < 0
|
117
|
+
raise IndexOutOfRangeException.new, "Column index too small"
|
118
|
+
return false
|
119
|
+
else
|
120
|
+
return true
|
121
|
+
end
|
122
|
+
end # checkInputBounds
|
64
123
|
|
65
124
|
##
|
66
125
|
# MATRIX DECOMPOSITION FUNCTIONS
|
67
126
|
#
|
68
127
|
|
128
|
+
Contract C::None => C::ArrayOf[C::ArrayOf[C::Num]]
|
69
129
|
def decompose()
|
130
|
+
# decompose the matrix into an array
|
131
|
+
# pre csrmatrix
|
132
|
+
# post array from the csrmartix
|
70
133
|
res = Array.new(@rows) { Array.new(@columns, 0) }
|
71
134
|
row_counter = 0
|
72
135
|
row_idx = 0
|
@@ -78,7 +141,13 @@ class TwoDMatrix
|
|
78
141
|
row_idx += 1
|
79
142
|
end
|
80
143
|
return res
|
81
|
-
end
|
144
|
+
end # decompose
|
145
|
+
|
146
|
+
Contract C::None => Matrix
|
147
|
+
def decomp_to_matrix()
|
148
|
+
@matrix = Matrix.rows(self.decompose())
|
149
|
+
return @matrix
|
150
|
+
end # decomp_to_matrix
|
82
151
|
|
83
152
|
##
|
84
153
|
# MATRIX GENERATION FUNCTIONS
|
@@ -86,94 +155,27 @@ class TwoDMatrix
|
|
86
155
|
#
|
87
156
|
|
88
157
|
# Builds when given a 2d array to CSR
|
158
|
+
Contract C::ArrayOf[C::ArrayOf[C::Num]] => C::Any
|
89
159
|
def build_from_array(array)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
dimensions = convert_to_csr(array)
|
94
|
-
@columns = dimensions[0]
|
95
|
-
@rows = dimensions[1]
|
96
|
-
nonzero_count = dimensions[2] # FIXME: consider removing
|
97
|
-
@val = dimensions[3]
|
98
|
-
@row_ptr = dimensions[4]
|
99
|
-
@col_ind = dimensions[5]
|
100
|
-
return true
|
101
|
-
end
|
102
|
-
raise MatrixDimException.new, "Invalid row/column pairs imported."
|
103
|
-
return false
|
104
|
-
end
|
105
|
-
raise MatrixDimException.new, "Invalid dimensions."
|
160
|
+
#Contracts: Pre
|
161
|
+
if !same_sublength(array)
|
162
|
+
raise MatrixDimException.new, "Invalid row/column pairs imported."
|
106
163
|
return false
|
107
164
|
end
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
# imports a matrix from a matrix library
|
112
|
-
def build_from_matrix(matrix)
|
113
|
-
if matrix.is_a?(Matrix)
|
114
|
-
build_from_array(matrix.to_a())
|
115
|
-
return true
|
116
|
-
end
|
117
|
-
raise MatrixTypeException.new, "Wrong type convert to matrix."
|
118
|
-
end
|
119
|
-
|
120
|
-
# builds a matrix given its rows
|
121
|
-
def build_from_rows(array)
|
122
|
-
build_from_array(array)
|
123
|
-
self.transpose()
|
124
|
-
end
|
125
|
-
|
126
|
-
|
127
|
-
# builds a matrix given its columns ;; redirect to array build
|
128
|
-
def build_from_columns(array)
|
129
|
-
self.build_from_array(array)
|
130
|
-
end
|
131
|
-
|
132
|
-
# generates an identity matrix
|
133
|
-
def build_identity_matrix(size)
|
134
|
-
# FIXME: test code: replace with CSR identity gen
|
135
|
-
if size.is_a?(Numeric)
|
136
|
-
self.build_from_array(Matrix.identity(size).to_a())
|
137
|
-
return true
|
138
|
-
end
|
139
|
-
raise MatrixTypeException.new, "Wrong type convert to matrix."
|
140
|
-
return false
|
141
|
-
end
|
142
|
-
|
143
|
-
# generates a zero matrix
|
144
|
-
def build_zero_matrix(rows, columns = rows)
|
145
|
-
# FIXME: test code: replace with CSR zero gen
|
146
|
-
if rows.is_a?(Numeric) && columns.is_a?(Numeric)
|
147
|
-
self.build_from_array(Matrix.zero(rows, columns).to_a())
|
148
|
-
return true
|
149
|
-
end
|
150
|
-
raise MatrixTypeException.new, "Wrong type convert to matrix."
|
151
|
-
return false
|
152
|
-
end
|
153
|
-
|
154
|
-
# Builds array using user-generated CSR values
|
155
|
-
def build_from_csr(row_ptr, col_ind, val, col_siz, row_siz)
|
156
|
-
# generate
|
157
|
-
@val = val
|
158
|
-
@row_ptr = row_ptr
|
159
|
-
@col_ind = col_ind
|
160
|
-
@rows = row_siz
|
161
|
-
@columns = col_siz
|
162
|
-
end
|
163
|
-
|
164
|
-
# ensures that all subarrays are of same length
|
165
|
-
def same_sublength(array)
|
166
|
-
testLength = array[0].length
|
167
|
-
array.each do |subarray|
|
168
|
-
if(subarray.length != testLength)
|
169
|
-
return false
|
170
|
-
end
|
171
|
-
end
|
165
|
+
#END Contracts: Pre
|
166
|
+
@val = [] #Allows Invariant Check to Pass for following method call
|
167
|
+
@columns, @rows, nonzero_count, @val, @row_ptr, @col_ind = convert_to_csr(array)
|
172
168
|
return true
|
173
|
-
end
|
169
|
+
end # build_from_array
|
174
170
|
|
175
|
-
|
171
|
+
# Finds the column count, row count and non-zero values in one loop.
|
172
|
+
Contract C::ArrayOf[C::ArrayOf[C::Num]] => C::Any
|
176
173
|
def convert_to_csr(array)
|
174
|
+
# converts a given array to csr format
|
175
|
+
# pre array
|
176
|
+
|
177
|
+
|
178
|
+
# post csrmatrix from array
|
177
179
|
row_count = 0
|
178
180
|
col_count = 0
|
179
181
|
nonzero_count = 0
|
@@ -198,6 +200,9 @@ class TwoDMatrix
|
|
198
200
|
col_tmp += 1
|
199
201
|
if array[i][x] != 0
|
200
202
|
# use nonzero value in CSR
|
203
|
+
if array[i][x] == nil
|
204
|
+
return false
|
205
|
+
end
|
201
206
|
nonzero_count += 1
|
202
207
|
value_array << array[i][x]
|
203
208
|
col_ind << col_val
|
@@ -212,6 +217,111 @@ class TwoDMatrix
|
|
212
217
|
row_prev_sum = row_val
|
213
218
|
row_ptr << row_prev_sum
|
214
219
|
return [col_count, row_count, nonzero_count, value_array, row_ptr, col_ind]
|
215
|
-
end
|
220
|
+
end # convert_to_csr
|
221
|
+
|
222
|
+
# builds matrix dependent on input
|
223
|
+
Contract String, C::Or[C::ArrayOf[C::Or[C::ArrayOf[C::Num], C::Num]],C::Nat, Matrix], C::Or[nil, Any, C::None] => C::Bool
|
224
|
+
def build(type, data, extra = nil)
|
225
|
+
# builds matrix dependent on input
|
226
|
+
# pre string type
|
227
|
+
# data, dependent type
|
228
|
+
# post generated matrix
|
229
|
+
# boolean depending on build
|
230
|
+
case type
|
231
|
+
when "matrix"
|
232
|
+
self.build_from_matrix(data)
|
233
|
+
when "row", "rows"
|
234
|
+
self.build_from_rows(data)
|
235
|
+
when "array"
|
236
|
+
self.build_from_array(data)
|
237
|
+
when "column", "columns"
|
238
|
+
self.build_from_columns(data)
|
239
|
+
when "identity", "i", "unit"
|
240
|
+
if extra != nil
|
241
|
+
self.build_identity_matrix(data, extra)
|
242
|
+
else
|
243
|
+
self.build_identity_matrix(data)
|
244
|
+
end
|
245
|
+
when "zero"
|
246
|
+
self.build_zero_matrix(data)
|
247
|
+
when "csr"
|
248
|
+
self.build_from_csr(data[0], data[1], data[2], data[3], data[4])
|
249
|
+
else
|
250
|
+
raise MatrixTypeException.new, "Bad build type, no build response."
|
251
|
+
return false;
|
252
|
+
end
|
253
|
+
return true;
|
254
|
+
end
|
255
|
+
|
256
|
+
# imports a matrix from a matrix library
|
257
|
+
Contract Matrix => C::Bool
|
258
|
+
def build_from_matrix(matrix)
|
259
|
+
# builds a csr matrix a ruby matrix
|
260
|
+
build_from_array(matrix.to_a())
|
261
|
+
return true
|
262
|
+
end # build_from_matrix
|
263
|
+
|
264
|
+
# builds a matrix given its rows
|
265
|
+
Contract C::ArrayOf[C::ArrayOf[C::Num]] => C::ArrayOf[C::Num]
|
266
|
+
def build_from_rows(array)
|
267
|
+
# builds a csr matrix from rows
|
268
|
+
build_from_array(array)
|
269
|
+
self.transpose()
|
270
|
+
end # build_from_rows
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
Contract C::ArrayOf[C::ArrayOf[C::Num]] => C::Bool
|
275
|
+
def build_from_columns(array)
|
276
|
+
# builds a matrix given its columns ;; redirect to array build
|
277
|
+
# build a matrix given columns. same implimentation as array build
|
278
|
+
self.build_from_array(array)
|
279
|
+
end # build_from_columns
|
280
|
+
|
281
|
+
# generates an identity matrix
|
282
|
+
Contract C::Nat => true
|
283
|
+
def build_identity_matrix(size)
|
284
|
+
# FIXME: test code: replace with CSR identity gen
|
285
|
+
# generate identity matrix of a given size
|
286
|
+
self.build_from_array(Matrix.identity(size).to_a())
|
287
|
+
end # build_identity_matrix
|
288
|
+
|
289
|
+
# generates a zero matrix
|
290
|
+
Contract C::Nat, C::Nat => true
|
291
|
+
def build_zero_matrix(rows, columns = rows)
|
292
|
+
# FIXME: test code: replace with CSR zero gen
|
293
|
+
# generate a matrix with all values equaling zero for a given number of rows and columns
|
294
|
+
self.build_from_array(Matrix.zero(rows, columns).to_a())
|
295
|
+
return true
|
296
|
+
end # build_zero_matrix
|
297
|
+
|
298
|
+
# Builds array using user-generated CSR values
|
299
|
+
Contract C::ArrayOf[C::Nat], C::ArrayOf[C::Nat], C::ArrayOf[C::Num], C::Nat, C::Nat => TwoDMatrix
|
300
|
+
def build_from_csr(row_ptr, col_ind, val, col_siz, row_siz)
|
301
|
+
# generate an array from user generated csr values
|
302
|
+
@val = val
|
303
|
+
@row_ptr = row_ptr
|
304
|
+
@col_ind = col_ind
|
305
|
+
@rows = row_siz
|
306
|
+
@columns = col_siz
|
307
|
+
self
|
308
|
+
end # build_from_csr
|
309
|
+
|
310
|
+
# ensures that all subarrays are of same length
|
311
|
+
|
312
|
+
#FIXME: Could be a contract in itself
|
313
|
+
# Contract C::ArrayOf[C::ArrayOf[C::Num]] => C::Bool #Causes Invariant issues
|
314
|
+
def same_sublength(array)
|
315
|
+
# ensures that all sub arrays have the same length
|
316
|
+
testLength = array[0].length
|
317
|
+
array.each do |subarray|
|
318
|
+
if(subarray.length != testLength)
|
319
|
+
return false
|
320
|
+
end
|
321
|
+
end
|
322
|
+
return true
|
323
|
+
end #same_sublength
|
324
|
+
|
325
|
+
|
216
326
|
|
217
|
-
end
|
327
|
+
end # TwoDMatrix
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csrmatrix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anson Li, Quentin Lautischer, Aaron Plamondon
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- bin/console
|
74
74
|
- bin/matrix
|
75
75
|
- bin/setup
|
76
|
+
- csrmatrix-1.0.0.gem
|
76
77
|
- csrmatrix.gemspec
|
77
78
|
- lib/csrmatrix.rb
|
78
79
|
- lib/csrmatrix/arithmetic.rb
|
@@ -80,6 +81,7 @@ files:
|
|
80
81
|
- lib/csrmatrix/exceptions.rb
|
81
82
|
- lib/csrmatrix/functions.rb
|
82
83
|
- lib/csrmatrix/helpers.rb
|
84
|
+
- lib/csrmatrix/mcontracts.rb
|
83
85
|
- lib/csrmatrix/operations.rb
|
84
86
|
- lib/csrmatrix/properties.rb
|
85
87
|
- lib/csrmatrix/version.rb
|