sparsematrix 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +101 -0
- data/bin/console +11 -0
- data/bin/setup +7 -0
- data/lib/sparsematrix.rb +2 -0
- data/lib/sparsematrix/version.rb +3 -0
- data/lib/sparsematrix/yale.rb +293 -0
- data/sparsematrix.gemspec +31 -0
- metadata +185 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 75a863cc44635687c0dbbdef870967237ed1f591
|
4
|
+
data.tar.gz: 2aef4b2397b7bed56dc45f4442192a6b159f791c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec3371816390c08de7defe518b26b9eccd3c973d64287b42e4bcd1dbc224a1a17eef2a71c3215b26100bcae4ba4fadb60d524d7fb1add2b823fdbea0475ffbe9
|
7
|
+
data.tar.gz: 89cb75aaa47a1b380dad896f3ef565e92c2d0d6ba63d9d8565ccb21448735dbde5c4e3514c7e04e6a3c412535469696bc6cdae78ce96980b47527381f40c2a9d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Reed Kraft-Murphy
|
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,68 @@
|
|
1
|
+
# SparseMatrix
|
2
|
+
|
3
|
+
Sparse matrix implementations (just Yale currently) in pure Ruby.
|
4
|
+
|
5
|
+
Why? Why not.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sparsematrix'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sparsematrix
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'sparsematrix'
|
27
|
+
sparse = SparseMatrix::YaleSparseMatrix.new 0
|
28
|
+
(1..10).each { |n| sparse[n, n] = 1 }
|
29
|
+
sparse.each_with_index { |value, row, column| puts "sparse[#{row}, #{column}] = #{value}" }
|
30
|
+
# sparse[1, 1] = 1
|
31
|
+
# sparse[2, 2] = 1
|
32
|
+
# sparse[3, 3] = 1
|
33
|
+
# sparse[4, 4] = 1
|
34
|
+
# sparse[5, 5] = 1
|
35
|
+
# sparse[6, 6] = 1
|
36
|
+
# sparse[7, 7] = 1
|
37
|
+
# sparse[8, 8] = 1
|
38
|
+
# sparse[9, 9] = 1
|
39
|
+
# sparse[10, 10] = 1
|
40
|
+
puts sparse.inspect(true)
|
41
|
+
# SparseMatrix::YaleSparseMatrix[
|
42
|
+
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
43
|
+
# [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
44
|
+
# [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
45
|
+
# [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
46
|
+
# [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
|
47
|
+
# [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
48
|
+
# [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
49
|
+
# [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
50
|
+
# [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
51
|
+
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
52
|
+
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]] # efficient 8.26% density
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
+
|
59
|
+
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).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sparsematrix. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
64
|
+
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
rescue LoadError
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'yard'
|
11
|
+
YARD::Rake::YardocTask.new do |t|
|
12
|
+
t.options = [
|
13
|
+
'--markup', 'markdown',
|
14
|
+
'-o', 'doc/',
|
15
|
+
'--private'
|
16
|
+
]
|
17
|
+
end
|
18
|
+
rescue LoadError
|
19
|
+
end
|
20
|
+
|
21
|
+
begin
|
22
|
+
require 'stackprof'
|
23
|
+
desc 'profile the benchmark task'
|
24
|
+
task :prof do
|
25
|
+
stackprof_data = StackProf.run(mode: :wall, out: 'stackprof.out') do
|
26
|
+
Rake::Task["bench"].execute
|
27
|
+
end
|
28
|
+
end
|
29
|
+
rescue LoadError
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'benchmark'
|
34
|
+
desc 'benchmark YaleSparseMatrix against Matrix'
|
35
|
+
task :bench do
|
36
|
+
require 'sparsematrix/yale'
|
37
|
+
require 'matrix'
|
38
|
+
|
39
|
+
SIZES = [
|
40
|
+
[10, 10],
|
41
|
+
[100, 100],
|
42
|
+
# [1000, 1000],
|
43
|
+
# [10_000, 10_000]
|
44
|
+
]
|
45
|
+
DENSITIES = [0.1, 0.3, 0.5, 0.7, 0.9]
|
46
|
+
N = 100
|
47
|
+
SIZES.each do |rows, cols|
|
48
|
+
printf "Size: %d rows x %d cols\n", rows, cols
|
49
|
+
space_efficient_density_threshold = (rows * (cols - 1) - 1) / (2.0 * rows * cols)
|
50
|
+
printf "Expect density threshold: %.2f", space_efficient_density_threshold
|
51
|
+
DENSITIES.each do |density|
|
52
|
+
zero = 0
|
53
|
+
puts
|
54
|
+
printf "Density: %.2f\n", density
|
55
|
+
puts "YaleSparseMatrix"
|
56
|
+
Benchmark.bm(16) do |bm|
|
57
|
+
sparse = nil
|
58
|
+
bm.report("initialize") do
|
59
|
+
N.times do
|
60
|
+
sparse = SparseMatrix::YaleSparseMatrix.build(zero, rows, cols) { rand <= density ? 1 : zero }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
bm.report("iterate all") do
|
64
|
+
N.times do
|
65
|
+
sparse.each(true) { |e| e == 1 }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
bm.report("iterate non zero") do
|
69
|
+
N.times do
|
70
|
+
sparse.each { |e| e == 1 }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
puts
|
76
|
+
puts "Matrix"
|
77
|
+
Benchmark.bm(16) do |bm|
|
78
|
+
matrix = nil
|
79
|
+
bm.report("initialize") do
|
80
|
+
N.times do
|
81
|
+
matrix = Matrix.build(rows, cols) { rand <= density ? 1 : zero }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
bm.report("iterate all") do
|
85
|
+
N.times do
|
86
|
+
matrix.each { |e| e == 1 }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
bm.report("iterate non zero") do
|
90
|
+
N.times do
|
91
|
+
matrix.each.select{ |e| e!= zero }.each { |e| e == 1 }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
puts
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
rescue LoadError
|
101
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sparsematrix"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
data/bin/setup
ADDED
data/lib/sparsematrix.rb
ADDED
@@ -0,0 +1,293 @@
|
|
1
|
+
module SparseMatrix
|
2
|
+
# The Yale sparse matrix format stores an initial sparse `m` x `n` matrix, `M`, in
|
3
|
+
# row form using three (one-dimensional) arrays (`A`, `IA`, `JA`). Let `NNZ` denote
|
4
|
+
# the number of nonzero entries in `M`. (Note that zero-based indices shall be
|
5
|
+
# used here.)
|
6
|
+
#
|
7
|
+
# * The array `A` is of length `NNZ` and holds all the nonzero entries of `M` in
|
8
|
+
# left-to-right top-to-bottom ("row-major") order.
|
9
|
+
#
|
10
|
+
# * The array `IA` is of length `m + 1` and contains the index in `A` of the first
|
11
|
+
# element in each row, followed by the total number of nonzero elements `NNZ`.
|
12
|
+
# `IA[i]` contains the index in `A` of the first nonzero element of row `i`. Row `i`
|
13
|
+
# of the original matrix extends from `A[IA[i]]` to `A[IA[i + 1] - 1]`, i.e.
|
14
|
+
# from the start of one row to the last index before the start of the next.
|
15
|
+
# The last entry, `IA[m]`, must be the number of elements in `A`.
|
16
|
+
#
|
17
|
+
# * The third array, `JA`, contains the column index in `M` of each element of `A`
|
18
|
+
# and hence is of length `NNZ` as well.
|
19
|
+
#
|
20
|
+
# For example, the matrix
|
21
|
+
#
|
22
|
+
# 0 0 0 0
|
23
|
+
# 5 8 0 0
|
24
|
+
# 0 0 3 0
|
25
|
+
# 0 6 0 0
|
26
|
+
#
|
27
|
+
# is a 4 x 4 matrix with 4 nonzero elements, hence
|
28
|
+
#
|
29
|
+
# A = [ 5 8 3 6 ]
|
30
|
+
# IA = [ 0 0 2 3 4 ]
|
31
|
+
# JA = [ 0 1 2 1 ]
|
32
|
+
#
|
33
|
+
# So, in array `JA`, the element "5" from `A` has column index 0, "8" and "6"
|
34
|
+
# have index 1, and element "3" has index 2.
|
35
|
+
#
|
36
|
+
# In this case the Yale representation contains 13 entries, compared to 16 in
|
37
|
+
# the original matrix. The Yale format saves on memory only when
|
38
|
+
# `NNZ < (m (n - 1) - 1) / 2`. Another example, the matrix
|
39
|
+
#
|
40
|
+
# 10 20 0 0 0 0
|
41
|
+
# 0 30 0 40 0 0
|
42
|
+
# 0 0 50 60 70 0
|
43
|
+
# 0 0 0 0 0 80
|
44
|
+
#
|
45
|
+
# is a 4 x 6 matrix (24 entries) with 8 nonzero elements, so
|
46
|
+
#
|
47
|
+
# A = [ 10 20 30 40 50 60 70 80 ]
|
48
|
+
# IA = [ 0 2 4 7 8 ]
|
49
|
+
# JA = [ 0 1 1 3 2 3 4 5 ]
|
50
|
+
#
|
51
|
+
# The whole is stored as 21 entries.
|
52
|
+
#
|
53
|
+
# `IA` splits the array `A` into rows: `(10, 20) (30, 40) (50, 60, 70) (80)`;
|
54
|
+
# `JA` aligns values in columns:
|
55
|
+
# `(10, 20, ...) (0, 30, 0, 40, ...)(0, 0, 50, 60, 70, 0) (0, 0, 0, 0, 0, 80)`.
|
56
|
+
# Note that in this format, the first value of `IA` is always zero and the last
|
57
|
+
# is always `NNZ`, so they are in some sense redundant. However, they can make
|
58
|
+
# accessing and traversing the array easier for the programmer.
|
59
|
+
#
|
60
|
+
# https://en.wikipedia.org/wiki/Sparse_matrix#Yale
|
61
|
+
class YaleSparseMatrix
|
62
|
+
def self.build(zero, rows, columns, &block)
|
63
|
+
return enum_for :build, rows, columns unless block_given?
|
64
|
+
smx = new zero
|
65
|
+
rows.times do |row|
|
66
|
+
columns.times do |column|
|
67
|
+
value = yield row, column
|
68
|
+
smx[row, column] = value unless value == zero
|
69
|
+
end
|
70
|
+
end
|
71
|
+
smx
|
72
|
+
end
|
73
|
+
|
74
|
+
# @param zero The value to return for zero / unused elements
|
75
|
+
def initialize(zero = nil)
|
76
|
+
@zero = zero
|
77
|
+
@elements = []
|
78
|
+
@row_index = [0]
|
79
|
+
@index_column = []
|
80
|
+
end
|
81
|
+
attr_reader :zero, :elements, :row_index,
|
82
|
+
:index_column
|
83
|
+
|
84
|
+
alias_method :a, :elements
|
85
|
+
alias_method :ia, :row_index
|
86
|
+
alias_method :ja, :index_column
|
87
|
+
|
88
|
+
# @param row [Numeric] row part of the index to return
|
89
|
+
# @param column [Numeric] column part of the index to return
|
90
|
+
# @return the element at the given index, or #zero
|
91
|
+
def [](row, column)
|
92
|
+
index = element_index row, column
|
93
|
+
return zero unless index
|
94
|
+
elements[index]
|
95
|
+
end
|
96
|
+
alias_method :element, :[]
|
97
|
+
alias_method :component, :[]
|
98
|
+
|
99
|
+
# @param row [Numeric] row part of the index to set
|
100
|
+
# @param column [Numeric] column part of the index to set
|
101
|
+
# @param value the value to set
|
102
|
+
# @return the value that was passed in
|
103
|
+
def []=(row, column, value)
|
104
|
+
if row >= row_count
|
105
|
+
add_rows(row - row_count + 1)
|
106
|
+
end
|
107
|
+
|
108
|
+
index = row_start = row_index[row]
|
109
|
+
row_end = row_index[row + 1]
|
110
|
+
|
111
|
+
while index < row_end
|
112
|
+
current_column = index_column[index]
|
113
|
+
if current_column == column
|
114
|
+
@elements[index] = value
|
115
|
+
return value
|
116
|
+
end
|
117
|
+
break if current_column > column
|
118
|
+
index += 1
|
119
|
+
end
|
120
|
+
|
121
|
+
@elements.insert(index, value)
|
122
|
+
@index_column.insert(index, column)
|
123
|
+
while row < row_count
|
124
|
+
row += 1
|
125
|
+
@row_index[row] += 1
|
126
|
+
end
|
127
|
+
value
|
128
|
+
end
|
129
|
+
|
130
|
+
# Compares two SparseMatrix's for equality
|
131
|
+
# @param other [YaleSparseMatrix]
|
132
|
+
# @return [Boolean]
|
133
|
+
def ==(other)
|
134
|
+
return false unless other.is_a? YaleSparseMatrix
|
135
|
+
nonzero_element_count == other.nonzero_element_count &&
|
136
|
+
elements == other.elements &&
|
137
|
+
row_index == other.row_index &&
|
138
|
+
index_column == other.index_column
|
139
|
+
end
|
140
|
+
|
141
|
+
# @param block
|
142
|
+
# @return [YaleSparseMatrix] a new SparseMatrix whos elements have been mapped
|
143
|
+
# through the supplied block
|
144
|
+
def collect(&block)
|
145
|
+
return enum_for :collect unless block_given?
|
146
|
+
clone.instance_eval { @elements.collect(&block) }
|
147
|
+
end
|
148
|
+
alias_method :map, :collect
|
149
|
+
|
150
|
+
# @return [Numeric] The number of columns in the matrix
|
151
|
+
def column_count
|
152
|
+
row_index
|
153
|
+
index_column.last + 1
|
154
|
+
end
|
155
|
+
alias_method :n, :column_count
|
156
|
+
|
157
|
+
# @return the density of the SparseMatrix; that is, how many elements are
|
158
|
+
# non-zero divided by the total size of the matrix
|
159
|
+
def density
|
160
|
+
nonzero_element_count / (row_count * column_count * 1.0)
|
161
|
+
end
|
162
|
+
alias_method :sparsity, :density
|
163
|
+
|
164
|
+
# Passes each element of the matrix to the supplied block
|
165
|
+
# @param block
|
166
|
+
# @return [YaleSparseMatrix]
|
167
|
+
def each(zeroes = false, &block)
|
168
|
+
return enum_for :each, zeroes unless block_given?
|
169
|
+
if zeroes
|
170
|
+
row_count.times do |row|
|
171
|
+
column_count.times do |col|
|
172
|
+
yield self[row, col]
|
173
|
+
end
|
174
|
+
end
|
175
|
+
else
|
176
|
+
@elements.each(&block)
|
177
|
+
end
|
178
|
+
self
|
179
|
+
end
|
180
|
+
|
181
|
+
# Passes each element of the matrix and its index to the supplied block
|
182
|
+
# @param block
|
183
|
+
# @return [YaleSparseMatrix]
|
184
|
+
def each_with_index(zeroes = false, &_block)
|
185
|
+
return enum_for :each_with_index, zeroes unless block_given?
|
186
|
+
if zeroes
|
187
|
+
row_count.times do |row|
|
188
|
+
column_count.times do |_col|
|
189
|
+
element = self[row, column]
|
190
|
+
yield [element, row, column]
|
191
|
+
end
|
192
|
+
end
|
193
|
+
else
|
194
|
+
ia.each_cons(2).each_with_index do |indexes, row|
|
195
|
+
row_start, row_end = *indexes
|
196
|
+
a[row_start...row_end].each_with_index do |element, index|
|
197
|
+
yield [element, row, ja[row_start + index]]
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
# Returns true if the matrix is empty; i.e. if {nonzero_element_count} is 0
|
204
|
+
# @return [Boolean]
|
205
|
+
def empty?
|
206
|
+
nonzero_element_count == 0
|
207
|
+
end
|
208
|
+
alias_method :zero?, :empty?
|
209
|
+
|
210
|
+
# Returns true if the matrix includes element
|
211
|
+
# @param element
|
212
|
+
# @return [Boolean]
|
213
|
+
def include?(element)
|
214
|
+
@elements.include? element
|
215
|
+
end
|
216
|
+
|
217
|
+
# Returns the index of element in the matrix, or nil
|
218
|
+
# @param element
|
219
|
+
# @return [Array(Numeric, Numeric), nil]
|
220
|
+
def index(element)
|
221
|
+
each_with_index { |e, index| return index if e == element }
|
222
|
+
nil
|
223
|
+
end
|
224
|
+
|
225
|
+
def inspect(zeroes = false)
|
226
|
+
"#{self.class}[\n" +
|
227
|
+
row_count.times.map do |row|
|
228
|
+
column_count.times.map { |col| self[row, col] }
|
229
|
+
.reject { |e| !zeroes && e == zero }.inspect
|
230
|
+
end.join(",\n") +
|
231
|
+
'] # ' \
|
232
|
+
"#{saving_memory? ? 'efficient' : 'not efficient'} " \
|
233
|
+
"#{format '%.02f', density * 100.0}% density"
|
234
|
+
end
|
235
|
+
|
236
|
+
# The number of non-zero elements in the matrix
|
237
|
+
# @return [Numeric]
|
238
|
+
def nonzero_element_count
|
239
|
+
@row_index.last
|
240
|
+
end
|
241
|
+
alias_method :nnz, :nonzero_element_count
|
242
|
+
|
243
|
+
# The number of rows in the matrix
|
244
|
+
# @return [Numeric]
|
245
|
+
def row_count
|
246
|
+
row_index.length - 1
|
247
|
+
end
|
248
|
+
alias_method :m, :row_count
|
249
|
+
|
250
|
+
# Returns true if the SparseMatrix uses less memory than a naive matrix
|
251
|
+
# @return [Boolean]
|
252
|
+
def saving_memory?
|
253
|
+
nnz < (m * (n - 1) - 1) / 2
|
254
|
+
end
|
255
|
+
|
256
|
+
private
|
257
|
+
|
258
|
+
# Extends #{@row_index}
|
259
|
+
# @param count [Numeric] the number of rows to add
|
260
|
+
def add_row(count = 1)
|
261
|
+
@row_index += [row_index.last] * count
|
262
|
+
end
|
263
|
+
alias_method :add_rows, :add_row
|
264
|
+
|
265
|
+
# Calculates the index in {@elements} of the given matrix index. Returns nil
|
266
|
+
# if the index is outside the matrix's bounds.
|
267
|
+
# @param row [Numeric]
|
268
|
+
# @param column [Numeric]
|
269
|
+
# @return [Numeric, nil]
|
270
|
+
def element_index(row, column)
|
271
|
+
index = row_start = row_index[row]
|
272
|
+
row_end = row_index[row + 1]
|
273
|
+
return nil unless row_start && row_end
|
274
|
+
return nil if row_start == row_end
|
275
|
+
while index < row_end
|
276
|
+
current_column = index_column[index]
|
277
|
+
return index if current_column == column
|
278
|
+
if current_column > column
|
279
|
+
return nil
|
280
|
+
end
|
281
|
+
index += 1
|
282
|
+
end
|
283
|
+
nil
|
284
|
+
end
|
285
|
+
|
286
|
+
# Sets the number of non-zero elements. For reasons, this is stored as the
|
287
|
+
# last element in {@row_index}.
|
288
|
+
# @param nnz [Numeric]
|
289
|
+
def nonzero_element_count=(nnz)
|
290
|
+
@row_index[-1] = nnz
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sparsematrix/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'sparsematrix'
|
8
|
+
spec.version = SparseMatrix::VERSION
|
9
|
+
spec.authors = ['Reed Kraft-Murphy']
|
10
|
+
spec.email = ['reed@reedmurphy.net']
|
11
|
+
|
12
|
+
spec.summary = 'Yale sparse matrix'
|
13
|
+
spec.description = 'Yale sparse matrix'
|
14
|
+
spec.homepage = 'https://github.com/RWJMurphy/'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
23
|
+
spec.add_development_dependency 'pry'
|
24
|
+
spec.add_development_dependency 'pry-rescue'
|
25
|
+
spec.add_development_dependency 'pry-stack_explorer'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
spec.add_development_dependency 'simplecov'
|
29
|
+
spec.add_development_dependency 'stackprof'
|
30
|
+
spec.add_development_dependency 'yard'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sparsematrix
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Reed Kraft-Murphy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-21 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-rescue
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-stack_explorer
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: stackprof
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: yard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: Yale sparse matrix
|
140
|
+
email:
|
141
|
+
- reed@reedmurphy.net
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".travis.yml"
|
149
|
+
- CODE_OF_CONDUCT.md
|
150
|
+
- Gemfile
|
151
|
+
- LICENSE.txt
|
152
|
+
- README.md
|
153
|
+
- Rakefile
|
154
|
+
- bin/console
|
155
|
+
- bin/setup
|
156
|
+
- lib/sparsematrix.rb
|
157
|
+
- lib/sparsematrix/version.rb
|
158
|
+
- lib/sparsematrix/yale.rb
|
159
|
+
- sparsematrix.gemspec
|
160
|
+
homepage: https://github.com/RWJMurphy/
|
161
|
+
licenses:
|
162
|
+
- MIT
|
163
|
+
metadata: {}
|
164
|
+
post_install_message:
|
165
|
+
rdoc_options: []
|
166
|
+
require_paths:
|
167
|
+
- lib
|
168
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
requirements: []
|
179
|
+
rubyforge_project:
|
180
|
+
rubygems_version: 2.4.7
|
181
|
+
signing_key:
|
182
|
+
specification_version: 4
|
183
|
+
summary: Yale sparse matrix
|
184
|
+
test_files: []
|
185
|
+
has_rdoc:
|