csrmatrix 1.0.1 → 1.1.0
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.lock +1 -1
- data/csrmatrix.gemspec +1 -1
- data/lib/csrmatrix.rb +34 -15
- data/lib/csrmatrix/exceptions.rb +3 -0
- data/lib/csrmatrix/functions.rb +10 -2
- data/lib/csrmatrix/operations.rb +109 -61
- data/lib/csrmatrix/version.rb +1 -1
- data/pkg/csrmatrix-1.0.1.gem +0 -0
- metadata +3 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ca96a6fc0d6250a281a09c47ae5a51a0f3ed466
|
4
|
+
data.tar.gz: 4a1abf788a4a2c8552209bfde11346a829a42608
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a738eedcb306751917db6aef46b49b778c7614467bdebd01d3ed0a5c5149c7d78b29da69c2432840e3dbb1cec8e0a52423ae6ec1eb4b48cb944d601f60c8b55
|
7
|
+
data.tar.gz: 147b40740fd26c79613732e6a0ad5dcc5e43444c539e75ecc74d0f56d3e406a77044ba17bf4f974ccea7f0fd971620a66e4a6c6c46ae32c5d3de6513df5d084f
|
data/Gemfile.lock
CHANGED
data/csrmatrix.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["hello@teamaqua.ca"]
|
11
11
|
|
12
12
|
spec.summary = %q{Efficient sparse matrix library.}
|
13
|
-
spec.description = %q{
|
13
|
+
spec.description = %q{CSR matrix implementation for Ruby. https://github.com/Team-Aqua/Matrix-Library}
|
14
14
|
spec.homepage = "https://github.com/Team-Aqua/Matrix-Library/"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
data/lib/csrmatrix.rb
CHANGED
@@ -30,9 +30,43 @@ class TwoDMatrix
|
|
30
30
|
include Contracts::Invariants
|
31
31
|
|
32
32
|
C = Contracts
|
33
|
+
#These only get called post Methods with Contract Decorator
|
33
34
|
invariant(@rows) { @rows >= 0}
|
34
35
|
invariant(@columns) { @columns >= 0}
|
35
36
|
invariant(:val) {self.val != nil}
|
37
|
+
# @nonzero_count.is_a?(array) FIXME????????? and @nonzero_count.count() >= 0
|
38
|
+
# invariant(@nonzero_count) { @nonzero_count >= 0}
|
39
|
+
# invariant(@row_ptr) { @row_ptr.count() >= 0 and @row_ptr.is_a?(Array)}
|
40
|
+
# @row_pointer.is_a?(array) and @row_pointer.count() >= 0
|
41
|
+
# @col_ind.is_a?(array) and @col_ind.count() >= 0
|
42
|
+
# @val.is_a?(array) and @val.count() >= 0
|
43
|
+
# @dimension == 2
|
44
|
+
|
45
|
+
def is_invariant?
|
46
|
+
if @val == nil
|
47
|
+
raise InvariantError.new, "Empty Matrix"
|
48
|
+
return false
|
49
|
+
end
|
50
|
+
if @columns < 0
|
51
|
+
raise InvariantError.new, "Invalid Column Dimension"
|
52
|
+
return false
|
53
|
+
end
|
54
|
+
if @rows < 0
|
55
|
+
raise InvariantError.new, "Invalid Row Dimension"
|
56
|
+
return false
|
57
|
+
end
|
58
|
+
#!@nonzero_count.is_a?(Array) or IS THIS AN ARRAY?
|
59
|
+
# if @nonzero_count == nil
|
60
|
+
# raise InvariantError.new, "Invalid Non-Zero Count"
|
61
|
+
# return false
|
62
|
+
# end
|
63
|
+
# @nonzero_count.is_a?(array) and @nonzero_count.count() >= 0
|
64
|
+
# @row_pointer.is_a?(array) and @row_pointer.count() >= 0
|
65
|
+
# @col_ind.is_a?(array) and @col_ind.count() >= 0
|
66
|
+
# @val.is_a?(array) and @val.count() >= 0
|
67
|
+
# @dimension == 2
|
68
|
+
return true
|
69
|
+
end
|
36
70
|
|
37
71
|
# The current website ref. Used for verification of rb systems.
|
38
72
|
Url = "https://github.com/Team-Aqua/Matrix-Library/"
|
@@ -40,14 +74,6 @@ class TwoDMatrix
|
|
40
74
|
|
41
75
|
# Blank setup; setup module.
|
42
76
|
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
|
51
77
|
@nonzero_count = nil
|
52
78
|
@row_ptr = nil
|
53
79
|
@col_ind = nil
|
@@ -61,13 +87,6 @@ class TwoDMatrix
|
|
61
87
|
# SPARSE MATRIX ATTRIBUTE OPERATORS
|
62
88
|
# matrix attributes and overloaded operators
|
63
89
|
#
|
64
|
-
def is_invariant?
|
65
|
-
if @val == nil
|
66
|
-
raise InvariantError.new, "Empty Matrix"
|
67
|
-
return false
|
68
|
-
end
|
69
|
-
return true
|
70
|
-
end
|
71
90
|
|
72
91
|
# equals override for matrix operations
|
73
92
|
def ==(o)
|
data/lib/csrmatrix/exceptions.rb
CHANGED
data/lib/csrmatrix/functions.rb
CHANGED
@@ -53,8 +53,16 @@ module CsrMatrix
|
|
53
53
|
def trace()
|
54
54
|
# identifies the trace of the matrix
|
55
55
|
is_invariant?
|
56
|
-
|
57
|
-
|
56
|
+
sum = 0
|
57
|
+
for i in 0..self.columns-1
|
58
|
+
for j in self.row_ptr[i]..self.row_ptr[i+1]-1
|
59
|
+
if self.col_ind[j] == i
|
60
|
+
sum += self.val[j]
|
61
|
+
break
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
return sum
|
58
66
|
end # trace
|
59
67
|
|
60
68
|
Contract C::None => Contracts::Num
|
data/lib/csrmatrix/operations.rb
CHANGED
@@ -1,75 +1,123 @@
|
|
1
1
|
require "csrmatrix/exceptions"
|
2
2
|
|
3
3
|
module CsrMatrix
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
module Operations
|
5
|
+
include Contracts::Core
|
6
|
+
C = Contracts
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
Contract C::Num, C::Nat, C::Nat => C::Bool
|
9
|
+
def insert(value, row, column)
|
10
|
+
# gets the value off of the index of matrix
|
11
|
+
is_invariant?
|
12
|
+
# post value inserted or updated in the given row and column
|
13
|
+
|
14
|
+
# insert if the value exists
|
15
|
+
for i in self.row_ptr[row-1]..self.row_ptr[row]-1
|
16
|
+
if column-1 == self.col_ind[i]
|
17
|
+
self.val[i] = value
|
18
|
+
return true
|
19
|
+
end
|
20
|
+
end
|
14
21
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
# add value if it does not exist
|
23
|
+
for i in self.columns-1..0
|
24
|
+
index = self.row_ptr[row]-1 + i
|
25
|
+
if self.col_ind[index] < column-1
|
26
|
+
#add value
|
27
|
+
self.col_ind.insert(index+1, column-1)
|
28
|
+
self.val.insert(index+1, value)
|
29
|
+
|
30
|
+
#increment row pointers
|
31
|
+
for j in row..self.row_ptr.count()-1
|
32
|
+
self.row_ptr[j] += 1
|
33
|
+
end
|
25
34
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
raise CsrMatrix::Exceptions::IndexOutOfRangeException.new, "Index out of Bounds"
|
30
|
-
return false
|
31
|
-
end
|
35
|
+
return true
|
36
|
+
end
|
37
|
+
end
|
32
38
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
#add value
|
40
|
+
self.col_ind.insert(self.row_ptr[row]-1, column-1)
|
41
|
+
self.val.insert(self.row_ptr[row]-1, value)
|
42
|
+
|
43
|
+
#increment row pointers
|
44
|
+
for j in row..self.row_ptr.count()-1
|
45
|
+
self.row_ptr[j] += 1
|
46
|
+
end
|
47
|
+
return true
|
48
|
+
|
49
|
+
#post
|
50
|
+
if self.index(row, column) != value
|
51
|
+
raise ContractReturnError.new "index not as expected"
|
52
|
+
end
|
53
|
+
end # insert
|
54
|
+
|
55
|
+
Contract C::Nat => C::Num
|
56
|
+
def get_value(index)
|
57
|
+
# gets the value off of the index of matrix
|
58
|
+
is_invariant?
|
59
|
+
return @val[index]
|
60
|
+
end # get_value
|
61
|
+
|
62
|
+
Contract C::Nat, C::Or[C::Nat, nil] => C::Num
|
63
|
+
def index(row, col=nil)
|
64
|
+
# gets the index in the matrix at row, col
|
65
|
+
is_invariant?
|
66
|
+
|
67
|
+
if col == nil
|
68
|
+
if @val.count < row
|
69
|
+
raise CsrMatrix::Exceptions::IndexOutOfRangeException.new, "Index out of Bounds"
|
70
|
+
return false
|
42
71
|
end
|
43
|
-
end # index
|
44
72
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
73
|
+
return @val[row-1]
|
74
|
+
else
|
75
|
+
if !checkInputBounds(row, col)
|
76
|
+
raise CsrMatrix::Exceptions::IndexOutOfRangeException.new, "Index out of Bounds"
|
77
|
+
return false
|
78
|
+
end
|
79
|
+
|
80
|
+
num_elm_in_prev = row_ptr[row-1]
|
81
|
+
num_elm_in_row = row_ptr[row] - num_elm_in_prev
|
82
|
+
|
83
|
+
(0...num_elm_in_row).each do | x |
|
84
|
+
if ( col-1 == @col_ind[num_elm_in_prev+x] )
|
85
|
+
return @val[num_elm_in_prev+x]
|
55
86
|
end
|
56
|
-
|
87
|
+
end
|
88
|
+
return 0
|
89
|
+
end
|
90
|
+
end # index
|
91
|
+
|
92
|
+
Contract C::None => C::Any
|
93
|
+
def print_full()
|
94
|
+
# prints the full matrix for user
|
95
|
+
is_invariant?
|
96
|
+
full_matrix = self.decompose()
|
97
|
+
full_matrix.each do | row |
|
98
|
+
row.each do | val |
|
99
|
+
print "#{val} "
|
100
|
+
end
|
101
|
+
puts ""
|
102
|
+
end
|
103
|
+
puts ""
|
104
|
+
end # print_full
|
57
105
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
106
|
+
Contract C::None => C::Any
|
107
|
+
def print_sparse()
|
108
|
+
# prints all nonzero values of matrix for user
|
109
|
+
is_invariant?
|
110
|
+
full_matrix = self.decompose()
|
111
|
+
full_matrix.each do | row |
|
112
|
+
row.each do | val |
|
113
|
+
if val == 0
|
114
|
+
print "---"
|
115
|
+
else
|
116
|
+
print " #{val} "
|
70
117
|
end
|
71
|
-
puts ""
|
72
118
|
end
|
73
|
-
|
74
|
-
|
119
|
+
puts ""
|
120
|
+
end
|
121
|
+
end # print_sparse
|
122
|
+
end # Operations
|
75
123
|
end # CsrMatrix
|
data/lib/csrmatrix/version.rb
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csrmatrix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anson Li, Quentin Lautischer, Aaron Plamondon
|
@@ -52,10 +52,7 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description:
|
56
|
-
- insofar that the system can be reused for different structures; 2. Efficiency
|
57
|
-
- in both storage and runtime; 3. Light - we need lightweight process stuctures
|
58
|
-
in order to reduce overhead to a minimum.'
|
55
|
+
description: CSR matrix implementation for Ruby. https://github.com/Team-Aqua/Matrix-Library
|
59
56
|
email:
|
60
57
|
- hello@teamaqua.ca
|
61
58
|
executables: []
|
@@ -86,6 +83,7 @@ files:
|
|
86
83
|
- lib/csrmatrix/properties.rb
|
87
84
|
- lib/csrmatrix/version.rb
|
88
85
|
- pkg/csrmatrix-1.0.0.gem
|
86
|
+
- pkg/csrmatrix-1.0.1.gem
|
89
87
|
homepage: https://github.com/Team-Aqua/Matrix-Library/
|
90
88
|
licenses:
|
91
89
|
- MIT
|