Learn R Programming

RandomFieldsUtils (version 1.2.5)

rowMeansx: Some Further Row and Column Functions

Description

The function rowMeansx returns weighted row means; the function colMax returns column maxima; the function rowProd returns the product of each row; the function quadratic calculates a quadratic form the function SelfDivByRow devides each column by a scalar; the function dotXV calculates columnwise the dot product; the function crossprodx calculates the cross product (using AVX); the function scalarx calculates the scalar product (using AVX);

Usage

rowMeansx(x, weight=NULL)
colMax(x)
rowProd(x)
SelfDivByRow(x, v)
quadratic(x, v)
dotXV(x, w)
crossprodx(x,y,mode=-1)
scalarx(x, y, mode=0)

Arguments

x

numerical (or logical) matrix

v

vector whose length equals the number of columns of x

w

vector whose length equals the number of rows of x

weight

numerical or logical vector of length nrow(x)

y

numerical matrix

mode

integer between 0 and 8 or negative, indicating that the default value should be used. Determine the algorithm how the scalar product is calculated. These values are experimental and may change their meaning.

Value

rowMeansx returns a vector of lengthnrow(x).

colMax returns a vector of length ncol(x).

rowProd returns a vector of length nrow(x).

quadratic returns a scalar.

SelfDivByRow returns a matrix of same size as x.

dotXV returns a matrix of same size as x.

Details

quadratic(x, v) calculates the quadratic form \(v^\top x v\); The matrix x must be squared.

Examples

Run this code
# NOT RUN {
<!-- % library(RandomFieldsUtils) -->
# }
# NOT RUN {
c <- if (interactive()) 10000 else 10
r <- if (interactive()) 20000 else 20
M <- matrix(nr=r, 1:(c * r))

## unweighted means, compare to rowMeans
print(system.time(m1 <- rowMeans(M)))
print(system.time(m2 <- rowMeansx(M)))
stopifnot(all.equal(m1, m2))

## weighted row means, compare to rowMeans
W <- 1 / (ncol(M) : 1)
print(system.time({M0 <- t(W * t(M)); m1 <- rowMeans(M0)}))
print(system.time(m2 <- rowMeansx(M, W)))
stopifnot(all.equal(m1, m2))

print(system.time(m1 <- apply(M, 2, max)))
print(system.time(m2 <- colMax(M)))
stopifnot(m1 == m2)

# }

Run the code above in your browser using DataLab