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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 842f69d5be23f5b6a2ea2057504f416eff0648f8
4
- data.tar.gz: f05e25a4c9f740e9691e94bc3aeb6b4a476be910
3
+ metadata.gz: 8ca96a6fc0d6250a281a09c47ae5a51a0f3ed466
4
+ data.tar.gz: 4a1abf788a4a2c8552209bfde11346a829a42608
5
5
  SHA512:
6
- metadata.gz: 767ba6ea5d5bdad6e30e986c052f93b6bd9b3822f28ee98326b132bc9010c01e3a17241ed7fe7ba558395e8853aca3fe9fd782ff36efa77fd43b0fbab825fd7c
7
- data.tar.gz: 73afd742117835704942a8cf5202a1cbbd9e3c8eecf013709957191707ca38b9a5574e56082477d7ab6491b95cea71049718a4145699bfc1636c21029a83acfa
6
+ metadata.gz: 5a738eedcb306751917db6aef46b49b778c7614467bdebd01d3ed0a5c5149c7d78b29da69c2432840e3dbb1cec8e0a52423ae6ec1eb4b48cb944d601f60c8b55
7
+ data.tar.gz: 147b40740fd26c79613732e6a0ad5dcc5e43444c539e75ecc74d0f56d3e406a77044ba17bf4f974ccea7f0fd971620a66e4a6c6c46ae32c5d3de6513df5d084f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- csrmatrix (1.0.1)
4
+ csrmatrix (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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{Generation of a sparse matrix package in order to satisfy: 1. Reusability - insofar that the system can be reused for different structures; 2. Efficiency - in both storage and runtime; 3. Light - we need lightweight process stuctures in order to reduce overhead to a minimum.}
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)
@@ -20,5 +20,8 @@ module CsrMatrix
20
20
 
21
21
  #
22
22
  class InvariantError < StandardError; end
23
+
24
+ class ContractReturnError < StandardError; end
25
+
23
26
  end # exceptions
24
27
  end # csrmatrix
@@ -53,8 +53,16 @@ module CsrMatrix
53
53
  def trace()
54
54
  # identifies the trace of the matrix
55
55
  is_invariant?
56
- m = Matrix.rows(self.decompose)
57
- return m.trace()
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
@@ -1,75 +1,123 @@
1
1
  require "csrmatrix/exceptions"
2
2
 
3
3
  module CsrMatrix
4
- module Operations
5
- include Contracts::Core
6
- C = Contracts
4
+ module Operations
5
+ include Contracts::Core
6
+ C = Contracts
7
7
 
8
- Contract C::Nat => C::Num
9
- def get_value(index)
10
- # gets the value off of the index of matrix
11
- is_invariant?
12
- return @val[index]
13
- end # get_value
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
- Contract C::Nat, C::Or[C::Nat, nil] => C::Num
16
- def index(row, col=nil)
17
- # gets the index in the matrix at row, col
18
- is_invariant?
19
-
20
- if col == nil
21
- if @val.count < row
22
- raise CsrMatrix::Exceptions::IndexOutOfRangeException.new, "Index out of Bounds"
23
- return false
24
- end
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
- return @val[row-1]
27
- else
28
- if !checkInputBounds(row, col)
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
- num_elm_in_prev = row_ptr[row-1]
34
- num_elm_in_row = row_ptr[row] - num_elm_in_prev
35
-
36
- (0...num_elm_in_row).each do | x |
37
- if ( col-1 == @col_ind[num_elm_in_prev+x] )
38
- return @val[num_elm_in_prev+x]
39
- end
40
- end
41
- return 0
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
- Contract C::None => C::Any
46
- def print_full()
47
- # prints the full matrix for user
48
- is_invariant?
49
- full_matrix = self.decompose()
50
- full_matrix.each do | row |
51
- row.each do | val |
52
- print "#{val} "
53
- end
54
- puts ""
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
- end # print_full
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
- Contract C::None => C::Any
59
- def print_sparse()
60
- # prints all nonzero values of matrix for user
61
- is_invariant?
62
- full_matrix = self.decompose()
63
- full_matrix.each do | row |
64
- row.each do | val |
65
- if val == 0
66
- print "---"
67
- else
68
- print " #{val} "
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
- end # print_sparse
74
- end # Operations
119
+ puts ""
120
+ end
121
+ end # print_sparse
122
+ end # Operations
75
123
  end # CsrMatrix
@@ -1,3 +1,3 @@
1
1
  module CsrMatrix
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
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.1
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: 'Generation of a sparse matrix package in order to satisfy: 1. Reusability
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