nmatrix_extras 0.0.4.1

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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWQ1N2MxY2ExZTI5YTg0MzNmMTI5M2FmY2NmYjI4ZDRmZDZlMmVhYw==
5
+ data.tar.gz: !binary |-
6
+ MGQ2NjRiYmY4ZDg0YzE2YzFhYmIxMzU5Yzc4YWRjMjk3NGViZDg0Mg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NzIwMWY3MzRiMTUzMzdjNDQwMDkwMmIzZjEzNTQxMDU1NWU3ZTc1ZWYzODk4
10
+ ZTNiNTI5YzM4YWRlNDAyYWFjYTg3ZmVjN2QzODViM2YxNzkwY2RkNGQ0NWRl
11
+ ZGY5YTcyMDdkMDdhMWZmMTY0YTk5MGIwZjVjMDA3ODRlMTU0ZmM=
12
+ data.tar.gz: !binary |-
13
+ MjZkZmYzYTczYTI5YjdlNmYwYTU1ODE4ZTMwZTkwZDg2MzNiOTdkNGUxYzQx
14
+ YzljZDI1MjRmZjZkODE0NmMyZTM5M2ZjNTkxZmFmNGU0MzMxYzhiYWU1MzMy
15
+ OWFkZjYyMDAwMGE4NWM2OGFjYjZjZjZmYmUzMmZkMjFlZDQ0NTg=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nmatrix_extras.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Colin J. Fuller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # NmatrixExtras
2
+
3
+ Convenience functions for NMatrix, including some like mean, min, max, present in numpy, as well as mapping and reduction functions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nmatrix_extras'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nmatrix_extras
18
+
19
+ ## Usage
20
+
21
+ require 'nmatrix_extras'
22
+
23
+ This will add the extra methods to the NMatrix class.
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
32
+
33
+ ##License
34
+
35
+ Licensed under the MIT/X11 license. See LICENSE.txt for the full license text.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => [:spec]
7
+
@@ -0,0 +1,29 @@
1
+ #--
2
+ # Copyright (c) 2013 Colin J. Fuller
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the Software), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+ #++
22
+
23
+ require "nmatrix_extras/version"
24
+ require "nmatrix_extras/nmatrix"
25
+
26
+ class NMatrix
27
+ include Enumerable
28
+ include NMatrixExtras
29
+ end
@@ -0,0 +1,155 @@
1
+ #--
2
+ # Copyright (c) 2013 Colin J. Fuller
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the Software), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+ #++
22
+
23
+ require 'nmatrix'
24
+
25
+ module NMatrixExtras
26
+
27
+ ##
28
+ # Successively yields submatrices at each coordinate along a specified dimension. Each submatrix will have the same number of dimensions as the matrix being iterated,
29
+ # but with the specified dimension's size equal to 1.
30
+ #
31
+ # @param [Integer] dim the dimension being iterated over.
32
+ #
33
+ def each_along_dim(dim=0)
34
+ dims = shape
35
+ shape.each_index { |i| dims[i] = 0...(shape[i]) unless i == dim }
36
+ 0.upto(shape[dim]-1) do |i|
37
+ dims[dim] = i
38
+ yield self[*dims]
39
+ end
40
+ end
41
+
42
+ ##
43
+ # Reduces an NMatrix using a supplied block over a specified dimension. The block should behave the same way as for Enumerable#reduce.
44
+ #
45
+ # @param [Integer] dim the dimension being reduced
46
+ # @param [Numeric] initial the initial value for the reduction (i.e. the usual parameter to Enumerable#reduce). Note that unlike Enumerable#reduce, if not specified, this will default to 0.0.
47
+ #
48
+ # @return [NMatrix] an NMatrix with the same number of dimensions as the input, but with the input dimension now having size 1.
49
+ # Each element is the result of the reduction at that position along the specified dimension.
50
+ #
51
+ def reduce_along_dim(dim=0, initial=0.0, &bl)
52
+
53
+ if dim > shape.size then
54
+ raise ArgumentError, "Requested dimension does not exist. Requested: #{dim}, shape: #{shape}"
55
+ end
56
+
57
+ new_shape = shape
58
+ new_shape[dim] = 1
59
+
60
+ acc = NMatrix.new(new_shape, initial)
61
+
62
+ each_along_dim(dim) do |sub_mat|
63
+ acc = bl.call(acc, sub_mat)
64
+ end
65
+
66
+ acc
67
+
68
+ end
69
+
70
+ ##
71
+ # Calculates the mean along the specified dimension.
72
+ #
73
+ # @see #reduce_along_dim
74
+ #
75
+ def mean(dim=0)
76
+ reduce_along_dim(dim, 0.0) do |mean, sub_mat|
77
+ mean + sub_mat/shape[dim]
78
+ end
79
+ end
80
+
81
+ ##
82
+ # Calculates the minimum along the specified dimension.
83
+ #
84
+ # @see #reduce_along_dim
85
+ #
86
+ def min(dim=0)
87
+ reduce_along_dim(dim, Float::MAX) do |min, sub_mat|
88
+ min * (min <= sub_mat) + ((min)*0.0 + (min > sub_mat)) * sub_mat
89
+ end
90
+ end
91
+
92
+ ##
93
+ # Calculates the maximum along the specified dimension.
94
+ #
95
+ # @see #reduce_along_dim
96
+ #
97
+ def max(dim=0)
98
+ reduce_along_dim(dim, -1.0*Float::MAX) do |max, sub_mat|
99
+ max * (max >= sub_mat) + ((max)*0.0 + (max < sub_mat)) * sub_mat
100
+ end
101
+ end
102
+
103
+ ##
104
+ # Calculates the median along the specified dimension.
105
+ #
106
+ # Not yet implemented.
107
+ #
108
+ # @see #reduce_along_dim
109
+ #
110
+ def median(dim=0)
111
+ raise NotImplementedError, "median not yet implemented"
112
+ end
113
+
114
+ ##
115
+ # Calculates the sample variance along the specified dimension.
116
+ #
117
+ # @see #reduce_along_dim
118
+ #
119
+ def variance(dim=0)
120
+ m = mean(dim)
121
+ reduce_along_dim(dim, 0.0) do |var, sub_mat|
122
+ var + (m - sub_mat)*(m - sub_mat)/(shape[dim]-1)
123
+ end
124
+ end
125
+
126
+ ##
127
+ # Converts an nmatrix with a single element (but any number of dimensions) to a float.
128
+ #
129
+ # Raises an IndexError if the matrix does not have just a single element.
130
+ #
131
+ def to_f
132
+ raise IndexError, 'to_f only valid for matrices with a single element' unless shape.all? { |e| e == 1 }
133
+ self[*Array.new(shape.size, 0)]
134
+ end
135
+
136
+ ##
137
+ # See Enumerable#map
138
+ #
139
+ def map(&bl)
140
+ cp = self.dup
141
+ cp.map! &bl
142
+ cp
143
+ end
144
+
145
+ ##
146
+ # Maps in place.
147
+ # See #map
148
+ #
149
+ def map!
150
+ self.each_stored_with_indices do |e, *i|
151
+ self[*i] = (yield e)
152
+ end
153
+ end
154
+
155
+ end
@@ -0,0 +1,3 @@
1
+ module NmatrixExtras
2
+ VERSION = "0.0.4.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nmatrix_extras/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nmatrix_extras"
8
+ spec.version = NmatrixExtras::VERSION
9
+ spec.authors = ["Colin J. Fuller"]
10
+ spec.email = ["cjfuller@gmail.com"]
11
+ spec.description = %q{Extra functions for nmatrix}
12
+ spec.summary = %q{Adds a number of basic functions present in numpy to nmatrix and adds mapping and reduction.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+ spec.add_development_dependency "bundler", "~> 1.3"
19
+ spec.add_development_dependency "rake"
20
+ spec.add_development_dependency "rspec"
21
+ spec.add_runtime_dependency "nmatrix", ">= 0.0.4"
22
+ end
@@ -0,0 +1,120 @@
1
+ #--
2
+ # Copyright (c) 2013 Colin J. Fuller
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the Software), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+ #++
22
+
23
+ require 'nmatrix'
24
+ require 'nmatrix_extras'
25
+
26
+ describe NMatrix do
27
+
28
+ before :each do
29
+ @nm_1d = N[5.0,0.0,1.0,2.0,3.0]
30
+ @nm_2d = N[[0.0,1.0],[2.0,3.0]]
31
+ end
32
+
33
+ context "_like constructors" do
34
+
35
+ it "should create an nmatrix of ones with dimensions and type the same as its argument" do
36
+ pending
37
+ end
38
+
39
+ it "should create an nmatrix of zeros with dimensions and type the same as its argument" do
40
+ pending
41
+ end
42
+
43
+ end
44
+
45
+ it "should calculate the mean along the specified dimension" do
46
+ @nm_1d.mean.should eq N[2.2]
47
+ @nm_2d.mean.should eq N[[1.0,2.0]]
48
+ end
49
+
50
+ it "should calculate the minimum along the specified dimension" do
51
+ @nm_1d.min.should eq N[0.0]
52
+ @nm_2d.min.should eq N[[0.0, 1.0]]
53
+ @nm_2d.min(1).should eq N[[0.0], [2.0]]
54
+
55
+ end
56
+
57
+ it "should calculate the maximum along the specified dimension" do
58
+ @nm_1d.max.should eq N[5.0]
59
+ @nm_2d.max.should eq N[[2.0, 3.0]]
60
+ end
61
+
62
+ it "should calculate the median along the specified dimension" do
63
+ @nm_1d.median.should eq N[2.0]
64
+ @nm_2d.median(1).should eq N[[0.5], [2.5]]
65
+ end
66
+
67
+ it "should calculate the variance along the specified dimension" do
68
+ @nm_1d.variance.should eq N[3.7]
69
+ @nm_2d.variance(1).should eq N[[0.5], [0.5]]
70
+ end
71
+
72
+ it "should calculate the sum along the specified dimension" do
73
+ pending
74
+ end
75
+
76
+ it "should calculate the standard deviation along the specified dimension" do
77
+ pending
78
+ end
79
+
80
+ it "should calculate the correlation coefficient with another same-sized nmatrix" do
81
+ pending
82
+ end
83
+
84
+ it "should calculate the covariance with another same-sized nmatrix" do
85
+ pending
86
+ end
87
+
88
+ it "should raise an ArgumentError when any invalid dimension is provided" do
89
+ pending
90
+ end
91
+
92
+ it "should convert to float if it contains only a single element" do
93
+ N[4.0].to_f.should eq 4.0
94
+ N[[[[4.0]]]].to_f.should eq 4.0
95
+ end
96
+
97
+ it "should raise an index error if it contains more than a single element" do
98
+ expect { @nm_1d.to_f }.to raise_error(IndexError)
99
+ end
100
+
101
+ it "should map a block to all elements" do
102
+ @nm_1d.map { |e| e ** 2 }.should eq N[25.0,0.0,1.0,4.0,9.0]
103
+ @nm_2d.map { |e| e ** 2 }.should eq N[[0.0,1.0],[4.0,9.0]]
104
+ end
105
+
106
+ it "should map! a block to all elements in place" do
107
+ fct = Proc.new { |e| e ** 2 }
108
+ expected1 = @nm_1d.map &fct
109
+ expected2 = @nm_2d.map &fct
110
+ @nm_1d.map! &fct
111
+ @nm_1d.should eq expected1
112
+ @nm_2d.map! &fct
113
+ @nm_2d.should eq expected2
114
+ end
115
+
116
+
117
+
118
+ end
119
+
120
+
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nmatrix_extras
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Colin J. Fuller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
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: nmatrix
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.4
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.4
69
+ description: Extra functions for nmatrix
70
+ email:
71
+ - cjfuller@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/nmatrix_extras.rb
83
+ - lib/nmatrix_extras/nmatrix.rb
84
+ - lib/nmatrix_extras/version.rb
85
+ - nmatrix_extras.gemspec
86
+ - spec/nmatrix_extras_spec.rb
87
+ homepage: ''
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.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Adds a number of basic functions present in numpy to nmatrix and adds mapping
111
+ and reduction.
112
+ test_files:
113
+ - spec/nmatrix_extras_spec.rb