hamster-matrix 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61e59b284862474298af13c153843d0e1e112332
4
+ data.tar.gz: 13b92fc3f0fb26dd4f1b2bb6dee74d88b5c3615d
5
+ SHA512:
6
+ metadata.gz: b5e2621641b7e7d731386805a07c92f5f616b5bd3d73cfdddc1b16f8d0d07d00b4b2439f7a2848937077b24e5b7317ef2742ba6cf17ee7d6c48d1a5dee4b4d5f
7
+ data.tar.gz: 962341fd89e08a89f775f8666aee6ed548ab340c83489bc21f6c0fc4d9f6b1ec5408487e626192c4ccc3f999072059f3b7ab41e9aac348b4ad7ace7a271d65af
@@ -0,0 +1,37 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
36
+ ruby/
37
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hamster-matrix.gemspec
4
+ gemspec
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hamster-matrix (0.9.0)
5
+ hamster (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ atomic (1.1.99)
11
+ diff-lcs (1.2.5)
12
+ hamster (1.0.0)
13
+ atomic (~> 1.1)
14
+ rake (10.4.2)
15
+ rspec (3.3.0)
16
+ rspec-core (~> 3.3.0)
17
+ rspec-expectations (~> 3.3.0)
18
+ rspec-mocks (~> 3.3.0)
19
+ rspec-core (3.3.2)
20
+ rspec-support (~> 3.3.0)
21
+ rspec-expectations (3.3.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.3.0)
24
+ rspec-mocks (3.3.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.3.0)
27
+ rspec-support (3.3.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.10)
34
+ hamster-matrix!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 1.10.6
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org>
25
+
@@ -0,0 +1,33 @@
1
+ <img src="https://travis-ci.org/bradurani/hamster-matrix.svg"/>
2
+
3
+ # Hamster Matrix
4
+
5
+ Hamster Matrix is a gem providing a persietent immutable matrix for functional programming in Ruby. It is an extension for the popular [Hamster](https://github.com/hamstergem/hamster/) gem, using Hamster [Vectors](http://www.rubydoc.info/github/hamstergem/hamster/master/Hamster/Vector) under the hood.
6
+
7
+ It's great for using as a board in 2D video games where the matrix must be modified as the game state changes while leaving references to the old state unchanged, such as when using a time-travelling debugger. It copies the interface from Ruby's [Matrix](http://ruby-doc.org/stdlib-2.0.0/libdoc/matrix/rdoc/Matrix.html) class as much as possible.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'hamster-matrix'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install hamster-matrix
25
+
26
+ ## Usage
27
+
28
+ ###Coming Soon!###
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bradurani/hamster-matrix. 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.
33
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hamster/matrix"
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
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hamster/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hamster-matrix"
8
+ spec.version = Hamster::Matrix::VERSION
9
+ spec.authors = ["Brad Urani"]
10
+ spec.email = ["bradurani@gmail.com"]
11
+
12
+ spec.summary = "Immutable Persistent Matrix using Hamster"
13
+ spec.description = "Immutable Persistent Matrix using Hamster that aims to copy as much of the API from Ruby's native Matrix class as possible"
14
+ spec.homepage = "https://github.com/bradurani/hamster-matrix."
15
+ spec.licenses = "public domain"
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+
25
+ spec.add_runtime_dependency 'hamster', '~> 1.0'
26
+ end
@@ -0,0 +1,183 @@
1
+ require "hamster/version"
2
+ require 'hamster'
3
+ require 'matrix'
4
+
5
+ module Hamster
6
+
7
+ def self.matrix(array = Hamster.vector)
8
+ Hamster::Matrix.new(array)
9
+ end
10
+
11
+ class Matrix
12
+
13
+ def self.[](*array)
14
+ Hamster::Matrix.new(array)
15
+ end
16
+
17
+ def self.identity(n)
18
+ return new if n == 0
19
+ new(::Matrix.identity(n).row_vectors)
20
+ end
21
+
22
+ def self.build(row_count, column_count = row_count, &block)
23
+ matrix = ::Matrix.build(row_count, column_count, &block)
24
+ new(matrix.row_vectors)
25
+ end
26
+
27
+ def self.column_vector(array)
28
+ matrix = ::Matrix.column_vector(array)
29
+ new(matrix.row_vectors)
30
+ end
31
+
32
+ def self.columns(array)
33
+ matrix = ::Matrix.columns(array)
34
+ new(matrix.row_vectors)
35
+ end
36
+
37
+ def self.diagonal(*array)
38
+ matrix = ::Matrix.diagonal(*array)
39
+ new(matrix.row_vectors)
40
+ end
41
+
42
+ def self.row_vector(row)
43
+ matrix = ::Matrix.row_vector(row)
44
+ new(matrix.row_vectors)
45
+ end
46
+
47
+ def self.rows(row)
48
+ matrix = ::Matrix.rows(row)
49
+ new(matrix.row_vectors)
50
+ end
51
+
52
+ def self.scalar(n, value)
53
+ matrix = ::Matrix.scalar(n, value)
54
+ new(matrix.row_vectors)
55
+ end
56
+
57
+ class << self
58
+ alias_method :I, :identity
59
+ alias_method :unit, :identity
60
+ end
61
+
62
+ def initialize(array = Hamster.vector)
63
+ enumerable_check!(array)
64
+ width = nil
65
+ @row_vectors = Hamster.vector(*array.map do |a|
66
+ enumerable_check!(a)
67
+ width ||= a.size
68
+ unless(width == a.size)
69
+ raise ExceptionForMatrix::ErrDimensionMismatch.new("row size differs (#{a.size} should be #{width})")
70
+ end
71
+ Hamster.vector(*a)
72
+ end)
73
+ end
74
+
75
+ def ==(other)
76
+ return false unless other.is_a? Hamster::Matrix
77
+ self.row_vectors == other.row_vectors
78
+ end
79
+ alias_method :eql?, :==
80
+
81
+ def get(i,j)
82
+ row = row(i)
83
+ return nil unless row
84
+ row[j]
85
+ end
86
+ alias_method :[], :get
87
+ alias_method :component, :get
88
+ alias_method :element, :get
89
+
90
+ def set(i,j,value)
91
+ if(i >= row_vectors.size)
92
+ raise ExceptionForMatrix::ErrDimensionMismatch.new("I index #{i} outside of array bounds")
93
+ end
94
+ old_row = row(i)
95
+ if(j >= old_row.size)
96
+ raise ExceptionForMatrix::ErrDimensionMismatch.new("J index #{j} outside of array bounds")
97
+ end
98
+ new_row = old_row.set(j, value)
99
+ Hamster::Matrix.new(row_vectors.set(i, new_row))
100
+ end
101
+
102
+ def row(i)
103
+ row_vectors[i]
104
+ end
105
+
106
+ def column_count
107
+ return 0 if row_vectors.size == 0
108
+ return row_vectors.first.size
109
+ end
110
+ alias_method :column_size, :column_count
111
+
112
+ def row_count
113
+ return row_vectors.size
114
+ end
115
+ alias_method :row_size, :row_count
116
+
117
+ def collect(&block)
118
+ raise 'You must pass a block to collect' unless block_given?
119
+ new_rows = row_vectors.map { |row| row.map(&block) }
120
+ return Hamster::Matrix.new(new_rows)
121
+ end
122
+ alias_method :map, :collect
123
+
124
+ def column(n)
125
+ row_vectors.map { |row| row[n] }
126
+ end
127
+
128
+ def column_vectors
129
+ Hamster.vector(*(0..row_vectors.size).map { |n| column(n) })
130
+ end
131
+
132
+ def empty?
133
+ row_vectors.size == 0
134
+ end
135
+
136
+ def hash
137
+ row_vectors.hash
138
+ end
139
+
140
+ def inspect
141
+ if empty?
142
+ "#{self.class}.empty"
143
+ else
144
+ "#{self.class}#{row_string(16)}"
145
+ end
146
+ end
147
+
148
+ def square?
149
+ row_count == column_count
150
+ end
151
+
152
+ def to_a
153
+ row_vectors.map { |row| row.to_a }.to_a
154
+ end
155
+
156
+ def to_s
157
+ row_string(1)
158
+ end
159
+
160
+ def zero?
161
+ row_vectors.all? { |row| row.all? { |elem| elem.is_a?(Numeric) && elem.zero? } }
162
+ end
163
+
164
+ attr_reader :row_vectors
165
+
166
+ private
167
+ def enumerable_check!(enumerable)
168
+ unless enumerable.is_a?(::Enumerable)
169
+ raise TypeError.new('Matrix rows must be Enumerable')
170
+ end
171
+ end
172
+
173
+ def row_string(spaces)
174
+ br = ''
175
+ row_vectors.inject('[') do |acc, row|
176
+ l = "#{acc}#{br}#{row.to_a.to_s}"
177
+ br = "\n#{' ' * spaces}"
178
+ l
179
+ end + ']'
180
+ end
181
+
182
+ end
183
+ end
@@ -0,0 +1,5 @@
1
+ module Hamster
2
+ class Matrix
3
+ VERSION = "0.9.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hamster-matrix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Brad Urani
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-22 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: 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
+ - !ruby/object:Gem::Dependency
56
+ name: hamster
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: Immutable Persistent Matrix using Hamster that aims to copy as much of
70
+ the API from Ruby's native Matrix class as possible
71
+ email:
72
+ - bradurani@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - hamster-matrix.gemspec
89
+ - lib/hamster/matrix.rb
90
+ - lib/hamster/version.rb
91
+ homepage: https://github.com/bradurani/hamster-matrix.
92
+ licenses:
93
+ - public domain
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Immutable Persistent Matrix using Hamster
115
+ test_files: []