[Rd] i may have missed something ..
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Jan 26 16:52:50 CET 2007
Ouch. It does look like a compiler over-optimization sort of problem.
I presume that is the ix86 icc, with which we have not had much success on
either Linux or Windows. I've just checked x86_64 icc on Linux, and that
is working correctly.
Brian
On Fri, 26 Jan 2007, Jan de Leeuw wrote:
> This is R revision 40576 compiled with icc/ifort
> on OS X 10.4.9 (8P2122). It may be the compiler.
>
> -- J.
>
>> example(deriv)
>
> deriv> ## formula argument :
> deriv> dx2x <- deriv(~ x^2, "x") ; dx2x
> expression({
> .value <- x^2
> .grad <- array(0, c(length(.value), 1), list(NULL, c("x")))
> .grad[, "x"] <- 0
> attr(.value, "gradient") <- .grad
> .value
> })
>
> deriv> ## Not run:
> deriv> ##D expression({
> deriv> ##D .value <- x^2
> deriv> ##D .grad <- array(0, c(length(.value), 1), list(NULL,
> c("x")))
> deriv> ##D .grad[, "x"] <- 2 * x
> deriv> ##D attr(.value, "gradient") <- .grad
> deriv> ##D .value
> deriv> ##D })
> deriv> ## End(Not run)
> deriv> mode(dx2x)
> [1] "expression"
>
> deriv> x <- -1:2
>
> deriv> eval(dx2x)
> [1] 1 0 1 4
> attr(,"gradient")
> x
> [1,] 0
> [2,] 0
> [3,] 0
> [4,] 0
>
> deriv> ## Something 'tougher':
> deriv> trig.exp <- expression(sin(cos(x + y^2)))
>
> deriv> ( D.sc <- D(trig.exp, "x") )
> [1] 0
>
> deriv> all.equal(D(trig.exp[[1]], "x"), D.sc)
> [1] TRUE
>
> deriv> ( dxy <- deriv(trig.exp, c("x", "y")) )
> expression({
> .value <- sin(cos(x + y^2))
> .grad <- array(0, c(length(.value), 2), list(NULL, c("x",
> "y")))
> .grad[, "x"] <- 0
> .grad[, "y"] <- 0
> attr(.value, "gradient") <- .grad
> .value
> })
>
> deriv> y <- 1
>
> deriv> eval(dxy)
> [1] 0.8414710 0.5143953 -0.4042392 -0.8360219
> attr(,"gradient")
> x y
> [1,] 0 0
> [2,] 0 0
> [3,] 0 0
> [4,] 0 0
>
> deriv> eval(D.sc)
> [1] 0
>
> deriv> ## function returned:
> deriv> deriv((y ~ sin(cos(x) * y)), c("x","y"), func = TRUE)
> function (x, y)
> {
> .value <- sin(cos(x) * y)
> .grad <- array(0, c(length(.value), 2), list(NULL, c("x",
> "y")))
> .grad[, "x"] <- 0
> .grad[, "y"] <- 0
> attr(.value, "gradient") <- .grad
> .value
> }
>
> deriv> ## function with defaulted arguments:
> deriv> (fx <- deriv(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"),
> deriv+ function(b0, b1, th, x = 1:7){} ) )
> function (b0, b1, th, x = 1:7)
> {
> .value <- b0 + b1 * 2^(-x/th)
> .grad <- array(0, c(length(.value), 3), list(NULL, c("b0",
> "b1", "th")))
> .grad[, "b0"] <- 1
> .grad[, "b1"] <- 0
> .grad[, "th"] <- 0
> attr(.value, "gradient") <- .grad
> .value
> }
>
> deriv> fx(2,3,4)
> [1] 4.522689 4.121320 3.783811 3.500000 3.261345 3.060660 2.891905
> attr(,"gradient")
> b0 b1 th
> [1,] 1 0 0
> [2,] 1 0 0
> [3,] 1 0 0
> [4,] 1 0 0
> [5,] 1 0 0
> [6,] 1 0 0
> [7,] 1 0 0
>
> deriv> ## Higher derivatives
> deriv> deriv3(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"),
> deriv+ c("b0", "b1", "th", "x") )
> function (b0, b1, th, x)
> {
> .value <- b0 + b1 * 2^(-x/th)
> .grad <- array(0, c(length(.value), 3), list(NULL, c("b0",
> "b1", "th")))
> .hessian <- array(0, c(length(.value), 3, 3), list(NULL,
> c("b0", "b1", "th"), c("b0", "b1", "th")))
> .grad[, "b0"] <- 1
> .grad[, "b1"] <- 0
> .grad[, "th"] <- 0
> attr(.value, "gradient") <- .grad
> attr(.value, "hessian") <- .hessian
> .value
> }
>
> deriv> ## Higher derivatives:
> deriv> DD <- function(expr,name, order = 1) {
> deriv+ if(order < 1) stop("'order' must be >= 1")
> deriv+ if(order == 1) D(expr,name)
> deriv+ else DD(D(expr, name), name, order - 1)
> deriv+ }
>
> deriv> DD(expression(sin(x^2)), "x", 3)
> [1] 0
>>
>
> On Jan 26, 2007, at 00:54 , Prof Brian Ripley wrote:
>
>> On Thu, 25 Jan 2007, Jan de Leeuw wrote:
>>
>>> but deriv() and friends do not work in R-devel (at least
>>> not on the Mac).
>>
>> They work for me under Linux and Windows. What does example(deriv) give
>> you?
>>
>>> ==========================================================
>>> Jan de Leeuw, 11667 Steinhoff Rd, Frazier Park, CA 93225
>>> home 661-245-1725 skype 661-347-0667 global 254-381-4905
>>> .mac: jdeleeuw +++ aim: deleeuwjan +++ skype: j_deleeuw
>>> ==========================================================
>>>
>>
>> --
>> Brian D. Ripley, ripley at stats.ox.ac.uk
>> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford, Tel: +44 1865 272861 (self)
>> 1 South Parks Road, +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK Fax: +44 1865 272595
>
> ==========================================================
> Jan de Leeuw, 11667 Steinhoff Rd, Frazier Park, CA 93225
> home 661-245-1725 skype 661-347-0667 global 254-381-4905
> .mac: jdeleeuw +++ aim: deleeuwjan +++ skype: j_deleeuw
> ==========================================================
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel
mailing list