Implements the Math S4groupGeneric functions
for dbMatrix objects. This includes various mathematical operations such as
logarithms, exponentials, trigonometric functions, and other transformations.
Arguments
- x
A
dbMatrixobject.
Value
A dbMatrix object with the mathematical operation applied to each element.
Details
Supported functions:
Rounding and sign:
abs(),sign(),sqrt(),ceiling(),floor(),trunc()Logarithms and exponentials:
log(),log10(),log2(),log1p(),exp(),expm1()Trigonometric:
cos(),sin(),tan(),acos(),asin(),atan(),cosh(),sinh(),tanh(),acosh(),asinh(),atanh()
Not supported: cumprod(), cospi(), sinpi(), tanpi(), digamma() and
trigamma(). cumsum(), cummax() and cummin() run but do not match base R,
which returns a vector.
Values outside a function's domain (for example log(0) on a dense matrix or
acos(2)) give a database error instead of -Inf or NaN.
Sparse matrices: a dbSparseMatrix stores only non-zero values. Functions
that turn 0 into something else, such as exp() or cos(), first fill in the
zeros (see dbMatrix.allow_densify in dbMatrix_options). log(), log2()
and log10() are the exception: they transform only the stored values and
leave zeros at 0, so log(x + 1)-style normalization can stay sparse.
DuckDB Log Function Mappings:
| R Function | DuckDB Function | Notes |
log(x) | LN(x) | Natural logarithm |
log10(x) | LOG10(x) | Base-10 logarithm |
log2(x) | LOG2(x) | Base-2 logarithm |
log1p(x) | LN(x + 1) | log(1+x), computed as LN |
Sparsity-Preserving Log:
For dbSparseMatrix with pending operations, log(x + 1) operations preserve sparsity
since log(0 + 1) = 0. The multiplicative component is applied first,
then the log transformation is applied to sparse values only.
Examples
mat <- matrix(1, nrow = 3, ncol = 3)
dbmat <- as.dbMatrix(mat)
#> duckdb keeps downloaded extensions and secrets in a temporary directory:
#> ℹ /tmp/RtmpQoK7i1/duckdb
#> This is removed when the R session ends.
#> • Extensions are re-downloaded each session.
#> • Secrets are lost.
#> ℹ Run duckdb(shared_home = TRUE) (or create ~/.duckdb) to keep them (suitable for most users).
#> ℹ Run duckdb(shared_home = FALSE) to accept the temporary directory (and silence this message).
#> ℹ See ?duckdb_storage for details and alternatives.
log(dbmat)
#> 3 x 3 dbMatrix of class "dbDenseMatrix"
#> [[ Colnames: 'col1', 'col2', 'col3' ]]
#>
#> row1 0.0000000 0.0000000 0.0000000
#> row2 0.0000000 0.0000000 0.0000000
#> row3 0.0000000 0.0000000 0.0000000
sqrt(dbmat)
#> 3 x 3 dbMatrix of class "dbDenseMatrix"
#> [[ Colnames: 'col1', 'col2', 'col3' ]]
#>
#> row1 1.0000000 1.0000000 1.0000000
#> row2 1.0000000 1.0000000 1.0000000
#> row3 1.0000000 1.0000000 1.0000000
sin(dbmat)
#> 3 x 3 dbMatrix of class "dbDenseMatrix"
#> [[ Colnames: 'col1', 'col2', 'col3' ]]
#>
#> row1 0.8414710 0.8414710 0.8414710
#> row2 0.8414710 0.8414710 0.8414710
#> row3 0.8414710 0.8414710 0.8414710