Learn R Programming

fntl (version 0.1.1)

outer: Outer Matrix

Description

Compute "outer" matrices and matrix-vector products based on a function that operators on pairs of rows. See details.

Usage

outer1(X, f)

outer2(X, Y, f)

outer1_matvec(X, f, a)

outer2_matvec(X, Y, f, a)

Value

outer1 and outer2 return a matrix. outer1_matvec and outer2_matvec

return a vector. See section "Outer" of the package vignette for details.

Arguments

X

A numerical matrix.

f

Function \(f(x, y)\) that operates on a pair of rows. Depending on the context, rows \(x\) and \(y\) are both rows of \(X\), or \(x\) is a row from \(X\) and \(y\) is a row from from \(Y\).

Y

A numerical matrix.

a

A scalar vector.

Details

The outer1 function computes the \(n \times n\) symmetric matrix

$$ \text{\texttt outer1}(X, f) = \begin{bmatrix} f(x_1, x_1) & \cdots & f(x_1, x_n) \cr \vdots & \ddots & \vdots \cr f(x_n, x_1) & \cdots & f(x_n, x_n) \cr \end{bmatrix} $$

and the outer1_matvec operation computes the \(n\)-dimensional vector

$$ \text{\texttt outer1\_matvec}(X, f, a) = \begin{bmatrix} f(x_1, x_1) & \cdots & f(x_1, x_n) \cr \vdots & \ddots & \vdots \cr f(x_n, x_1) & \cdots & f(x_n, x_n) \cr \end{bmatrix} \begin{bmatrix} a_1 \cr \vdots \cr a_n \cr \end{bmatrix}. $$

The outer2 operation computes the \(m \times n\) matrix

$$ \text{\texttt outer2}(X, Y, f) = \begin{bmatrix} f(x_1, y_1) & \cdots & f(x_1, y_n) \cr \vdots & \ddots & \vdots \cr f(x_m, y_1) & \cdots & f(x_m, y_n) \cr \end{bmatrix} $$

and the outer2_matvec operation computes the \(m\)-dimensional vector

$$ \text{\texttt outer2\_matvec}(X, Y, f, a) = \begin{bmatrix} f(x_1, y_1) & \cdots & f(x_1, y_n) \cr \vdots & \ddots & \vdots \cr f(x_m, y_1) & \cdots & f(x_m, y_n) \cr \end{bmatrix} \begin{bmatrix} a_1 \cr \vdots \cr a_n \cr \end{bmatrix}. $$

Examples

Run this code
set.seed(1234)
f = function(x,y) { sum( (x - y)^2 ) }
X = matrix(rnorm(12), 6, 2)
Y = matrix(rnorm(10), 5, 2)
outer1(X, f)
outer2(X, Y, f)

a = rep(1, 6)
b = rep(1, 5)
outer1_matvec(X, f, a)
outer2_matvec(X, Y, f, b)

Run the code above in your browser using DataLab