interpolator 0.11 → 0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/interpolator.rb +102 -18
  2. metadata +3 -3
@@ -1,5 +1,37 @@
1
+ # Copyright (c) 2009 Eric Todd Meyers
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person
4
+ # obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without
6
+ # restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the
9
+ # Software is furnished to do so, subject to the following
10
+ # conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ # OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+
1
25
  module Interpolator
2
26
 
27
+ # Table holds a series of independent and dependent values that can be interpolated or extrapolated.
28
+ # The independent values are always floating points numbers. The dependent values can either be floating point
29
+ # numbers or themselves Table instances. By nesting the Table in this way a Table of any dimension can be
30
+ # created.
31
+ #
32
+ # Tables can be told to not extrapolate by setting .extrapolate=false
33
+ # The interpolation method is controlled with .style = LINEAR,LAGRANGE2, or LAGRANGE3
34
+ #
3
35
  class Table
4
36
 
5
37
  attr_reader :extrapolate,:style
@@ -8,9 +40,43 @@ module Interpolator
8
40
  LINEAR = 1
9
41
  LAGRANGE2 = 2
10
42
  LAGRANGE3 = 3
11
-
43
+
44
+ # Tables are constructed from either a pair of Arrays or a single Hash.
45
+ #
46
+ # The 2 argument constructor accepts and array of independents and an array of dependents. The
47
+ # independent Array should be floating point values. The dependent Array can be either floating
48
+ # point values or Tables (aka sub tables.) There is no limit to how deep a Table can be.
49
+ #
50
+ # The single argument constructor is similar. The keys of the Hash are the independents. The values
51
+ # of the Hash are the dependent values, and can either be floating point numbers or Tables.
52
+ #
53
+ # Examples
54
+ # * simple univariant table
55
+ #
56
+ # t = Table.new [1.0,2.0],[3.0,4.0]
57
+ # and is equivalent to
58
+ # t = Table.new(1.0=>3.0,2.0=>4.0)
59
+ #
60
+ # * bivariant table
61
+ #
62
+ # t = Table.new([1.0,2.0],[Table.new([1.0,2.0],[3.0,4.0]),Table.new([2.0,3.0,5.0],[6.0,-1.0,7.0])])
63
+ #
64
+ # * trivariant table
65
+ #
66
+ # t = Table.new(
67
+ # 1=>Table.new(
68
+ # 1=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
69
+ # 4=>Table.new([11.0,12.0,13.0],[14.0,15.0,16.0]),
70
+ # 5=>Table.new([11.0,12.0,13.0],[-14.0,-15.0,-16.0])),
71
+ # 2=>Table.new(
72
+ # 2=>Table.new([1.1,2.0,3.0],[4.0,5.0,6.0]),
73
+ # 5=>Table.new([11.0,12.5,13.0],[14.0,15.0,16.0]),
74
+ # 6.2=>Table.new([1.0,12.0],[-14.0,-16.0])),
75
+ # 8=>Table.new(
76
+ # 1=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
77
+ # 5=>Table.new([11.0,12.0,13.0],[-14.0,-15.0,-16.0]))
78
+ # )
12
79
  def initialize (*args)
13
-
14
80
  if (args.size==2) then
15
81
  raise "duel argument table constructor must be 2 Arrays" unless args[0].kind_of? Array
16
82
  raise "duel argument table constructor must be 2 Arrays" unless args[1].kind_of? Array
@@ -35,20 +101,18 @@ module Interpolator
35
101
  @extrapolate = true
36
102
  @style = LINEAR
37
103
  end
38
-
39
- # replace later with a high speed bisection
40
-
41
- def bracket (x)
42
- ileft=0
43
- for i in (0..@inds.size-2) do
44
- ileft=i
45
- if ( (x >= @inds[i]) && (x < @inds[i+1]) ) then
46
- break
47
- end
48
- end
49
- ileft
50
- end
51
-
104
+ # Interpolate or extrapolate the Table. Pass as many arguments as there are independent dimensions to the table (univariant a
105
+ # single argument, bivariant 2 arguments, etc.)
106
+ #
107
+ # Examples
108
+ # * univariant
109
+ # t = Table.new [1.0,2.0],[3.0,4.0]
110
+ # t.read(1.5) returns 3.5
111
+ # * bivariant
112
+ # t = Table.new([1.0,2.0],[Table.new([1.0,2.0],[3.0,4.0]),Table.new([2.0,3.0,5.0],[6.0,-1.0,7.0])])
113
+ # t.read(2.0,3.0) returns -1.0
114
+ # t.read(1.7,2.0) returns 5.4
115
+ #
52
116
  def read(*args)
53
117
 
54
118
  raise "table requires at least 2 points for linear interpolation" if (@style == LINEAR && @inds.size<2)
@@ -163,9 +227,27 @@ module Interpolator
163
227
  ans
164
228
  end
165
229
 
230
+ #
231
+ # Same as read
232
+ #
166
233
  def interpolate(*args)
167
234
  read(*args)
168
- end
235
+ end
236
+
237
+ protected
238
+
239
+ # replace later with a high speed bisection
240
+
241
+ def bracket (x)
242
+ ileft=0
243
+ for i in (0..@inds.size-2) do
244
+ ileft=i
245
+ if ( (x >= @inds[i]) && (x < @inds[i+1]) ) then
246
+ break
247
+ end
248
+ end
249
+ ileft
250
+ end
169
251
 
170
252
  def linear (x,x1,x2,y1,y2)
171
253
  r = (y2-y1) / (x2-x1) * (x-x1) + y1
@@ -205,7 +287,9 @@ module Interpolator
205
287
  if __FILE__ == $0 then
206
288
 
207
289
  require 'test/unit'
208
-
290
+ #
291
+ # Unit test for Table class
292
+ #
209
293
  class TC_LookupTest < Test::Unit::TestCase
210
294
  def setup
211
295
  @t1 = Table.new [1.0,2.0],[3.0,4.0]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interpolator
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.11"
4
+ version: "0.12"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric T Meyers
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-02 00:00:00 -04:00
12
+ date: 2009-08-03 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -51,6 +51,6 @@ rubyforge_project: interpolator
51
51
  rubygems_version: 1.3.5
52
52
  signing_key:
53
53
  specification_version: 3
54
- summary: Module Interpolation proves a table class that supports n-dimensional numerical table construction, interpolation and extrapolation. Includes linear, 2nd order and 3rd order la grange techniques.
54
+ summary: Module Interpolator proves a table class that supports n-dimensional numerical table construction, interpolation and extrapolation. Includes linear, 2nd order and 3rd order la grange techniques.
55
55
  test_files: []
56
56