rray_sum() computes the sum along a given axis or axes. The dimensionality of x is retained in the result.

rray_sum(x, axes = NULL)

Arguments

x

A vector, matrix, or array to reduce.

axes

An integer vector specifying the axes to reduce over. 1 reduces the number of rows to 1, performing the reduction along the way. 2 does the same, but with the columns, and so on for higher dimensions. The default reduces along all axes.

Value

The result of the reduction as a double with the same shape as x, except along axes, which have been reduced to size 1.

See also

Examples

x <- rray(1:10, c(5, 2)) # Reduce the number of rows to 1, # summing along the way rray_sum(x, 1)
#> <rray<dbl>[,2][1]> #> [,1] [,2] #> [1,] 15 40
# Reduce the number of columns to 1, # summing along the way rray_sum(x, 2)
#> <rray<dbl>[,1][5]> #> [,1] #> [1,] 7 #> [2,] 9 #> [3,] 11 #> [4,] 13 #> [5,] 15
# Reduce along all axes, but keep dimensions rray_sum(x)
#> <rray<dbl>[,1][1]> #> [,1] #> [1,] 55
# Column-wise proportions x / rray_sum(x, 1)
#> <rray<dbl>[,2][5]> #> [,1] [,2] #> [1,] 0.06666667 0.150 #> [2,] 0.13333333 0.175 #> [3,] 0.20000000 0.200 #> [4,] 0.26666667 0.225 #> [5,] 0.33333333 0.250
# Row-wise proportions x / rray_sum(x, 2)
#> <rray<dbl>[,2][5]> #> [,1] [,2] #> [1,] 0.1428571 0.8571429 #> [2,] 0.2222222 0.7777778 #> [3,] 0.2727273 0.7272727 #> [4,] 0.3076923 0.6923077 #> [5,] 0.3333333 0.6666667
# Reducing over multiple axes # This reduces over the rows and columns # of each mini-matrix in the 3rd dimension y <- rray(1:24, c(2, 3, 4)) rray_sum(y, c(1, 2))
#> <rray<dbl>[,1,4][1]> #> , , 1 #> #> [,1] #> [1,] 21 #> #> , , 2 #> #> [,1] #> [1,] 57 #> #> , , 3 #> #> [,1] #> [1,] 93 #> #> , , 4 #> #> [,1] #> [1,] 129 #>