R Under development (unstable) (2024-11-03 r87286 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(sample(1:6)) > A <- x[1:3] > B <- x[4:6] > S <- A + B > P <- A * B > D <- A - B > Q <- A / B > N <- -A > > X <- c(S,P,D,Q,N) > X0 <- new("dual", x = c(7, 6, 8, 6, 8, 15, -5, 2, -2, 1/6, + 2, 3/5, -1, -4, -3), d = structure(list(x1 = c(1, 0, 0, 6, 0, + 0, 1, 0, 0, 1/6, 0, 0, -1, 0, 0), x2 = c(0, 1, + 0, 0, 2, 0, 0, 1, 0, 0, 0.5, 0, 0, -1, 0), x3 = c(0, 0, 1, 0, + 0, 5, 0, 0, 1, 0, 0, 0.2, 0, 0, -1), x4 = c(1, 0, 0, 1, 0, 0, + -1, 0, 0, -1/36, 0, 0, 0, 0, 0), x5 = c(0, 1, 0, + 0, 4, 0, 0, -1, 0, 0, -1, 0, 0, 0, 0), x6 = c(0, 0, 1, 0, 0, + 3, 0, 0, -1, 0, 0, -0.12, 0, 0, 0)), class = "differential")) > > stopifnot(salad:::test_near_equality(X, X0)) > > Z <- (2 + B) + (A - 1) + (1 - B*4) / 3 + 5 / (2*A + 3) > Z0 <- new("dual", x = c(1.33333333333333, 5.12121212121212, 3.22222222222222 + ), d = structure(list(x1 = c(0.6, 0, 0), x2 = c(0, 0.917355371900826, + 0), x3 = c(0, 0, 0.876543209876543), x4 = c(-0.333333333333333, + 0, 0), x5 = c(0, -0.333333333333333, 0), x6 = c(0, 0, -0.333333333333333 + )), class = "differential")) > > stopifnot(salad:::test_near_equality(Z, Z0)) > > X2 <- x**2 > A.B <- A**B > pA <- 3**A > T <- c(X2, A.B, pA) > T0 <- new("dual", x = c(1, 16, 9, 36, 4, 25, 1, 16, 243, 3, 81, 27), + d = structure(list(x1 = c(2, 0, 0, 0, 0, 0, 6, 0, 0, 3.29583686600433, + 0, 0), x2 = c(0, 8, 0, 0, 0, 0, 0, 8, 0, 0, 88.9875953821169, + 0), x3 = c(0, 0, 6, 0, 0, 0, 0, 0, 405, 0, 0, 29.662531794039 + ), x4 = c(0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0), x5 = c(0, + 0, 0, 0, 4, 0, 0, 22.1807097779182, 0, 0, 0, 0), x6 = c(0, + 0, 0, 0, 0, 10, 0, 0, 266.962786146351, 0, 0, 0)), class = "differential")) > > stopifnot(salad:::test_near_equality(T, T0)) > > > # mb <- microbenchmark::microbenchmark(A + B, A * B, A - B, A / B, -A, times = 1000) > # print(mb) > > # mb <- microbenchmark::microbenchmark(x**2, A**B, 3**A, times = 1000) > # print(mb) > > # mb <- microbenchmark::microbenchmark((2 + B) + (A - 1) + (1 - B*4) / 3 + 5 / (2*A + 3), times = 1000) > # print(mb) > > > proc.time() user system elapsed 0.21 0.06 0.26