rutils

rutils

Small numerical helpers that reproduce R semantics exactly (or to the last bit where possible).

R functions reproduced: approx, signif, quantile (type 7), sum/mean/sd (R accumulates in long double), loess (via scikit-misc, which wraps the same netlib code as R), %*% (reference BLAS summation order), and write.csv / write.table number formatting.

Functions

Name Description
load_pkl
r_apply_sd apply(a, margin, sd, na.rm = TRUE) where axis is the axis that is reduced.
r_approx
r_as_character as.character() as used for row/column labels (15 significant digits).
r_format_num
r_loess_fit
r_loess_predict
r_matvec A %*% w with the summation order of the reference BLAS (dgemm) used by R.
r_mean mean(x) (R: long double sum, then a refinement step; ~ correctly rounded).
r_quantile quantile(x, probs) (type 7) as implemented in R’s quantile.default.
r_round_half_even
r_rowsums apply(a, 1, sum) for a 2-d array (NA propagates, as in R without na.rm).
r_sd sd(x, na.rm = TRUE) as computed by R’s var (two-pass, long double accumulation).
r_signif signif(x, digits): port of R’s fprec() (nmath/fprec.c).
r_sum sum(x): R accumulates in long double; math.fsum (correctly rounded) matches it.
save_pkl
write_r_csv write.csv(data, file) for a table given as a list of columns.

load_pkl

rutils.load_pkl(path)

r_apply_sd

rutils.r_apply_sd(a, axis)

apply(a, margin, sd, na.rm = TRUE) where axis is the axis that is reduced.

r_approx

rutils.r_approx(x, y, xout, method='linear', rule=1, na_rm=True)

r_as_character

rutils.r_as_character(v)

as.character() as used for row/column labels (15 significant digits).

r_format_num

rutils.r_format_num(v, digits=15)

r_loess_fit

rutils.r_loess_fit(
    x,
    y,
    span=0.75,
    degree=2,
    surface='interpolate',
    statistics='approximate',
)

r_loess_predict

rutils.r_loess_predict(model, newx)

r_matvec

rutils.r_matvec(A, w)

A %*% w with the summation order of the reference BLAS (dgemm) used by R.

r_mean

rutils.r_mean(x)

mean(x) (R: long double sum, then a refinement step; ~ correctly rounded).

r_quantile

rutils.r_quantile(x, probs)

quantile(x, probs) (type 7) as implemented in R’s quantile.default.

r_round_half_even

rutils.r_round_half_even(x)

r_rowsums

rutils.r_rowsums(a)

apply(a, 1, sum) for a 2-d array (NA propagates, as in R without na.rm).

r_sd

rutils.r_sd(x)

sd(x, na.rm = TRUE) as computed by R’s var (two-pass, long double accumulation).

r_signif

rutils.r_signif(x, digits)

signif(x, digits): port of R’s fprec() (nmath/fprec.c).

r_sum

rutils.r_sum(x)

sum(x): R accumulates in long double; math.fsum (correctly rounded) matches it.

save_pkl

rutils.save_pkl(path, **objects)

write_r_csv

rutils.write_r_csv(path, columns, colnames, rownames=None, row_names=True)

write.csv(data, file) for a table given as a list of columns.

columns: list of 1-d sequences (numeric or str); colnames: list of str; rownames: list of str (default “1”, “2”, …); row_names=False drops the row-name column.