csrmatrix 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5239814e3bf4d761150bacbe7d8fa44ca321446d
4
+ data.tar.gz: 6f72c0b95e218e513b7372e1264054ea33c36ad2
5
+ SHA512:
6
+ metadata.gz: 0f1313cec202a6ca9d303229629615d0422342847e1b1f1e446c704f806b1aecf574c2945ae51e93158387dba3a020e992f9e3983b4aafa7d23b911060b9a566
7
+ data.tar.gz: 1734545e1f6a6f2387466768bd62896757935deb648f002077e84bce4b972928f67c5a7af678cd8ad503fbe0d1300bc33b820b83fcd35296305dbdb04bfc16e4
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at ansonli.94@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in matrix.gemspec
4
+ gem "minitest"
5
+
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ csrmatrix (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ minitest (5.8.3)
11
+ rake (10.5.0)
12
+ rspec (3.4.0)
13
+ rspec-core (~> 3.4.0)
14
+ rspec-expectations (~> 3.4.0)
15
+ rspec-mocks (~> 3.4.0)
16
+ rspec-core (3.4.1)
17
+ rspec-support (~> 3.4.0)
18
+ rspec-expectations (3.4.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.4.0)
21
+ rspec-mocks (3.4.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.4.0)
24
+ rspec-support (3.4.1)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.11)
31
+ csrmatrix!
32
+ minitest
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.11.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Anson Li, Quentin Lautischer, Aaron Plamondon
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Matrix
2
+
3
+ Welcome to the Matrix gem! This gem is designed to offer high-performance, efficient processes for sparse matrix processes.
4
+
5
+ If you want to try it out, install the gem and run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'matrix'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install matrix
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Team-Aqua/Matrix-Library/tree/master/matrix. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = FileList['test/*_test.rb']
6
+ end
7
+
8
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "matrix"
5
+ require "csrmatrix"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start
data/bin/matrix ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csrmatrix'
4
+
5
+ puts csrmatrix::Url
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/csrmatrix.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csrmatrix/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csrmatrix"
8
+ spec.version = CsrMatrix::VERSION
9
+ spec.authors = ["Anson Li, Quentin Lautischer, Aaron Plamondon"]
10
+ spec.email = ["hello@teamaqua.ca"]
11
+
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.}
14
+ spec.homepage = "https://github.com/Team-Aqua/Matrix-Library/"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "http://mygemserver.com"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
@@ -0,0 +1,241 @@
1
+ require "matrix"
2
+ require "csrmatrix/exceptions"
3
+
4
+ module CsrMatrix
5
+ module Arithmetic
6
+
7
+ def self.included(exceptions)
8
+ exceptions.send :include, Exceptions
9
+ end
10
+
11
+ # class MatrixDimException < StandardError; end
12
+ # class ArgumentNullException < StandardError; end
13
+ # class MatrixTypeException < StandardError; end
14
+
15
+ # REFERENCE: To use module functions in module
16
+ # Class.new.extend(Decompositions).lup()
17
+
18
+ def scalar_multiply(value)
19
+ if value == nil
20
+ raise ArgumentNullException.new, "Multiply by nil error."
21
+ return false
22
+ end
23
+ @val.each_index do |i|
24
+ @val[i] = @val[i] * value
25
+ end
26
+ end
27
+
28
+ def scalar_add(value)
29
+ if value == nil
30
+ raise ArgumentNullException.new, "Add by nil error."
31
+ return false
32
+ end
33
+ # create an identity matrix with the value
34
+ # matrix_add the new identity matrix to the given matrix
35
+ @val.each_index do |i|
36
+ @val[i] = @val[i] + value
37
+ end
38
+ end
39
+
40
+ def scalar_subtract(value)
41
+ if value == nil
42
+ raise ArgumentNullException.new, "Subtract by nil error."
43
+ return false
44
+ end
45
+ # create an identity matrix with the value
46
+ # matrix_subtract the new identity matrix to the given matrix
47
+ @val.each_index do |i|
48
+ @val[i] = @val[i] - value
49
+ end
50
+ end
51
+
52
+ def scalar_division(value)
53
+ if value == nil
54
+ raise ArgumentNullException.new, "Divide by nil error."
55
+ return false
56
+ end
57
+ @val.each_index do |i|
58
+ @val[i] = @val[i] / value.to_f
59
+ end
60
+ end
61
+
62
+ def scalar_exp(value)
63
+ if value == nil
64
+ raise ArgumentNullException.new, "Exp. by nil error."
65
+ return false
66
+ end
67
+ @val.each_index do |i|
68
+ @val[i] = @val[i] ** value
69
+ end
70
+ end
71
+
72
+ def inverse()
73
+ m = Matrix.rows(self.decompose)
74
+ self.build_from_array(m.inv().to_a())
75
+ end
76
+
77
+ # FIXME: Convert with CSR functions
78
+ def transpose()
79
+ m = Matrix.rows(self.decompose)
80
+ self.build_from_array(m.transpose.to_a())
81
+ end
82
+
83
+ def t()
84
+ self.transpose()
85
+ end
86
+
87
+ def matrix_vector(vector)
88
+ # dev based on http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/3-C/sparse.html
89
+ result = Array.new(vector.length, 0)
90
+ i = 0
91
+ while i < vector.length do
92
+ k = @row_ptr[i]
93
+ while k < @row_ptr[i+1] do
94
+ result[i] = result[i] + @val[k] * vector[@col_ind[k]]
95
+ k += 1
96
+ end
97
+ i += 1
98
+ end
99
+ return result
100
+ end
101
+
102
+ # dev based on http://stackoverflow.com/questions/29598299/csr-matrix-matrix-multiplication
103
+ # multiply dense (non-dense) matrix to csr matrix [eg. [1, 2]] x 2d array
104
+ # key: the dense matrix is LEFT SIDE, the csr matrix is RIGHT SIDE
105
+ def matrix_multiply(matrix)
106
+ # matrix order, assumes both matrices are square
107
+ res = Array.new(max_row(matrix)) { Array.new(@columns, 0) } # first denotes row, second denotes columns
108
+ n = matrix.length
109
+ i = 0
110
+ # res = dense X csr
111
+ csr_row = 0 # Current row in CSR matrix
112
+ while i < n do
113
+ startv = @row_ptr[i]
114
+ endv = @row_ptr[i + 1]
115
+ j = startv
116
+ while j < endv do
117
+ col = @col_ind[j]
118
+ csr_value = @val[j]
119
+ k = 0
120
+ while k < n do
121
+ dense_value = matrix[k][csr_row]
122
+ res[k][col] += csr_value * dense_value
123
+ k += 1
124
+ end
125
+ j += 1
126
+ end
127
+ csr_row += 1
128
+ i += 1
129
+ end
130
+ return res
131
+ end
132
+
133
+ # multiply two csr together - ref: http://www.mcs.anl.gov/papers/P5007-0813_1.pdf
134
+ def multiply_csr(matrix)
135
+ return matrix.matrix_multiply(self.decompose())
136
+ end
137
+
138
+ # helper function to determine deim count is equal
139
+ def is_same_dim(matrix)
140
+ return self.dimensions() == matrix.dimensions()
141
+ end
142
+
143
+ def matrix_add(matrix)
144
+ if self.is_same_dim(matrix)
145
+ res = Array.new(@rows) { Array.new(@columns, 0) }
146
+ row_idx = 0
147
+ cnta = 0 # total logged entries for matrix a
148
+ cntb = 0
149
+ while row_idx < @rows # eg. 0 1 2
150
+ rowa = @row_ptr[row_idx + 1] # eg. 0 2 4 7
151
+ rowb = matrix.row_ptr[row_idx + 1]
152
+ while cnta < rowa
153
+ # keep adding values to res until they're equal
154
+ res[row_idx][@col_ind[cnta]] += @val[cnta]
155
+ cnta += 1;
156
+ end
157
+ while cntb < rowb
158
+ res[row_idx][@col_ind[cntb]] += matrix.val[cntb]
159
+ cntb += 1;
160
+ end
161
+ row_idx += 1;
162
+ end
163
+ return res
164
+ end
165
+ raise Exceptions::MatrixDimException.new, "Matrix does not have same dimensions; cannot add."
166
+ return false
167
+ end
168
+
169
+ def matrix_subtract(matrix)
170
+ if self.is_same_dim(matrix)
171
+ res = Array.new(@rows) { Array.new(@columns, 0) }
172
+ row_idx = 0
173
+ cnta = 0 # total logged entries for matrix a
174
+ cntb = 0
175
+ while row_idx < @rows # eg. 0 1 2
176
+ rowa = @row_ptr[row_idx + 1] # eg. 0 2 4 7
177
+ rowb = matrix.row_ptr[row_idx + 1]
178
+ while cnta < rowa
179
+ # keep adding values to res until they're equal
180
+ res[row_idx][@col_ind[cnta]] += @val[cnta]
181
+ cnta += 1;
182
+ end
183
+ while cntb < rowb
184
+ res[row_idx][@col_ind[cntb]] -= matrix.val[cntb]
185
+ cntb += 1;
186
+ end
187
+ row_idx += 1;
188
+ end
189
+ return res
190
+ end
191
+ raise Exceptions::MatrixDimException.new, "Matrix does not have same dimensions; cannot subtract."
192
+ return false
193
+ end
194
+
195
+ # multiply y by x^-1; y * x^-1
196
+ # FIXME: Possibly consider rewording for context:
197
+ def matrix_left_division(matrix)
198
+ if !matrix.is_a?(TwoDMatrix)
199
+ raise Exceptions::MatrixTypeException.new, "Matrix is not usable type."
200
+ return false
201
+ end
202
+ if !matrix.square? || !self.square?
203
+ raise Exceptions::MatrixDimException.new, "Matrices does not have usable dimensions; cannot divide."
204
+ return false
205
+ end
206
+ tmpmatrix = TwoDMatrix.new
207
+ tmpmatrix = matrix
208
+ tmpmatrix.inverse()
209
+ return self.multiply_csr(tmpmatrix)
210
+ end
211
+
212
+ # multiply x by y^-1; x * y^-1
213
+ # FIXME: Possibly consider rewording for context: not doing x / y; doing x * y^-1
214
+ def matrix_right_division(matrix)
215
+ if !matrix.is_a?(TwoDMatrix)
216
+ raise Exceptions::MatrixTypeException.new, "Matrix is not usable type."
217
+ return false
218
+ end
219
+ if !matrix.square? || !self.square?
220
+ raise Exceptions::MatrixDimException.new, "Matrices does not have usable dimensions; cannot divide."
221
+ return false
222
+ end
223
+ tmpmatrix = TwoDMatrix.new
224
+ tmpmatrix = self
225
+ tmpmatrix.inverse()
226
+ return matrix.multiply_csr(tmpmatrix)
227
+ end
228
+
229
+ # Identifies the 'row' value of an array (eg. the number of entries in a row)
230
+ # Helper function, will clean after.
231
+ def max_row(array)
232
+ values = array
233
+ max_count = 0
234
+ values.each_index do |i|
235
+ max_count += 1
236
+ end
237
+ return max_count
238
+ end
239
+
240
+ end
241
+ end
@@ -0,0 +1,22 @@
1
+ module CsrMatrix
2
+ module Decompositions
3
+
4
+ def eigen()
5
+ eigensystem()
6
+ end
7
+
8
+ def eigensystem()
9
+ m = Matrix.rows(self.decompose)
10
+ return m.eigensystem().to_a[0].to_a
11
+ end
12
+
13
+ def lup()
14
+ puts "lup"
15
+ end
16
+
17
+ def lup_decomposition()
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module CsrMatrix
2
+ module Exceptions
3
+ class NullMatrixException < StandardError; end
4
+ class MatrixDimException < StandardError; end
5
+ class ArithmeticException < StandardError; end
6
+ class MatrixTypeException < StandardError; end
7
+ class DivideByZeroException < StandardError; end
8
+ class IndexOutOfRangeException < StandardError; end
9
+ class ArgumentNullException < StandardError; end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ module CsrMatrix
2
+ module Functions
3
+
4
+ def determinant()
5
+ m = Matrix.rows(self.decompose)
6
+ m.det()
7
+ end
8
+
9
+ def det()
10
+ self.determinant()
11
+ end
12
+
13
+ def rank()
14
+ m = Matrix.rows(self.decompose)
15
+ m.rank()
16
+ end
17
+
18
+ def round(ndig = 0)
19
+ m = Matrix.rows(self.decompose)
20
+ self.build_from_array(m.round(ndig).to_a())
21
+ end
22
+
23
+ def trace()
24
+ m = Matrix.rows(self.decompose)
25
+ m.trace()
26
+ end
27
+
28
+ def tr()
29
+ self.trace()
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,77 @@
1
+ module CsrMatrix
2
+ module Helpers
3
+
4
+ ##
5
+ # ARRAY FUNCTIONS
6
+ # for pre-processing of matrix
7
+ #
8
+
9
+ # Identifies the 'column' value of an array (eg. the number of entries in a column)
10
+ def max_col(array)
11
+ values = array
12
+ max_count = 0
13
+ # Loop over indexes.
14
+ values.each_index do |i|
15
+ counter = 0
16
+ # Get subarray and loop over its indexes also.
17
+ subarray = values[i]
18
+ subarray.each_index do |x|
19
+ counter += 1
20
+ end
21
+ if counter > max_count
22
+ max_count = counter
23
+ end
24
+ end
25
+ return max_count
26
+ end
27
+
28
+ # Identifies the 'row' value of an array (eg. the number of entries in a row)
29
+ def max_row(array)
30
+ values = array
31
+ max_count = 0
32
+ values.each_index do |i|
33
+ max_count += 1
34
+ end
35
+ return max_count
36
+ end
37
+
38
+ def count_nonzero(array)
39
+ max_count = 0
40
+ array.each_index do |i|
41
+ subarray = array[i]
42
+ subarray.each_index do |x|
43
+ if array[i][x] != 0
44
+ max_count += 1
45
+ end
46
+ end
47
+ end
48
+ return max_count
49
+ end
50
+
51
+ # Code taken from http://stackoverflow.com/questions/9545613/getting-dimension-of-multidimensional-array-in-ruby
52
+ def depth(array)
53
+ return 0 if array.class != Array
54
+ result = 1
55
+ array.each do |sub_a|
56
+ if sub_a.class == Array
57
+ dim = depth(sub_a)
58
+ result = dim + 1 if dim + 1 > result
59
+ end
60
+ end
61
+ return result
62
+ end
63
+
64
+ # Counts all elements in array - assumed 2d
65
+ def count_total(array)
66
+ max_count = 0
67
+ array.each_index do |i|
68
+ subarray = array[i]
69
+ subarray.each_index do |x|
70
+ max_count += 1
71
+ end
72
+ end
73
+ return max_count
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,33 @@
1
+ module CsrMatrix
2
+ module Operations
3
+ def get_value(index)
4
+ return @val[index]
5
+ end
6
+
7
+ def print_full()
8
+ (0...@col_ind.length).each do |iter|
9
+ (0...@col_ind.length).each do |col|
10
+ if (iter == @row_ptr[iter] and col == @col_ind[iter])
11
+ print "#{get_value(iter)} "
12
+ else
13
+ print "0 "
14
+ end
15
+ end
16
+ puts ""
17
+ end
18
+ end
19
+
20
+ def print_sparse()
21
+ (0...@col_ind.length).each do |iter|
22
+ (0...@col_ind.length).each do |col|
23
+ if (iter == @row_ptr[iter] and col == @col_ind[iter])
24
+ print "#{get_value(iter)} "
25
+ else
26
+ print "0 "
27
+ end
28
+ end
29
+ puts ""
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,98 @@
1
+ module CsrMatrix
2
+ module Properties
3
+
4
+ def self.included(exceptions)
5
+ exceptions.send :include, Exceptions
6
+ end
7
+
8
+ # Determines if the matrix is diagonal; wherein the values outside the main diagonal are all zero.
9
+ def diagonal?
10
+ m = Matrix.rows(self.decompose)
11
+ return m.diagonal?
12
+ end
13
+
14
+ # Determines if the matrix is empty; wherein all the values are zero.
15
+ def empty?
16
+ m = Matrix.rows(self.decompose)
17
+ return m.empty?
18
+ end
19
+
20
+ # Determine if the matrix is hermitian.
21
+ def hermitian?
22
+ m = Matrix.rows(self.decompose)
23
+ return m.hermitian?
24
+ end
25
+
26
+ # Determines if the matrix is lower-diagonal; wherein all the values only exist on and below the diagonal line.
27
+ def lower_triangular?
28
+ m = Matrix.rows(self.decompose)
29
+ return m.lower_triangular?
30
+ end
31
+
32
+ # Determines if the matrix is normal; wherein if the number of rows or columns is 0
33
+ def normal?
34
+ m = Matrix.rows(self.decompose)
35
+ return m.normal?
36
+ end
37
+
38
+ # Determines if the matrix is orthogonal; wherein the rows and columns are orthogonal unit vectors.
39
+ def orthogonal?
40
+ m = Matrix.rows(self.decompose)
41
+ return m.orthogonal?
42
+ end
43
+
44
+ # Determines if the matrix is a permutation; wherein it is an nxn version of the identity matrix.
45
+ def permutation?
46
+ m = Matrix.rows(self.decompose)
47
+ return m.permutation?
48
+ end
49
+
50
+ # Determines if the matrix is real; wherein the matrix consists entirely of real numbers.
51
+ def real?
52
+ m = Matrix.rows(self.decompose)
53
+ return m.real?
54
+ end
55
+
56
+ # Determines if the matrix is nonsingular
57
+ def nonsingular?
58
+ return !self.singular?
59
+ end
60
+
61
+ # Determines if the matrix is singular
62
+ def singular?
63
+ m = Matrix.rows(self.decompose)
64
+ return m.singular?
65
+ end
66
+
67
+ # Determines if the matrix is square
68
+ def square?
69
+ m = Matrix.rows(self.decompose)
70
+ return m.square?
71
+ end
72
+
73
+ # Determines if the matrix is symmetric
74
+ def symmetric?
75
+ m = Matrix.rows(self.decompose)
76
+ return m.symmetric?
77
+ end
78
+
79
+ # Determines if the matrix is unitary
80
+ def unitary?
81
+ m = Matrix.rows(self.decompose)
82
+ return m.unitary?
83
+ end
84
+
85
+ # Determines if the matrix is upper-triangular
86
+ def upper_triangular?
87
+ m = Matrix.rows(self.decompose)
88
+ return m.upper_triangular?
89
+ end
90
+
91
+ # Determines if the matrix is zero
92
+ def zero?
93
+ m = Matrix.rows(self.decompose)
94
+ return m.zero?
95
+ end
96
+
97
+ end
98
+ end
@@ -0,0 +1,3 @@
1
+ module CsrMatrix
2
+ VERSION = "1.0.0"
3
+ end
data/lib/csrmatrix.rb ADDED
@@ -0,0 +1,217 @@
1
+ require "csrmatrix/version"
2
+ require "csrmatrix/properties"
3
+ require "csrmatrix/arithmetic"
4
+ require "csrmatrix/functions"
5
+ require "csrmatrix/decompositions"
6
+ require "csrmatrix/operations"
7
+ require "csrmatrix/helpers"
8
+ require "csrmatrix/exceptions"
9
+
10
+ module CsrMatrix
11
+ # The current website ref. Used for verificationn of rb systems.
12
+ Url = "https://github.com/Team-Aqua/Matrix-Library/"
13
+ end
14
+
15
+ # General code convention in this manner - generate documentation via 'rdoc lib'.
16
+ class TwoDMatrix
17
+ #Need to ensure we include Object Class overwrites.
18
+ include CsrMatrix::Operations
19
+ include CsrMatrix::Properties
20
+ include CsrMatrix::Arithmetic
21
+ include CsrMatrix::Functions
22
+ include CsrMatrix::Decompositions
23
+ include CsrMatrix::Helpers
24
+ include CsrMatrix::Exceptions
25
+
26
+ # The current website ref. Used for verification of rb systems.
27
+ Url = "https://github.com/Team-Aqua/Matrix-Library/"
28
+ attr_reader :row_ptr, :col_ind, :val, :rows, :columns, :ndim
29
+
30
+ # Blank setup; setup module.
31
+ def initialize()
32
+ @nonzero_count = nil
33
+ @row_ptr = nil
34
+ @col_ind = nil
35
+ @val = nil
36
+ @rows = 0
37
+ @columns = 0
38
+ @ndim = 2
39
+ end
40
+
41
+ ##
42
+ # SPARSE MATRIX ATTRIBUTE OPERATORS
43
+ # matrix attributes and overloaded operators
44
+ #
45
+
46
+ # equals override for matrix operations
47
+ def ==(o)
48
+ o.class == self.class && o.state == state
49
+ end
50
+
51
+ # FIXME: convert to protected value
52
+ def state
53
+ [@val, @row_ptr, @col_ind, @rows, @columns, @ndim]
54
+ end
55
+
56
+ # Finds column and row value of an array.
57
+ def dimensions()
58
+ return [@rows, @columns]
59
+ end
60
+
61
+ def square?
62
+ return self.rows == self.columns
63
+ end
64
+
65
+ ##
66
+ # MATRIX DECOMPOSITION FUNCTIONS
67
+ #
68
+
69
+ def decompose()
70
+ res = Array.new(@rows) { Array.new(@columns, 0) }
71
+ row_counter = 0
72
+ row_idx = 0
73
+ @row_ptr.drop(1).each do |i| #eg. 2 4 7 10
74
+ while row_counter < i
75
+ res[row_idx][@col_ind[row_counter]] = @val[row_counter]
76
+ row_counter += 1
77
+ end
78
+ row_idx += 1
79
+ end
80
+ return res
81
+ end
82
+
83
+ ##
84
+ # MATRIX GENERATION FUNCTIONS
85
+ # generation of csr matrix
86
+ #
87
+
88
+ # Builds when given a 2d array to CSR
89
+ def build_from_array(array)
90
+ if array.is_a?(Array)
91
+ if depth(array) == 2
92
+ if same_sublength(array)
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."
106
+ return false
107
+ end
108
+ raise MatrixTypeException.new, "Wrong type convert to matrix"
109
+ end
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
172
+ return true
173
+ end
174
+
175
+ # Finds the column count, row count and non-zero values in one loop.
176
+ def convert_to_csr(array)
177
+ row_count = 0
178
+ col_count = 0
179
+ nonzero_count = 0
180
+
181
+ row_val = 0
182
+ row_prev_sum = 0
183
+
184
+ col_val = 0
185
+
186
+ value_array = Array.new
187
+ row_ptr = Array.new
188
+ col_ind = Array.new
189
+
190
+ array.each_index do |i| # each row
191
+ col_val = 0 # eg. for pos [0, 1, 2, 3] it goes 0, 1, 2, 3
192
+ col_tmp = 0
193
+ row_count += 1
194
+ row_prev_sum = row_val
195
+ row_ptr << row_prev_sum # ref: http://op2.github.io/PyOP2/linear_algebra.html
196
+ subarray = array[i]
197
+ subarray.each_index do |x| # each column entry in row
198
+ col_tmp += 1
199
+ if array[i][x] != 0
200
+ # use nonzero value in CSR
201
+ nonzero_count += 1
202
+ value_array << array[i][x]
203
+ col_ind << col_val
204
+ row_val += 1
205
+ end
206
+ col_val += 1 # eg. col_val add at the end
207
+ end
208
+ if col_tmp >= col_count
209
+ col_count = col_tmp
210
+ end
211
+ end
212
+ row_prev_sum = row_val
213
+ row_ptr << row_prev_sum
214
+ return [col_count, row_count, nonzero_count, value_array, row_ptr, col_ind]
215
+ end
216
+
217
+ end
Binary file
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csrmatrix
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anson Li, Quentin Lautischer, Aaron Plamondon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
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.'
59
+ email:
60
+ - hello@teamaqua.ca
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/matrix
75
+ - bin/setup
76
+ - csrmatrix.gemspec
77
+ - lib/csrmatrix.rb
78
+ - lib/csrmatrix/arithmetic.rb
79
+ - lib/csrmatrix/decompositions.rb
80
+ - lib/csrmatrix/exceptions.rb
81
+ - lib/csrmatrix/functions.rb
82
+ - lib/csrmatrix/helpers.rb
83
+ - lib/csrmatrix/operations.rb
84
+ - lib/csrmatrix/properties.rb
85
+ - lib/csrmatrix/version.rb
86
+ - pkg/csrmatrix-1.0.0.gem
87
+ homepage: https://github.com/Team-Aqua/Matrix-Library/
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.6
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Efficient sparse matrix library.
111
+ test_files: []