|
| 1 | +funcint <- function(func,cvec, basisobj, nderiv=0, JMAX=15, EPS=1e-7) |
| 2 | +{ |
| 3 | + |
| 4 | +# computes the definite integral of a function defined in terms of |
| 5 | +# a functional data object with basis BASISOBJ and coefficient vector CVEC. |
| 6 | + |
| 7 | +# FUNC ... the name of a function |
| 8 | +# CVEC .. a vector of coefficients defining the functional data |
| 9 | +# object required to compute the value of the function. |
| 10 | +# BASISOBJ ... a functional data basis object |
| 11 | +# NDERIV ... a non-negative integer defining a derivative to be used. |
| 12 | +# JMAX maximum number of allowable iterations |
| 13 | +# EPS convergence criterion for relative stop |
| 14 | + |
| 15 | +# Return: the value of the function |
| 16 | + |
| 17 | +# Last modified 29 April 2020 by Jim Ramsay |
| 18 | + |
| 19 | +# set iter |
| 20 | + |
| 21 | +iter <- 0 |
| 22 | + |
| 23 | +# The default case, no multiplicities. |
| 24 | + |
| 25 | +rng <- basisobj$rangeval |
| 26 | + |
| 27 | +nbasis <- basisobj$nbasis |
| 28 | + |
| 29 | +nrep <- dim(basisobj$coef)[2] |
| 30 | + |
| 31 | +inprodvec <- matrix(0,nrep,1) |
| 32 | + |
| 33 | + # set up first iteration |
| 34 | + |
| 35 | + iter <- 1 |
| 36 | + width <- rng[2] - rng[1] |
| 37 | + JMAXP <- JMAX + 1 |
| 38 | + h <- rep(1,JMAXP) |
| 39 | + h[2] <- 0.25 |
| 40 | + s <- matrix(0,JMAXP,nrep) |
| 41 | + sdim <- length(dim(s)) |
| 42 | + # the first iteration uses just the endpoints |
| 43 | + fdobj <- fd(cvec, basisobj) |
| 44 | + fx <- func(eval.fd(rng, fdobj, nderiv)) |
| 45 | + # multiply by values of weight function if necessary |
| 46 | + s[1,] <- width*apply(fx,2,sum)/2 |
| 47 | + tnm <- 0.5 |
| 48 | + |
| 49 | + # now iterate to convergence |
| 50 | + |
| 51 | + for (iter in 2:JMAX) { |
| 52 | + tnm <- tnm*2 |
| 53 | + if (iter == 2) { |
| 54 | + x <- mean(rng) |
| 55 | + } else { |
| 56 | + del <- width/tnm |
| 57 | + x <- seq(rng[1]+del/2, rng[2]-del/2, del) |
| 58 | + } |
| 59 | + fx <- func(eval.fd(x, fdobj, nderiv)) |
| 60 | + chs <- width*apply(fx,2,sum)/tnm |
| 61 | + s[iter,] <- (s[iter-1,] + chs)/2 |
| 62 | + if (iter >= 5) { |
| 63 | + ind <- (iter-4):iter |
| 64 | + ya <- s[ind,,] |
| 65 | + ya <- matrix(ya,5,nrep) |
| 66 | + xa <- h[ind] |
| 67 | + absxa <- abs(xa) |
| 68 | + absxamin <- min(absxa) |
| 69 | + ns <- min((1:length(absxa))[absxa == absxamin]) |
| 70 | + cs <- ya |
| 71 | + ds <- ya |
| 72 | + y <- ya[ns,,] |
| 73 | + ns <- ns - 1 |
| 74 | + for (m in 1:4) { |
| 75 | + for (i in 1:(5-m)) { |
| 76 | + ho <- xa[i] |
| 77 | + hp <- xa[i+m] |
| 78 | + w <- (cs[i+1,] - ds[i,])/(ho - hp) |
| 79 | + ds[i,] <- hp*w |
| 80 | + cs[i,] <- ho*w |
| 81 | + } |
| 82 | + if (2*ns < 5-m) { |
| 83 | + dy <- cs[ns+1,] |
| 84 | + } else { |
| 85 | + dy <- ds[ns,] |
| 86 | + ns <- ns - 1 |
| 87 | + } |
| 88 | + y <- y + dy |
| 89 | + } |
| 90 | + ss <- y |
| 91 | + errval <- max(abs(dy)) |
| 92 | + ssqval <- max(abs(ss)) |
| 93 | + if (all(ssqval > 0)) { |
| 94 | + crit <- errval/ssqval |
| 95 | + } else { |
| 96 | + crit <- errval |
| 97 | + } |
| 98 | + if (crit < EPS && iter >= JMIN) break |
| 99 | + } |
| 100 | + s[iter+1,] <- s[iter,] |
| 101 | + h[iter+1] <- 0.25*h[iter] |
| 102 | + if (iter == JMAX) warning("Failure to converge.") |
| 103 | + } |
| 104 | + inprodvec <- inprodvec + ss |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | + |
0 commit comments