Learn R Programming

rray (version 0.1.0)

rray_arith: Arithmetic operations

Description

These functions provide the implementations for their underlying infix operators (i.e. rray_add() powers +). All operators apply broadcasting to their input.

Usage

x %b+% y

rray_add(x, y)

x %b-% y

rray_subtract(x, y)

x %b*% y

rray_multiply(x, y)

x %b/% y

rray_divide(x, y)

x %b^% y

rray_pow(x, y)

rray_identity(x)

rray_opposite(x)

Arguments

x, y

A pair of vectors.

Value

The value of the arithmetic operation, with dimensions identical to the common dimensions of the input.

Details

In case you want to apply arithmetic operations with broadcasting to purely base R objects using infix operators, custom infix functions have been exported, such as %b+%, which will perform addition with broadcasting no matter what type the input is.

Examples

Run this code
# NOT RUN {
library(magrittr)

x <- rray(1:8, c(2, 2, 2)) %>%
  rray_set_row_names(c("r1", "r2")) %>%
  rray_set_col_names(c("c1", "c2"))

y <- matrix(1:2, nrow = 1)

# All arithmetic functions are applied with broadcasting
rray_add(x, y)

# And the power `+` when any underlying input
# is an rray
x + y

# If you happen to only have base R matrices/arrays
# you can use `rray_add()` or `%b+%` to get the
# broadcasting behavior
rray_add(y, matrix(1:2))

y %b+% matrix(1:2)

# }

Run the code above in your browser using DataLab