https://github.com/cran/pracma
Revision b5e4bf28fcba9f5eaffbeecfb0bc307452d074ee authored by Hans W. Borchers on 01 November 2014, 00:00:00 UTC, committed by Gabor Csardi on 01 November 2014, 00:00:00 UTC
1 parent d57c14d
Tip revision: b5e4bf28fcba9f5eaffbeecfb0bc307452d074ee authored by Hans W. Borchers on 01 November 2014, 00:00:00 UTC
version 1.7.7
version 1.7.7
Tip revision: b5e4bf2
piecewise.R
##
## p i e c e w i s e . R Piecewise Linear Function
##
piecewise <- function(x, y, abs = FALSE)
{
n <- length(x)
areas <- 0.0
zeros <- if (y[1] == 0) c(x[1]) else c()
for (i in 2:n) {
if (y[i]*y[i-1] >= 0) {
if (y[i] == 0) zeros <- c(zeros, x[i])
areas <- c(areas, (y[i]+y[i-1]) * (x[i]-x[i-1]) / 2.0)
} else {
x0 <- (x[i-1]*y[i] - x[i]*y[i-1])/(y[i] - y[i-1])
zeros <- c(zeros, x0)
areas <- c(areas, y[i-1]*(x0-x[i-1])/2.0, y[i]*(x[i]-x0)/2.0)
}
}
area <- if (abs) sum(abs(areas)) else sum(areas)
return(list(area=area, zeros=zeros))
}
Computing file changes ...