| Title: | Additional Matrix Functionality |
|---|---|
| Description: | Additional matrix functionality for R including: (1) wrappers for the base matrix function that allow matrices to be created from character strings and lists (the former is especially useful for creating block matrices), (2) better printing of large matrices via the generic "pretty" print function, and (3) a number of convenience functions for users more familiar with other scientific languages like 'Julia', 'Matlab'/'Octave', or 'Python'+'NumPy'. |
| Authors: | Brandon M. Greenwell [aut, cre] (ORCID: <https://orcid.org/0000-0002-8120-0084>) |
| Maintainer: | Brandon M. Greenwell <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 0.4.0 |
| Built: | 2026-05-19 07:33:13 UTC |
| Source: | https://github.com/bgreenwell/ramify |
Returns the indices of the maximum or minimum values along an axis.
argmax(x, rows = TRUE) argmin(x, rows = TRUE)argmax(x, rows = TRUE) argmin(x, rows = TRUE)
x |
A matrix. |
rows |
If |
A vector of indices.
m <- mat("94, 20, 44; 40, 92, 51; 27, 69, 74") argmax(m) argmin(m)m <- mat("94, 20, 44; 40, 92, 51; 27, 69, 74") argmax(m) argmin(m)
Ensure that the input has at least two dimensions.
atleast_2d(x)atleast_2d(x)
x |
An appropriate R object supported by |
The same object, but with a "dim" attribute.
x <- 1:10 x atleast_2d(x)x <- 1:10 x atleast_2d(x)
Construct a block matrix using a character string initializer.
bmat(x, rows = TRUE, sep = ",", ...)bmat(x, rows = TRUE, sep = ",", ...)
x |
A data vector, character string, or a list. |
rows |
Logical. If TRUE (the default) the matrix is filled by rows, otherwise the matrix is filled by columns. |
sep |
Separator string. Values within each row/column of x are
separated by this string. Default is |
... |
Additional optional arguments. |
A matrix.
mat(), [dmat().
# Construct a block matrix from matrices A1, A2, and A3 A1 <- mat("1, 1; 1, 1") A2 <- mat("2, 2; 2, 2") A3 <- mat("3, 3, 3, 3") bmat("A1, A2; A3")# Construct a block matrix from matrices A1, A2, and A3 A1 <- mat("1, 1; 1, 1") A2 <- mat("2, 2; 2, 2") A3 <- mat("3, 3, 3, 3") bmat("A1, A2; A3")
Clip (i.e., limit) the values in a vector, matrix, or array.
clip(x, .min, .max, ...) ## Default S3 method: clip(x, .min, .max, ...)clip(x, .min, .max, ...) ## Default S3 method: clip(x, .min, .max, ...)
x |
|
.min |
Minimum value. |
.max |
Maximum value. |
... |
Additional optional arguments. |
Returns x with values outside the interval
[.min, .max] clipped to the interval edges. That is, values
in x smaller than .min become .min, and values larger
than .max become .max.
clip(1:10, 3, 8) # [1] 3 3 3 4 5 6 7 8 8 8 clip(randn(5, 5), .min = -1, .max = 1)clip(1:10, 3, 8) # [1] 3 3 3 4 5 6 7 8 8 8 clip(randn(5, 5), .min = -1, .max = 1)
Like mat, but returns a data frame.
dmat(x, ...)dmat(x, ...)
x |
A data vector, character string, or a list. |
... |
Aditional optional arguments passed on to mat. |
A data.frame.
dmat("1e-01, 2+5, 3, 4, 5; 6, 7, 8, 9^2, pi", rows = FALSE) z <- list(a = 1:10, b = 11:20, c = 21:30) dmat(z) # list elements form rows dmat(z, rows = FALSE) # list elements form columnsdmat("1e-01, 2+5, 3, 4, 5; 6, 7, 8, 9^2, pi", rows = FALSE) z <- list(a = 1:10, b = 11:20, c = 21:30) dmat(z) # list elements form rows dmat(z, rows = FALSE) # list elements form columns
Creates an nrow-by-ncol identity matrix.
eye(nrow = 1, ncol = nrow)eye(nrow = 1, ncol = nrow)
nrow |
The desired number of rows. |
ncol |
The desired number of columns. |
A nrow-by-ncol identity matrix.
diag.
eye(4) # 4-by-4 identity matrix eye(4, 4) # 4-by-4 identity matrix eye(3, 5) # 3-by-5 identity matrix eye(5, 3) # 5-by-3 identity matrixeye(4) # 4-by-4 identity matrix eye(4, 4) # 4-by-4 identity matrix eye(3, 5) # 3-by-5 identity matrix eye(5, 3) # 5-by-3 identity matrix
Create a matrix filled with the value x.
fill(x, nrow = 1, ncol = 1, ..., atleast_2d = NULL) falses(nrow = 1, ncol = 1, ..., atleast_2d = NULL) trues(nrow = 1, ncol = 1, ..., atleast_2d = NULL) ones(nrow = 1, ncol = 1, ..., atleast_2d = NULL) zeros(nrow = 1, ncol = 1, ..., atleast_2d = NULL)fill(x, nrow = 1, ncol = 1, ..., atleast_2d = NULL) falses(nrow = 1, ncol = 1, ..., atleast_2d = NULL) trues(nrow = 1, ncol = 1, ..., atleast_2d = NULL) ones(nrow = 1, ncol = 1, ..., atleast_2d = NULL) zeros(nrow = 1, ncol = 1, ..., atleast_2d = NULL)
x |
The (single) value to fill the matrix with. |
nrow |
The desired number of rows. |
ncol |
The desired number of columns. |
... |
Further dimensions of the array. |
atleast_2d |
Logical indicating whether or not to force column vectors
to have a second dimension equal to one. Defaults to |
A matrix or array filled with the value x.
ones, zeros, falses, trues, mat, and matrix
fill(pi, 3, 5) # 3-by-5 matrix filled with the value of pi fill(pi, 3, 5, 2, 2) # 3-by-5-by-2-by-2 array filled with the value of pi pi * ones(3, 5) zeros(10) zeros(10, atleast_2d = TRUE)fill(pi, 3, 5) # 3-by-5 matrix filled with the value of pi fill(pi, 3, 5, 2, 2) # 3-by-5-by-2-by-2 array filled with the value of pi pi * ones(3, 5) zeros(10) zeros(10, atleast_2d = TRUE)
Flatten (i.e., collapse) a matrix or array to one dimension.
flatten(x, across = c("rows", "columns"))flatten(x, across = c("rows", "columns"))
x |
A matrix. |
across |
Character string specifying whether to flatten the matrix
across |
A numeric vector.
mat.
m <- mat("2, 4, 6, 8; 10, 12, 14, 16") flatten(m) flatten(m, across = "columns")m <- mat("2, 4, 6, 8; 10, 12, 14, 16") flatten(m) flatten(m, across = "columns")
Concatenate matrices along the first or second dimension.
hcat(...) vcat(...)hcat(...) vcat(...)
... |
Vectors or matrices. |
A matrix formed by combining the ... arguments column-wise
(hcat) or row-wise (vcat).
m1 <- mat("1, 2, 3; 4, 5, 6") m2 <- mat("7, 8, 9; 10, 11, 12") hcat(m1, m2) # same as 'bmat("m1, m2")' vcat(m1, m2) # same as 'bmat("m1; m2")'m1 <- mat("1, 2, 3; 4, 5, 6") m2 <- mat("7, 8, 9; 10, 11, 12") hcat(m1, m2) # same as 'bmat("m1, m2")' vcat(m1, m2) # same as 'bmat("m1; m2")'
Calculates the inverse of a square matrix.
inv(x, ...)inv(x, ...)
x |
A square numeric or complex matrix. |
... |
Additional optional arguments. |
See solve for details.
m <- 3 * eye(5) inv(m)m <- 3 * eye(5) inv(m)
Determine if a matrix is lower triangular.
is.tril(x)is.tril(x)
x |
A matrix. |
Logical indicating whether the given matrix is lower triangular.
m <- mat("1, 0, 0, 0; -1, 1, 0, 0; -2, -2, 1, 0; -3, -3, -3, 1") is.tril(m) is.tril(eye(3, 5))m <- mat("1, 0, 0, 0; -1, 1, 0, 0; -2, -2, 1, 0; -3, -3, -3, 1") is.tril(m) is.tril(eye(3, 5))
Determine if a matrix is upper triangular.
is.triu(x)is.triu(x)
x |
A matrix. |
Logical indicating whether the given matrix is lower triangular.
m <- mat("1, -1, -1, -1; 0, 1, -2, -2; 0, 0, 1, -3; 0, 0, 0, 1") is.triu(m) is.triu(eye(3, 5))m <- mat("1, -1, -1, -1; 0, 1, -2, -2; 0, 0, 1, -3; 0, 0, 0, 1") is.triu(m) is.triu(eye(3, 5))
Construct a vector of n linearly-spaced elements from a
to b.
linspace(a, b, n = 50)linspace(a, b, n = 50)
a |
The starting value of the sequence. |
b |
The final value of the sequence. |
n |
The number of samples to generate. Default is 50. |
A vector of linearly-spaced elements.
linspace(0, 1) linspace(1, 5, 5) linspace(1 + 2i, 10 + 10i, 8) logspace(0, pi, 10)linspace(0, 1) linspace(1, 5, 5) linspace(1 + 2i, 10 + 10i, 8) logspace(0, pi, 10)
Construct a vector of n logarithmically-spaced elements from
10^a to 10^b.
logspace(a, b, n = 50, base = 10)logspace(a, b, n = 50, base = 10)
a |
|
b |
|
n |
The number of samples to generate. Default is 50. |
base |
The base of the log space. |
A vector of logarithmically-spaced elements.
If b = pi and base = 10, the points are between
10^a and pi, not 10^a and 10^pi, for
compatibility with the corresponding MATLAB/Octave, and NumPy functions.
Like matrix, this function creates a matrix from the given set of values. However, these values can also be represented by a character string, or a list of vectors. Initially inspired by NumPy's matrix function.
mat(x, ...) ## Default S3 method: mat(x, ...) ## S3 method for class 'character' mat(x, rows = TRUE, sep = ",", eval = FALSE, ...) ## S3 method for class 'list' mat(x, rows = TRUE, ...)mat(x, ...) ## Default S3 method: mat(x, ...) ## S3 method for class 'character' mat(x, rows = TRUE, sep = ",", eval = FALSE, ...) ## S3 method for class 'list' mat(x, rows = TRUE, ...)
x |
A data vector, character string, or a list. |
... |
Additional optional arguments to be passed on to matrix. |
rows |
Logical. If |
sep |
Separator string. Values within each row/column of x are separated
by this string. Default is |
eval |
Logical indicating whether or not the character string contains R
expressions that need to be evaluated. Default is |
A matrix.
# Creating a matrix from a character string mat("1, 2, 3, 4; 5, 6, 7, 8") # ";" separates rows mat("1, 2, 3, 4; 5, 6, 7, 8", rows = FALSE) # ";" separates columns mat("1 2 3 4; 5 6 7 8", sep = "") # use spaces instead of commas mat(c(1, 2, 3, 4, 5, 6, 7, 8), nrow = 2, byrow = TRUE) # works like matrix too # Character strings containing R expressions mat("rnorm(3); rnorm(3)") mat("rnorm(3); rnorm(3)", eval = TRUE) mat("1, 2, 3; 4, 5, pi") mat("1, 2, 3; 4, 5, pi", eval = TRUE) mat("-1, -.1; -0.1, -1.0") # Creating a matrix from a list z1 <- list(1:5, 6:10) z2 <- list(a = 1:5, b = 6:10) mat(z1) mat(z2) # preserves names as row names mat(z2, rows = FALSE) # preserves names as column names# Creating a matrix from a character string mat("1, 2, 3, 4; 5, 6, 7, 8") # ";" separates rows mat("1, 2, 3, 4; 5, 6, 7, 8", rows = FALSE) # ";" separates columns mat("1 2 3 4; 5 6 7 8", sep = "") # use spaces instead of commas mat(c(1, 2, 3, 4, 5, 6, 7, 8), nrow = 2, byrow = TRUE) # works like matrix too # Character strings containing R expressions mat("rnorm(3); rnorm(3)") mat("rnorm(3); rnorm(3)", eval = TRUE) mat("1, 2, 3; 4, 5, pi") mat("1, 2, 3; 4, 5, pi", eval = TRUE) mat("-1, -.1; -0.1, -1.0") # Creating a matrix from a list z1 <- list(1:5, 6:10) z2 <- list(a = 1:5, b = 6:10) mat(z1) mat(z2) # preserves names as row names mat(z2, rows = FALSE) # preserves names as column names
Compute the rank of a matrix using the singular value decomposition (SVD) method.
matrix_rank(x, tol) ## Default S3 method: matrix_rank(x, tol) ## S3 method for class 'matrix' matrix_rank(x, tol) ## S3 method for class 'data.frame' matrix_rank(x, tol)matrix_rank(x, tol) ## Default S3 method: matrix_rank(x, tol) ## S3 method for class 'matrix' matrix_rank(x, tol) ## S3 method for class 'data.frame' matrix_rank(x, tol)
x |
A matrix. |
tol |
Threshold below which SVD values are considered zero. |
The singular value decomposition (SVD) method simply computes the SVD of x
and returns the number of singular values of x that are greater than
tol. See Matrix::rankMatrix() for alternative methods.
matrix_rank(1:5) matrix_rank(randn(2, 2)) matrix_rank(cbind(c(1, 1, 1), c(2, 2, 2))) matrix_rank(ones(3, 3)) matrix_rank(zeros(3, 5))matrix_rank(1:5) matrix_rank(randn(2, 2)) matrix_rank(cbind(c(1, 1, 1), c(2, 2, 2))) matrix_rank(ones(3, 3)) matrix_rank(zeros(3, 5))
Creates matrices for vectorized evaluations of 2-D scalar/vector fields over 2-D grids.
meshgrid(x, y = x)meshgrid(x, y = x)
x |
Numeric vector representing the first coordinate of the grid. |
y |
Numeric vector representing the second coordinate of the grid. |
A list of matrices.
mg <- meshgrid(linspace(-4 * pi, 4 * pi, 27)) # list of input matrices z <- cos(mg[[1]]^2 + mg[[2]]^2) * exp(-sqrt(mg[[1]]^2 + mg[[2]]^2) / 6) image(z, axes = FALSE) # color image contour(z, add = TRUE, drawlabels = FALSE) # add contour linesmg <- meshgrid(linspace(-4 * pi, 4 * pi, 27)) # list of input matrices z <- cos(mg[[1]]^2 + mg[[2]]^2) * exp(-sqrt(mg[[1]]^2 + mg[[2]]^2) / 6) image(z, axes = FALSE) # color image contour(z, add = TRUE, drawlabels = FALSE) # add contour lines
Prettier printing for matrices and data frames.
pprint(x, ...) ## S3 method for class 'matrix' pprint(x, rowdots = NULL, coldots = NULL, digits = NULL, ...) ## S3 method for class 'data.frame' pprint(x, rowdots = NULL, coldots = NULL, digits = NULL, ...)pprint(x, ...) ## S3 method for class 'matrix' pprint(x, rowdots = NULL, coldots = NULL, digits = NULL, ...) ## S3 method for class 'data.frame' pprint(x, rowdots = NULL, coldots = NULL, digits = NULL, ...)
x |
A matrix or data.frame. |
... |
Additional optional arguments. None are used at present. |
rowdots |
Integer specifying the row to replace with |
coldots |
Integer specifying the column to replace with |
digits |
The minimum number of significant digits to be printed in values. |
For a matrix or data.frame (which are coerced to a matrix via
data.matrix, pprint() will replace all the rows starting from rowdots
up to and including the second-to-last row with a single row filled with
...s. The same is applied to the columns as well. Hence a large matrix
(or data.frame) will be printed in a much more compact form.
pprint(randn(100, 100)) pprint(resize(1:100, 10, 10))pprint(randn(100, 100)) pprint(resize(1:100, 10, 10))
Construct a matrix or multi-way array of uniform random deviates.
rand(nrow = 1, ncol = 1, ..., min = 0, max = 1, atleast_2d = NULL)rand(nrow = 1, ncol = 1, ..., min = 0, max = 1, atleast_2d = NULL)
nrow |
The desired number of rows. |
ncol |
The desired number of columns. |
... |
Further dimensions of the array. |
min |
Lower limit for the uniform distribution. Must be finite.
( |
max |
Upper limit for the uniform distribution. Must be finite.
( |
atleast_2d |
Logical indicating whether or not to force column vectors
to have a second dimension equal to one. Defaults to |
A matrix or array of pseudorandom numbers.
A matrix or array of pseudorandom numbers.
rand(100, 100) # 100 by 100 matrix of uniform random numbers rand(2, 3, min = 100, max = 200)rand(100, 100) # 100 by 100 matrix of uniform random numbers rand(2, 3, min = 100, max = 200)
Construct a matrix or multi-way array of uniform random integers.
randi(imax, nrow, ncol = 1, ..., atleast_2d = NULL)randi(imax, nrow, ncol = 1, ..., atleast_2d = NULL)
imax |
A positive integer. |
nrow |
The desired number of rows. |
ncol |
The desired number of columns. |
... |
Further dimensions of the array. |
atleast_2d |
Logical indicating whether or not to force column vectors
to have a second dimension equal to one. Defaults to |
A matrix or array of pseudorandom numbers.
randi(2, 5, 5)randi(2, 5, 5)
Construct a matrix or multi-way array of normal random deviates.
randn(nrow = 1, ncol = 1, ..., mean = 0, sd = 1, atleast_2d = NULL)randn(nrow = 1, ncol = 1, ..., mean = 0, sd = 1, atleast_2d = NULL)
nrow |
The desired number of rows. |
ncol |
The desired number of columns. |
... |
Further dimensions of the array. |
mean |
Mean for the normal distribution. ( |
sd |
Standard deviation for the normal distribution. ( |
atleast_2d |
Logical indicating whether or not to force column vectors
to have a second dimension equal to one. Defaults to |
A matrix or array of pseudorandom numbers.
randn(100, 100) # 100 by 100 matrix of standard normal random variates randn(2, 3, mean = 10, sd = 0.1)randn(100, 100) # 100 by 100 matrix of standard normal random variates randn(2, 3, mean = 10, sd = 0.1)
Repeat a vector or matrix a specific number of times.
repmat(x, m, n)repmat(x, m, n)
x |
|
m |
Integer specifying how many times to repeat |
n |
Integer specifying how many times to repeat |
A block matrix of dimension m*nrow(x) by n*ncol(x).
repmat(1:3, 3, 2) # will have dimension 9 by 2 repmat(randn(2, 2), 3, 2)repmat(1:3, 3, 2) # will have dimension 9 by 2 repmat(randn(2, 2), 3, 2)
Change shape and size of a matrix or array.
resize(x, nrow, ncol, ..., across = c("rows", "columns"), byrow = FALSE)resize(x, nrow, ncol, ..., across = c("rows", "columns"), byrow = FALSE)
x |
|
nrow |
The desired number of rows. |
ncol |
The desired number of columns. |
... |
Further dimensions of the array. |
across |
Character string specifying whether to flatten the matrix
across |
byrow |
Logical. If |
A matrix with dimension nrow-by-ncol.
m <- 1:9 resize(m) resize(m, 3, 3) resize(m, 2, 2)m <- 1:9 resize(m) resize(m, 3, 3) resize(m, 2, 2)
Retrieve the dimensions of a matrix or array.
size(x)size(x)
x |
A matrix, array, or data.frame. |
The dimensions of the object.
dim.
m <- mat("1, 3, 5; 7, 9, 11") size(m)m <- mat("1, 3, 5; 7, 9, 11") size(m)
Sum of diagonal elements of a matrix.
tr(x)tr(x)
x |
A matrix. |
The sum of the diagonal elements of x.
tr(ones(5, 10)) x <- replicate(1000, tr(rand(25, 25))) hist(x)tr(ones(5, 10)) x <- replicate(1000, tr(rand(25, 25))) hist(x)
Construct a matrix with ones at and below the given diagonal and zeros elsewhere.
tri(nrow, ncol = nrow, k = 0, diag = TRUE)tri(nrow, ncol = nrow, k = 0, diag = TRUE)
nrow |
The desired number of rows. |
ncol |
The desired number of columns. |
k |
The sub-diagonal at and below which the matrix is filled.
|
diag |
Logical indicating whether to include the diagonal. Default is
|
tri(5, 5) tri(5, 5, 2) tri(5, 5, -1)tri(5, 5) tri(5, 5, 2) tri(5, 5, -1)
Extract the lower triangular part of a matrix.
tril(x, k = 0, diag = TRUE)tril(x, k = 0, diag = TRUE)
x |
A matrix. |
k |
The sub-diagonal at and below which the matrix is filled.
|
diag |
Logical indicating whether to include the diagonal. Default is
|
tril(ones(5, 5)) tril(ones(5, 5), diag = TRUE)tril(ones(5, 5)) tril(ones(5, 5), diag = TRUE)
Extract the upper triangular part of a matrix.
triu(x, k = 0, diag = TRUE)triu(x, k = 0, diag = TRUE)
x |
A matrix. |
k |
The sub-diagonal at and below which the matrix is filled.
|
diag |
Logical indicating whether to include the diagonal. Default is
|
triu(ones(5, 5)) triu(ones(5, 5), diag = FALSE)triu(ones(5, 5)) triu(ones(5, 5), diag = FALSE)