Skip to content

Commit

Permalink
New 'set' math operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Apr 10, 2024
1 parent a352621 commit 6b4e906
Show file tree
Hide file tree
Showing 9 changed files with 656 additions and 3 deletions.
3 changes: 0 additions & 3 deletions CRAN-SUBMISSION

This file was deleted.

9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ export(seq_)
export(seq_id)
export(seq_size)
export(sequence_)
export(set_abs)
export(set_ceiling)
export(set_change_sign)
export(set_exp)
export(set_floor)
export(set_log)
export(set_round)
export(set_sqrt)
export(set_trunc)
export(setdiff_)
export(sset)
export(unlisted_length)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cheapr (Development version)

* New maths functions that transform data by reference.

* Fixed an inconsistency of when `sequence_()` would error when supplied with
a zero-length size argument.

Expand Down
36 changes: 36 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,42 @@ cpp_sequence_id <- function(size) {
.Call(`_cheapr_cpp_sequence_id`, size)
}

cpp_set_abs <- function(x) {
.Call(`_cheapr_cpp_set_abs`, x)
}

cpp_set_floor <- function(x) {
.Call(`_cheapr_cpp_set_floor`, x)
}

cpp_set_ceiling <- function(x) {
.Call(`_cheapr_cpp_set_ceiling`, x)
}

cpp_set_trunc <- function(x) {
.Call(`_cheapr_cpp_set_trunc`, x)
}

cpp_set_round <- function(x, digits) {
.Call(`_cheapr_cpp_set_round`, x, digits)
}

cpp_set_change_sign <- function(x) {
.Call(`_cheapr_cpp_set_change_sign`, x)
}

cpp_set_exp <- function(x) {
.Call(`_cheapr_cpp_set_exp`, x)
}

cpp_set_sqrt <- function(x) {
.Call(`_cheapr_cpp_set_sqrt`, x)
}

cpp_set_log <- function(x, base) {
.Call(`_cheapr_cpp_set_log`, x, base)
}

is_alt_compact_seq <- function(x) {
.Call(`_cheapr_is_alt_compact_seq`, x)
}
Expand Down
63 changes: 63 additions & 0 deletions R/set_math.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' Math operations by reference
#'
#' @description
#' These functions transform your variable by reference, with no copies being made.
#'
#' @param x A numeric vector.
#' @param digits Number of digits to round to.
#' @param base Logarithm base.
#'
#' @details
#' These functions are particularly useful for situations
#' where you have made a copy and then
#' wish to perform further operations without creating more copies. \cr
#' `NA` and `NaN` values are ignored and never updated. \cr
#' These functions will \bold{not work} on \bold{any} classed objects, meaning they
#' only work on standard integer and numeric vectors and matrices.
#'
#' @returns
#' The exact same object with no copy made, just transformed.
#'
#' @examples
#' library(cheapr)
#' library(bench)
#'
#' x <- rnorm(2e05)
#' options(cheapr.cores = 2)
#' mark(
#' base = exp(log(abs(x))),
#' cheapr = set_exp(set_log(set_abs(x)))
#' )
#' options(cheapr.cores = 1)
#'
#' @rdname set_math
#' @export
set_abs <- cpp_set_abs
#' @rdname set_math
#' @export
set_floor <- cpp_set_floor
#' @rdname set_math
#' @export
set_ceiling <- cpp_set_ceiling
#' @rdname set_math
#' @export
set_trunc <- cpp_set_trunc
#' @rdname set_math
#' @export
set_exp <- cpp_set_exp
#' @rdname set_math
#' @export
set_sqrt <- cpp_set_sqrt
#' @rdname set_math
#' @export
set_change_sign <- cpp_set_change_sign
#' @rdname set_math
#' @export
set_round <- function(x, digits = 0){
cpp_set_round(x, digits)
}
#' @rdname set_math
#' @export
set_log <- function(x, base = exp(1)){
cpp_set_log(x, base)
}
66 changes: 66 additions & 0 deletions man/set_math.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,69 @@ extern "C" SEXP _cheapr_cpp_sequence_id(SEXP size) {
return cpp11::as_sexp(cpp_sequence_id(cpp11::as_cpp<cpp11::decay_t<SEXP>>(size)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_abs(SEXP x);
extern "C" SEXP _cheapr_cpp_set_abs(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_abs(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_floor(SEXP x);
extern "C" SEXP _cheapr_cpp_set_floor(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_floor(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_ceiling(SEXP x);
extern "C" SEXP _cheapr_cpp_set_ceiling(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_ceiling(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_trunc(SEXP x);
extern "C" SEXP _cheapr_cpp_set_trunc(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_trunc(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_round(SEXP x, int digits);
extern "C" SEXP _cheapr_cpp_set_round(SEXP x, SEXP digits) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_round(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x), cpp11::as_cpp<cpp11::decay_t<int>>(digits)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_change_sign(SEXP x);
extern "C" SEXP _cheapr_cpp_set_change_sign(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_change_sign(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_exp(SEXP x);
extern "C" SEXP _cheapr_cpp_set_exp(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_exp(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_sqrt(SEXP x);
extern "C" SEXP _cheapr_cpp_set_sqrt(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_sqrt(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x)));
END_CPP11
}
// set_math.cpp
SEXP cpp_set_log(SEXP x, double base);
extern "C" SEXP _cheapr_cpp_set_log(SEXP x, SEXP base) {
BEGIN_CPP11
return cpp11::as_sexp(cpp_set_log(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x), cpp11::as_cpp<cpp11::decay_t<double>>(base)));
END_CPP11
}
// sset.cpp
bool is_alt_compact_seq(SEXP x);
extern "C" SEXP _cheapr_is_alt_compact_seq(SEXP x) {
Expand Down Expand Up @@ -347,10 +410,19 @@ static const R_CallMethodDef CallEntries[] = {
{"_cheapr_cpp_row_na_counts", (DL_FUNC) &_cheapr_cpp_row_na_counts, 1},
{"_cheapr_cpp_sequence", (DL_FUNC) &_cheapr_cpp_sequence, 3},
{"_cheapr_cpp_sequence_id", (DL_FUNC) &_cheapr_cpp_sequence_id, 1},
{"_cheapr_cpp_set_abs", (DL_FUNC) &_cheapr_cpp_set_abs, 1},
{"_cheapr_cpp_set_add_attr", (DL_FUNC) &_cheapr_cpp_set_add_attr, 3},
{"_cheapr_cpp_set_add_attributes", (DL_FUNC) &_cheapr_cpp_set_add_attributes, 3},
{"_cheapr_cpp_set_ceiling", (DL_FUNC) &_cheapr_cpp_set_ceiling, 1},
{"_cheapr_cpp_set_change_sign", (DL_FUNC) &_cheapr_cpp_set_change_sign, 1},
{"_cheapr_cpp_set_exp", (DL_FUNC) &_cheapr_cpp_set_exp, 1},
{"_cheapr_cpp_set_floor", (DL_FUNC) &_cheapr_cpp_set_floor, 1},
{"_cheapr_cpp_set_log", (DL_FUNC) &_cheapr_cpp_set_log, 2},
{"_cheapr_cpp_set_rm_attr", (DL_FUNC) &_cheapr_cpp_set_rm_attr, 2},
{"_cheapr_cpp_set_rm_attributes", (DL_FUNC) &_cheapr_cpp_set_rm_attributes, 1},
{"_cheapr_cpp_set_round", (DL_FUNC) &_cheapr_cpp_set_round, 2},
{"_cheapr_cpp_set_sqrt", (DL_FUNC) &_cheapr_cpp_set_sqrt, 1},
{"_cheapr_cpp_set_trunc", (DL_FUNC) &_cheapr_cpp_set_trunc, 1},
{"_cheapr_cpp_sset_df", (DL_FUNC) &_cheapr_cpp_sset_df, 2},
{"_cheapr_cpp_sset_range", (DL_FUNC) &_cheapr_cpp_sset_range, 4},
{"_cheapr_cpp_vec_length", (DL_FUNC) &_cheapr_cpp_vec_length, 1},
Expand Down
Loading

0 comments on commit 6b4e906

Please sign in to comment.