File: interpolate.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (374 lines) | stat: -rw-r--r-- 10,635 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# -*- tcl -*-
# interpolate.test --
#    Test cases for the ::math::interpolate package
#

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2.1

support {
    use      struct/matrix.tcl struct::matrix
    useLocal math.tcl          math
}
testing {
    useLocal interpolate.tcl math::interpolate
}

# -------------------------------------------------------------------------

#
# Compare a list of numbers
#
proc matchNumbers {expected actual} {
    set match 1
    foreach a $actual e $expected {
        if {$e != 0.0} {
            if {abs($a-$e) > 0.5e-4*abs($a+$e)} {
                set match 0
                break
            }
        } else {
            if {abs($a-$e) > 1.0e-5} {
                set match 0
                break
            }
        }
    }
    return $match
}

customMatch numbers matchNumbers

# -------------------------------------------------------------------------

#
# Test cases: interpolation in tables
#
# Add a dummy row to the table - ticket b25b826973edcbb5b3a95f6c284214925a1d5e67
# This makes it possible to use the same table in both 1D and 2D interpolations
#
set t [::math::interpolate::defineTable table1 \
   {    x     v1    v2    v3 } \
   {    -      1     2     3
        0      0    10     1
        1      1     9     4
        2      2     8     9
        5      5     5    25
        7      7     3    49
       10     10     0   100 }]

test "Interpolate-1.1" "Interpolate in a one-dimensional table" \
     -match numbers -body {
   set result {}
   foreach x { -1.0 0.0 3.0 5.0 9.9 11.0 } {
      set result [concat $result \
                 [::math::interpolate::interp-1d-table $t $x]]
   }
   set result
} -result {
   -1    0    10     1
    0    0    10     1
    3    3     7    14.333333
    5    5     5    25
  9.9  9.9     0.1  98.3
   11   10     0   100 }


# value = x+y
set t2 [::math::interpolate::defineTable table2 \
   {    x      y1   y2    y3 } \
   {    -      0     3    10
        1      1     4    11
        2      2     5    12
        5      5     8    15
        7      7    10    17
       10     10    13    20 }]

test "Interpolate-1.2" "Interpolate in a two-dimensional table" \
     -match numbers -body {
   set result {}
   foreach y { -1.0 0.0 3.0 5.0 9.9 11.0 } {
      foreach x { -1.0 0.0 3.0 5.0 9.9 11.0 } {
         set result [concat $result \
            $x $y [::math::interpolate::interp-table $t2 $x $y]]
      }
   }
   set result
} -result {
    -1.0 -1.0 1.0
     0.0 -1.0 1.0
     3.0 -1.0 3.0
     5.0 -1.0 5.0
     9.9 -1.0 9.9
    11.0 -1.0 10.0
    -1.0 0.0 1.0
     0.0 0.0 1.0
     3.0 0.0 3.0
     5.0 0.0 5.0
     9.9 0.0 9.9
    11.0 0.0 10.0
    -1.0 3.0 4.0
     0.0 3.0 4.0
     3.0 3.0 6.0
     5.0 3.0 8.0
     9.9 3.0 12.9
    11.0 3.0 13.0
    -1.0 5.0 6.0
     0.0 5.0 6.0
     3.0 5.0 8.0
     5.0 5.0 10.0
     9.9 5.0 14.9
    11.0 5.0 15.0
    -1.0 9.9 10.9
     0.0 9.9 10.9
     3.0 9.9 12.9
     5.0 9.9 14.9
     9.9 9.9 19.8
    11.0 9.9 19.9
    -1.0 11.0 11.0
     0.0 11.0 11.0
     3.0 11.0 13.0
     5.0 11.0 15.0
     9.9 11.0 19.9
    11.0 11.0 20.0
}

test "Interpolate-1.3" "Interpolate with integers" \
     -match numbers -body {
    set equalResults {}

    set table [::math::interpolate::defineTable table3 \
                   {A B C D E} \
                   {0   0.00000   0.00000   0.00000   0.0000
                   1   0.52000   0.52000   0.52000   0.5200
                   3   0.69831   0.63142   0.67758   0.68457
                   5   0.86111   0.71690   0.81118   0.80365
                   7   1.01367   0.78725   0.92891   0.89851}]

    foreach A {2 4 6} A2 {2.0 4.0 6.0} {
        set intResults   [::math::interpolate::interp-1d-table $table $A]
        set floatResults [::math::interpolate::interp-1d-table $table $A2]
        set equal 1
        foreach i $intResults f $floatResults {
            if { $i != $f } {
                set equal 0
                break
            }
       }
       lappend equalResults $equal
   }

   return $equalResults
} -result {1 1 1}

# linear interpolation: y = x + 1 and y = 2*x, x<5, or 20-2*x, x>5

test "Interpolate-2.1" "Linear interpolation - 1" \
     -match numbers -body {
   set result {}

   set xyvalues { 0.0 1.0  10.0 11.0 }
   foreach x { 0.0 4.0 7.0 10.0 101.0 } {
      lappend result [::math::interpolate::interp-linear $xyvalues $x]
   }
   set result
} -result { 1.0 5.0 8.0 11.0 11.0 }

test "Interpolate-2.2" "Linear interpolation - 2" \
     -match numbers -body {
   set result {}
   set xyvalues { 0.0 0.0  5.0 10.0 10.0 0.0 }
   foreach x { 0.0 4.0 7.0 10.0 11.0 } {
      lappend result [::math::interpolate::interp-linear $xyvalues $x]
   }
   set result
} -result { 0.0 8.0 6.0 0.0 0.0 }

# Lagrange interpolation: y = x + 1
test "Interpolate-3.1" "Lagrange interpolation - 1" \
     -match numbers -body {
   set result {}
   set xyvalues { 0.0 1.0  10.0 11.0 }
   foreach x { 0.0 4.0 7.0 10.0 101.0 } {
      lappend result [::math::interpolate::interp-lagrange $xyvalues $x]
   }
   set result
} -result { 1.0  5.0  8.0  11.0  102.0 }


#Lagrange interpolation (2) - expected: y=10-2*(x-5)**2/5
test "Interpolate-3.2" "Lagrange interpolation - 2" \
     -match numbers -body {
   set result {}
   set xyvalues { 0.0 0.0  5.0 10.0 10.0 0.0 }
   foreach x { 0.0 4.0 7.0 10.0 11.0 } {
      lappend result [::math::interpolate::interp-lagrange $xyvalues $x]
   }
   set result
} -result { 0.0 9.6 8.4 0.0 -4.4 }

# Spatial interpolation
test "Interpolate-4.1" "Spatial interpolation - 1" \
     -match numbers -body {
   set result {}
   set xyzvalues { {-1.0 0.0 -2.0 }
                   { 1.0 0.0  2.0 } }
   foreach coord { {0.0 0.0} {0.0 1.0} {3.0 0.0} {100.0 0.0} } {
      lappend result [::math::interpolate::interp-spatial $xyzvalues $coord]
   }
   set result
} -result { 0.0 0.0 1.2 0.039996 }

test "Interpolate-4.2" "Spatial interpolation - 2" \
   -match numbers -body {
   set result {}

   set xyzvalues { {-1.0 0.0 { -2.0  1.0 } }
                   { 1.0 0.0 {  2.0 -1.0 } } }
   foreach coord { {0.0 0.0} {0.0 1.0} {3.0 0.0} {100.0 0.0} } {
      set result [concat $result \
            [::math::interpolate::interp-spatial $xyzvalues $coord]]
   }
   set result
} -result { 0.0       0.0
            0.0       0.0
            1.2      -0.6
            0.039996 -0.019998 }

test "Interpolate-4.3" "Spatial interpolation - 3 - coincident points" \
   -match numbers -body {
   set result {}

   set xyzvalues { {-1.0 0.0 { -2.0  1.0 } }
                   { 1.0 0.0 {  2.0 -1.0 } } }
   set coord {-1.0 0.0}
      set result [::math::interpolate::interp-spatial $xyzvalues $coord]

   set result
} -result { -2.0 1.0 }

#
# Test TODO: parameters for spatial interpolation
#

test interpolate-5.1 "neville algorithm" \
    -body {
	set problems {}
	namespace import ::math::interpolate::neville
	set xtable [list 0. 30. 45. 60. 90. 120. 135. 150. 180.]
	set ytable [list 0. 0.5 [expr sqrt(0.5)] [expr sqrt(0.75)] 1. \
			[expr sqrt(0.75)] [expr sqrt(0.5)] 0.5 0.]
	for { set x -15 } { $x <= 195 } { incr x } {
	    foreach { y error } [neville $xtable $ytable $x] break
	    set diff [expr { abs( $y - sin( $x*3.1415926535897932/180. ) ) }]
	    if { $error > 3.e-4 || ( $diff > $error && $diff > 1.e-8 ) } {
		append problems \n "interpolating for sine of " $x " degrees" \
		    \n "value was " $y " +/- " $error \
		    \n "actual error was " $diff
	    }
	}
	set problems
    } \
    -result {}

proc matchNumbers {expected actual} {
    set match 1
    foreach a $actual e $expected {
        if {abs($a-$e) > 0.1e-6} {
            set match 0
            break
        }
    }
    return $match
}

customMatch numbers matchNumbers

test "cubic-splines-1.0" "Interpolate linear function" \
   -match numbers -body {
    set xcoord {1 2 3 4 5}
    set ycoord {1 2 3 4 5}
    set coeffs [::math::interpolate::prepare-cubic-splines $xcoord $ycoord]
    set yvalues {}
    foreach x {1.5 2.5 3.5 4.5} {
        lappend yvalues [::math::interpolate::interp-cubic-splines $coeffs $x]
    }
    set yvalues
} -result {1.5 2.5 3.5 4.5}

test "cubic-splines-1.1" "Interpolate quadratic function" \
   -match numbers -body {
    set xcoord {1 2 3 4 5}
    set ycoord {1 4 9 16 25}
    set coeffs [::math::interpolate::prepare-cubic-splines $xcoord $ycoord]
    set yvalues {}
    foreach x $xcoord {
        lappend yvalues [::math::interpolate::interp-cubic-splines $coeffs $x]
    }
    set yvalues
} -result {1 4 9 16 25}

test "cubic-splines-1.2" "Interpolate arbitrary function" \
   -match numbers -body {
    set coeffs [::math::interpolate::prepare-cubic-splines \
                  {0.1 0.3 0.4 0.8  1.0} \
                  {1.0 2.1 2.2 4.11 4.12}]
    set yvalues {}
    foreach x {0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0} {
        lappend yvalues [::math::interpolate::interp-cubic-splines $coeffs $x]
    }
    set yvalues
} -result {1.0 1.6804411764705884 2.1 2.2 2.5380974264705882
           3.1041911764705885 3.695689338235294 4.11 4.2099448529411765 4.12}

test "cubic-splines-2.1" "Too few data" \
-match glob -body {
   set xcoord {1 2}
   set ycoord {1 4}
   set coeffs [::math::interpolate::prepare-cubic-splines $xcoord $ycoord]
} -result "At least *" -returnCodes error

test "cubic-splines-2.2" "Unequal lengths" \
-match glob -body {
   set xcoord {1 2 4 5}
   set ycoord {1 4 5 5 6}
   set coeffs [::math::interpolate::prepare-cubic-splines $xcoord $ycoord]
} -result "Equal number *" -returnCodes error

test "cubic-splines-2.3" "Not-ascending x-coordinates" \
-match glob -body {
   set xcoord {1 2 1.5}
   set ycoord {1 4 5}
   set coeffs [::math::interpolate::prepare-cubic-splines $xcoord $ycoord]
} -result "* ascending" -returnCodes error

test "cubic-splines-2.4" "X too small" \
-match glob -body {
   set xcoord {1 2 3}
   set ycoord {1 4 5}
   set coeffs [::math::interpolate::prepare-cubic-splines $xcoord $ycoord]
   set yvalue [::math::interpolate::interp-cubic-splines $coeffs -1]
} -result "* too small" -returnCodes error

test "cubic-splines-2.5" "X too large" \
-match glob -body {
   set xcoord {1 2 3}
   set ycoord {1 4 5}
   set coeffs [::math::interpolate::prepare-cubic-splines $xcoord $ycoord]
   set yvalue [::math::interpolate::interp-cubic-splines $coeffs 6]
} -result "* too large" -returnCodes error



# -------------------------------------------------------------------------
testsuiteCleanup

# Local Variables:
# mode: tcl
# End: