R Under development (unstable) (2024-06-20 r86796 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > require(salad) Loading required package: salad Attaching package: 'salad' The following object is masked from 'package:stats': dnorm > set.seed(1) > x <- dual( runif(25) ) > A <- matrix( rep(x, 25) + runif(25*25), 25, 25) > > # vA <- value(A) > # dA <- d(A) > > # vx <- value(x) > # dx <- d(x) > > f1 <- function(A, x) (A %*% x)@d > > f2 <- function(A, x) { + vA <- A@x + vx <- x@x + dA <- unclass(A@d) + dx <- unclass(x@d) + B <- vA %*% vx + dB <- vector("list", length(dA)) + for(k in seq_along(dB)) dB[[k]] <- dA[[k]] %*% vx + vA %*% dx[[k]] + # this is 0.2 - 0.5 µs + # names(dB) <- names(dA) + # class(dB) <- "differential" + # this takes 15 µs ! + # A@x <- B + # A@d <- dB + # A@d + # this is still 3 µs + # BB <- salad:::fastNewDual(B, dB) + # BB@d + dB + } > > f3 <- function(A, x) matrixprod_dd(A, x)@d > > Z1 <- f1(A, x) > Z2 <- f2(A, x) > Z3 <- f3(A, x) > test <- TRUE > for(i in 1:5) test <- test & all(Z1[[i]] == Z2[[i]]) > for(i in 1:5) test <- test & all(Z1[[i]] == Z3[[i]]) > stopifnot(test) > > # mb <- microbenchmark::microbenchmark(f1(A, x), f2(A, x), f3(A, x), times = 1e4) > # print(mb) > > > proc.time() user system elapsed 0.25 0.03 0.28