interpolation 0.0.1 → 0.0.2
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 +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +2 -2
- data/README.md +18 -0
- data/interpolation.gemspec +0 -2
- data/lib/interpolation.rb +15 -1
- data/lib/interpolation/base.rb +16 -28
- data/lib/interpolation/one_dimensional.rb +181 -24
- data/lib/monkeys.rb +5 -0
- data/lib/version.rb +1 -1
- data/spec/lib/1d_interpolation_spec.rb +104 -49
- data/spec/lib/2d_interpolation_spec.rb +17 -0
- data/spec/spec_helper.rb +14 -0
- metadata +6 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7fc3cc6ff6163f8b9471a7a2b858426868d0b03
|
4
|
+
data.tar.gz: 47cf81100158e19aae3e99610c552c41c2df545b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c81a2efcec63b92ff04b50a9cd73dc790af2465878ca99e568e50590467e4426c68d611e04c5d4f63d5d7f315cb4953be79faf60f0ca431e4ea4f8f650a62a7
|
7
|
+
data.tar.gz: 09583536302d4b8fe3670008302e643b4b2f02b60754937c96e7406112225da191df03c31aebe8573db041d5e00cddb7d40d54ef5fb6cfb18f18ca6832370708
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,3 +2,21 @@ interpolation
|
|
2
2
|
=============
|
3
3
|
|
4
4
|
Interpolation routines for ruby.
|
5
|
+
|
6
|
+
Note : This library is still under development and currently does not support a lot of interpolation routines.
|
7
|
+
|
8
|
+
Supports the following 1D interpolation routines:
|
9
|
+
* Linear interpolation
|
10
|
+
* Cubic spline interpolation
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
See the documentation for detailed examples.
|
15
|
+
|
16
|
+
## Roadmap
|
17
|
+
|
18
|
+
* Support polynomial interpolation.
|
19
|
+
* N-dimensional interpolation routines.
|
20
|
+
* Optional fast data manipulation with NMatrix.
|
21
|
+
* Easy plotting of results.
|
22
|
+
* Complete NMatrix integration.
|
data/interpolation.gemspec
CHANGED
data/lib/interpolation.rb
CHANGED
@@ -1,5 +1,19 @@
|
|
1
|
+
#--
|
2
|
+
# = Interpolation
|
3
|
+
# Copyright (c) 2014, Sameer Deshmukh
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Interpolation is a library for executing various interpolation
|
7
|
+
# functions in Ruby.
|
8
|
+
#
|
9
|
+
# == spec/lib/1d_interpolation_spec.rb
|
10
|
+
#
|
11
|
+
# Tests for 1D interpolation.
|
12
|
+
#++
|
13
|
+
|
1
14
|
require 'nmatrix'
|
2
15
|
|
3
16
|
$:.unshift File.dirname(__FILE__)
|
4
17
|
|
5
|
-
require 'interpolation/one_dimensional.rb'
|
18
|
+
require 'interpolation/one_dimensional.rb'
|
19
|
+
require 'monkeys.rb'
|
data/lib/interpolation/base.rb
CHANGED
@@ -1,37 +1,31 @@
|
|
1
1
|
#--
|
2
|
-
# = Interpolation
|
2
|
+
# = Interpolation
|
3
|
+
# Copyright (c) 2014, Sameer Deshmukh
|
4
|
+
# All rights reserved.
|
3
5
|
#
|
4
6
|
# Interpolation is a library for executing various interpolation
|
5
|
-
# functions in Ruby.
|
7
|
+
# functions in Ruby.
|
6
8
|
#
|
7
|
-
# ==
|
9
|
+
# == spec/lib/1d_interpolation_spec.rb
|
8
10
|
#
|
9
|
-
#
|
11
|
+
# Tests for 1D interpolation.
|
10
12
|
#++
|
11
13
|
|
12
14
|
module Interpolation
|
13
15
|
class Base
|
14
16
|
def initialize x, y, opts
|
15
|
-
@x, @y
|
17
|
+
@x, @y = x, y
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
axis = (@opts[:axis] ? @opts[:axis] : 0)
|
19
|
+
@opts = {
|
20
|
+
precision: 3,
|
21
|
+
sorted: false,
|
22
|
+
type: :linear
|
23
|
+
}.merge(opts)
|
24
24
|
|
25
25
|
@size = @x.size # considers size of @x only
|
26
26
|
@x = @x.sort unless @opts[:sorted]
|
27
|
+
@x.map! { |n| n = Float(n)}
|
27
28
|
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
29
|
protected
|
36
30
|
|
37
31
|
def locate num
|
@@ -50,15 +44,9 @@ module Interpolation
|
|
50
44
|
end
|
51
45
|
end
|
52
46
|
|
53
|
-
if num == @x[0]
|
54
|
-
|
55
|
-
|
56
|
-
end
|
47
|
+
return 0 if num == @x[0]
|
48
|
+
return @size-2 if num == @x[@size - 1]
|
49
|
+
return jl
|
57
50
|
end
|
58
|
-
|
59
|
-
def invalid_axis?
|
60
|
-
@opts[:axis] and @opts[:axis] > @y.cols-1
|
61
|
-
end
|
62
|
-
|
63
51
|
end
|
64
52
|
end
|
@@ -1,16 +1,18 @@
|
|
1
|
-
$:.unshift File.dirname(__FILE__)
|
2
|
-
|
3
1
|
#--
|
4
|
-
# = Interpolation
|
2
|
+
# = Interpolation
|
3
|
+
# Copyright (c) 2014, Sameer Deshmukh
|
4
|
+
# All rights reserved.
|
5
5
|
#
|
6
6
|
# Interpolation is a library for executing various interpolation
|
7
|
-
# functions in Ruby.
|
7
|
+
# functions in Ruby.
|
8
8
|
#
|
9
|
-
# ==
|
9
|
+
# == spec/lib/1d_interpolation_spec.rb
|
10
10
|
#
|
11
|
-
#
|
11
|
+
# Tests for 1D interpolation.
|
12
12
|
#++
|
13
13
|
|
14
|
+
$:.unshift File.dirname(__FILE__)
|
15
|
+
|
14
16
|
require 'base.rb'
|
15
17
|
|
16
18
|
module Interpolation
|
@@ -19,15 +21,24 @@ module Interpolation
|
|
19
21
|
#
|
20
22
|
# ==== Usage
|
21
23
|
#
|
22
|
-
# x =
|
24
|
+
# x = (1..10).step(1).to_a
|
23
25
|
# y = x.exp
|
24
26
|
#
|
25
|
-
# f =
|
27
|
+
# f = Interpolation::OneDimensional.new x, y, {kind: :linear, sorted: true}
|
26
28
|
# i = f.interpolate 2.5
|
27
29
|
#
|
28
30
|
# puts "Interpolated value for 2.5 is #{i}"
|
29
31
|
class OneDimensional < Interpolation::Base
|
30
32
|
|
33
|
+
# !@attribute return_type
|
34
|
+
# The data type of the returned data. Set to :array, :matrix, nmatrix.
|
35
|
+
# :array by default (only supports Arrray as of now).
|
36
|
+
# @return [Array] Returns data as a ruby Array if set to :array
|
37
|
+
# @return [Matrix] Returns data as a ruby Matrix if set to :matrix
|
38
|
+
# @return [NMatrix] Returns data as an NMatrix if set to :nmatrix. Needs
|
39
|
+
# the NMatrix gem installed.
|
40
|
+
attr_writer :return_type
|
41
|
+
|
31
42
|
# Constructor for all One Dimensional interpolation operations.
|
32
43
|
#
|
33
44
|
# The function values to be supplied to this class are of the form y = f(x).
|
@@ -45,28 +56,41 @@ module Interpolation
|
|
45
56
|
# multiple columns, the interpolation is carried out on each column,
|
46
57
|
# unless specified.
|
47
58
|
#
|
48
|
-
# * +opts+ - Various options for carrying out the interpolation
|
49
|
-
# a hash.
|
59
|
+
# * +opts+ - Various options for carrying out the interpolation.
|
50
60
|
#
|
51
61
|
# ==== Options
|
52
62
|
#
|
53
|
-
# * +:
|
54
|
-
# specified as a symbol.
|
55
|
-
# interpolation supported as of now.
|
63
|
+
# * +:type+ - The kind of interpolation that the user wants to perform. Should be
|
64
|
+
# specified as a symbol. Defaults to linear. Only linear and cubic
|
65
|
+
# interpolation supported as of now. Cubic interpolation done with splines.
|
56
66
|
#
|
57
67
|
# * +:sorted+ - Set this option as *true* if the absicca collection is supplied in
|
58
68
|
# the arguments in a sorted manner. If not supplied, it will be assumed
|
59
69
|
# that absiccas are not sorted and they will sorted be sorted anyway.
|
60
70
|
#
|
61
71
|
# * +:axis+ - In case of a multidimensional ordinate matrix, specify the column over
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
72
|
+
# which interpolation must be performed. *axis* starts indexing from 0 and should be
|
73
|
+
# lower than the number of columns in the ordinate matrix.
|
74
|
+
#
|
65
75
|
# * +:precision+ - Specifies the precision of the interpolated values returned. Defaults
|
66
|
-
#
|
76
|
+
# to 3.
|
77
|
+
#
|
78
|
+
# * +:yp1+ - First derivative of the 0th point (cubic spline).
|
79
|
+
#
|
80
|
+
# * +:ypn+ - First derivative of the last (n-1)th point (cubic spline).
|
67
81
|
#
|
82
|
+
# == Usage
|
83
|
+
#
|
84
|
+
# x = (0..9).step(1).to_a
|
85
|
+
# y = x.map { |n| Math.exp(n) }
|
86
|
+
# f = Interpolation::OneDimensional.new x,y, type: :cubic
|
87
|
+
# f.interpolate 2.5
|
88
|
+
# #=> 12.287
|
68
89
|
def initialize x, y, opts={}
|
90
|
+
@return_type = :array
|
69
91
|
super(x,y,opts)
|
92
|
+
|
93
|
+
compute_second_derivatives if @opts[:type] == :cubic
|
70
94
|
end
|
71
95
|
|
72
96
|
# Performs the actual interpolation on the value passed as an argument. Kind of
|
@@ -81,21 +105,28 @@ module Interpolation
|
|
81
105
|
# all its values. Will return answer in the form of an NMatrix if
|
82
106
|
# *interpolant* is supplied as an NMatrix.
|
83
107
|
def interpolate interpolant
|
84
|
-
|
108
|
+
case @opts[:type]
|
85
109
|
when :linear
|
86
110
|
for_each (interpolant) { |x| linear_interpolation(x) }
|
111
|
+
when :cubic
|
112
|
+
cubic_spline_interpolation interpolant
|
87
113
|
else
|
88
|
-
raise
|
89
|
-
not supported")
|
114
|
+
raise ArgumentError, "1 D interpolation of type #{@opts[:type]} not supported"
|
90
115
|
end
|
91
|
-
|
92
|
-
# return result.to_nm(result.size) if interpolant.is_a?(NMatrix)
|
93
|
-
|
94
|
-
result
|
95
116
|
end
|
96
117
|
|
118
|
+
alias_method :[], :interpolate
|
119
|
+
|
120
|
+
# Return the data passed for interpolation alongwith the interpolated values.
|
121
|
+
# @param [Numeric, Array, NMatrix] interpolant the value of the X co-ordinate
|
122
|
+
# @return [Array, NMatrix] returns data based on the value of return_type
|
123
|
+
# def interp interpolant
|
124
|
+
|
125
|
+
# end
|
97
126
|
private
|
98
127
|
|
128
|
+
# Linear interpolation functions
|
129
|
+
|
99
130
|
def for_each interpolant
|
100
131
|
result = []
|
101
132
|
|
@@ -137,5 +168,131 @@ module Interpolation
|
|
137
168
|
((interpolant - @x[index]) / (@x[index + 1] - @x[index])) *
|
138
169
|
(y[index + 1] - y[index])).round(@opts[:precision])
|
139
170
|
end
|
171
|
+
|
172
|
+
# Cubic spline interpolation functions
|
173
|
+
|
174
|
+
def compute_second_derivatives
|
175
|
+
@opts.merge!({
|
176
|
+
yp1: 1E99,
|
177
|
+
ypn: 1E99
|
178
|
+
})
|
179
|
+
|
180
|
+
if @opts[:axis] == :all
|
181
|
+
compute_multi_axis_second_derivatives
|
182
|
+
else
|
183
|
+
compute_single_axis_second_derivatives
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def compute_single_axis_second_derivatives
|
188
|
+
@y_sd = compute_second_derivatives_for(axis_or_array_for(@y))
|
189
|
+
end
|
190
|
+
|
191
|
+
def compute_multi_axis_second_derivatives
|
192
|
+
@y_sd = []
|
193
|
+
@y.each_column do |column|
|
194
|
+
@y_sd << compute_second_derivatives_for(column)
|
195
|
+
end
|
196
|
+
|
197
|
+
@y_sd = Matrix.columns @y_sd
|
198
|
+
end
|
199
|
+
|
200
|
+
def cubic_spline_interpolation interpolant
|
201
|
+
if @opts[:axis] == :all
|
202
|
+
multi_axis_evaluation_for interpolant
|
203
|
+
else
|
204
|
+
single_axis_evaluation_for interpolant
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def axis_or_array_for object
|
209
|
+
object.is_a?(Array) ? object : object.column(@opts[:axis])
|
210
|
+
end
|
211
|
+
|
212
|
+
def interpolate_over_all_y_columns interpolant
|
213
|
+
results = []
|
214
|
+
0.upto(@y.column_count - 1) do |col_num|
|
215
|
+
results << evaluate_cubic_spline_polynomial(interpolant, @y.column(col_num), @y_sd.column(col_num))
|
216
|
+
end
|
217
|
+
|
218
|
+
results
|
219
|
+
end
|
220
|
+
|
221
|
+
def multi_axis_evaluation_for interpolant
|
222
|
+
if interpolant.is_a?(Array)
|
223
|
+
interpolant.inject([]) do |acc, int|
|
224
|
+
acc << interpolate_over_all_y_columns(int)
|
225
|
+
end
|
226
|
+
else
|
227
|
+
interpolate_over_all_y_columns interpolant
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def single_axis_evaluation_for interpolant
|
232
|
+
y = axis_or_array_for @y
|
233
|
+
y_sd = axis_or_array_for @y_sd
|
234
|
+
|
235
|
+
if interpolant.is_a?(Array)
|
236
|
+
interpolant.inject([]) do |results, int|
|
237
|
+
results << evaluate_cubic_spline_polynomial(int, y, y_sd)
|
238
|
+
end
|
239
|
+
else
|
240
|
+
evaluate_cubic_spline_polynomial interpolant, y, y_sd
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
def evaluate_cubic_spline_polynomial interpolant, y, y_sd
|
245
|
+
klo = locate interpolant
|
246
|
+
khi = klo + 1
|
247
|
+
|
248
|
+
h = @x[khi] - @x[klo]
|
249
|
+
|
250
|
+
raise StandardError, "Wrong input at X index #{klo} and #{khi} for cubic spline" if h == 0
|
251
|
+
|
252
|
+
a = (@x[khi] - interpolant) / h
|
253
|
+
b = (interpolant - @x[klo]) / h
|
254
|
+
(a * y[klo] + b * y[khi] + ((a * a * a - a) * y_sd[klo] +
|
255
|
+
( b * b * b - b) * y_sd[khi]) * ( h * h ) / 6.0).round(@opts[:precision])
|
256
|
+
end
|
257
|
+
|
258
|
+
# References: Numerical Recipes Edition 3. Chapter 3.3
|
259
|
+
def compute_second_derivatives_for y
|
260
|
+
y_sd = Array.new(@size)
|
261
|
+
|
262
|
+
n = y_sd.size
|
263
|
+
u = Array.new(n-1)
|
264
|
+
yp1 = @opts[:yp1] # first derivative of the 0th point as specified by the user
|
265
|
+
ypn = @opts[:ypn] # first derivative of the nth point as specified by the user
|
266
|
+
qn, un = nil, nil
|
267
|
+
|
268
|
+
if yp1 > 0.99E30
|
269
|
+
y_sd[0], u[0] = 0.0, 0.0
|
270
|
+
else
|
271
|
+
y_sd[0] = -0.5
|
272
|
+
u[0] = (3.0 / (@x[1] - @x[0])) * ((y[1] - y[0]) / (@x[1] - @x[0]) - yp1)
|
273
|
+
end
|
274
|
+
|
275
|
+
1.upto(n-2) do |i| # decomposition loop for tridiagonal algorithm
|
276
|
+
sig = ( @x[i] - @x[i-1] ) / ( @x[i+1] - @x[i-1] )
|
277
|
+
p = sig * y_sd[i-1] + 2
|
278
|
+
y_sd[i] = ( sig - 1) / p
|
279
|
+
u[i] = (( y[i+1] - y[i]) / (@x[i+1] - @x[i])) - ((y[i] - y[i-1]) / (@x[i] - @x[i-1]))
|
280
|
+
u[i] = ( 6 * u[i] / ( @x[i+1] - @x[i-1] ) - sig * u[i-1] ) / p;
|
281
|
+
end
|
282
|
+
|
283
|
+
if ypn > 0.99E30
|
284
|
+
qn, un = 0.0, 0.0
|
285
|
+
else
|
286
|
+
qn = 0.5
|
287
|
+
un = (3.0 / ( @x[n-1] - @x[n-2] )) * ( ypn - ( y[n-1] - y[n-2] ) / ( @x[n-1] - @x[n-2] ))
|
288
|
+
end
|
289
|
+
y_sd[n-1] = ( un - qn * u[n-2] ) / ( qn * y_sd[n-2] + 1.0 )
|
290
|
+
|
291
|
+
(n-2).downto(0) do |k|
|
292
|
+
y_sd[k] = y_sd[k] * y_sd[k+1] + u[k]
|
293
|
+
end
|
294
|
+
|
295
|
+
y_sd
|
296
|
+
end
|
140
297
|
end
|
141
298
|
end
|
data/lib/monkeys.rb
ADDED
data/lib/version.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
#--
|
2
|
-
# = Interpolation
|
2
|
+
# = Interpolation
|
3
|
+
# Copyright (c) 2014, Sameer Deshmukh
|
4
|
+
# All rights reserved.
|
3
5
|
#
|
4
6
|
# Interpolation is a library for executing various interpolation
|
5
|
-
# functions in Ruby.
|
7
|
+
# functions in Ruby.
|
6
8
|
#
|
7
9
|
# == spec/lib/1d_interpolation_spec.rb
|
8
10
|
#
|
@@ -13,66 +15,119 @@ require 'spec_helper.rb'
|
|
13
15
|
|
14
16
|
describe Interpolation::OneDimensional do
|
15
17
|
before :each do
|
16
|
-
@x
|
17
|
-
@y
|
18
|
-
@nd
|
18
|
+
@x = (0..9).step(1).to_a
|
19
|
+
@y = @x.map { |e| Math.exp(e) }
|
20
|
+
@nd = Matrix.columns [@y, @y, @y]
|
19
21
|
end
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
context :linear do
|
24
|
+
context "#interpolate" do
|
25
|
+
it "interpolates with 1-D ruby Array" do
|
26
|
+
f = Interpolation::OneDimensional.new([0,1,2,3,4,5,6,7,8,9], [1.0,
|
27
|
+
2.718281828459045, 7.38905609893065, 20.085536923187668, 54.598150033144236,
|
28
|
+
148.4131591025766, 403.4287934927351, 1096.6331584284585, 2980.9579870417283,
|
29
|
+
8103.083927575384], {type: :linear, sorted: true})
|
26
30
|
|
27
|
-
|
28
|
-
|
31
|
+
expect(f.interpolate(2.5)).to eq 13.737
|
32
|
+
end
|
29
33
|
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
it "interpolates with single axis inout" do
|
35
|
+
f = Interpolation::OneDimensional.new(@x, @y, {type: :linear,
|
36
|
+
precision: 3})
|
33
37
|
|
34
|
-
|
38
|
+
expect(f.interpolate(2.5)) .to eq 13.737
|
35
39
|
|
36
|
-
|
37
|
-
|
40
|
+
expect(f.interpolate([2.5,6.7,0.3,8.6])).to eq [13.737, 888.672,
|
41
|
+
1.515, 6054.234]
|
38
42
|
|
39
|
-
|
40
|
-
|
41
|
-
|
43
|
+
# expect(f.interpolate(NMatrix.new([4,1], [2.5,6.7,0.3,8.6]))).to eq NMatrix.new(
|
44
|
+
# [4], [13.737, 888.672, 1.515, 6054.234])
|
45
|
+
end
|
42
46
|
|
43
|
-
|
47
|
+
it "interpolates over all axes" do
|
48
|
+
f = Interpolation::OneDimensional.new(@x,@nd, {type: :linear,
|
49
|
+
sorted: true, precision: 3})
|
44
50
|
|
45
|
-
|
46
|
-
|
51
|
+
expect(f.interpolate(2.5)) .to eq [13.737,13.737,13.737]
|
52
|
+
|
53
|
+
expect(f.interpolate([2.5,6.7,0.3,8.6])).to eq \
|
54
|
+
[ [13.737 , 13.737 , 13.737 ],
|
55
|
+
[888.672 , 888.672 , 888.672],
|
56
|
+
[1.515 , 1.515 , 1.515 ],
|
57
|
+
[6054.234, 6054.234, 6054.234 ]
|
58
|
+
]
|
47
59
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
# expect(f.interpolate(NMatrix.new([4,1], [2.5,6.7,0.3,8.6]))).to eq \
|
61
|
+
# NMatrix.new([4,3],
|
62
|
+
# [ 13.737 , 13.737 , 13.737 ,
|
63
|
+
# 888.672 , 888.672 , 888.672,
|
64
|
+
# 1.515 , 1.515 , 1.515 ,
|
65
|
+
# 6054.234, 6054.234, 6054.234
|
66
|
+
# ])
|
67
|
+
end
|
68
|
+
|
69
|
+
it "interpolates on specified axis" do
|
70
|
+
f = Interpolation::OneDimensional.new(@x, @nd, {type: :linear, axis: 1,
|
71
|
+
sorted: true, precision: 3})
|
72
|
+
|
73
|
+
expect(f.interpolate(3.5)) .to eq 37.342
|
74
|
+
|
75
|
+
expect(f.interpolate([2.5,6.7,0.3,8.6])).to eq [13.737, 888.672,
|
76
|
+
1.515, 6054.234]
|
77
|
+
|
78
|
+
# expect(f.interpolate(NMatrix.new([4,1], [2.5,6.7,0.3,8.6]))).to eq NMatrix.new(
|
79
|
+
# [4], [13.737, 888.672, 1.515, 6054.234])
|
80
|
+
end
|
64
81
|
end
|
65
82
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
83
|
+
context "#interp"
|
84
|
+
end
|
85
|
+
|
86
|
+
context :slinear do
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
context :quadratic do
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
context :cubic do
|
95
|
+
context "#interpolation" do
|
96
|
+
it "correctly interpolates for single axis co-ordinates" do
|
97
|
+
f = Interpolation::OneDimensional.new(@x, @y, type: :cubic, sorted: true)
|
98
|
+
|
99
|
+
expect(f.interpolate(2.5)).to eq(12.287)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "interpolates for multiple points" do
|
103
|
+
f = Interpolation::OneDimensional.new(@x, @y, type: :cubic, sorted: true)
|
104
|
+
|
105
|
+
expect(f.interpolate([2.5,3.5,4,6.5])).to eq([12.287, 32.577, 54.598, 688.288])
|
106
|
+
end
|
107
|
+
|
108
|
+
it "interpolates over all axes" do
|
109
|
+
f = Interpolation::OneDimensional.new(@x, @nd, type: :cubic, sorted: true, axis: :all)
|
71
110
|
|
72
|
-
|
73
|
-
1.515, 6054.234]
|
111
|
+
expect(f.interpolate(2.5)).to eq([12.287, 12.287, 12.287])
|
74
112
|
|
75
|
-
|
76
|
-
|
113
|
+
expect(f.interpolate([2.5,3.5,4,6.5])). to eq([
|
114
|
+
[12.287, 12.287, 12.287],
|
115
|
+
[32.577, 32.577, 32.577],
|
116
|
+
[54.598, 54.598, 54.598],
|
117
|
+
[688.288, 688.288, 688.288]
|
118
|
+
]
|
119
|
+
)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "interpolates only on the specified axis" do
|
123
|
+
f = Interpolation::OneDimensional.new(@x, @nd, type: :cubic, sorted: true, axis: 1)
|
124
|
+
|
125
|
+
expect(f.interpolate([2.5,3.5,4,6.5])).to eq([12.287, 32.577, 54.598, 688.288])
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "#interp" do
|
130
|
+
# TODO
|
131
|
+
end
|
77
132
|
end
|
78
133
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#--
|
2
|
+
# = Interpolation
|
3
|
+
# Copyright (c) 2014, Sameer Deshmukh
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Interpolation is a library for executing various interpolation
|
7
|
+
# functions in Ruby.
|
8
|
+
#
|
9
|
+
# == spec/lib/2d_interpolation_spec.rb
|
10
|
+
#
|
11
|
+
# Tests for 2D interpolation.
|
12
|
+
#++
|
13
|
+
|
14
|
+
|
15
|
+
# describe Interpolation::TwoDimensional do
|
16
|
+
|
17
|
+
# end
|
data/spec/spec_helper.rb
CHANGED
@@ -1 +1,15 @@
|
|
1
|
+
#--
|
2
|
+
# = Interpolation
|
3
|
+
# Copyright (c) 2014, Sameer Deshmukh
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Interpolation is a library for executing various interpolation
|
7
|
+
# functions in Ruby.
|
8
|
+
#
|
9
|
+
# == spec/lib/1d_interpolation_spec.rb
|
10
|
+
#
|
11
|
+
# Tests for 1D interpolation.
|
12
|
+
#++
|
13
|
+
|
14
|
+
require 'matrix'
|
1
15
|
require 'interpolation'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interpolation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sameer Deshmukh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
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
41
|
description: Interpolation is a library for executing various interpolation functions
|
56
42
|
in Ruby.
|
57
43
|
email:
|
@@ -60,6 +46,7 @@ executables: []
|
|
60
46
|
extensions: []
|
61
47
|
extra_rdoc_files: []
|
62
48
|
files:
|
49
|
+
- ".gitignore"
|
63
50
|
- ".rspec"
|
64
51
|
- Gemfile
|
65
52
|
- Gemfile.lock
|
@@ -70,8 +57,10 @@ files:
|
|
70
57
|
- lib/interpolation.rb
|
71
58
|
- lib/interpolation/base.rb
|
72
59
|
- lib/interpolation/one_dimensional.rb
|
60
|
+
- lib/monkeys.rb
|
73
61
|
- lib/version.rb
|
74
62
|
- spec/lib/1d_interpolation_spec.rb
|
63
|
+
- spec/lib/2d_interpolation_spec.rb
|
75
64
|
- spec/spec_helper.rb
|
76
65
|
homepage: https://github.com/v0dro/interpolation
|
77
66
|
licenses:
|
@@ -98,3 +87,4 @@ signing_key:
|
|
98
87
|
specification_version: 4
|
99
88
|
summary: Interpolation routines in ruby.
|
100
89
|
test_files: []
|
90
|
+
has_rdoc:
|