interpolation 0.0.1

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: 736c1de695651f5d9b074dbc0e4dfe56614c3ad4
4
+ data.tar.gz: 4ce0806e958fbff6c78fbe94992f0f8259f46955
5
+ SHA512:
6
+ metadata.gz: ce4fe2d42c2bf757b8cea3b647c754e7797878ad22c29fd1ae388b9b9a4a55d80f21015bb5f590b9dcd3fb70869e27d21f421ca529e4f2bfa5b378dfbe57440a
7
+ data.tar.gz: 9bc487d1338da2ecfd509028d9b9fdbc4962f2e54b525d821bd403c5b2af1009b3b423ab87f0cd20814d5b2d36cf3a83b5c2560927a0abef5b1fc4b89cbb6cb2
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ interpolation (0.0.1)
5
+ nmatrix (~> 0.1.0.rc5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ json (1.8.1)
12
+ nmatrix (0.1.0.rc5)
13
+ rdoc (~> 4.0, >= 4.0.1)
14
+ rake (10.3.2)
15
+ rdoc (4.1.1)
16
+ json (~> 1.4)
17
+ rspec (3.1.0)
18
+ rspec-core (~> 3.1.0)
19
+ rspec-expectations (~> 3.1.0)
20
+ rspec-mocks (~> 3.1.0)
21
+ rspec-core (3.1.1)
22
+ rspec-support (~> 3.1.0)
23
+ rspec-expectations (3.1.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.1.0)
26
+ rspec-mocks (3.1.0)
27
+ rspec-support (~> 3.1.0)
28
+ rspec-support (3.1.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ interpolation!
35
+ rake
36
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ Copyright (c) 2014, Sameer Deshmukh
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of the {organization} nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
@@ -0,0 +1,4 @@
1
+ interpolation
2
+ =============
3
+
4
+ Interpolation routines for ruby.
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ require 'version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "interpolation"
8
+ gem.version = Interpolation::VERSION
9
+ gem.summary = "Interpolation routines in ruby."
10
+ gem.description = "Interpolation is a library for executing various interpolation functions in Ruby."
11
+ gem.homepage = 'https://github.com/v0dro/interpolation'
12
+ gem.authors = ['Sameer Deshmukh']
13
+ gem.email = ['sameer.deshmukh93@gmail.com']
14
+ gem.license = 'BSD 3-clause'
15
+ gem.files = `git ls-files`.split("\n")
16
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.required_ruby_version = '>= 1.9.3'
20
+
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'rspec'
23
+
24
+ gem.add_runtime_dependency 'nmatrix', '~> 0.1.0.rc5'
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'nmatrix'
2
+
3
+ $:.unshift File.dirname(__FILE__)
4
+
5
+ require 'interpolation/one_dimensional.rb'
@@ -0,0 +1,64 @@
1
+ #--
2
+ # = Interpolation
3
+ #
4
+ # Interpolation is a library for executing various interpolation
5
+ # functions in Ruby. Works with NMatrix.
6
+ #
7
+ # == interpolation/base.rb
8
+ #
9
+ # Base class for all interpolation methods.
10
+ #++
11
+
12
+ module Interpolation
13
+ class Base
14
+ def initialize x, y, opts
15
+ @x, @y, @opts = x, y, opts
16
+
17
+ raise ArgumentError, "Specify the kind of interpolation?" if !@opts[:kind]
18
+ raise DataTypeError, "Invalid data type of x/y" if !valid_dtypes?
19
+ raise ArgumentError, "Axis specified out of bounds" if invalid_axis?
20
+
21
+ @opts[:precision] = 3 if !@opts[:precision]
22
+
23
+ axis = (@opts[:axis] ? @opts[:axis] : 0)
24
+
25
+ @size = @x.size # considers size of @x only
26
+ @x = @x.sort unless @opts[:sorted]
27
+ end
28
+ private
29
+
30
+ def valid_dtypes?
31
+ (@x.is_a?(Array) or @x.is_a?(NMatrix)) and
32
+ (@y.is_a?(Array) or @y.is_a?(NMatrix))
33
+ end
34
+
35
+ protected
36
+
37
+ def locate num
38
+ # Returns the index of the value 'num' such that x[j] > num > x[j+1]
39
+ ascnd = (@x[-1] >= @x[0])
40
+
41
+ jl, jm, ju = 0, 0,@x.size-1
42
+
43
+ while ju - jl > 1
44
+ jm = (ju+jl)/2
45
+
46
+ if num >= @x[jm] == ascnd
47
+ jl = jm
48
+ else
49
+ ju = jm
50
+ end
51
+ end
52
+
53
+ if num == @x[0] then return 0
54
+ elsif num == @x[@size-1] then return @size-2
55
+ else return jl
56
+ end
57
+ end
58
+
59
+ def invalid_axis?
60
+ @opts[:axis] and @opts[:axis] > @y.cols-1
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,141 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ #--
4
+ # = Interpolation
5
+ #
6
+ # Interpolation is a library for executing various interpolation
7
+ # functions in Ruby. Works with NMatrix.
8
+ #
9
+ # == interpolation/one_dimensional.rb
10
+ #
11
+ # Methods for performing one dimensional interpolation.
12
+ #++
13
+
14
+ require 'base.rb'
15
+
16
+ module Interpolation
17
+
18
+ # Implements one dimensional interpolation routines.
19
+ #
20
+ # ==== Usage
21
+ #
22
+ # x = NMatrix.seq [10]
23
+ # y = x.exp
24
+ #
25
+ # f = NMatrix::Interpolation::OneDimensional.new x, y, {kind: :linear, sorted: true}
26
+ # i = f.interpolate 2.5
27
+ #
28
+ # puts "Interpolated value for 2.5 is #{i}"
29
+ class OneDimensional < Interpolation::Base
30
+
31
+ # Constructor for all One Dimensional interpolation operations.
32
+ #
33
+ # The function values to be supplied to this class are of the form y = f(x).
34
+ #
35
+ # Henceforth, y will be referred to as ordinate and x as absicca. If absicca
36
+ # and ordinate arrays are not of the same length, then the effective size used
37
+ # for interpolation will be MIN(x.size, y.size).
38
+ #
39
+ # ==== Arguments
40
+ #
41
+ # * +x+ - The collection of absiccas. Must be a 1 D NMatrix or ruby Array.
42
+ #
43
+ # * +y+ - The collection of ordinates corresponding to the absicca. 'y' can
44
+ # either be a 1D NMatrix or Array OR a 2D NMatrix. In case y contains
45
+ # multiple columns, the interpolation is carried out on each column,
46
+ # unless specified.
47
+ #
48
+ # * +opts+ - Various options for carrying out the interpolation, to be specified as
49
+ # a hash.
50
+ #
51
+ # ==== Options
52
+ #
53
+ # * +:kind+ - The kind of interpolation that the user wants to perform. Should be
54
+ # specified as a symbol. This option is compulsory. Only linear
55
+ # interpolation supported as of now.
56
+ #
57
+ # * +:sorted+ - Set this option as *true* if the absicca collection is supplied in
58
+ # the arguments in a sorted manner. If not supplied, it will be assumed
59
+ # that absiccas are not sorted and they will sorted be sorted anyway.
60
+ #
61
+ # * +:axis+ - In case of a multidimensional ordinate matrix, specify the column over
62
+ # which interpolation must be performed. *axis* starts indexing from 0
63
+ # and should be lower than the number of columns in the ordinate matrix.
64
+ #
65
+ # * +:precision+ - Specifies the precision of the interpolated values returned. Defaults
66
+ # to 3.
67
+ #
68
+ def initialize x, y, opts={}
69
+ super(x,y,opts)
70
+ end
71
+
72
+ # Performs the actual interpolation on the value passed as an argument. Kind of
73
+ # interpolation performed is determined according to what is specified in the
74
+ # constructor.
75
+ #
76
+ # ==== Arguments
77
+ #
78
+ # * +interpolant+ - The value for which the interpolation is to be performed. Can
79
+ # either be a Numeric, Array of Numerics or NMatrix. If multidimensional
80
+ # NMatrix is supplied then will flatten it and interpolate over
81
+ # all its values. Will return answer in the form of an NMatrix if
82
+ # *interpolant* is supplied as an NMatrix.
83
+ def interpolate interpolant
84
+ result = case @opts[:kind]
85
+ when :linear
86
+ for_each (interpolant) { |x| linear_interpolation(x) }
87
+ else
88
+ raise(ArgumentError, "1 D interpolation of kind #{@opts[:kind]} \
89
+ not supported")
90
+ end
91
+
92
+ # return result.to_nm(result.size) if interpolant.is_a?(NMatrix)
93
+
94
+ result
95
+ end
96
+
97
+ private
98
+
99
+ def for_each interpolant
100
+ result = []
101
+
102
+ if interpolant.kind_of? Numeric
103
+ return yield interpolant
104
+ else
105
+ interpolant.each { |x| result << yield(x) }
106
+ end
107
+
108
+ result
109
+ end
110
+
111
+ def linear_interpolation interpolant
112
+ # TODO : Make this more efficient by using hunt/locate from NR
113
+ index = locate(interpolant)
114
+ same = @x[index] == @x[index+1]
115
+ result = []
116
+
117
+ if (@y.respond_to?(:vector?) and @y.vector?) or
118
+ @y.instance_of?(Array)
119
+
120
+ return @y[index] if same
121
+ return _lin_interpolator @y, index, interpolant
122
+ elsif @opts[:axis]
123
+
124
+ return @y.column(@opts[:axis])[index] if same
125
+ return _lin_interpolator @y.column(@opts[:axis]), index, interpolant
126
+ else
127
+ @y.each_column do |c|
128
+ result << (same ? c[index] : _lin_interpolator(c, index, interpolant))
129
+ end
130
+ end
131
+
132
+ result
133
+ end
134
+
135
+ def _lin_interpolator y, index, interpolant
136
+ (y[index] +
137
+ ((interpolant - @x[index]) / (@x[index + 1] - @x[index])) *
138
+ (y[index + 1] - y[index])).round(@opts[:precision])
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,3 @@
1
+ module Interpolation
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,78 @@
1
+ #--
2
+ # = Interpolation
3
+ #
4
+ # Interpolation is a library for executing various interpolation
5
+ # functions in Ruby. Works with NMatrix.
6
+ #
7
+ # == spec/lib/1d_interpolation_spec.rb
8
+ #
9
+ # Tests for 1D interpolation.
10
+ #++
11
+
12
+ require 'spec_helper.rb'
13
+
14
+ describe Interpolation::OneDimensional do
15
+ before :each do
16
+ @x = NMatrix.seq [10]
17
+ @y = @x.exp
18
+ @nd = NMatrix.new([10,3]).each_column { |c| c[0..9] = @y }
19
+ end
20
+
21
+ it "tests if OneDimensional accepts Array inputs" do
22
+ f = Interpolation::OneDimensional.new([0,1,2,3,4,5,6,7,8,9], [1.0,
23
+ 2.718281828459045, 7.38905609893065, 20.085536923187668, 54.598150033144236,
24
+ 148.4131591025766, 403.4287934927351, 1096.6331584284585, 2980.9579870417283,
25
+ 8103.083927575384], {kind: :linear, sorted: true})
26
+
27
+ expect(f.interpolate(2.5)).to eq 13.737
28
+ end
29
+
30
+ it "tests for linear interpolation for 1-dimensional y values" do
31
+ f = Interpolation::OneDimensional.new(@x, @y, {kind: :linear,
32
+ precision: 3})
33
+
34
+ expect(f.interpolate(2.5)) .to eq 13.737
35
+
36
+ expect(f.interpolate([2.5,6.7,0.3,8.6])).to eq [13.737, 888.672,
37
+ 1.515, 6054.234]
38
+
39
+ # expect(f.interpolate(NMatrix.new([4,1], [2.5,6.7,0.3,8.6]))).to eq NMatrix.new(
40
+ # [4], [13.737, 888.672, 1.515, 6054.234])
41
+ end
42
+
43
+ it "tests linear interpolation for N-dimensional y values" do
44
+
45
+ f = Interpolation::OneDimensional.new(@x,@nd, {kind: :linear,
46
+ sorted: true, precision: 3})
47
+
48
+ expect(f.interpolate(2.5)) .to eq [13.737,13.737,13.737]
49
+
50
+ expect(f.interpolate([2.5,6.7,0.3,8.6])).to eq \
51
+ [ [13.737 , 13.737 , 13.737 ],
52
+ [888.672 , 888.672 , 888.672],
53
+ [1.515 , 1.515 , 1.515 ],
54
+ [6054.234, 6054.234, 6054.234 ]
55
+ ]
56
+
57
+ # expect(f.interpolate(NMatrix.new([4,1], [2.5,6.7,0.3,8.6]))).to eq \
58
+ # NMatrix.new([4,3],
59
+ # [ 13.737 , 13.737 , 13.737 ,
60
+ # 888.672 , 888.672 , 888.672,
61
+ # 1.515 , 1.515 , 1.515 ,
62
+ # 6054.234, 6054.234, 6054.234
63
+ # ])
64
+ end
65
+
66
+ it "tests linear interpolation for N-dimensional y on another axis" do
67
+ f = Interpolation::OneDimensional.new(@x, @nd, {kind: :linear, axis: 1,
68
+ sorted: true, precision: 3})
69
+
70
+ expect(f.interpolate(3.5)) .to eq 37.342
71
+
72
+ expect(f.interpolate([2.5,6.7,0.3,8.6])).to eq [13.737, 888.672,
73
+ 1.515, 6054.234]
74
+
75
+ # expect(f.interpolate(NMatrix.new([4,1], [2.5,6.7,0.3,8.6]))).to eq NMatrix.new(
76
+ # [4], [13.737, 888.672, 1.515, 6054.234])
77
+ end
78
+ end
@@ -0,0 +1 @@
1
+ require 'interpolation'
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interpolation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sameer Deshmukh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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: nmatrix
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.0.rc5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0.rc5
55
+ description: Interpolation is a library for executing various interpolation functions
56
+ in Ruby.
57
+ email:
58
+ - sameer.deshmukh93@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rspec"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - interpolation.gemspec
70
+ - lib/interpolation.rb
71
+ - lib/interpolation/base.rb
72
+ - lib/interpolation/one_dimensional.rb
73
+ - lib/version.rb
74
+ - spec/lib/1d_interpolation_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/v0dro/interpolation
77
+ licenses:
78
+ - BSD 3-clause
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 1.9.3
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Interpolation routines in ruby.
100
+ test_files: []