/usr/share/axiom-20170501/src/algebra/NUMQUAD.spad is in axiom-source 20170501-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | )abbrev package NUMQUAD NumericalQuadrature
++ Author: Yurij A. Baransky
++ Date Created: October 90
++ Date Last Updated: October 90
++ Description:
++ This suite of routines performs numerical quadrature using
++ algorithms derived from the basic trapezoidal rule. Because
++ the error term of this rule contains only even powers of the
++ step size (for open and closed versions), fast convergence
++ can be obtained if the integrand is sufficiently smooth.
++
++ Each routine returns a Record of type TrapAns, which contains
++ value Float: estimate of the integral
++ error Float: estimate of the error in the computation
++ totalpts Integer: total number of function evaluations
++ success Boolean: if the integral was computed within the user
++ specified error criterion
++ To produce this estimate, each routine generates an internal
++ sequence of sub-estimates, denoted by S(i), depending on the
++ routine, to which the various convergence criteria are applied.
++ The user must supply a relative accuracy, \spad{eps_r}, and an absolute
++ accuracy, \spad{eps_a}. Convergence is obtained when either\br
++ \tab{5}\spad{ABS(S(i) - S(i-1)) < eps_r * ABS(S(i-1))}\br
++ \tab{5}or \spad{ABS(S(i) - S(i-1)) < eps_a}
++ are true statements.
++
++ The routines come in three families and three flavors:
++ closed: romberg, simpson, trapezoidal
++ open: rombergo, simpsono, trapezoidalo
++ adaptive closed: aromberg, asimpson, atrapezoidal
++
++ The S(i) for the trapezoidal family is the value of the
++ integral using an equally spaced absicca trapezoidal rule for
++ that level of refinement.
++
++ The S(i) for the simpson family is the value of the integral
++ using an equally spaced absicca simpson rule for that level of
++ refinement.
++
++ The S(i) for the romberg family is the estimate of the integral
++ using an equally spaced absicca romberg method. For
++ the i-th level, this is an appropriate combination of all the
++ previous trapezodial estimates so that the error term starts
++ with the 2*(i+1) power only.
++
++ The three families come in a closed version, where the formulas
++ include the endpoints, an open version where the formulas do not
++ include the endpoints and an adaptive version, where the user
++ is required to input the number of subintervals over which the
++ appropriate closed family integrator will apply with the usual
++ convergence parmeters for each subinterval. This is useful
++ where a large number of points are needed only in a small fraction
++ of the entire domain.
++
++ Each routine takes as arguments:\br
++ f integrand\br
++ a starting point\br
++ b ending point\br
++ eps_r relative error\br
++ eps_a absolute error\br
++ nmin refinement level when to start checking for convergence (> 1)\br
++ nmax maximum level of refinement\br
++
++ The adaptive routines take as an additional parameter,
++ nint, the number of independent intervals to apply a closed
++ family integrator of the same name.
++
++ Notes:\br
++ Closed family level i uses \spad{1 + 2**i} points.\br
++ Open family level i uses \spad{1 + 3**i} points.\br
NumericalQuadrature() : SIG == CODE where
L ==> List
V ==> Vector
I ==> Integer
B ==> Boolean
E ==> OutputForm
F ==> Float
PI ==> PositiveInteger
OFORM ==> OutputForm
TrapAns ==> Record(value:F, error:F, totalpts:I, success:B )
SIG ==> with
aromberg : (F -> F,F,F,F,F,I,I,I) -> TrapAns
++ aromberg(fn,a,b,epsrel,epsabs,nmin,nmax,nint)
++ uses the adaptive romberg method to numerically integrate function
++ \spad{fn} over the closed interval from \spad{a} to \spad{b},
++ with relative accuracy \spad{epsrel} and absolute accuracy
++ \spad{epsabs}, with the refinement levels for convergence checking
++ vary from \spad{nmin} to \spad{nmax}, and where \spad{nint}
++ is the number of independent intervals to apply the integrator.
++ The value returned is a record containing the value of the integral,
++ the estimate of the error in the computation, the total number of
++ function evaluations, and either a boolean value which is true if
++ the integral was computed within the user specified error criterion.
++ See \spadtype{NumericalQuadrature} for details.
asimpson : (F -> F,F,F,F,F,I,I,I) -> TrapAns
++ asimpson(fn,a,b,epsrel,epsabs,nmin,nmax,nint) uses the
++ adaptive simpson method to numerically integrate function \spad{fn}
++ over the closed interval from \spad{a} to \spad{b}, with relative
++ accuracy \spad{epsrel} and absolute accuracy \spad{epsabs}, with the
++ refinement levels for convergence checking vary from \spad{nmin}
++ to \spad{nmax}, and where \spad{nint} is the number of independent
++ intervals to apply the integrator. The value returned is a record
++ containing the value of the integral, the estimate of the error in
++ the computation, the total number of function evaluations, and
++ either a boolean value which is true if the integral was computed
++ within the user specified error criterion.
++ See \spadtype{NumericalQuadrature} for details.
atrapezoidal : (F -> F,F,F,F,F,I,I,I) -> TrapAns
++ atrapezoidal(fn,a,b,epsrel,epsabs,nmin,nmax,nint) uses the
++ adaptive trapezoidal method to numerically integrate function
++ \spad{fn} over the closed interval from \spad{a} to \spad{b}, with
++ relative accuracy \spad{epsrel} and absolute accuracy \spad{epsabs},
++ with the refinement levels for convergence checking vary from
++ \spad{nmin} to \spad{nmax}, and where \spad{nint} is the number
++ of independent intervals to apply the integrator. The value returned
++ is a record containing the value of the integral, the estimate of
++ the error in the computation, the total number of function
++ evaluations, and either a boolean value which is true if
++ the integral was computed within the user specified error criterion.
++ See \spadtype{NumericalQuadrature} for details.
romberg : (F -> F,F,F,F,F,I,I) -> TrapAns
++ romberg(fn,a,b,epsrel,epsabs,nmin,nmax) uses the romberg
++ method to numerically integrate function \spadvar{fn} over the closed
++ interval \spad{a} to \spad{b}, with relative accuracy \spad{epsrel}
++ and absolute accuracy \spad{epsabs}, with the refinement levels
++ for convergence checking vary from \spad{nmin} to \spad{nmax}.
++ The value returned is a record containing the value
++ of the integral, the estimate of the error in the computation, the
++ total number of function evaluations, and either a boolean value
++ which is true if the integral was computed within the user specified
++ error criterion. See \spadtype{NumericalQuadrature} for details.
simpson : (F -> F,F,F,F,F,I,I) -> TrapAns
++ simpson(fn,a,b,epsrel,epsabs,nmin,nmax) uses the simpson
++ method to numerically integrate function \spad{fn} over the closed
++ interval \spad{a} to \spad{b}, with
++ relative accuracy \spad{epsrel} and absolute accuracy \spad{epsabs},
++ with the refinement levels for convergence checking vary from
++ \spad{nmin} to \spad{nmax}. The value returned
++ is a record containing the value of the integral, the estimate of
++ the error in the computation, the total number of function
++ evaluations, and either a boolean value which is true if
++ the integral was computed within the user specified error criterion.
++ See \spadtype{NumericalQuadrature} for details.
trapezoidal : (F -> F,F,F,F,F,I,I) -> TrapAns
++ trapezoidal(fn,a,b,epsrel,epsabs,nmin,nmax) uses the
++ trapezoidal method to numerically integrate function \spadvar{fn} over
++ the closed interval \spad{a} to \spad{b}, with relative accuracy
++ \spad{epsrel} and absolute accuracy \spad{epsabs}, with the
++ refinement levels for convergence checking vary
++ from \spad{nmin} to \spad{nmax}. The value
++ returned is a record containing the value of the integral, the
++ estimate of the error in the computation, the total number of
++ function evaluations, and either a boolean value which is true
++ if the integral was computed within the user specified error criterion.
++ See \spadtype{NumericalQuadrature} for details.
rombergo : (F -> F,F,F,F,F,I,I) -> TrapAns
++ rombergo(fn,a,b,epsrel,epsabs,nmin,nmax) uses the romberg
++ method to numerically integrate function \spad{fn} over
++ the open interval from \spad{a} to \spad{b}, with
++ relative accuracy \spad{epsrel} and absolute accuracy \spad{epsabs},
++ with the refinement levels for convergence checking vary from
++ \spad{nmin} to \spad{nmax}. The value returned
++ is a record containing the value of the integral, the estimate of
++ the error in the computation, the total number of function
++ evaluations, and either a boolean value which is true if
++ the integral was computed within the user specified error criterion.
++ See \spadtype{NumericalQuadrature} for details.
simpsono : (F -> F,F,F,F,F,I,I) -> TrapAns
++ simpsono(fn,a,b,epsrel,epsabs,nmin,nmax) uses the
++ simpson method to numerically integrate function \spad{fn} over
++ the open interval from \spad{a} to \spad{b}, with
++ relative accuracy \spad{epsrel} and absolute accuracy \spad{epsabs},
++ with the refinement levels for convergence checking vary from
++ \spad{nmin} to \spad{nmax}. The value returned
++ is a record containing the value of the integral, the estimate of
++ the error in the computation, the total number of function
++ evaluations, and either a boolean value which is true if
++ the integral was computed within the user specified error criterion.
++ See \spadtype{NumericalQuadrature} for details.
trapezoidalo : (F -> F,F,F,F,F,I,I) -> TrapAns
++ trapezoidalo(fn,a,b,epsrel,epsabs,nmin,nmax) uses the
++ trapezoidal method to numerically integrate function \spad{fn}
++ over the open interval from \spad{a} to \spad{b}, with
++ relative accuracy \spad{epsrel} and absolute accuracy \spad{epsabs},
++ with the refinement levels for convergence checking vary from
++ \spad{nmin} to \spad{nmax}. The value returned
++ is a record containing the value of the integral, the estimate of
++ the error in the computation, the total number of function
++ evaluations, and either a boolean value which is true if
++ the integral was computed within the user specified error criterion.
++ See \spadtype{NumericalQuadrature} for details.
CODE ==> add
trapclosed : (F -> F,F,F,F,I) -> F
trapopen : (F -> F,F,F,F,I) -> F
import OutputPackage
aromberg(func,a,b,epsrel,epsabs,nmin,nmax,nint) ==
ans : TrapAns
sum : F := 0.0
err : F := 0.0
pts : I := 1
done : B := true
hh : F := (b-a) / nint
x1 : F := a
x2 : F := a + hh
io : L OFORM := [x1::E,x2::E]
i : I
for i in 1..nint repeat
ans := romberg(func,x1,x2,epsrel,epsabs,nmin,nmax)
if (not ans.success) then
io.1 := x1::E
io.2 := x2::E
print blankSeparate cons("accuracy not reached in interval"::E,io)
sum := sum + ans.value
err := err + abs(ans.error)
pts := pts + ans.totalpts-1
done := (done and ans.success)
x1 := x2
x2 := x2 + hh
return( [sum , err , pts , done] )
asimpson(func,a,b,epsrel,epsabs,nmin,nmax,nint) ==
ans : TrapAns
sum : F := 0.0
err : F := 0.0
pts : I := 1
done : B := true
hh : F := (b-a) / nint
x1 : F := a
x2 : F := a + hh
io : L OFORM := [x1::E,x2::E]
i : I
for i in 1..nint repeat
ans := simpson(func,x1,x2,epsrel,epsabs,nmin,nmax)
if (not ans.success) then
io.1 := x1::E
io.2 := x2::E
print blankSeparate cons("accuracy not reached in interval"::E,io)
sum := sum + ans.value
err := err + abs(ans.error)
pts := pts + ans.totalpts-1
done := (done and ans.success)
x1 := x2
x2 := x2 + hh
return( [sum , err , pts , done] )
atrapezoidal(func,a,b,epsrel,epsabs,nmin,nmax,nint) ==
ans : TrapAns
sum : F := 0.0
err : F := 0.0
pts : I := 1
i : I
done : B := true
hh : F := (b-a) / nint
x1 : F := a
x2 : F := a + hh
io : L OFORM := [x1::E,x2::E]
for i in 1..nint repeat
ans := trapezoidal(func,x1,x2,epsrel,epsabs,nmin,nmax)
if (not ans.success) then
io.1 := x1::E
io.2 := x2::E
print blankSeparate cons("accuracy not reached in interval"::E,io)
sum := sum + ans.value
err := err + abs(ans.error)
pts := pts + ans.totalpts-1
done := (done and ans.success)
x1 := x2
x2 := x2 + hh
return( [sum , err , pts , done] )
romberg(func,a,b,epsrel,epsabs,nmin,nmax) ==
length : F := (b-a)
delta : F := length
newsum : F := 0.5 * length * (func(a)+func(b))
newest : F := 0.0
oldsum : F := 0.0
oldest : F := 0.0
change : F := 0.0
qx1 : F := newsum
table : V F := new((nmax+1)::PI,0.0)
n : I := 1
pts : I := 1
four : I
j : I
i : I
if (nmin < 2) then
output("romberg: nmin to small (nmin > 1) nmin = ",nmin::E)
return([0.0,0.0,0,false])
if (nmax < nmin) then
output("romberg: nmax < nmin : nmax = ",nmax::E)
output(" nmin = ",nmin::E)
return([0.0,0.0,0,false])
if (a = b) then
output("romberg: integration limits are equal = ",a::E)
return([0.0,0.0,1,true])
if (epsrel < 0.0) then
output("romberg: eps_r < 0.0 eps_r = ",epsrel::E)
return([0.0,0.0,0,false])
if (epsabs < 0.0) then
output("romberg: eps_a < 0.0 eps_a = ",epsabs::E)
return([0.0,0.0,0,false])
for n in 1..nmax repeat
oldsum := newsum
newsum := trapclosed(func,a,delta,oldsum,pts)
newest := (4.0 * newsum - oldsum) / 3.0
four := 4
table(n) := newest
for j in 2..n repeat
i := n+1-j
four := four * 4
table(i) := table(i+1) + (table(i+1)-table(i)) / (four-1)
if n > nmin then
change := abs(table(1) - qx1)
if change < abs(epsrel*qx1) then
return( [table(1) , change , 2*pts+1 , true] )
if change < epsabs then
return( [table(1) , change , 2*pts+1 , true] )
oldsum := newsum
oldest := newest
delta := 0.5*delta
pts := 2*pts
qx1 := table(1)
return( [table(1) , 1.25*change , pts+1 ,false] )
simpson(func,a,b,epsrel,epsabs,nmin,nmax) ==
length : F := (b-a)
delta : F := length
newsum : F := 0.5*(b-a)*(func(a)+func(b))
newest : F := 0.0
oldsum : F := 0.0
oldest : F := 0.0
change : F := 0.0
n : I := 1
pts : I := 1
if (nmin < 2) then
output("simpson: nmin to small (nmin > 1) nmin = ",nmin::E)
return([0.0,0.0,0,false])
if (nmax < nmin) then
output("simpson: nmax < nmin : nmax = ",nmax::E)
output(" nmin = ",nmin::E)
return([0.0,0.0,0,false])
if (a = b) then
output("simpson: integration limits are equal = ",a::E)
return([0.0,0.0,1,true])
if (epsrel < 0.0) then
output("simpson: eps_r < 0.0 : eps_r = ",epsrel::E)
return([0.0,0.0,0,false])
if (epsabs < 0.0) then
output("simpson: eps_a < 0.0 : eps_a = ",epsabs::E)
return([0.0,0.0,0,false])
for n in 1..nmax repeat
oldsum := newsum
newsum := trapclosed(func,a,delta,oldsum,pts)
newest := (4.0 * newsum - oldsum) / 3.0
if n > nmin then
change := abs(newest-oldest)
if change < abs(epsrel*oldest) then
return( [newest , 1.25*change , 2*pts+1 , true] )
if change < epsabs then
return( [newest , 1.25*change , 2*pts+1 , true] )
oldsum := newsum
oldest := newest
delta := 0.5*delta
pts := 2*pts
return( [newest , 1.25*change , pts+1 ,false] )
trapezoidal(func,a,b,epsrel,epsabs,nmin,nmax) ==
length : F := (b-a)
delta : F := length
newsum : F := 0.5*(b-a)*(func(a)+func(b))
change : F := 0.0
oldsum : F
n : I := 1
pts : I := 1
if (nmin < 2) then
output("trapezoidal: nmin to small (nmin > 1) nmin = ",nmin::E)
return([0.0,0.0,0,false])
if (nmax < nmin) then
output("trapezoidal: nmax < nmin : nmax = ",nmax::E)
output(" nmin = ",nmin::E)
return([0.0,0.0,0,false])
if (a = b) then
output("trapezoidal: integration limits are equal = ",a::E)
return([0.0,0.0,1,true])
if (epsrel < 0.0) then
output("trapezoidal: eps_r < 0.0 : eps_r = ",epsrel::E)
return([0.0,0.0,0,false])
if (epsabs < 0.0) then
output("trapezoidal: eps_a < 0.0 : eps_a = ",epsabs::E)
return([0.0,0.0,0,false])
for n in 1..nmax repeat
oldsum := newsum
newsum := trapclosed(func,a,delta,oldsum,pts)
if n > nmin then
change := abs(newsum-oldsum)
if change < abs(epsrel*oldsum) then
return( [newsum , 1.25*change , 2*pts+1 , true] )
if change < epsabs then
return( [newsum , 1.25*change , 2*pts+1 , true] )
delta := 0.5*delta
pts := 2*pts
return( [newsum , 1.25*change , pts+1 ,false] )
rombergo(func,a,b,epsrel,epsabs,nmin,nmax) ==
length : F := (b-a)
delta : F := length / 3.0
newsum : F := length * func( 0.5*(a+b) )
newest : F := 0.0
oldsum : F := 0.0
oldest : F := 0.0
change : F := 0.0
qx1 : F := newsum
table : V F := new((nmax+1)::PI,0.0)
four : I
j : I
i : I
n : I := 1
pts : I := 1
for n in 1..nmax repeat
oldsum := newsum
newsum := trapopen(func,a,delta,oldsum,pts)
newest := (9.0 * newsum - oldsum) / 8.0
table(n) := newest
nine := 9
output(newest::E)
for j in 2..n repeat
i := n+1-j
nine := nine * 9
table(i) := table(i+1) + (table(i+1)-table(i)) / (nine-1)
if n > nmin then
change := abs(table(1) - qx1)
if change < abs(epsrel*qx1) then
return( [table(1) , 1.5*change , 3*pts , true] )
if change < epsabs then
return( [table(1) , 1.5*change , 3*pts , true] )
output(table::E)
oldsum := newsum
oldest := newest
delta := delta / 3.0
pts := 3*pts
qx1 := table(1)
return( [table(1) , 1.5*change , pts ,false] )
simpsono(func,a,b,epsrel,epsabs,nmin,nmax) ==
length : F := (b-a)
delta : F := length / 3.0
newsum : F := length * func( 0.5*(a+b) )
newest : F := 0.0
oldsum : F := 0.0
oldest : F := 0.0
change : F := 0.0
n : I := 1
pts : I := 1
for n in 1..nmax repeat
oldsum := newsum
newsum := trapopen(func,a,delta,oldsum,pts)
newest := (9.0 * newsum - oldsum) / 8.0
output(newest::E)
if n > nmin then
change := abs(newest - oldest)
if change < abs(epsrel*oldest) then
return( [newest , 1.5*change , 3*pts , true] )
if change < epsabs then
return( [newest , 1.5*change , 3*pts , true] )
oldsum := newsum
oldest := newest
delta := delta / 3.0
pts := 3*pts
return( [newest , 1.5*change , pts ,false] )
trapezoidalo(func,a,b,epsrel,epsabs,nmin,nmax) ==
length : F := (b-a)
delta : F := length/3.0
newsum : F := length*func( 0.5*(a+b) )
change : F := 0.0
pts : I := 1
oldsum : F
n : I
for n in 1..nmax repeat
oldsum := newsum
newsum := trapopen(func,a,delta,oldsum,pts)
output(newsum::E)
if n > nmin then
change := abs(newsum-oldsum)
if change < abs(epsrel*oldsum) then
return([newsum , 1.5*change , 3*pts , true] )
if change < epsabs then
return([newsum , 1.5*change , 3*pts , true] )
delta := delta / 3.0
pts := 3*pts
return([newsum , 1.5*change , pts ,false] )
trapclosed(func,start,h,oldsum,numpoints) ==
x : F := start + 0.5*h
sum : F := 0.0
i : I
for i in 1..numpoints repeat
sum := sum + func(x)
x := x + h
return( 0.5*(oldsum + sum*h) )
trapopen(func,start,del,oldsum,numpoints) ==
ddel : F := 2.0*del
x : F := start + 0.5*del
sum : F := 0.0
i : I
for i in 1..numpoints repeat
sum := sum + func(x)
x := x + ddel
sum := sum + func(x)
x := x + del
return( (oldsum/3.0 + sum*del) )
|