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 Greenwell [aut, cre] |
Maintainer: | Brandon Greenwell <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.3.3 |
Built: | 2024-11-04 05:19:20 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 R object, for example a vector, matrix, array, or data frame. |
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 |
... |
Aditional optional arguments. |
A matrix (i.e., an object of class "matrix"
).
# 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 |
A vector, matrix, or multi-way array. |
.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 |
A data frame (i.e., an object of class "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 columns
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 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 matrix
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 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
, 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 object. |
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 the documentation for the base
function
solve
.
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
, mat
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. |
... |
Aditional optional arguments to be passed on to |
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 (i.e., an object of class "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 |
an object that inherits from class |
tol |
Threshold below which SVD values are considered zero. |
The singular value decomposition method simply computes the SVD of x
and returns the number of singular values of x
that are greater than
tol
. See the function rankMatrix
in package
Matrix
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 lines
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 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 |
An object of class |
... |
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 object of class "matrix"
or "data.frame"
(which are coerced
to a matrix via the data.matrix
function), 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))
Additional matrix functionality for R including: (1) wrappers for the base matrix function that allows matrices to be created from character strings and lists (the former is especially useful for creating block matrices), (ii) better printing of large matrices via a new generic function for "pretty" printing, and (iii) a number of convenience functions for users more familiar with other scientific languages like 'Julia', 'Matlab'/'Octave', or 'Python'+'NumPy'.
To learn more about ramify, read the introductory vignette:
browseVignettes(package = "ramify")
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.
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 |
A vector or matrix. |
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 |
A matrix or multi-way array. |
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 FALSE (default) the new matrix is filled by columns, otherwise it is filled by rows. This option is ignored for multi-way arrays. |
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 |
Diagonal above which to zero elements. |
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 |
Diagonal below which to zero elements. |
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)