Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",

"/Library/Frameworks/R.framework/Resources/include",
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
"/usr/local/include"
],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c99",
"cppStandard": "c++11"
}
],
"version": 4
}
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(sparse_is_na)
export(sparse_logical)
export(sparse_mean)
export(sparse_median)
export(sparse_multiplication)
export(sparse_multiplication_scalar)
export(sparse_positions)
export(sparse_replace_na)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* Helper function `sparse_replace_na()` has been added. (#91)

* Adding the arithmatic function `sparse_multiplication()`. (#93)

# sparsevctrs 0.2.0

## New Functions
Expand Down
83 changes: 83 additions & 0 deletions R/arithmatic.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,86 @@ sparse_subtraction_scalar <- function(x, val) {

res
}

#' Vector arithmatic with sparse vectors
#'
#' Do arithmatic operations on sparse vectors while trying to void destroying
#' the sparsity.
#'
#' @param x A numeric vector.
#' @param y A numeric vector.
#'
#' @details
#'
#' Note that this function works with both sparse and dense vectors for both `x`
#' and `y`, returning a sparse or dense vector according to the input.
#'
#' For `sparse_multiplication()` the class of the resulting vector depends on
#' the classes of `x` and `y`. If both `x` and `y` are integer vectors then an
#' integer vector is returned, otherwise a double vector is returned.
#'
#' `sparse_multiplication()` will return a non-sparse vector if both `x` and `y`
#' is non-sparse. Otherwise a sparse vector is returned.
#'
#' `sparse_multiplication()` will destroy sparsity of sparse vectors with non-0
#' `default` values.
#'
#' @return A sparse vector of same type.
#'
#' @examples
#' x_sparse <- sparse_double(c(pi, 5, 0.1), c(2, 5, 10), 10)
#'
#' sparse_multiplication(x_sparse, x_sparse)
#' @name sparse-arithmatic
NULL

#' @rdname sparse-arithmatic
#' @export
sparse_multiplication <- function(x, y) {
if (!is.numeric(x)) {
cli::cli_abort("{.arg x} must me numeric, not {.obj_type_friendly {x}}.")
}
if (!is.numeric(y)) {
cli::cli_abort("{.arg y} must me numeric not {.obj_type_friendly {x}}.")
}

if (length(x) != length(y)) {
x_len <- length(x)
y_len <- length(y)
cli::cli_abort(
"{.arg x} ({x_len}) and {.arg y} ({y_len}) must be the same length."
)
}

x_class <- class(x)
y_class <- class(y)

if (x_class != y_class) {
if (x_class == "integer") {
if (is_sparse_vector(x)) {
x <- as_sparse_double(x, default = sparse_default(x))
} else {
x <- as.double(x)
}
} else {
if (is_sparse_vector(y)) {
y <- as_sparse_double(y, default = sparse_default(y))
} else {
y <- as.double(y)
}
}
}

x_default <- sparse_default(x)
y_default <- sparse_default(y)

if (!is.na(x_default) && x_default != 0) {
x <- x[]
}

if (!is.na(y_default) && y_default != 0) {
y <- y[]
}

.Call(ffi_sparse_multiplication, x, y)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ reference:
contents:
- type-predicates
- sparse-arithmatic-scalar
- sparse-arithmatic
- extractors
- has_sparse_elements
- sparsevctrs_options
40 changes: 40 additions & 0 deletions man/sparse-arithmatic.Rd

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

2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "sparse-extractors.h"
#include "sparse-utils.h"
#include "sparse-dummy.h"
#include "sparse-arithmatic.h"

// Defined in altrep-sparse-double.c
extern SEXP ffi_altrep_new_sparse_double(SEXP);
Expand Down Expand Up @@ -37,6 +38,7 @@ static const R_CallMethodDef CallEntries[] = {
{"ffi_is_sparse_vector", (DL_FUNC) &ffi_is_sparse_vector, 1},
{"ffi_sparse_dummy", (DL_FUNC) &ffi_sparse_dummy, 4},
{"ffi_sparse_dummy_na", (DL_FUNC) &ffi_sparse_dummy_na, 4},
{"ffi_sparse_multiplication", (DL_FUNC) &ffi_sparse_multiplication, 2},
{NULL, NULL, 0}
};

Expand Down
Loading
Loading