interpolator 0.14 → 0.15
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.
- data/interpolator.rb +537 -446
- metadata +5 -7
data/interpolator.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# Copyright (c) 2009 Eric Todd Meyers
|
|
2
|
-
#
|
|
2
|
+
#
|
|
3
3
|
# Permission is hereby granted, free of charge, to any person
|
|
4
4
|
# obtaining a copy of this software and associated documentation
|
|
5
5
|
# files (the "Software"), to deal in the Software without
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
# copies of the Software, and to permit persons to whom the
|
|
9
9
|
# Software is furnished to do so, subject to the following
|
|
10
10
|
# conditions:
|
|
11
|
-
#
|
|
11
|
+
#
|
|
12
12
|
# The above copyright notice and this permission notice shall be
|
|
13
13
|
# included in all copies or substantial portions of the Software.
|
|
14
|
-
#
|
|
14
|
+
#
|
|
15
15
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
16
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
17
|
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
@@ -27,501 +27,592 @@
|
|
|
27
27
|
#
|
|
28
28
|
module Interpolator
|
|
29
29
|
|
|
30
|
-
# Table holds a series of independent and dependent values that can be interpolated or extrapolated.
|
|
31
|
-
# The independent values are always floating points numbers. The dependent values can either be floating point
|
|
32
|
-
# numbers or themselves Table instances. By nesting the Table in this way a Table of any dimension can be
|
|
33
|
-
# created.
|
|
34
|
-
#
|
|
35
|
-
# Tables can be told to not extrapolate by setting .extrapolate=false
|
|
36
|
-
# The interpolation method is controlled with .style = LINEAR, LAGRANGE2, LAGRANGE3, CUBIC (also known as natural spline), or CATMULL (spline)
|
|
37
|
-
#
|
|
38
|
-
# The style and extrapolate attributes are only applied to that specific Table. They are not propagated to subtables. Each subtable
|
|
39
|
-
# can of course accept it's own attributes.
|
|
40
|
-
#
|
|
41
|
-
# More information, source code and gems @ http://rubyforge.org/projects/interpolator/
|
|
42
|
-
#
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
attr_reader :extrapolate,:style
|
|
46
|
-
attr_writer :extrapolate,:style
|
|
47
|
-
|
|
48
|
-
LINEAR = 1
|
|
49
|
-
LAGRANGE2 = 2
|
|
50
|
-
LAGRANGE3 = 3
|
|
51
|
-
CUBIC = 4
|
|
52
|
-
CATMULL = 5
|
|
53
|
-
#
|
|
54
|
-
# Tables are constructed from either a pair of Arrays or a single Hash.
|
|
55
|
-
#
|
|
56
|
-
# The 2 argument constructor accepts an array of independents and an array of dependents. The
|
|
57
|
-
# independent Array should be floating point values. The dependent Array can be either floating
|
|
58
|
-
# point values or Tables (aka sub tables.) There is no limit to how deep a Table can be.
|
|
59
|
-
#
|
|
60
|
-
# The single argument constructor is similar. The keys of the Hash are the independents. The values
|
|
61
|
-
# of the Hash are the dependent values, and can either be floating point numbers or Tables.
|
|
62
|
-
#
|
|
63
|
-
# Examples
|
|
64
|
-
# * simple univariant table
|
|
65
|
-
#
|
|
66
|
-
# t = Table.new [1.0,2.0],[3.0,4.0]
|
|
67
|
-
# and is equivalent to
|
|
68
|
-
# t = Table.new(1.0=>3.0,2.0=>4.0)
|
|
69
|
-
#
|
|
70
|
-
# set attributes inline
|
|
71
|
-
# t = Table.new([1.0,2.0],[3.0,4.0]) do |tab| tab.extrapolate=false end
|
|
72
|
-
#
|
|
73
|
-
# * bivariant table
|
|
74
|
-
#
|
|
75
|
-
# 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])])
|
|
76
|
-
#
|
|
77
|
-
# * trivariant table
|
|
78
|
-
#
|
|
79
|
-
# t = Table.new(
|
|
80
|
-
# 1=>Table.new(
|
|
81
|
-
# 1=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
|
|
82
|
-
# 4=>Table.new([11.0,12.0,13.0],[14.0,15.0,16.0]),
|
|
83
|
-
# 5=>Table.new([11.0,12.0,13.0],[-14.0,-15.0,-16.0])),
|
|
84
|
-
# 2=>Table.new(
|
|
85
|
-
# 2=>Table.new([1.1,2.0,3.0],[4.0,5.0,6.0]),
|
|
86
|
-
# 5=>Table.new([11.0,12.5,13.0],[14.0,15.0,16.0]),
|
|
87
|
-
# 6.2=>Table.new([1.0,12.0],[-14.0,-16.0])),
|
|
88
|
-
# 8=>Table.new(
|
|
89
|
-
# 1=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
|
|
90
|
-
# 5=>Table.new([11.0,12.0,13.0],[-14.0,-15.0,-16.0]))
|
|
91
|
-
# )
|
|
92
|
-
#
|
|
93
|
-
# Note: notice how the Hash version of the table constructor makes it easier to view multidimensional Tables.
|
|
94
|
-
#
|
|
95
|
-
#
|
|
96
|
-
# The amount of Table nesting is only limited by RAM.
|
|
97
|
-
#
|
|
98
|
-
# As a convienance the constructor accepts a block and will pass back the
|
|
99
|
-
# Table instance. This makes it easy to set the style and extrapolation inline.
|
|
100
|
-
# For example,
|
|
101
|
-
#
|
|
102
|
-
# tabfoo = Table.new [1,2,3],[4,5.5,6] do |t| t.extrapolate=false end
|
|
103
|
-
#
|
|
104
|
-
#
|
|
105
|
-
def initialize (*args)
|
|
106
|
-
if (args.size==2) then
|
|
107
|
-
raise "duel argument table constructor must be 2 Arrays" unless args[0].kind_of? Array
|
|
108
|
-
raise "duel argument table constructor must be 2 Arrays" unless args[1].kind_of? Array
|
|
109
|
-
@inds = args[0] # better be numbers
|
|
110
|
-
@deps = args[1] # can either be numbers or sub tables
|
|
111
|
-
elsif (args.size == 1) then # hash version
|
|
112
|
-
raise "single argument table constructor must be a Hash" unless args[0].kind_of? Hash
|
|
113
|
-
# Ruby 1.8 doesnt maintain hash order so lets help it
|
|
114
|
-
f = args[0].sort.transpose
|
|
115
|
-
@inds = f[0]
|
|
116
|
-
@deps = f[1]
|
|
117
|
-
else
|
|
118
|
-
raise(args.size.to_s + " argument Table constructor not valid");
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
raise "number of independents must equal the number of dependents" unless @inds.size == @deps.size
|
|
122
|
-
ii = nil
|
|
123
|
-
@inds.each do |i|
|
|
124
|
-
raise "independents must be monotonically increasing" unless (ii == nil || i > ii)
|
|
125
|
-
ii = i
|
|
126
|
-
end
|
|
127
|
-
@extrapolate = true
|
|
128
|
-
@style = LINEAR
|
|
129
|
-
@ilast = 0 # index of last bracket operation. theory is subsequent table reads may be close to this index so remember it
|
|
130
|
-
@secderivs = []
|
|
131
|
-
if block_given?
|
|
132
|
-
yield self # makes it easy for users to set Table attributes inline
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
#
|
|
136
|
-
# Interpolate or extrapolate the Table. Pass as many arguments as there are independent dimensions to the table (univariant a
|
|
137
|
-
# single argument, bivariant 2 arguments, etc.)
|
|
138
|
-
#
|
|
139
|
-
# Examples
|
|
140
|
-
# * univariant
|
|
141
|
-
# t = Table.new [1.0,2.0],[3.0,4.0]
|
|
142
|
-
# t.read(1.5) returns 3.5
|
|
143
|
-
# * bivariant
|
|
144
|
-
# 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])])
|
|
145
|
-
# t.read(2.0,3.0) returns -1.0
|
|
146
|
-
# t.read(1.7,2.0) returns 5.4
|
|
147
|
-
#
|
|
148
|
-
def read(*args)
|
|
149
|
-
|
|
150
|
-
raise "table requires at least 2 points for linear interpolation" if (@style == LINEAR && @inds.size<2)
|
|
151
|
-
raise "table requires at least 3 points for lagrange2 interpolation" if (@style == LAGRANGE2 && @inds.size<3)
|
|
152
|
-
raise "table requires at least 4 points for lagrange3 interpolation" if (@style == LAGRANGE3 && @inds.size<4)
|
|
153
|
-
raise "table requires at least 3 points for cubic spline interpolation" if (@style == CUBIC && @inds.size<3)
|
|
154
|
-
raise "table requires at least 2 points for catmull-rom interpolation" if (@style == CATMULL && @inds.size<2)
|
|
155
|
-
raise "insufficient number of arguments to read table" unless args.size>=1
|
|
156
|
-
raise "insufficient number of arguments to read table" if (args.size==1 && @deps[0].kind_of?(Table))
|
|
157
|
-
raise "too many arguments to read table" if (args.size>1 && !@deps[0].kind_of?(Table))
|
|
158
|
-
|
|
159
|
-
xval = args[0]
|
|
160
|
-
subargs = args[1..-1]
|
|
161
|
-
|
|
162
|
-
if (@extrapolate == false) && (xval < @inds[0]) then
|
|
163
|
-
ans = subread(0,*subargs)
|
|
164
|
-
|
|
165
|
-
elsif (@extrapolate == false) && (xval > @inds[-1])
|
|
166
|
-
ans = subread(-1,*subargs)
|
|
30
|
+
# Table holds a series of independent and dependent values that can be interpolated or extrapolated.
|
|
31
|
+
# The independent values are always floating points numbers. The dependent values can either be floating point
|
|
32
|
+
# numbers or themselves Table instances. By nesting the Table in this way a Table of any dimension can be
|
|
33
|
+
# created.
|
|
34
|
+
#
|
|
35
|
+
# Tables can be told to not extrapolate by setting .extrapolate=false
|
|
36
|
+
# The interpolation method is controlled with .style = LINEAR, LAGRANGE2, LAGRANGE3, CUBIC (also known as natural spline), or CATMULL (spline)
|
|
37
|
+
#
|
|
38
|
+
# The style and extrapolate attributes are only applied to that specific Table. They are not propagated to subtables. Each subtable
|
|
39
|
+
# can of course accept it's own attributes.
|
|
40
|
+
#
|
|
41
|
+
# More information, source code and gems @ http://rubyforge.org/projects/interpolator/
|
|
42
|
+
#
|
|
43
|
+
class Table
|
|
167
44
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
45
|
+
attr_accessor :extrapolate,:style
|
|
46
|
+
|
|
47
|
+
LINEAR = 1
|
|
48
|
+
LAGRANGE2 = 2
|
|
49
|
+
LAGRANGE3 = 3
|
|
50
|
+
CUBIC = 4
|
|
51
|
+
CATMULL = 5
|
|
52
|
+
|
|
53
|
+
#
|
|
54
|
+
# Tables are constructed from either a pair of Arrays or a single Hash.
|
|
55
|
+
#
|
|
56
|
+
# The 2 argument constructor accepts an array of independents and an array of dependents. The
|
|
57
|
+
# independent Array should be floating point values. The dependent Array can be either floating
|
|
58
|
+
# point values or Tables (aka sub tables.) There is no limit to how deep a Table can be.
|
|
59
|
+
#
|
|
60
|
+
# The single argument constructor is similar. The keys of the Hash are the independents. The values
|
|
61
|
+
# of the Hash are the dependent values, and can either be floating point numbers or Tables.
|
|
62
|
+
#
|
|
63
|
+
# Examples
|
|
64
|
+
# * simple univariant table
|
|
65
|
+
#
|
|
66
|
+
# t = Table.new [1.0,2.0],[3.0,4.0]
|
|
67
|
+
# and is equivalent to
|
|
68
|
+
# t = Table.new(1.0=>3.0,2.0=>4.0)
|
|
69
|
+
#
|
|
70
|
+
# set attributes inline
|
|
71
|
+
# t = Table.new([1.0,2.0],[3.0,4.0]) do |tab| tab.extrapolate=false end
|
|
72
|
+
#
|
|
73
|
+
# * bivariant table
|
|
74
|
+
#
|
|
75
|
+
# 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])])
|
|
76
|
+
#
|
|
77
|
+
# * trivariant table
|
|
78
|
+
#
|
|
79
|
+
# t = Table.new(
|
|
80
|
+
# 1=>Table.new(
|
|
81
|
+
# 1=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
|
|
82
|
+
# 4=>Table.new([11.0,12.0,13.0],[14.0,15.0,16.0]),
|
|
83
|
+
# 5=>Table.new([11.0,12.0,13.0],[-14.0,-15.0,-16.0])),
|
|
84
|
+
# 2=>Table.new(
|
|
85
|
+
# 2=>Table.new([1.1,2.0,3.0],[4.0,5.0,6.0]),
|
|
86
|
+
# 5=>Table.new([11.0,12.5,13.0],[14.0,15.0,16.0]),
|
|
87
|
+
# 6.2=>Table.new([1.0,12.0],[-14.0,-16.0])),
|
|
88
|
+
# 8=>Table.new(
|
|
89
|
+
# 1=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
|
|
90
|
+
# 5=>Table.new([11.0,12.0,13.0],[-14.0,-15.0,-16.0]))
|
|
91
|
+
# )
|
|
92
|
+
#
|
|
93
|
+
# Note: notice how the Hash version of the table constructor makes it easier to view multidimensional Tables.
|
|
94
|
+
#
|
|
95
|
+
#
|
|
96
|
+
# The amount of Table nesting is only limited by RAM.
|
|
97
|
+
#
|
|
98
|
+
# As a convienance the constructor accepts a block and will pass back the
|
|
99
|
+
# Table instance. This makes it easy to set the style and extrapolation inline.
|
|
100
|
+
# For example,
|
|
101
|
+
#
|
|
102
|
+
# tabfoo = Table.new [1,2,3],[4,5.5,6] do |t| t.extrapolate=false end
|
|
103
|
+
#
|
|
104
|
+
#
|
|
105
|
+
def initialize (*args)
|
|
106
|
+
if (args.size==2) then
|
|
107
|
+
raise "duel argument table constructor must be 2 Arrays" unless args[0].kind_of? Array
|
|
108
|
+
raise "duel argument table constructor must be 2 Arrays" unless args[1].kind_of? Array
|
|
109
|
+
@inds = args[0] # better be numbers
|
|
110
|
+
@deps = args[1] # can either be numbers or sub tables
|
|
111
|
+
elsif (args.size == 1) then # hash version
|
|
112
|
+
raise "single argument table constructor must be a Hash" unless args[0].kind_of? Hash
|
|
113
|
+
# Ruby 1.8 doesnt maintain hash order so lets help it
|
|
114
|
+
f = args[0].sort.transpose
|
|
115
|
+
@inds = f[0]
|
|
116
|
+
@deps = f[1]
|
|
117
|
+
else
|
|
118
|
+
raise(args.size.to_s + " argument Table constructor not valid");
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
raise "number of independents must equal the number of dependents" unless @inds.size == @deps.size
|
|
122
|
+
ii = nil
|
|
123
|
+
@inds.each do |i|
|
|
124
|
+
raise "independents must be monotonically increasing" unless (ii == nil || i > ii)
|
|
125
|
+
ii = i
|
|
126
|
+
end
|
|
127
|
+
@extrapolate = true
|
|
128
|
+
@style = LINEAR
|
|
129
|
+
@ilast = 0 # index of last bracket operation. theory is subsequent table reads may be close to this index so remember it
|
|
130
|
+
@secderivs = []
|
|
131
|
+
|
|
132
|
+
if block_given?
|
|
133
|
+
yield self # makes it easy for users to set Table attributes inline
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
end
|
|
137
|
+
#
|
|
138
|
+
# Interpolate or extrapolate the Table. Pass as many arguments as there are independent dimensions to the table (univariant a
|
|
139
|
+
# single argument, bivariant 2 arguments, etc.)
|
|
140
|
+
#
|
|
141
|
+
# Examples
|
|
142
|
+
# * univariant
|
|
143
|
+
# t = Table.new [1.0,2.0],[3.0,4.0]
|
|
144
|
+
# t.read(1.5) returns 3.5
|
|
145
|
+
# * bivariant
|
|
146
|
+
# 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])])
|
|
147
|
+
# t.read(2.0,3.0) returns -1.0
|
|
148
|
+
# t.read(1.7,2.0) returns 5.4
|
|
149
|
+
#
|
|
150
|
+
def read(*args)
|
|
151
|
+
raise "table requires at least 2 points for linear interpolation" if (@style == LINEAR && @inds.size<2)
|
|
152
|
+
raise "table requires at least 3 points for lagrange2 interpolation" if (@style == LAGRANGE2 && @inds.size<3)
|
|
153
|
+
raise "table requires at least 4 points for lagrange3 interpolation" if (@style == LAGRANGE3 && @inds.size<4)
|
|
154
|
+
raise "table requires at least 3 points for cubic spline interpolation" if (@style == CUBIC && @inds.size<3)
|
|
155
|
+
raise "table requires at least 2 points for catmull-rom interpolation" if (@style == CATMULL && @inds.size<2)
|
|
156
|
+
raise "insufficient number of arguments to read table" unless args.size>=1
|
|
157
|
+
raise "insufficient number of arguments to read table" if (args.size==1 && @deps[0].kind_of?(Table))
|
|
158
|
+
raise "too many arguments to read table" if (args.size>1 && !@deps[0].kind_of?(Table))
|
|
159
|
+
|
|
160
|
+
xval = args[0]
|
|
161
|
+
subargs = args[1..-1]
|
|
162
|
+
|
|
163
|
+
if (@extrapolate == false) && (xval < @inds[0]) then
|
|
164
|
+
ans = subread(0,*subargs)
|
|
165
|
+
elsif (@extrapolate == false) && (xval > @inds[-1])
|
|
166
|
+
ans = subread(-1,*subargs)
|
|
167
|
+
else
|
|
168
|
+
|
|
169
|
+
ileft = bracket(xval)
|
|
170
|
+
|
|
171
|
+
case @style
|
|
172
|
+
when LINEAR
|
|
173
|
+
x1 = @inds[ileft]
|
|
174
|
+
x2 = @inds[ileft+1]
|
|
175
|
+
y1 = subread(ileft,*subargs)
|
|
176
|
+
y2 = subread(ileft+1,*subargs)
|
|
177
|
+
ans = linear(xval,x1,x2,y1,y2)
|
|
178
|
+
|
|
179
|
+
when LAGRANGE2
|
|
180
|
+
indx = ileft
|
|
181
|
+
if ileft == @inds.size-2
|
|
182
|
+
indx = ileft - 1
|
|
238
183
|
end
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
184
|
+
x1 = @inds[indx]
|
|
185
|
+
x2 = @inds[indx+1]
|
|
186
|
+
x3 = @inds[indx+2]
|
|
187
|
+
y1 = subread(indx,*subargs)
|
|
188
|
+
y2 = subread(indx+1,*subargs)
|
|
189
|
+
y3 = subread(indx+2,*subargs)
|
|
190
|
+
ans = lagrange2(xval,x1,x2,x3,y1,y2,y3)
|
|
191
|
+
|
|
192
|
+
when LAGRANGE3
|
|
193
|
+
indx = ileft
|
|
194
|
+
|
|
195
|
+
if (ileft > @inds.size-3)
|
|
196
|
+
indx = @inds.size - 3;
|
|
197
|
+
elsif (ileft == 0)
|
|
198
|
+
indx = ileft + 1
|
|
254
199
|
end
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
200
|
+
|
|
201
|
+
x1 = @inds[indx-1]
|
|
202
|
+
x2 = @inds[indx]
|
|
203
|
+
x3 = @inds[indx+1]
|
|
204
|
+
x4 = @inds[indx+2]
|
|
205
|
+
y1 = subread(indx-1,*subargs)
|
|
206
|
+
y2 = subread(indx,*subargs)
|
|
207
|
+
y3 = subread(indx+1,*subargs)
|
|
208
|
+
y4 = subread(indx+2,*subargs)
|
|
209
|
+
ans = lagrange3(xval,x1,x2,x3,x4,y1,y2,y3,y4)
|
|
210
|
+
|
|
211
|
+
when CUBIC
|
|
212
|
+
indx = ileft
|
|
213
|
+
x1 = @inds[indx]
|
|
214
|
+
x2 = @inds[indx+1]
|
|
215
|
+
y1 = subread(indx,*subargs)
|
|
216
|
+
y2 = subread(indx+1,*subargs)
|
|
217
|
+
ans = cubic(xval,indx,x1,x2,y1,y2,*subargs)
|
|
218
|
+
|
|
219
|
+
when CATMULL
|
|
220
|
+
indx = ileft
|
|
221
|
+
tinds = @inds.dup # were gonna prepend and append 2 control points temporarily
|
|
222
|
+
tdeps = @deps.dup
|
|
223
|
+
tinds.insert(0,@inds[0])
|
|
224
|
+
tinds << @inds[-1]
|
|
225
|
+
tdeps.insert(0,@deps[0])
|
|
226
|
+
tdeps << @deps[-1]
|
|
227
|
+
indx = indx+1
|
|
228
|
+
x0 = tinds[indx-1]
|
|
229
|
+
x1 = tinds[indx]
|
|
230
|
+
x2 = tinds[indx+1]
|
|
231
|
+
x3 = tinds[indx+2]
|
|
232
|
+
y0 = catsubread(indx-1,tdeps,*subargs)
|
|
233
|
+
y1 = catsubread(indx,tdeps,*subargs)
|
|
234
|
+
y2 = catsubread(indx+1,tdeps,*subargs)
|
|
235
|
+
y3 = catsubread(indx+2,tdeps,*subargs)
|
|
236
|
+
ans = catmull(xval,x0,x1,x2,x3,y0,y1,y2,y3)
|
|
237
|
+
else
|
|
238
|
+
raise("invalid interpolation type")
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
ans
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
#
|
|
245
|
+
# Same as read
|
|
246
|
+
#
|
|
247
|
+
alias_method :interpolate,:read
|
|
248
|
+
|
|
249
|
+
#
|
|
250
|
+
# Human readable form of the Table. Pass a format string for the values to use. The default is %12.4f
|
|
251
|
+
#
|
|
252
|
+
def inspect(format="%12.4f",indent=0)
|
|
253
|
+
indt = " " * indent
|
|
254
|
+
s = ""
|
|
255
|
+
if @deps[0].kind_of? Table
|
|
256
|
+
@inds.each_index do |i|
|
|
257
|
+
s << indt << format % @inds[i]
|
|
258
|
+
s << "\n"
|
|
259
|
+
s << @deps[i].inspect(format,indent+1)
|
|
260
|
+
s << "\n" if i!=(@inds.size-1)
|
|
261
|
+
end
|
|
262
|
+
else
|
|
263
|
+
s << indt
|
|
264
|
+
@inds.each_index do |i|
|
|
265
|
+
s << format % @inds[i]
|
|
266
|
+
end
|
|
267
|
+
s << "\n"
|
|
268
|
+
s << indt
|
|
269
|
+
@deps.each_index do |i|
|
|
270
|
+
s << format % @deps[i]
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
s
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
#########
|
|
277
|
+
protected
|
|
278
|
+
#########
|
|
279
|
+
|
|
280
|
+
def subread (i,*subargs)
|
|
281
|
+
if subargs == []
|
|
282
|
+
@deps[i]
|
|
283
|
+
else
|
|
284
|
+
@deps[i].read(*subargs)
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def catsubread (i,tdeps,*subargs)
|
|
289
|
+
if subargs == []
|
|
290
|
+
tdeps[i]
|
|
291
|
+
else
|
|
292
|
+
tdeps[i].read(*subargs)
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
#
|
|
297
|
+
# high speed bracket via last index and bisection
|
|
298
|
+
#
|
|
299
|
+
def bracket (x)
|
|
300
|
+
if (x<=@inds[0])
|
|
301
|
+
@ilast=0
|
|
302
|
+
elsif (x>=@inds[-2])
|
|
303
|
+
@ilast = @inds.size-2
|
|
304
|
+
else
|
|
305
|
+
low = 0
|
|
306
|
+
high = @inds.size-1
|
|
307
|
+
while !(x>=@inds[@ilast] && x<@inds[@ilast+1])
|
|
308
|
+
if (x>@inds[@ilast])
|
|
309
|
+
low = @ilast + 1
|
|
310
|
+
@ilast = (high - low) / 2 + low
|
|
259
311
|
else
|
|
260
|
-
|
|
312
|
+
high = @ilast - 1
|
|
313
|
+
@ilast = high - (high - low) / 2
|
|
261
314
|
end
|
|
262
|
-
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
@ilast
|
|
318
|
+
end
|
|
263
319
|
|
|
320
|
+
def linear (x,x1,x2,y1,y2)
|
|
321
|
+
r = (y2-y1) / (x2-x1) * (x-x1) + y1
|
|
322
|
+
end
|
|
264
323
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
while !(x>=@inds[@ilast] && x<@inds[@ilast+1])
|
|
277
|
-
if (x>@inds[@ilast])
|
|
278
|
-
low = @ilast + 1
|
|
279
|
-
@ilast = (high - low) / 2 + low
|
|
280
|
-
else
|
|
281
|
-
high = @ilast - 1
|
|
282
|
-
@ilast = high - (high - low) / 2
|
|
283
|
-
end
|
|
284
|
-
end
|
|
285
|
-
end
|
|
286
|
-
@ilast
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
def linear (x,x1,x2,y1,y2)
|
|
290
|
-
r = (y2-y1) / (x2-x1) * (x-x1) + y1
|
|
291
|
-
end
|
|
292
|
-
|
|
293
|
-
def lagrange2(x,x1,x2,x3,y1,y2,y3)
|
|
294
|
-
c12 = x1 - x2
|
|
295
|
-
c13 = x1 - x3
|
|
296
|
-
c23 = x2 - x3
|
|
297
|
-
q1 = y1/(c12*c13)
|
|
298
|
-
q2 = y2/(c12*c23)
|
|
299
|
-
q3 = y3/(c13*c23)
|
|
300
|
-
xx1 = x - x1
|
|
301
|
-
xx2 = x - x2
|
|
302
|
-
xx3 = x - x3
|
|
303
|
-
r = xx3*(q1*xx2 - q2*xx1) + q3*xx1*xx2
|
|
304
|
-
end
|
|
305
|
-
|
|
306
|
-
def lagrange3(x,x1,x2,x3,x4,y1,y2,y3,y4)
|
|
307
|
-
c12 = x1 - x2
|
|
308
|
-
c13 = x1 - x3
|
|
309
|
-
c14 = x1 - x4
|
|
310
|
-
c23 = x2 - x3
|
|
311
|
-
c24 = x2 - x4
|
|
312
|
-
c34 = x3 - x4
|
|
313
|
-
q1 = y1/(c12 * c13 * c14)
|
|
314
|
-
q2 = y2/(c12 * c23 * c24)
|
|
315
|
-
q3 = y3/(c13 * c23 * c34)
|
|
316
|
-
q4 = y4/(c14 * c24 * c34)
|
|
317
|
-
xx1 = x - x1
|
|
318
|
-
xx2 = x - x2
|
|
319
|
-
xx3 = x - x3
|
|
320
|
-
xx4 = x - x4
|
|
321
|
-
r = xx4*(xx3*(q1*xx2 - q2*xx1) + q3*xx1*xx2) - q4*xx1*xx2*xx3
|
|
322
|
-
end
|
|
323
|
-
|
|
324
|
-
def catmull(xval,x0,x1,x2,x3,y0,y1,y2,y3)
|
|
325
|
-
m0 = (y2-y0)/(x2-x0)
|
|
326
|
-
m1 = (y3-y1)/(x3-x1)
|
|
327
|
-
h = x2-x1
|
|
328
|
-
t = (xval - x1)/h
|
|
329
|
-
h00 = 2.0*t**3 - 3.0*t**2+1.0
|
|
330
|
-
h10 = t**3-2.0*t**2+t
|
|
331
|
-
h01 = -2.0*t**3+3.0*t**2
|
|
332
|
-
h11 = t**3-t**2
|
|
333
|
-
ans = h00*y1+h10*h*m0+h01*y2+h11*h*m1
|
|
334
|
-
end
|
|
335
|
-
|
|
336
|
-
def cubic(x,indx,x1,x2,y1,y2,*subargs)
|
|
337
|
-
if @secderivs == []
|
|
338
|
-
@secderivs = second_derivs(*subargs) # this is painful so lets just do it once
|
|
339
|
-
end
|
|
340
|
-
step = x2 - x1
|
|
341
|
-
a = (x2 - x) / step
|
|
342
|
-
b = (x - x1) / step
|
|
343
|
-
r = a * y1 + b * y2 + ((a*a*a-a) * @secderivs[indx] + (b*b*b-b) * @secderivs[indx+1]) * (step*step) / 6.0
|
|
344
|
-
end
|
|
345
|
-
|
|
346
|
-
def second_derivs(*subargs)
|
|
347
|
-
# natural spline has 0 second derivative at the ends
|
|
348
|
-
temp = [0.0]
|
|
349
|
-
secder = [0.0]
|
|
350
|
-
if subargs.size==0
|
|
351
|
-
deps2 = @deps
|
|
352
|
-
else
|
|
353
|
-
deps2 = @deps.map do |a|
|
|
354
|
-
a.read(*subargs)
|
|
355
|
-
end
|
|
356
|
-
end
|
|
357
|
-
1.upto(@inds.size-2) do |i|
|
|
358
|
-
sig = (@inds[i] - @inds[i-1])/(@inds[i+1] - @inds[i-1])
|
|
359
|
-
prtl = sig * secder[i-1] + 2.0
|
|
360
|
-
secder << (sig-1.0)/prtl
|
|
361
|
-
temp << ((deps2[i+1]-deps2[i])/(@inds[i+1]-@inds[i]) - (deps2[i]-deps2[i-1])/(@inds[i]-@inds[i-1]))
|
|
362
|
-
temp[i]=(6.0*temp[i]/(@inds[i+1]-@inds[i-1])-sig*temp[i-1])/prtl
|
|
363
|
-
end
|
|
364
|
-
# natural spline has 0 second derivative at the ends
|
|
365
|
-
secder << 0.0
|
|
366
|
-
(@inds.size-2).downto(0) do |i|
|
|
367
|
-
secder[i]=secder[i]*secder[i+1]+temp[i]
|
|
368
|
-
end
|
|
369
|
-
secder
|
|
370
|
-
end
|
|
324
|
+
def lagrange2(x,x1,x2,x3,y1,y2,y3)
|
|
325
|
+
c12 = x1 - x2
|
|
326
|
+
c13 = x1 - x3
|
|
327
|
+
c23 = x2 - x3
|
|
328
|
+
q1 = y1/(c12*c13)
|
|
329
|
+
q2 = y2/(c12*c23)
|
|
330
|
+
q3 = y3/(c13*c23)
|
|
331
|
+
xx1 = x - x1
|
|
332
|
+
xx2 = x - x2
|
|
333
|
+
xx3 = x - x3
|
|
334
|
+
r = xx3*(q1*xx2 - q2*xx1) + q3*xx1*xx2
|
|
371
335
|
end
|
|
372
336
|
|
|
373
|
-
|
|
337
|
+
def lagrange3(x,x1,x2,x3,x4,y1,y2,y3,y4)
|
|
338
|
+
c12 = x1 - x2
|
|
339
|
+
c13 = x1 - x3
|
|
340
|
+
c14 = x1 - x4
|
|
341
|
+
c23 = x2 - x3
|
|
342
|
+
c24 = x2 - x4
|
|
343
|
+
c34 = x3 - x4
|
|
344
|
+
q1 = y1/(c12 * c13 * c14)
|
|
345
|
+
q2 = y2/(c12 * c23 * c24)
|
|
346
|
+
q3 = y3/(c13 * c23 * c34)
|
|
347
|
+
q4 = y4/(c14 * c24 * c34)
|
|
348
|
+
xx1 = x - x1
|
|
349
|
+
xx2 = x - x2
|
|
350
|
+
xx3 = x - x3
|
|
351
|
+
xx4 = x - x4
|
|
352
|
+
r = xx4*(xx3*(q1*xx2 - q2*xx1) + q3*xx1*xx2) - q4*xx1*xx2*xx3
|
|
353
|
+
end
|
|
374
354
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
355
|
+
def catmull(xval,x0,x1,x2,x3,y0,y1,y2,y3)
|
|
356
|
+
m0 = (y2-y0)/(x2-x0)
|
|
357
|
+
m1 = (y3-y1)/(x3-x1)
|
|
358
|
+
h = x2-x1
|
|
359
|
+
t = (xval - x1)/h
|
|
360
|
+
h00 = 2.0*t**3 - 3.0*t**2+1.0
|
|
361
|
+
h10 = t**3-2.0*t**2+t
|
|
362
|
+
h01 = -2.0*t**3+3.0*t**2
|
|
363
|
+
h11 = t**3-t**2
|
|
364
|
+
ans = h00*y1+h10*h*m0+h01*y2+h11*h*m1
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def cubic(x,indx,x1,x2,y1,y2,*subargs)
|
|
368
|
+
if @secderivs == []
|
|
369
|
+
@secderivs = second_derivs(*subargs) # this is painful so lets just do it once
|
|
370
|
+
end
|
|
371
|
+
step = x2 - x1
|
|
372
|
+
a = (x2 - x) / step
|
|
373
|
+
b = (x - x1) / step
|
|
374
|
+
r = a * y1 + b * y2 + ((a*a*a-a) * @secderivs[indx] + (b*b*b-b) * @secderivs[indx+1]) * (step*step) / 6.0
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def second_derivs(*subargs)
|
|
378
|
+
# natural spline has 0 second derivative at the ends
|
|
379
|
+
temp = [0.0]
|
|
380
|
+
secder = [0.0]
|
|
381
|
+
if subargs.size==0
|
|
382
|
+
deps2 = @deps
|
|
383
|
+
else
|
|
384
|
+
deps2 = @deps.map do |a|
|
|
385
|
+
a.read(*subargs)
|
|
386
|
+
end
|
|
387
|
+
end
|
|
388
|
+
1.upto(@inds.size-2) do |i|
|
|
389
|
+
sig = (@inds[i] - @inds[i-1])/(@inds[i+1] - @inds[i-1])
|
|
390
|
+
prtl = sig * secder[i-1] + 2.0
|
|
391
|
+
secder << (sig-1.0)/prtl
|
|
392
|
+
temp << ((deps2[i+1]-deps2[i])/(@inds[i+1]-@inds[i]) - (deps2[i]-deps2[i-1])/(@inds[i]-@inds[i-1]))
|
|
393
|
+
temp[i]=(6.0*temp[i]/(@inds[i+1]-@inds[i-1])-sig*temp[i-1])/prtl
|
|
394
|
+
end
|
|
395
|
+
# natural spline has 0 second derivative at the ends
|
|
396
|
+
secder << 0.0
|
|
397
|
+
(@inds.size-2).downto(0) do |i|
|
|
398
|
+
secder[i]=secder[i]*secder[i+1]+temp[i]
|
|
399
|
+
end
|
|
400
|
+
secder
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
if __FILE__ == $0 then
|
|
405
|
+
|
|
406
|
+
require 'test/unit'
|
|
407
|
+
#
|
|
408
|
+
# Unit test for Table class
|
|
409
|
+
#
|
|
379
410
|
class TC_LookupTest < Test::Unit::TestCase
|
|
380
411
|
def setup
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
[4.0,5.0,6.0]),
|
|
402
|
-
5.0=>Table.new([11.0,12.0,13.0],
|
|
403
|
-
[-14.0,-15.0,-16.0]))
|
|
404
|
-
)
|
|
405
|
-
@t5 = Table.new [1.0,2.0,3.0],[1.0,4.0,9.0]
|
|
406
|
-
@t6 = Table.new [1.0,2.0,3.0,4.0],[1.0,8.0,27.0,64.0]
|
|
407
|
-
@t7 = Table.new [0.0,0.8,1.9,3.1,4.2,5.0],[1.0,1.0,1.0,2.0,2.0,2.0]
|
|
408
|
-
@t8 = Table.new [0.0,1.0,2.0,3.0,4.0,5.0,6.0],[0.0,0.8415,0.9093,0.1411,-0.7568,-0.9589,-0.2794]
|
|
409
|
-
@t9 = Table.new([1.0,2.0,3.0],
|
|
410
|
-
[Table.new([1.0,2.0],[3.0,4.0]),Table.new([2.0,3.0,5.0],[6.0,-1.0,7.0]),Table.new([4.0,5.0,6.0],[7.0,8.0,9.0])])
|
|
411
|
-
@t10 = Table.new [1.5,2.0,3.0,4.0],[4.0,5.0,6.0,7.0]
|
|
412
|
+
@t1 = Table.new [1.0,2.0],[3.0,4.0]
|
|
413
|
+
@t2 = 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])])
|
|
414
|
+
@t3 = Table.new [1.0,2.0],[3.0,4.0]
|
|
415
|
+
@t4 = Table.new(
|
|
416
|
+
1.0=>Table.new(
|
|
417
|
+
1.0=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
|
|
418
|
+
4.0=>Table.new([11.0,12.0,13.0],[14.0,15.0,16.0]),
|
|
419
|
+
5.0=>Table.new([11.0,12.0,13.0],[-14.0,-15.0,-16.0])),
|
|
420
|
+
2.0=>Table.new(2.0=>Table.new([1.1,2.0,3.0],[4.0,5.0,6.0]),
|
|
421
|
+
5.0=>Table.new([11.0,12.5,13.0],[14.0,15.0,16.0]),
|
|
422
|
+
6.2=>Table.new([1.0,12.0],[-14.0,-16.0])),
|
|
423
|
+
8.0=>Table.new(
|
|
424
|
+
1.0=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
|
|
425
|
+
5.0=>Table.new([11.0,12.0,13.0],[-14.0,-15.0,-16.0])))
|
|
426
|
+
@t5 = Table.new [1.0,2.0,3.0],[1.0,4.0,9.0]
|
|
427
|
+
@t6 = Table.new [1.0,2.0,3.0,4.0],[1.0,8.0,27.0,64.0]
|
|
428
|
+
@t7 = Table.new [0.0,0.8,1.9,3.1,4.2,5.0],[1.0,1.0,1.0,2.0,2.0,2.0]
|
|
429
|
+
@t8 = Table.new [0.0,1.0,2.0,3.0,4.0,5.0,6.0],[0.0,0.8415,0.9093,0.1411,-0.7568,-0.9589,-0.2794]
|
|
430
|
+
@t9 = Table.new([1.0,2.0,3.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]),Table.new([4.0,5.0,6.0],[7.0,8.0,9.0])])
|
|
431
|
+
@t10 = Table.new [1.5,2.0,3.0,4.0],[4.0,5.0,6.0,7.0]
|
|
412
432
|
end
|
|
413
433
|
|
|
414
434
|
def test_uni
|
|
415
|
-
assert_equal(@t1.read(1.0) ,
|
|
416
|
-
assert_equal(@t1.read(2.0) ,
|
|
417
|
-
assert_equal(@t1.read(1.5) ,
|
|
435
|
+
assert_equal(@t1.read(1.0) ,3.0)
|
|
436
|
+
assert_equal(@t1.read(2.0) ,4.0)
|
|
437
|
+
assert_equal(@t1.read(1.5) ,3.5)
|
|
418
438
|
end
|
|
439
|
+
|
|
419
440
|
def test_bi
|
|
420
|
-
assert_equal(@t2.read(1.0,1.0) ,
|
|
421
|
-
assert_equal(@t2.read(2.0,3.0)
|
|
422
|
-
assert_equal(@t2.read(1.5,2.0) ,
|
|
441
|
+
assert_equal(@t2.read(1.0,1.0) , 3.0)
|
|
442
|
+
assert_equal(@t2.read(2.0,3.0) ,-1.0)
|
|
443
|
+
assert_equal(@t2.read(1.5,2.0) , 5.0)
|
|
423
444
|
end
|
|
445
|
+
|
|
424
446
|
def test_tri
|
|
425
447
|
assert_equal(@t4.read(1.5,5,13),0.0)
|
|
426
448
|
end
|
|
449
|
+
|
|
427
450
|
def test_create
|
|
428
451
|
assert_nothing_raised{
|
|
429
|
-
|
|
452
|
+
Table.new(1.0=>3.0,2.0=>4.0)
|
|
430
453
|
}
|
|
431
454
|
assert_nothing_raised( RuntimeError ){
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
455
|
+
Table.new(
|
|
456
|
+
1=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
|
|
457
|
+
2=>Table.new([2.0,4.0,7.0],[14.0,15.0,16.0]))
|
|
458
|
+
}
|
|
435
459
|
assert_nothing_raised( RuntimeError ){
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
[2.0,4.0,7.0],
|
|
441
|
-
[14.0,15.0,16.0])
|
|
442
|
-
}
|
|
460
|
+
Table.new(
|
|
461
|
+
1=>Table.new([1.0,2.0,3.0],[4.0,5.0,6.0]),
|
|
462
|
+
2=>Table.new([2.0,4.0,7.0],[14.0,15.0,16.0]))
|
|
463
|
+
}
|
|
443
464
|
assert_nothing_raised( RuntimeError ){
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
3.0=>6.0),
|
|
448
|
-
2=>Table.new(
|
|
449
|
-
[2.0,4.0,7.0,12.0],
|
|
450
|
-
[14.0,15.0,16.0,-4.0])
|
|
451
|
-
}
|
|
452
|
-
assert_raise( RuntimeError ) {Table.new(1,2,3)}
|
|
465
|
+
Table.new 1=>Table.new(1.0=>4.0,2.0=>5.0,3.0=>6.0),2=>Table.new([2.0,4.0,7.0,12.0],[14.0,15.0,16.0,-4.0])
|
|
466
|
+
}
|
|
467
|
+
assert_raise( RuntimeError ) {Table.new(1,2,3)}
|
|
453
468
|
end
|
|
469
|
+
|
|
454
470
|
def test_size
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
471
|
+
@t3.style=Table::LAGRANGE2
|
|
472
|
+
assert_raise( RuntimeError ) {@t3.read(1.0)}
|
|
473
|
+
@t3.style=Table::LAGRANGE3
|
|
474
|
+
assert_raise( RuntimeError ) {@t3.read(1.0)}
|
|
475
|
+
@t3.style=Table::CUBIC
|
|
476
|
+
assert_raise( RuntimeError ) {@t3.read(1.0)}
|
|
477
|
+
@t3.style=Table::LINEAR
|
|
478
|
+
assert_nothing_raised( RuntimeError ) {@t3.read(1)}
|
|
463
479
|
end
|
|
480
|
+
|
|
464
481
|
def test_notmono
|
|
465
482
|
assert_raise( RuntimeError ) {Table.new [1.0,2.0,1.5],[1.0,2.0,3.0]}
|
|
466
483
|
assert_raise( RuntimeError ) {Table.new [1.0,-2.0,1.5],[1.0,2.0,3.0]}
|
|
467
484
|
end
|
|
485
|
+
|
|
468
486
|
def test_extrap
|
|
469
487
|
@t1.extrapolate = false
|
|
470
|
-
assert_equal(@t1.read(0.0) ,
|
|
471
|
-
assert_equal(@t1.read(3.0) ,
|
|
488
|
+
assert_equal(@t1.read(0.0) , 3.0)
|
|
489
|
+
assert_equal(@t1.read(3.0) , 4.0)
|
|
472
490
|
@t1.extrapolate = true
|
|
473
|
-
assert_equal(@t1.read(0.0) ,
|
|
474
|
-
assert_equal(@t1.read(3.0) ,
|
|
491
|
+
assert_equal(@t1.read(0.0) , 2.0)
|
|
492
|
+
assert_equal(@t1.read(3.0) , 5.0)
|
|
475
493
|
end
|
|
494
|
+
|
|
476
495
|
def test_style
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
496
|
+
@t5.style=Table::LAGRANGE2
|
|
497
|
+
assert_equal(@t5.read(2.0),4.0)
|
|
498
|
+
assert_equal(@t5.read(2.5),2.5*2.5)
|
|
499
|
+
@t6.style=Table::LAGRANGE3
|
|
500
|
+
assert_equal(@t6.read(2.0),8.0)
|
|
501
|
+
assert_equal(@t6.read(3.5),3.5*3.5*3.5)
|
|
502
|
+
@t5.style=Table::LINEAR
|
|
503
|
+
assert_equal(@t5.read(1.5),2.5)
|
|
504
|
+
@t6.style=Table::LINEAR
|
|
505
|
+
assert_equal(@t6.read(1.5),4.5)
|
|
506
|
+
assert_raise( RuntimeError ) {
|
|
507
|
+
t = Table.new [1.0,-2.0,1.5],[1.0,2.0,3.0]
|
|
508
|
+
t.style=10
|
|
509
|
+
t.read(1.0)
|
|
510
|
+
}
|
|
511
|
+
@t7.style = Table::CUBIC
|
|
512
|
+
assert_in_delta(0.93261392,@t7.read(1.2),0.000001)
|
|
513
|
+
@t8.style = Table::CUBIC
|
|
514
|
+
assert_in_delta(0.59621,@t8.read(2.5),0.000001)
|
|
515
|
+
@t9.style = Table::CUBIC
|
|
516
|
+
assert_in_delta(8.98175,@t9.read(2.3,1.5),0.000001)
|
|
517
|
+
@t10.style=Table::CATMULL
|
|
518
|
+
assert_in_delta(3.666666,@t10.read(1.0),0.00001)
|
|
519
|
+
assert_in_delta(5.5416666,@t10.read(2.5),0.00001)
|
|
520
|
+
assert_in_delta(6.0,@t10.read(0.5),0.00001)
|
|
521
|
+
assert_in_delta(8.0,@t10.read(5.0),0.00001)
|
|
522
|
+
assert_in_delta(6.5,@t10.read(3.5),0.00001)
|
|
523
|
+
assert_in_delta(4.8427,@t10.read(1.9),0.0001)
|
|
524
|
+
@t10.extrapolate=false
|
|
525
|
+
assert_equal(4.0,@t10.read(1.0))
|
|
526
|
+
assert_equal(7.0,@t10.read(99.0))
|
|
508
527
|
end
|
|
528
|
+
|
|
509
529
|
def test_block
|
|
510
530
|
t = Table.new [1.0,2.0,3.0],[3.0,4.0,5.0] do |tab| tab.extrapolate=false;tab.style=Table::LAGRANGE2 end
|
|
511
531
|
assert_equal(t.read(4.0),5.0)
|
|
512
532
|
end
|
|
533
|
+
|
|
513
534
|
def test_alias
|
|
514
535
|
assert_equal(@t1.read(1.5),@t1.interpolate(1.5))
|
|
515
536
|
end
|
|
537
|
+
|
|
516
538
|
def test_numargs
|
|
517
539
|
assert_raise( RuntimeError ) {@t1.read}
|
|
518
540
|
assert_raise( RuntimeError ) {@t2.read(1.0)}
|
|
519
541
|
assert_raise( RuntimeError ) {@t2.read(1.0,1.0,1.0)}
|
|
520
542
|
assert_nothing_raised( RuntimeError ) {@t2.read(1.0,1.0)}
|
|
521
543
|
end
|
|
522
|
-
|
|
544
|
+
def test_inspect
|
|
545
|
+
s=
|
|
546
|
+
" 1.0000
|
|
547
|
+
1.0000
|
|
548
|
+
1.0000 2.0000 3.0000
|
|
549
|
+
4.0000 5.0000 6.0000
|
|
550
|
+
4.0000
|
|
551
|
+
11.0000 12.0000 13.0000
|
|
552
|
+
14.0000 15.0000 16.0000
|
|
553
|
+
5.0000
|
|
554
|
+
11.0000 12.0000 13.0000
|
|
555
|
+
-14.0000 -15.0000 -16.0000
|
|
556
|
+
2.0000
|
|
557
|
+
2.0000
|
|
558
|
+
1.1000 2.0000 3.0000
|
|
559
|
+
4.0000 5.0000 6.0000
|
|
560
|
+
5.0000
|
|
561
|
+
11.0000 12.5000 13.0000
|
|
562
|
+
14.0000 15.0000 16.0000
|
|
563
|
+
6.2000
|
|
564
|
+
1.0000 12.0000
|
|
565
|
+
-14.0000 -16.0000
|
|
566
|
+
8.0000
|
|
567
|
+
1.0000
|
|
568
|
+
1.0000 2.0000 3.0000
|
|
569
|
+
4.0000 5.0000 6.0000
|
|
570
|
+
5.0000
|
|
571
|
+
11.0000 12.0000 13.0000
|
|
572
|
+
-14.0000 -15.0000 -16.0000"
|
|
573
|
+
assert_equal(s,@t4.inspect)
|
|
574
|
+
s=
|
|
575
|
+
" 1.00
|
|
576
|
+
1.00
|
|
577
|
+
1.00 2.00 3.00
|
|
578
|
+
4.00 5.00 6.00
|
|
579
|
+
4.00
|
|
580
|
+
11.00 12.00 13.00
|
|
581
|
+
14.00 15.00 16.00
|
|
582
|
+
5.00
|
|
583
|
+
11.00 12.00 13.00
|
|
584
|
+
-14.00 -15.00 -16.00
|
|
585
|
+
2.00
|
|
586
|
+
2.00
|
|
587
|
+
1.10 2.00 3.00
|
|
588
|
+
4.00 5.00 6.00
|
|
589
|
+
5.00
|
|
590
|
+
11.00 12.50 13.00
|
|
591
|
+
14.00 15.00 16.00
|
|
592
|
+
6.20
|
|
593
|
+
1.00 12.00
|
|
594
|
+
-14.00 -16.00
|
|
595
|
+
8.00
|
|
596
|
+
1.00
|
|
597
|
+
1.00 2.00 3.00
|
|
598
|
+
4.00 5.00 6.00
|
|
599
|
+
5.00
|
|
600
|
+
11.00 12.00 13.00
|
|
601
|
+
-14.00 -15.00 -16.00"
|
|
602
|
+
assert_equal(s,@t4.inspect("%8.2f"))
|
|
603
|
+
s=
|
|
604
|
+
" 1.0000
|
|
605
|
+
1.0000 2.0000
|
|
606
|
+
3.0000 4.0000
|
|
607
|
+
2.0000
|
|
608
|
+
2.0000 3.0000 5.0000
|
|
609
|
+
6.0000 -1.0000 7.0000"
|
|
610
|
+
assert_equal(s,@t2.inspect)
|
|
611
|
+
s=
|
|
612
|
+
" 1.0000 2.0000
|
|
613
|
+
3.0000 4.0000"
|
|
614
|
+
assert_equal(s,@t1.inspect)
|
|
615
|
+
end
|
|
523
616
|
end
|
|
524
|
-
|
|
525
|
-
end
|
|
526
|
-
|
|
617
|
+
end
|
|
527
618
|
end
|
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.
|
|
4
|
+
version: "0.15"
|
|
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-
|
|
12
|
+
date: 2009-09-13 00:00:00 -04:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|
|
@@ -24,10 +24,8 @@ extra_rdoc_files: []
|
|
|
24
24
|
files:
|
|
25
25
|
- interpolator.rb
|
|
26
26
|
- LICENSE
|
|
27
|
-
has_rdoc:
|
|
27
|
+
has_rdoc: false
|
|
28
28
|
homepage: http://interpolator.rubyforge.org/
|
|
29
|
-
licenses: []
|
|
30
|
-
|
|
31
29
|
post_install_message:
|
|
32
30
|
rdoc_options: []
|
|
33
31
|
|
|
@@ -48,9 +46,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
48
46
|
requirements: []
|
|
49
47
|
|
|
50
48
|
rubyforge_project: interpolator
|
|
51
|
-
rubygems_version: 1.3.
|
|
49
|
+
rubygems_version: 1.3.1
|
|
52
50
|
signing_key:
|
|
53
|
-
specification_version:
|
|
51
|
+
specification_version: 2
|
|
54
52
|
summary: Module Interpolator proves a table class that supports n-dimensional numerical table construction, interpolation and extrapolation. Includes linear, 2nd order and 3rd order lagrange, natural and catmull-rom spline techniques.
|
|
55
53
|
test_files: []
|
|
56
54
|
|