Title: | By-Reference Routines for Numeric Vectors |
---|---|
Description: | Using by-reference semantics, functions in this package are crafted to modify the input objects in-place and avoid allocating new memory. By avoiding memory allocations (and the associated garbage colection these require), operations performed by-reference can be faster than those performed with R's default copy-on-modify semantics. |
Authors: | Mike Cheng [aut, cre, cph] |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3.9019 |
Built: | 2024-12-24 03:25:43 UTC |
Source: | https://github.com/coolbutuseless/insitu |
A * B
Allocate empty matrix for the result of the matrix multiplication A * B
alloc_mat_mat_mul(A, B, ta = FALSE, tb = FALSE)
alloc_mat_mat_mul(A, B, ta = FALSE, tb = FALSE)
A , B
|
Primary multiplication matrices |
ta |
Should matrix 'A' be transposed? Default: FALSE |
tb |
Should matrix 'B' be transposed? Default: FALSE |
Uninitialized matrix of the correct size to hold the result
A <- matrix(1, 2, 4) B <- matrix(1, 4, 7) C <- alloc_mat_mat_mul(A, B) is.matrix(C) dim(C)
A <- matrix(1, 2, 4) B <- matrix(1, 4, 7) C <- alloc_mat_mat_mul(A, B) is.matrix(C) dim(C)
A * x
Allocate empty vector for the result of the matrix-vector multiplication A * x
alloc_mat_vec_mul(A, x)
alloc_mat_vec_mul(A, x)
A |
Matrix |
x |
vector |
Uninitialized vector of the correct size to hold the result of A * x
A <- matrix(1, 2, 4) x <- matrix(1, 4, 1) y <- alloc_mat_vec_mul(A, x) dim(y) br_mat_vec_mul(y, A, x) y # Compare to base R A %*% x
A <- matrix(1, 2, 4) x <- matrix(1, 4, 1) y <- alloc_mat_vec_mul(A, x) dim(y) br_mat_vec_mul(y, A, x) y # Compare to base R A %*% x
The values are in the matrix are not initialised to zero.
alloc_matrix(nrow, ncol)
alloc_matrix(nrow, ncol)
nrow , ncol
|
dimensions |
Matrix of the requested dimensions. Values are not initialised.
m <- alloc_matrix(nrow = 3, ncol = 2) is.matrix(m) dim(m)
m <- alloc_matrix(nrow = 3, ncol = 2) is.matrix(m) dim(m)
These functions are like numeric()
except the contents of the vector
are not initialized.
alloc_n(n) alloc_along(x)
alloc_n(n) alloc_along(x)
n |
length of new numeric vector |
x |
vector used as template. New vector will be the same length as this vector |
alloc_n
Allocates a numeric vector which can hold 'n' values
alloc_along
Allocates a numeric vector which can hold the same number of values as the given vector
new numeric vector of the required length. Note: This vector is not initialized to zero.
x <- alloc_n(10) br_seq(x) x
x <- alloc_n(10) br_seq(x) x
Math operations taking only a single input vector
br_abs(x, idx = NULL, where = NULL, cols = NULL) br_sqrt(x, idx = NULL, where = NULL, cols = NULL) br_floor(x, idx = NULL, where = NULL, cols = NULL) br_ceil(x, idx = NULL, where = NULL, cols = NULL) br_trunc(x, idx = NULL, where = NULL, cols = NULL) br_exp(x, idx = NULL, where = NULL, cols = NULL) br_log(x, idx = NULL, where = NULL, cols = NULL) br_log2(x, idx = NULL, where = NULL, cols = NULL) br_log10(x, idx = NULL, where = NULL, cols = NULL) br_cos(x, idx = NULL, where = NULL, cols = NULL) br_sin(x, idx = NULL, where = NULL, cols = NULL) br_tan(x, idx = NULL, where = NULL, cols = NULL) br_not(x, idx = NULL, where = NULL, cols = NULL) br_expm1(x, idx = NULL, where = NULL, cols = NULL) br_log1p(x, idx = NULL, where = NULL, cols = NULL) br_acos(x, idx = NULL, where = NULL, cols = NULL) br_asin(x, idx = NULL, where = NULL, cols = NULL) br_atan(x, idx = NULL, where = NULL, cols = NULL) br_acosh(x, idx = NULL, where = NULL, cols = NULL) br_asinh(x, idx = NULL, where = NULL, cols = NULL) br_atanh(x, idx = NULL, where = NULL, cols = NULL) br_cosh(x, idx = NULL, where = NULL, cols = NULL) br_sinh(x, idx = NULL, where = NULL, cols = NULL) br_tanh(x, idx = NULL, where = NULL, cols = NULL) br_cospi(x, idx = NULL, where = NULL, cols = NULL) br_sinpi(x, idx = NULL, where = NULL, cols = NULL) br_tanpi(x, idx = NULL, where = NULL, cols = NULL) br_sign(x, idx = NULL, where = NULL, cols = NULL) br_is_na(x, idx = NULL, where = NULL, cols = NULL) br_zero(x, idx = NULL, where = NULL, cols = NULL) br_pow2(x, idx = NULL, where = NULL, cols = NULL)
br_abs(x, idx = NULL, where = NULL, cols = NULL) br_sqrt(x, idx = NULL, where = NULL, cols = NULL) br_floor(x, idx = NULL, where = NULL, cols = NULL) br_ceil(x, idx = NULL, where = NULL, cols = NULL) br_trunc(x, idx = NULL, where = NULL, cols = NULL) br_exp(x, idx = NULL, where = NULL, cols = NULL) br_log(x, idx = NULL, where = NULL, cols = NULL) br_log2(x, idx = NULL, where = NULL, cols = NULL) br_log10(x, idx = NULL, where = NULL, cols = NULL) br_cos(x, idx = NULL, where = NULL, cols = NULL) br_sin(x, idx = NULL, where = NULL, cols = NULL) br_tan(x, idx = NULL, where = NULL, cols = NULL) br_not(x, idx = NULL, where = NULL, cols = NULL) br_expm1(x, idx = NULL, where = NULL, cols = NULL) br_log1p(x, idx = NULL, where = NULL, cols = NULL) br_acos(x, idx = NULL, where = NULL, cols = NULL) br_asin(x, idx = NULL, where = NULL, cols = NULL) br_atan(x, idx = NULL, where = NULL, cols = NULL) br_acosh(x, idx = NULL, where = NULL, cols = NULL) br_asinh(x, idx = NULL, where = NULL, cols = NULL) br_atanh(x, idx = NULL, where = NULL, cols = NULL) br_cosh(x, idx = NULL, where = NULL, cols = NULL) br_sinh(x, idx = NULL, where = NULL, cols = NULL) br_tanh(x, idx = NULL, where = NULL, cols = NULL) br_cospi(x, idx = NULL, where = NULL, cols = NULL) br_sinpi(x, idx = NULL, where = NULL, cols = NULL) br_tanpi(x, idx = NULL, where = NULL, cols = NULL) br_sign(x, idx = NULL, where = NULL, cols = NULL) br_is_na(x, idx = NULL, where = NULL, cols = NULL) br_zero(x, idx = NULL, where = NULL, cols = NULL) br_pow2(x, idx = NULL, where = NULL, cols = NULL)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
idx |
vector of indices |
where |
logical vector stored as floating point values. 0 = FALSE,
all non-zero values treated as TRUE. Default: NULL.
This value indicates if the operation should be performed for the
corresponding element in |
cols |
which matrix columns should this apply to? Argument only valid
if |
x
argument is modified by-reference and returned invisibly
# By-reference \code{abs()} x <- c(-1, 0, 1) br_abs(x) x # Conditional application of sqrt() x <- as.numeric(-5:5) t <- duplicate(x) # will use this to hold a numeric logical vector br_gt(t, 0) # where is the value > 0 br_sqrt(x, where = t) # only sqrt(x) where x > 0 x
# By-reference \code{abs()} x <- c(-1, 0, 1) br_abs(x) x # Conditional application of sqrt() x <- as.numeric(-5:5) t <- duplicate(x) # will use this to hold a numeric logical vector br_gt(t, 0) # where is the value > 0 br_sqrt(x, where = t) # only sqrt(x) where x > 0 x
Math operations taking two arguments
br_add(x, y, idx = NULL, where = NULL, cols = NULL) br_sub(x, y, idx = NULL, where = NULL, cols = NULL) br_mul(x, y, idx = NULL, where = NULL, cols = NULL) br_div(x, y, idx = NULL, where = NULL, cols = NULL) br_pow(x, y, idx = NULL, where = NULL, cols = NULL) br_eq(x, y, idx = NULL, where = NULL, cols = NULL) br_ne(x, y, idx = NULL, where = NULL, cols = NULL) br_lt(x, y, idx = NULL, where = NULL, cols = NULL) br_le(x, y, idx = NULL, where = NULL, cols = NULL) br_gt(x, y, idx = NULL, where = NULL, cols = NULL) br_ge(x, y, idx = NULL, where = NULL, cols = NULL) br_and(x, y, idx = NULL, where = NULL, cols = NULL) br_or(x, y, idx = NULL, where = NULL, cols = NULL) br_rem(x, y, idx = NULL, where = NULL, cols = NULL) br_idiv(x, y, idx = NULL, where = NULL, cols = NULL) br_max(x, y, idx = NULL, where = NULL, cols = NULL) br_min(x, y, idx = NULL, where = NULL, cols = NULL) br_assign(x, y, idx = NULL, where = NULL, cols = NULL) br_sumsq(x, y, idx = NULL, where = NULL, cols = NULL) br_diffsq(x, y, idx = NULL, where = NULL, cols = NULL) br_mod(x, y, idx = NULL, where = NULL, cols = NULL)
br_add(x, y, idx = NULL, where = NULL, cols = NULL) br_sub(x, y, idx = NULL, where = NULL, cols = NULL) br_mul(x, y, idx = NULL, where = NULL, cols = NULL) br_div(x, y, idx = NULL, where = NULL, cols = NULL) br_pow(x, y, idx = NULL, where = NULL, cols = NULL) br_eq(x, y, idx = NULL, where = NULL, cols = NULL) br_ne(x, y, idx = NULL, where = NULL, cols = NULL) br_lt(x, y, idx = NULL, where = NULL, cols = NULL) br_le(x, y, idx = NULL, where = NULL, cols = NULL) br_gt(x, y, idx = NULL, where = NULL, cols = NULL) br_ge(x, y, idx = NULL, where = NULL, cols = NULL) br_and(x, y, idx = NULL, where = NULL, cols = NULL) br_or(x, y, idx = NULL, where = NULL, cols = NULL) br_rem(x, y, idx = NULL, where = NULL, cols = NULL) br_idiv(x, y, idx = NULL, where = NULL, cols = NULL) br_max(x, y, idx = NULL, where = NULL, cols = NULL) br_min(x, y, idx = NULL, where = NULL, cols = NULL) br_assign(x, y, idx = NULL, where = NULL, cols = NULL) br_sumsq(x, y, idx = NULL, where = NULL, cols = NULL) br_diffsq(x, y, idx = NULL, where = NULL, cols = NULL) br_mod(x, y, idx = NULL, where = NULL, cols = NULL)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
y |
Either scalar numeric value or numeric vector of the same
length as |
idx |
idx |
where |
logical vector stored as floating point values. 0 = FALSE,
all non-zero values treated as TRUE. Default: NULL.
This value indicates if the operation should be performed for the
corresponding element in |
cols |
columns |
x
argument is modified by-reference and returned invisibly
# x <- x + y x <- as.numeric(1:10) y <- as.numeric(1:10) br_add(x, y) x # x <- x + 1 br_add(x, 1) x
# x <- x + y x <- as.numeric(1:10) y <- as.numeric(1:10) br_add(x, y) x # x <- x + 1 br_add(x, 1) x
Copy all or part of one vector into another
br_copy(x, y, n = NULL, xi = 1L, yi = 1L)
br_copy(x, y, n = NULL, xi = 1L, yi = 1L)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
y |
Either scalar numeric value or numeric vector of the same
length as |
n |
number of elements to copy. Default: NULL. This default requires
|
xi , yi
|
starting indices for the copy operation within the two vectors. Default: 1 i.e. the first element |
x
argument is modified by-reference and returned invisibly
# x, y are the same length. copy all of 'y' into 'x' x <- as.numeric(1:10) y <- as.numeric(10:1) br_copy(x, y) x # copy the last 3 elements of 'y' into the first 3 elements of x x <- as.numeric(1:10) y <- as.numeric(10:1) br_copy(x, y, n = 3, xi = 1, yi = 8) x # copy a scalar value into the last 3 elements of x x <- as.numeric(1:10) br_copy(x, y = 99, n = 3, xi = -3) x
# x, y are the same length. copy all of 'y' into 'x' x <- as.numeric(1:10) y <- as.numeric(10:1) br_copy(x, y) x # copy the last 3 elements of 'y' into the first 3 elements of x x <- as.numeric(1:10) y <- as.numeric(10:1) br_copy(x, y, n = 3, xi = 1, yi = 8) x # copy a scalar value into the last 3 elements of x x <- as.numeric(1:10) br_copy(x, y = 99, n = 3, xi = -3) x
Math operations taking only a single input vector
br_cumsum(x) br_cumprod(x) br_cummax(x) br_cummin(x)
br_cumsum(x) br_cumprod(x) br_cummax(x) br_cummin(x)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
x
argument is modified by-reference and returned invisibly
# By-reference \code{abs()} x <- c(-1, 0, 1) br_abs(x) x # Conditional application of sqrt() x <- as.numeric(-5:5) t <- duplicate(x) # will use this to hold a numeric logical vector br_gt(t, 0) # where is the value > 0 br_sqrt(x, where = t) # only sqrt(x) where x > 0 x
# By-reference \code{abs()} x <- c(-1, 0, 1) br_abs(x) x # Conditional application of sqrt() x <- as.numeric(-5:5) t <- duplicate(x) # will use this to hold a numeric logical vector br_gt(t, 0) # where is the value > 0 br_sqrt(x, where = t) # only sqrt(x) where x > 0 x
Performs a compound calculation equivalent to x <- x * a + b
using
the best precision from the math library.
br_fmadd(x, a, b) br_fmsub(x, a, b) br_fnmadd(x, a, b) br_fnmsub(x, a, b)
br_fmadd(x, a, b) br_fmsub(x, a, b) br_fnmadd(x, a, b) br_fnmsub(x, a, b)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
a , b
|
Either scalar numeric values or numeric vectors of the same
length as |
br_fmadd()
Equivalent to: x <- x * a + b
br_fmsub()
Equivalent to: x <- x * a - b
br_fnmadd()
Equivalent to: x <- -x * a + b
br_fnmsub()
Equivalent to: x <- -x * a - b
x
argument is modified by-reference and returned invisibly
x <- c(1, 2, 3) a <- c(2, 3, 4) b <- c(10, 10, 10) br_fmadd(x, a, b) x
x <- c(1, 2, 3) a <- c(2, 3, 4) b <- c(10, 10, 10) br_fmadd(x, a, b) x
Get/set a column or row of a matrix
br_mat_col_get(mat, i, vec) br_mat_col_set(mat, i, vec) br_mat_row_get(mat, i, vec) br_mat_row_set(mat, i, vec)
br_mat_col_get(mat, i, vec) br_mat_col_set(mat, i, vec) br_mat_row_get(mat, i, vec) br_mat_row_set(mat, i, vec)
mat |
matrix |
i |
index of row or column |
vec |
vector space. For get operations, vector length must
match the row or column length of the matirx. When doing a |
None. For get
operations, vec
is modified by-reference
and returned invisibly. For set
operations, mat
is modified
by reference and returned invisibly.
m <- matrix(as.numeric(1:6), 2, 3) m v <- alloc_n(nrow(m)) br_mat_col_get(m, 2, v) v
m <- matrix(as.numeric(1:6), 2, 3) m v <- alloc_n(nrow(m)) br_mat_col_get(m, 2, v) v
Distance calculation
br_mat_dist2(d, mat1, mat2) br_mat_dist3(d, mat1, mat2)
br_mat_dist2(d, mat1, mat2) br_mat_dist3(d, mat1, mat2)
d |
pre-allocated vector to hold the result |
mat1 , mat2
|
matrices holding (x, y) or (x, y, z) coordinates |
None. Argument d
modified in-place and returned invisibly
N <- 10 x1 <- rep(1, N) y1 <- runif(N) z1 <- runif(N) x2 <- runif(N) y2 <- runif(N) z2 <- runif(N) mat1 <- cbind(x1, y1, z1) mat2 <- cbind(x2, y2, z2) d <- alloc_n(N) br_mat_dist2(d, mat1, mat2) d
N <- 10 x1 <- rep(1, N) y1 <- runif(N) z1 <- runif(N) x2 <- runif(N) y2 <- runif(N) z2 <- runif(N) mat1 <- cbind(x1, y1, z1) mat2 <- cbind(x2, y2, z2) d <- alloc_n(N) br_mat_dist2(d, mat1, mat2) d
Hypotenuse length calculation (distance from origin)
br_mat_hypot2(d, mat) br_mat_hypot3(d, mat)
br_mat_hypot2(d, mat) br_mat_hypot3(d, mat)
d |
pre-allocated numeric vector with length matching |
mat |
numeric matrix |
None. Argument d
modified in-place and returned invisibly.
# Distance from origin in 3D N <- 10 x <- rep(1, N) y <- runif(N) z <- runif(N) mat <- cbind(x, y, z) d1 <- alloc_n(N) br_mat_hypot3(d1, mat) d1 # Compare to base R d2 <- sqrt(x * x + y * y + z * z) all.equal(d1, d2)
# Distance from origin in 3D N <- 10 x <- rep(1, N) y <- runif(N) z <- runif(N) mat <- cbind(x, y, z) d1 <- alloc_n(N) br_mat_hypot3(d1, mat) d1 # Compare to base R d2 <- sqrt(x * x + y * y + z * z) all.equal(d1, d2)
This function exposes the general matrix multiplication operation from R's
included BLAS. C = alpha * A * B + beta * C
br_mat_mat_mul(C, A, B, alpha = 1, beta = 0, ta = FALSE, tb = FALSE)
br_mat_mat_mul(C, A, B, alpha = 1, beta = 0, ta = FALSE, tb = FALSE)
C |
Matrix which will be modified by-reference |
A , B
|
Primary multiplication matrices |
alpha |
Scaling factor for |
beta |
Scaling factor for |
ta |
Should matrix 'A' be transposed? Default: FALSE |
tb |
Should matrix 'B' be transposed? Default: FALSE |
C is modified by-reference and returned invisibly
A <- matrix(as.numeric(1:32), 8, 4) B <- matrix(as.numeric(1:24), 4, 6) C <- alloc_mat_mat_mul(A, B) br_mat_mat_mul(C, A, B) # By reference. Overwriting 'C' C A %*% B # Compare to base R
A <- matrix(as.numeric(1:32), 8, 4) B <- matrix(as.numeric(1:24), 4, 6) C <- alloc_mat_mat_mul(A, B) br_mat_mat_mul(C, A, B) # By reference. Overwriting 'C' C A %*% B # Compare to base R
This function exposes the general matrix multiplication operation from R's
included BLAS. A = alpha * A * B
br_mat_mat_mul_bsq(A, B, alpha = 1, tb = FALSE)
br_mat_mat_mul_bsq(A, B, alpha = 1, tb = FALSE)
A , B
|
Primary multiplication matrices |
alpha |
Scaling factor for |
tb |
Should matrix 'B' be transposed? Default: FALSE |
C is modified by-reference and returned invisibly
A <- matrix(as.numeric(1:32), 8, 4) B <- matrix(as.numeric(1:16), 4, 4) A %*% B # Compare to base R br_mat_mat_mul_bsq(A, B) # By reference. Overwriting 'A' A
A <- matrix(as.numeric(1:32), 8, 4) B <- matrix(as.numeric(1:16), 4, 4) A %*% B # Compare to base R br_mat_mat_mul_bsq(A, B) # By reference. Overwriting 'A' A
These functions will normalise matrices of (x, y) or (x, y, z) coordinates so that the represented vector quantity have a length of 1.
br_mat_normalise2(mat) br_mat_normalise3(mat)
br_mat_normalise2(mat) br_mat_normalise3(mat)
mat |
numeric matrix |
Input matrix modified in-place and returned invisibly.
N <- 10 x <- runif(N) y <- runif(N) mat <- cbind(x, y) br_mat_normalise2(mat) # Lengths should now be 1. sqrt(mat[,1]^2 + mat[,2]^2)
N <- 10 x <- runif(N) y <- runif(N) mat <- cbind(x, y) br_mat_normalise2(mat) # Lengths should now be 1. sqrt(mat[,1]^2 + mat[,2]^2)
Roll elements of a matrix
br_mat_roll(mat, rows = 0, cols = 0)
br_mat_roll(mat, rows = 0, cols = 0)
mat |
matrix |
rows , cols
|
Number of rows and columns to roll |
None. Matrix is modified by-reference and returned invisibly
m <- matrix(as.numeric(1:6), 2, 3) m br_mat_roll(m, rows = 3) m
m <- matrix(as.numeric(1:6), 2, 3) m br_mat_roll(m, rows = 3) m
Transpose matrix
br_mat_transpose(mat)
br_mat_transpose(mat)
mat |
matrix |
None. Matrix is modified by-reference and returned invisibly
m <- matrix(as.numeric(1:6), 2, 3) m br_mat_transpose(m) m
m <- matrix(as.numeric(1:6), 2, 3) m br_mat_transpose(m) m
This function exposes the general matrix-vector multiplication operation from R's
included BLAS. y = alpha * A * x + beta * y
br_mat_vec_mul(y, A, x, alpha = 1, beta = 0, ta = FALSE)
br_mat_vec_mul(y, A, x, alpha = 1, beta = 0, ta = FALSE)
y |
Vector which will be modified by-reference |
A |
Matrix |
x |
vector |
alpha |
Scaling factor for |
beta |
Scaling factor for |
ta |
Should matrix 'A' be transposed? Default: FALSE |
y is modified by-reference and returned invisibly
A <- matrix(1, 3, 5) x <- rep(1, 5) y <- rep(0, 3) # Calculate. By-reference. Overwriting 'y' br_mat_vec_mul(y, A, x) y # Compare to R's method A %*% x
A <- matrix(1, 3, 5) x <- rep(1, 5) y <- rep(0, 3) # Calculate. By-reference. Overwriting 'y' br_mat_vec_mul(y, A, x) y # Compare to R's method A %*% x
This function exposes the general matrix-vector multiplication operation from R's
included BLAS. x = alpha * A * x
br_mat_vec_mul_asq(A, x, alpha = 1, ta = FALSE)
br_mat_vec_mul_asq(A, x, alpha = 1, ta = FALSE)
A |
Matrix |
x |
vector |
alpha |
Scaling factor for |
ta |
Should matrix 'A' be transposed? Default: FALSE |
None. x is modified by-reference and returned invisibly
A <- matrix(1, 3, 3) x <- rep(1, 3) # Compare to R's method A %*% x # Calculate. By-reference. Overwriting 'x' br_mat_vec_mul_asq(A, x) x
A <- matrix(1, 3, 3) x <- rep(1, 3) # Compare to R's method A %*% x # Calculate. By-reference. Overwriting 'x' br_mat_vec_mul_asq(A, x) x
Reverse a vector in place
br_rev(x)
br_rev(x)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
x
argument is modified by-reference and returned invisibly
x <- as.numeric(1:10) br_rev(x) x
x <- as.numeric(1:10) br_rev(x) x
Roll a vector
br_roll(x, dist)
br_roll(x, dist)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
dist |
Distance to roll. Can be postive or negative |
None. x
modified in-place and returned invisibly
x <- c(1, 2, 3, 4, 5) br_roll(x, 2) x br_roll(x, -2) x br_roll(x, -3)
x <- c(1, 2, 3, 4, 5) br_roll(x, 2) x br_roll(x, -2) x br_roll(x, -3)
Round
br_round(x, digits)
br_round(x, digits)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
digits |
number of decimal places |
x
modified by reference and returned invisibly
x <- c(1.234, 5.6789) br_round(x, 2) x
x <- c(1.234, 5.6789) br_round(x, 2) x
Random numbers are generated using a Lehmer RNG (also known as Park-Miller RNG).
Note: The standard R RNG is only used to seed the Lehmer RNG - which means
that set.seed()
can be used for generating repeatable streams.
br_runif(x, lower = 0, upper = 1)
br_runif(x, lower = 0, upper = 1)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
lower , upper
|
limits of random numbers generated. Default: 0, 1 |
x
argument is modified by-reference and returned invisibly
set_seed_lehmer(1) x <- alloc_n(10) br_runif(x) x
set_seed_lehmer(1) x <- alloc_n(10) br_runif(x) x
Fill vector with a numeric sequence
br_seq(x, from = 1, to = NULL, step = NULL)
br_seq(x, from = 1, to = NULL, step = NULL)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
from |
starting value in sequence. Default: 1 |
to |
ending value in sequence. Default: NULL. If NULL, then
|
step |
step size. This value is ignored if |
x
argument is modified by-reference and returned invisibly
# fill 1:10 x <- alloc_n(10) br_seq(x) x # fill from 1 to 100 x <- alloc_n(10) br_seq(x, from = 1, to = 100) x # fill from 0 in steps of 2.5 x <- alloc_n(10) br_seq(x, from = 0, step = 0.25) x
# fill 1:10 x <- alloc_n(10) br_seq(x) x # fill from 1 to 100 x <- alloc_n(10) br_seq(x, from = 1, to = 100) x # fill from 0 in steps of 2.5 x <- alloc_n(10) br_seq(x, from = 0, step = 0.25) x
Uses fisher-yates.
br_shuffle(x)
br_shuffle(x)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
The fast variant of this function uses a xoshiro256++ PRNG which is much faster than sourcing randomness from R's random number generator.
x
argument is modified by-reference and returned invisibly
set.seed(1) set_seed_lehmer() x <- as.numeric(1:10) br_shuffle(x) x
set.seed(1) set_seed_lehmer() x <- as.numeric(1:10) br_shuffle(x) x
Sort a numeric vector by-reference
br_sort(x, decreasing = FALSE)
br_sort(x, decreasing = FALSE)
x |
numeric vector. This vector will be modified by-reference to contain the result of the calculation |
decreasing |
Logical. Sort values in decreasing order? default FALSE |
x
argument is modified by-reference and returned invisibly
set.seed(1) x <- sample(as.numeric(1:10)) br_sort(x) x
set.seed(1) x <- sample(as.numeric(1:10)) br_sort(x) x
List of all functions
by_reference
by_reference
An object of class list
of length 66.
x <- as.numeric(1:10) with(by_reference, {x + 1; x * 2}) x
x <- as.numeric(1:10) with(by_reference, {x + 1; x * 2}) x
Duplicate an R object
duplicate(x)
duplicate(x)
x |
Any R object |
duplicate of the given object
x <- c(1, 2, 3) y <- duplicate(x) y
x <- c(1, 2, 3) y <- duplicate(x) y
Initialise the state of this package's Lehmer RNG
set_seed_lehmer(seed)
set_seed_lehmer(seed)
seed |
initeger seed value |
None.
set_seed_lehmer(1) x <- alloc_n(10) br_runif(x) x
set_seed_lehmer(1) x <- alloc_n(10) br_runif(x) x
Add rotation to a transformation matrix
tf2_add_rotate(mat, theta)
tf2_add_rotate(mat, theta)
mat |
3x3 transformation matrix |
theta |
rotation angle (radians) |
None. mat
modified by reference and returned invisibly
tf <- tf2_new() tf2_add_rotate(tf, pi/6) tf
tf <- tf2_new() tf2_add_rotate(tf, pi/6) tf
Add scaling to a transformation matrix
tf2_add_scale(mat, x = 1, y = x)
tf2_add_scale(mat, x = 1, y = x)
mat |
3x3 transformation matrix |
x , y
|
scaling |
None. mat
modified by reference and returned invisibly
tf <- tf2_new() tf2_add_scale(tf, 1, 2) tf
tf <- tf2_new() tf2_add_scale(tf, 1, 2) tf
Add translation to a transformation matrix
tf2_add_translate(mat, x = 0, y = 0)
tf2_add_translate(mat, x = 0, y = 0)
mat |
3x3 transformation matrix |
x , y
|
translation |
None. mat
modified by reference and returned invisibly
tf <- tf2_new() tf2_add_translate(tf, 1, 2) tf
tf <- tf2_new() tf2_add_translate(tf, 1, 2) tf
Apply a 2-D affine transform to a matrix or data.frame of coordinates
tf2_apply(x, tf)
tf2_apply(x, tf)
x |
matrix or data.frame. If a matrix, the first two columns must be x,y coordinates. If a data.frame, must contain columns 'x' and 'y'. Other columns will not be affected. |
tf |
3x3 transformation matrix |
None. mat
is modified by reference and returned invisibly
# Transform a matrix tf <- tf2_new() tf <- tf2_add_translate(tf, 1, 10) x <- cbind( x = as.numeric(1:6), y = as.numeric(1:6), other = 999 ) tf x tf2_apply(x, tf) x # Transform a data.frame x <- data.frame( other = 'hello', x = as.numeric(1:6), y = as.numeric(1:6) ) tf2_apply(x, tf) x
# Transform a matrix tf <- tf2_new() tf <- tf2_add_translate(tf, 1, 10) x <- cbind( x = as.numeric(1:6), y = as.numeric(1:6), other = 999 ) tf x tf2_apply(x, tf) x # Transform a data.frame x <- data.frame( other = 'hello', x = as.numeric(1:6), y = as.numeric(1:6) ) tf2_apply(x, tf) x
Create identity transform for 2-D
tf2_new()
tf2_new()
3x3 identity matrix
tf2_new()
tf2_new()
Reset a 2-D transformation matrix back to the identity matrix
tf2_reset(mat)
tf2_reset(mat)
mat |
3x3 transformation matrix |
None. mat
modified by reference and returned invisibly
tf <- matrix(1, 3, 3) tf2_reset(tf) tf
tf <- matrix(1, 3, 3) tf2_reset(tf) tf
Add rotation to a transformation matrix
tf3_add_rotate_x(mat, theta) tf3_add_rotate_y(mat, theta) tf3_add_rotate_z(mat, theta)
tf3_add_rotate_x(mat, theta) tf3_add_rotate_y(mat, theta) tf3_add_rotate_z(mat, theta)
mat |
4x4 transformation matrix |
theta |
rotation angle (radians) |
None. mat
modified by reference and returned invisibly
tf <- tf3_new() tf3_add_rotate_x(tf, pi/6) tf
tf <- tf3_new() tf3_add_rotate_x(tf, pi/6) tf
Add scaling to a transformation matrix
tf3_add_scale(mat, x = 1, y = x, z = x)
tf3_add_scale(mat, x = 1, y = x, z = x)
mat |
4x4 transformation matrix |
x , y , z
|
scaling |
None. mat
modified by reference and returned invisibly
tf <- tf3_new() tf3_add_scale(tf, 1, 2, 3) tf
tf <- tf3_new() tf3_add_scale(tf, 1, 2, 3) tf
Add translation to a transformation matrix
tf3_add_translate(mat, x = 0, y = 0, z = 0)
tf3_add_translate(mat, x = 0, y = 0, z = 0)
mat |
4x4 transformation matrix |
x , y , z
|
translation |
None. mat
modified by reference and returned invisibly
tf <- tf3_new() tf3_add_translate(tf, 1, 2, 3) tf
tf <- tf3_new() tf3_add_translate(tf, 1, 2, 3) tf
Apply a 3D affine transform to a matrix or data.frame of coordinates
tf3_apply(x, tf)
tf3_apply(x, tf)
x |
matrix or data.frame. If a matrix, the first two columns must be x,y coordinates. If a data.frame, must contain columns 'x' and 'y'. Other columns will not be affected. |
tf |
4x4 transformation matrix |
None. mat
is modified by reference and returned invisibly
# Transform a matrix tf <- tf3_new() tf <- tf3_add_translate(tf, 1, 10, 100) x <- cbind( x = as.numeric(1:6), y = as.numeric(1:6), z = as.numeric(1:6), other = 999 ) tf x tf3_apply(x, tf) x # Transform a data.frame x <- data.frame( other = 'hello', x = as.numeric(1:6), y = as.numeric(1:6), z = as.numeric(1:6) ) tf3_apply(x, tf) x
# Transform a matrix tf <- tf3_new() tf <- tf3_add_translate(tf, 1, 10, 100) x <- cbind( x = as.numeric(1:6), y = as.numeric(1:6), z = as.numeric(1:6), other = 999 ) tf x tf3_apply(x, tf) x # Transform a data.frame x <- data.frame( other = 'hello', x = as.numeric(1:6), y = as.numeric(1:6), z = as.numeric(1:6) ) tf3_apply(x, tf) x
Create identity transform
tf3_new()
tf3_new()
4x4 identity matrix
tf3_new()
tf3_new()
Reset a transformation matrix back to the identity matrix
tf3_reset(mat)
tf3_reset(mat)
mat |
4x4 transformation matrix |
None. mat
modified by reference and returned invisibly
tf <- matrix(1, 4, 4) tf3_reset(tf) tf
tf <- matrix(1, 4, 4) tf3_reset(tf) tf