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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
|
# calculus.test --
# Test cases for the Calculus package
#
# This file contains a collection of tests for one or more of the Tcllib
# procedures. Sourcing this file into Tcl runs the tests and
# generates output for errors. No output means no errors were found.
#
# Copyright (c) 2002, 2003, 2004 by Arjen Markus.
# Copyright (c) 2004 by Kevin B. Kenny
# All rights reserved.
#
# RCS: @(#) $Id: calculus.test,v 1.18 2011/01/18 07:49:53 arjenmarkus Exp $
# -------------------------------------------------------------------------
source [file join \
[file dirname [file dirname [file join [pwd] [info script]]]] \
devtools testutilities.tcl]
testsNeedTcl 8.5
testsNeedTcltest 2.1
support {
useLocal math.tcl math
useLocal interpolate.tcl math::interpolate
}
testing {
useLocal calculus.tcl math::calculus
}
# -------------------------------------------------------------------------
package require log
log::lvSuppress notice
# -------------------------------------------------------------------------
namespace eval ::math::calculus::test {
# From here on we are in a namespace.
namespace import ::tcltest::test
namespace import ::math::calculus::*
set prec $::tcl_precision
if {![package vsatisfies [package provide Tcl] 8.5]} {
set ::tcl_precision 17
} else {
set ::tcl_precision 0
}
proc matchNumbers {expected actual} {
set match 1
foreach a $actual e $expected {
if {abs($a-$e) > 0.1e-4} {
set match 0
break
}
}
return $match
}
# customMatch does not do namespaced command resolution, provide the FQN.
customMatch numbers ::math::calculus::test::matchNumbers
#
# Simple test functions - exact result predictable!
#
proc const_func { x } {
return 1
}
proc linear_func { x } {
return $x
}
proc downward_linear { x } {
return [expr {100.0-$x}]
}
#
# Test the Integral proc
#
test "Integral-1.0" "Integral of constant function" {
integral 0 100 100 const_func
} 100.0
test "Integral-1.1" "Integral of linear function" {
integral 0 100 100 linear_func
} 5000.0
test "Integral-1.2" "Integral of downward linear function" {
integral 0 100 100 downward_linear
} 5000.0
test "Integral-1.3" "Integral of expression" {
integralExpr 0 100 100 {100.0-$x}
} 5000.0
proc const_func2d { x y } {
return 1
}
proc linear_func2d { x y } {
return $x
}
test "Integral2D-1.0" "Integral of constant 2D function" {
integral2D { 0 100 10 } { 0 50 1 } const_func2d
} 5000.0
test "Integral2D-1.1" "Integral of constant 2D function (different step)" {
integral2D { 0 100 1 } { 0 50 1 } const_func2d
} 5000.0
test "Integral2D-1.2" "Integral of linear 2D function" {
integral2D { 0 100 10 } { 0 50 1 } linear_func2d
} 250000.0
proc const_func3d { x y z } {
return 1
}
proc linear_func3d { x y z } {
return $x
}
test "Integral3D-1.0" "Integral of constant 2D function" {
integral3D { 0 100 10 } { 0 50 1 } { 0 50 1 } const_func3d
} 250000.0
test "Integral3D-1.1" "Integral of constant 2D function (different step)" {
integral3D { 0 100 1 } { 0 50 1 } { 0 50 1 } const_func3d
} 250000.0
test "Integral3D-1.2" "Integral of linear 2D function" {
integral3D { 0 100 10 } { 0 50 1 } { 0 50 1 } linear_func3d
} 12500000.0
proc f2d_1 {x y} {
return 1
}
proc f2d_x {x y} {
return $x
}
proc f2d_y {x y} {
return $y
}
proc f2d_x2 {x y} {
return [expr {$x*$x}]
}
proc f2d_y2 {x y} {
return [expr {$y*$y}]
}
test "Integral2D-2.0" "Integrals of 2D functions - accurate" -match numbers -body {
set result {}
foreach f {f2d_1 f2d_x f2d_y f2d_x2 f2d_y2} {
lappend result [::math::calculus::integral2D_accurate {-1 1 1} {-1 1 1} $f]
}
return $result
} -result {4.0 0.0 0.0 1.333333333 1.333333333}
proc f3d_1 {x y z} {
return 1
}
proc f3d_x {x y z} {
return $x
}
proc f3d_y {x y z} {
return $y
}
proc f3d_z {x y z} {
return $z
}
proc f3d_x2 {x y z} {
return [expr {$x*$x}]
}
proc f3d_y2 {x y z} {
return [expr {$y*$y}]
}
proc f3d_z2 {x y z} {
return [expr {$z*$z}]
}
test "Integral2D-2.0" "Integrals of 2D functions - accurate" -match numbers -body {
set result {}
foreach f {f3d_1 f3d_x f3d_y f3d_z f3d_x2 f3d_y2 f3d_z2} {
lappend result [::math::calculus::integral3D_accurate {-1 1 1} {-1 1 1} {-1 1 1} $f]
}
return $result
} -result {8.0 0.0 0.0 0.0 2.666666667 2.666666667 2.666666667}
#
# Test cases: yet to be brought into the tcltest form!
#
# xvec should one long!
proc const_func { t xvec } { return 1.0 }
# xvec should be two long!
proc dampened_oscillator { t xvec } {
set x [lindex $xvec 0]
set x1 [lindex $xvec 1]
return [list $x1 [expr {-$x1-$x}]]
}
foreach method {eulerStep heunStep rungeKuttaStep} {
log::log notice "Method: $method"
set xvec 0.0
set t 0.0
set tstep 1.0
for { set i 0 } { $i < 10 } { incr i } {
set result [$method $t $tstep $xvec const_func]
log::log notice "Result ($t): $result"
set t [expr {$t+$tstep}]
set xvec $result
}
set xvec { 1.0 0.0 }
set t 0.0
set tstep 0.1
for { set i 0 } { $i < 20 } { incr i } {
set result [$method $t $tstep $xvec dampened_oscillator]
log::log notice "Result ($t): $result"
set t [expr {$t+$tstep}]
set xvec $result
}
}
#
# Boundary value problems:
#
proc coeffs { x } { return {1.0 0.0 0.0} }
proc forces { x } { return 0.0 }
log::log notice [boundaryValueSecondOrder coeffs forces {0.0 1.0} {100.0 0.0} 10]
log::log notice [boundaryValueSecondOrder coeffs forces {0.0 0.0} {100.0 1.0} 10]
#
# Determining the root of an equation
# use simple functions
#
proc func { x } { expr {$x*$x-1.0} }
proc deriv { x } { expr {2.0*$x} }
test "NewtonRaphson-1.0" "Result should be 1" {
set result [newtonRaphson func deriv 2.0]
if { abs($result-1.0) < 0.0001 } {
set answer 1
}
} 1
test "NewtonRaphson-1.1" "Result should be -1" {
set result [newtonRaphson func deriv -0.5]
if { abs($result+1.0) < 0.0001 } {
set answer 1
}
} 1
proc func2 { x } { expr {$x*exp($x)-1.0} }
proc deriv2 { x } { expr {exp($x)+$x*exp($x)} }
test "NewtonRaphson-2.1" "Result should be nearly 0.56714" {
set result [newtonRaphson func2 deriv2 2.0]
if { abs($result-0.56714) < 0.0001 } {
set answer 1
}
} 1
test "NewtonRaphson-2.2" "Result should be nearly 0.56714" {
set result [newtonRaphson func2 deriv2 -0.5]
if { abs($result-0.56714) < 0.0001 } {
set answer 1
}
} 1
proc checkout { expr integrator a b target } {
set problems {}
proc g x [list expr $expr]
set cmd $integrator
lappend cmd g $a $b
foreach { s error } [eval $cmd] break
set diff [expr { abs( $s - $target ) }]
if { $diff > 1.0e-6 * $target && $diff > 1.0e-10 } {
append problems \n "error underestimated!" \
\n "f =" $expr ", a=" $a ", b=" $b \
\n "machinery = " $integrator "," \
\n "estimated " $error " actual " $diff
}
return $problems
}
test romberg-1.1 {simple integral} {
checkout { pow( $x, 16 ) } romberg -1. 1. [expr { 2. / 17. }]
} {}
test romberg-1.2 {simple integral} {
checkout { exp( -$x * $x / 2. ) / sqrt( 2. * 3.1415926535897932 ) } \
romberg -1. 1. 0.68268949213708590
} {}
test romberg-1.3 {simple integral} {
checkout { sin($x) } romberg 0 3.1415926535897932 2.0
} {}
test romberg-1.4 { Singularity where limit exists } {
checkout { sin($x)/$x } romberg 0 3.1415926535897932 1.8519370519824662
} {}
test romberg-1.5 { Parameter error } {
catch {romberg irrelevant 0 1 -degree} result
set result
} "wrong \# args, should be \"romberg f x1 x2 ?-option value?...\""
test romberg-1.6 { Parameter error } {
catch {romberg irrelevant 0 1 -bad flag} result
set result
} "unknown option \"-bad\", should be -abserror, -degree, -relerror, or\
-maxiter"
test romberg-1.7 { Max iterations exceeded } \
-setup {
proc f x { expr { pow($x,4) } }
} \
-body {
foreach { value error } [romberg f -1. 1. -degree 1 -maxiter 3 ] break
expr { abs($value - 0.4) < $error }
} \
-cleanup {
rename f {}
} \
-result 1
test romberg-1.8 {Bad param} {
catch {romberg irrelevant 0 1 -degree bad} result
set result
} {expected an integer but found "bad"}
test romberg-1.9 {Bad param} {
catch {romberg irrelevant 0 1 -degree 0} result
set result
} {-degree must be positive}
test romberg-1.10 {Bad param} {
catch {romberg irrelevant 0 1 -maxiter bad} result
set result
} {expected an integer but found "bad"}
test romberg-1.11 {Bad param} {
catch {romberg irrelevant 0 1 -maxiter 0} result
set result
} {-maxiter must be positive}
test romberg-1.12 {Bad param} {
catch {romberg irrelevant 0 1 -abserror bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-1.13 {Bad param} {
catch {romberg irrelevant 0 1 -abserror 0.} result
set result
} {-abserror must be positive}
test romberg-1.14 {Bad param} {
catch {romberg irrelevant 0 1 -relerror bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-1.15 {Bad param} {
catch {romberg irrelevant 0 1 -relerror 0.} result
set result
} {-relerror must be positive}
test romberg-1.16 {Bad limit } {
catch {romberg irrelevant bad 1} result
set result
} {expected a floating-point number but found "bad"}
test romberg-1.17 {Bad limit} {
catch {romberg irrelevant 0 bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-2.1 {Integral over half-infinite interval} {
checkout { exp( -$x * $x / 2. ) / sqrt( 2. * 3.1415926535897932 ) } \
romberg_infinity -30. -1. 0.15865525393145705
} {}
test romberg-2.2 {Integral over half-infinite interval} {
checkout { exp( -$x * $x / 2. ) / sqrt( 2. * 3.1415926535897932 ) } \
romberg_infinity 1. 30. 0.15865525393145705
} {}
test romberg-2.3 {Integral over half-infinite interval} {
checkout { exp( $x ) } romberg_infinity -1.e38 -1. [expr { exp(-1.) }]
} {}
test romberg-2.4 {Parameter error} {
catch {romberg_infinity irrelevant -1.e38 2.} result
set result
} {limits of integration have opposite sign}
test romberg-2.5 {Bad limit } {
catch {romberg_infinity irrelevant bad 1} result
set result
} {expected a floating-point number but found "bad"}
test romberg-2.6 {Bad limit} {
catch {romberg_infinity irrelevant 0 bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-3.1 {Square root singularity at the upper bound} {
checkout { sqrt( 1.0 / ( 1.0 - $x ) ) } romberg_sqrtSingUpper 0. 1. 2.
} {}
test romberg-3.2 \
{Square root singularity in the derivative at the upper bound} {
checkout { 4. * sqrt( 1.0 - $x * $x ) } romberg_sqrtSingUpper 0. 1. \
3.1415926535897932
} {}
test romberg-3.3 {Square root singularity at the lower bound} {
checkout { 1.0 / sqrt($x) } romberg_sqrtSingLower 0. 4. 4.
} {}
test romberg-3.4 \
{Square root singularity in the derivative at the lower bound} {
checkout { 4. * sqrt( 1.0 - $x * $x ) } romberg_sqrtSingLower -1. 0. \
3.1415926535897932
} {}
test romberg-3.5 {Bad limit } {
catch {romberg_sqrtSingUpper irrelevant bad 1} result
set result
} {expected a floating-point number but found "bad"}
test romberg-3.6 {Bad limit} {
catch {romberg_sqrtSingUpper irrelevant 0 bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-3.7 {Bad limits} {
catch {romberg_sqrtSingUpper irrelevant 1 0} result
set result
} {limits of integration out of order}
test romberg-3.8 {Bad limit } {
catch {romberg_sqrtSingLower irrelevant bad 1} result
set result
} {expected a floating-point number but found "bad"}
test romberg-3.9 {Bad limit} {
catch {romberg_sqrtSingLower irrelevant 0 bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-3.10 {Bad limits} {
catch {romberg_sqrtSingLower irrelevant 1 0} result
set result
} {limits of integration out of order}
test romberg-4.1 {Power law singularity at the lower bound} {
checkout { 1.0 / sqrt($x) } [list romberg_powerLawLower 0.5] 0. 4. 4.
} {}
test romberg-4.2 \
{Power law signularity in the derivative at the lower bound.} {
checkout { sqrt( sqrt( $x ) ) } \
[list romberg_powerLawLower 0.75] 0. 1. 0.8
} {}
test romberg-4.3 {Power law singularity at the upper bound} {
checkout { 1.0 / sqrt(4.0 - $x) } \
[list romberg_powerLawUpper 0.5] 0. 4. 4.
} {}
test romberg-4.4 \
{Power law singularity in the derivative at the upper bound} {
checkout { sqrt( sqrt( -$x ) ) } \
[list romberg_powerLawUpper 0.75] -1. 0. 0.8
} {}
test romberg-4.5 {Bad limit } {
catch {romberg_powerLawUpper 0.5 irrelevant bad 1} result
set result
} {expected a floating-point number but found "bad"}
test romberg-4.6 {Bad limit} {
catch {romberg_powerLawUpper 0.5 irrelevant 0 bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-4.7 {Bad limits} {
catch {romberg_powerLawUpper 0.5 irrelevant 1 0} result
set result
} {limits of integration out of order}
test romberg-4.8 {Bad limit } {
catch {romberg_powerLawLower 0.5 irrelevant bad 1} result
set result
} {expected a floating-point number but found "bad"}
test romberg-4.9 {Bad limit} {
catch {romberg_powerLawLower 0.5 irrelevant 0 bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-4.10 {Bad limits} {
catch {romberg_powerLawLower 0.5 irrelevant 1 0} result
set result
} {limits of integration out of order}
test romberg-4.11 {Bad gamma} {
catch {romberg_powerLawUpper bad irrelevant 1 0} result
set result
} {expected a floating-point number but found "bad"}
test romberg-4.12 {Bad gamma} {
catch {romberg_powerLawUpper 0. irrelevant 1. 0.} result
set result
} {gamma must lie in the interval (0,1)}
test romberg-4.13 {Bad gamma} {
catch {romberg_powerLawUpper 1. irrelevant 1. 0.} result
set result
} {gamma must lie in the interval (0,1)}
test romberg-4.14 {Bad gamma} {
catch {romberg_powerLawLower bad irrelevant 1 0} result
set result
} {expected a floating-point number but found "bad"}
test romberg-4.15 {Bad gamma} {
catch {romberg_powerLawLower 0. irrelevant 1. 0.} result
set result
} {gamma must lie in the interval (0,1)}
test romberg-4.16 {Bad gamma} {
catch {romberg_powerLawLower 1. irrelevant 1. 0.} result
set result
} {gamma must lie in the interval (0,1)}
test romberg-5.1 {Function that decays exponentially} {
checkout { exp( -$x * $x / 2. ) / sqrt( 2. * 3.1415926535897932 ) } \
romberg_expUpper 1. 100. 0.15865525393145705
} {}
test romberg-5.2 {Function that grows exponentially} {
checkout { exp( -$x * $x / 2. ) / sqrt( 2. * 3.1415926535897932 ) } \
romberg_expLower -100. -1. 0.15865525393145705
} {}
test romberg-5.3 {Bad limit } {
catch {romberg_sqrtSingUpper irrelevant bad 1} result
set result
} {expected a floating-point number but found "bad"}
test romberg-5.4 {Bad limit} {
catch {romberg_sqrtSingUpper irrelevant 0 bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-5.5 {Bad limits} {
catch {romberg_sqrtSingUpper irrelevant 1 0} result
set result
} {limits of integration out of order}
test romberg-5.6 {Bad limit } {
catch {romberg_sqrtSingLower irrelevant bad 1} result
set result
} {expected a floating-point number but found "bad"}
test romberg-5.7 {Bad limit} {
catch {romberg_sqrtSingLower irrelevant 0 bad} result
set result
} {expected a floating-point number but found "bad"}
test romberg-5.8 {Bad limits} {
catch {romberg_sqrtSingLower irrelevant 1 0} result
set result
} {limits of integration out of order}
test romberg-6.1 {Fancy integration} \
-setup {
proc v {f u} {
set x [expr { sin($u) }]
set cmd $f; lappend cmd $x; set y [eval $cmd]
return [expr { $y * cos($u) }]
}
proc romberg_sine { f a b args } {
set f [lreplace $f 0 0 \
[uplevel 1 [list namespace which [lindex $f 0]]]]
set f [list v $f]
return [eval [linsert $args 0 \
romberg $f \
[expr { asin($a) }] [expr { asin($b) }]]]
}
} \
-body {
checkout { exp($x) / sqrt( 1. - $x * $x ) } romberg_sine -1. 1. \
3.97746326
} \
-cleanup {
rename v {}
rename romberg_sine {}
} \
-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 does not do namespaced command resolution, provide the FQN.
customMatch numbers ::math::calculus::test::matchNumbers
proc ::f1 {x} {expr {1.0-$x}}
proc ::f2 {x} {expr {1.0-$x*$x}}
proc ::f3 {x} {expr {cos($x)}}
test "regula-1.0" "Zero of linear function" \
-match numbers -body {
set x1 [::math::calculus::regula_falsi ::f1 0.0 5.0]
} -result 1.0
test "regula-1.1" "Zero of quadratic function" \
-match numbers -body {
set x1 [::math::calculus::regula_falsi ::f2 0.0 5.0]
} -result 0.99909822
test "regula-1.2" "Zero of quadratic function (more accurate)" \
-match numbers -body {
set x1 [::math::calculus::regula_falsi ::f2 0.0 5.0 1.0e-6]
} -result 0.99999305
test "regula-1.3" "Zero of cosine" \
-match numbers -body {
set x1 [::math::calculus::regula_falsi ::f3 0.0 3.0]
} -result 1.5707963
test "regula-2.1" "Negative relative error" \
-match glob -body {
set x1 [::math::calculus::regula_falsi ::f1 0.0 3.0 -1.0e-4]
} -result "Relative *" -returnCodes error
test "regula-2.2" "Invalid interval" \
-match glob -body {
set x1 [::math::calculus::regula_falsi ::f3 0.0 5.0]
} -result "Interval must be *" -returnCodes error
test "solveTriDiagonal-1.0" "Solve tridiagonal system" \
-match numbers -body {
set x [::math::calculus::solveTriDiagonal {3 3} {1 1 1} {2 2} {1 0 0}]
} -result [list [expr {5.0/11.0}] [expr {3.0/11.0}] [expr {-9.0/11.0}]]
proc fcos {x} {
expr {cos($x)}
}
test "integrateQk15-1.0" "Integration according to Gauss-Kronrod quadrature" \
-match numbers -body {
set x [::math::calculus::qk15 0.0 10.0 fcos]
} -result -0.5440211108893682
test "integrateQk15-1.1" "Integration according to Gauss-Kronrod quadrature (10 steps)" \
-match numbers -body {
set x [::math::calculus::qk15 0.0 10.0 fcos 10]
} -result -0.5440211108893697
test "integrateQk15-1.2" "Integration according to Gauss-Kronrod quadrature (with details)" \
-match numbers -body {
set x [::math::calculus::qk15_detailed 0.0 10.0 fcos 10]
} -result {-0.5440211108893697 6.577401743379832e-20 6.543992515206541 1.533698345844891}
# End of test cases
testsuiteCleanup
set ::tcl_precision $prec
testsuiteCleanup
}
namespace delete ::math::calculus::test
# Local Variables:
# mode: tcl
# End:
|